注意:部分文章发布时间较长,可能存在未知因素,购买时建议在本站搜索商家名称,先充分了解商家动态。
交流:唯一投稿邮箱:hostvps@88.com。
在Wall越来越高的年代,能够找到一款稳定过Wall的V#P#N,已经越来越不容易。先是Open#V#P#N的沦陷,然后AnyConnect也慢慢的被认证掉了,纯V#P#N类的工具越来越少,但很多时候预计Socks协议的代理却远远不能满足我们的使用需要。可能有的人会说,那就用WireGuard啊。是,WireGuard确实是个完美的方案,但WireGuard只能安装在KVM/Xen/物理架构上,并不能安装在某些基于OpenVZ的服务器上。难道真的没办法了么?其实WireGuard官方也提供了一个额外的解决方案,那就是 WireGuard-Go 。1. 什么是WireGuard-GoWireGuard-Go是WireGuard的Go语言实现。在原版的WireGuard中,由于是基于C/C++语言编写,而且需要将成品的WireGuard模块 (wireguard.ko) 编译到系统内核中,这样一来基于OpenVZ的共享核心型虚拟化架构来讲根本无法成功安装WireGuard。而在WireGuard-Go中,由于WireGuard模块已经通过Go语言实现,所以并不需要将模块编译到内核中,但相对于原版的WireGuard来讲,执行效率可能会略有下降。但这已经是在OpenVZ这种极端环境下的唯一选择。(谁让V#P#N没有其他选择了呢)(逃)2. 安装WireGuard-Go2.1 前提条件首先,在安装WireGuard-Go前,我们来看下安装WireGuard-Go的一些需求。编译环境:任何虚拟化架构或者物理机架构内存>512MB (推荐>1GB,如果内存不足推荐通过增加Swap的方式临时扩展内存)磁盘可用空间>5GB安装Golang环境 (下文会具体讲安装及编译过程)运行环境:OpenVZ虚拟化架构 (Docker/LXC尚未进行测试)内存>128MB (推荐>256MB)磁盘可用空间>500MB开启TUN/TAP (可以在VPS后台控制面板中检查并打开此项)编译环境和运行环境可以是同一台服务器,也可以在不同的服务器上,下文会具体讲如何导出编译结果。一定要注意,毕竟WireGuard怎么说也是个V#P#N,所以一定会用到TUN/TAP,请务必开启TUN/TAP,以防止WireGuard无法正常转发流量!2.2 准备编译环境首先我们登录编译环境服务器,并安装Golang环境:wget -O /tmp/golang.tar.gz https://dl.google.com/go/go1.12.4.linux-amd64.tar.gztar -C /usr/local -xvzf /tmp/golang.tar.gz之后配置Golang相关环境变量:export PATH=$PATH:/usr/local/go/bin2.3 编译WireGuard-Go在Golang安装完成之后,开始下载WireGuard-Go源码:mkdir -p /tmp/gobuild/ && cd /tmp/gobuild/git clone https://git.zx2c4.com/wireguard-gocd wireguard-go配置环境变量并开始编译(请确保你的网络状况良好,推荐使用海外服务器进行编译):export GOPATH=”/tmp/gobuild/”go build -v -o “wireguard-go”如果没有遇到任何错误的话,我们会在同目录看到一个 wireguard-go 的可执行文件。将此文件复制到系统目录中(本机编译本机安装):cp wireguard-go /usr/sbin/wireguard-go或者通过SSH、FTP等方式传输到目标运行环境服务器上,并将文件导入上述位置并配置可执行权限(异机编译安装)。如果无法自行编译安装,可以使用博主提供的成品编译二进制文件:https://download.ilemonrain.com/WireGuard-Go/precompile/wireguard-go.gz2.4 安装并配置WireGuard看到这里很多人会问了,我们安装的不是WireGuard-Go么?怎么又要安装WireGuard了?先别急,我在这里解释一下。在上一步里安装的WireGuard-Go,只是将WireGuard的内核部分 (wireguard.ko) 编译完成了,我们还需要编译WireGuard主程序 (wg 和 wg-quick),才能使得WireGuard能够正常使用,同时也可以像正常配置WireGuard那样配置WireGuard-Go。首先安装必要的编译环境组件:For Debian/Ubuntu:apt-get install libmnl-dev libelf-dev build-essential pkg-configFor CentOSyum install libmnl-devel elfutils-libelf-devel pkg-config @development-tools之后下载源码包:mkdir -p /tmp/build/ && cd /tmp/build/git clone https://git.zx2c4.com/WireGuardcd wireguard-go/src/tools编译安装WireGuard工具:make && make install到这里,WireGuard的wg和wg-quick这两条命令,应该就可以使用了。再加上上一步中编译好的wireguard-go,我们可以在OpenVZ平台上运行WireGuard了。3. 配置WireGuard-Go首先执行WireGuard-Go,启动WireGuard内核,并创建一块虚拟网卡(可能现在不会显示出来,但当使用wg或wg-quick命令行启动时就会自动出现):由于是测试版的原因,会自动弹出一个类似这样的警告:WARNING WARNING WARNING WARNING WARNING WARNING WARNINGW GW You are running this software on a Linux kernel, GW which is probably unnecessary and foolish. This GW is because the Linux kernel has built-in first GW class support for WireGuard, and this support is GW much more refined than this slower userspace GW implementation. For more information on GW installing the kernel module, please visit: GW https://www.wireguard.com/install GW GW If you still want to use this program, against GW the advice here, please first export this GW environment variable: GW WG_I_PREFER_BUGGY_USERSPACE_TO_POLISHED_KMOD=1 GW GWARNING WARNING WARNING WARNING WARNING WARNING WARNING执行以下命令即可继续使用:export WG_I_PREFER_BUGGY_USERSPACE_TO_POLISHED_KMOD=1之后执行命令,创建虚拟网卡:wireguard-go wg下述部分你可以参照doubi的WireGuard教程操作!传送门:https://doubibackup.com/qbc20cn3.html接下来我们创建WireGuard的配置文件:mkdir -p /etc/wireguard/ && cd /etc/wireguard/生成密钥对:wg genkey | tee sprivatekey | wg pubkey > spublickeywg genkey | tee cprivatekey | wg pubkey > cpublickey确认你的外网网卡(对于OpenVZ虚拟化架构来讲,一般都是 venet0 )root@ovzhost:~# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWNlink/void inet 127.0.0.2/32 scope host venet0inet X.X.X.X/32 brd X.X.X.X scope global venet0:03: wg: <POINTOPOINT,MULTICAST,NOARP> mtu 1420 qdisc noop state DOWN qlen 500link/none生成WireGuard服务端配置文件 wg0.conf :echo “[Interface]PrivateKey = $(cat sprivatekey)Address = 10.0.0.1/24 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o venet0 -j MASQUERADEPostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o venet0 -j MASQUERADEListenPort = 6666MTU = 1420[Peer]PublicKey = $(cat cpublickey)AllowedIPs = 10.0.0.2/32” | sed ‘/^#/d;/^\s*$/d’ > wg0.conf生成WireGuard客户端文件 client.conf :echo “[Interface]PrivateKey = $(cat cprivatekey)Address = 10.0.0.2/24DNS = 8.8.8.8MTU = 1420[Peer]PublicKey = $(cat spublickey)Endpoint = $(curl -s whatismyip.akamai.com):6666AllowedIPs = 0.0.0.0/0, ::0/0PersistentKeepalive = 30” | sed ‘/^#/d;/^\s*$/d’ > client.conf当然,不要忘了开启转发:echo 1 > /proc/sys/net/ipv4/ip_forwardecho “net.ipv4.ip_forward = 1” >> /etc/sysctl.confsysctl -p在确认配置无误后,启动WireGuard服务端:wg-quick up wg0会得到类似这样的结果:root@ovzhost:/etc/wireguard# wg-quick up wg0[#] ip link add wg0 type wireguardRTNETLINK answers: Operation not supported[!] Missing WireGuard kernel module. Falling back to slow userspace implementation.[#] wireguard-go wg0WARNING WARNING WARNING WARNING WARNING WARNING WARNINGW GW You are running this software on a Linux kernel, GW which is probably unnecessary and foolish. This GW is because the Linux kernel has built-in first GW class support for WireGuard, and this support is GW much more refined than this slower userspace GW implementation. For more information on GW installing the kernel module, please visit: GW https://www.wireguard.com/install GW GWARNING WARNING WARNING WARNING WARNING WARNING WARNINGINFO: (wg0) 2019/04/19 09:45:50 Starting wireguard-go version 0.0.20190409-9-gd024393[#] wg setconf wg0 /dev/fd/63[#] ip address add 10.0.0.1/24 dev wg0[#] ip link set mtu 1420 up dev wg0[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o venet0 -j MASQUERADE则说明WireGuard启动成功(出错为正常现象,因为无法加载基于内核模块的WireGuard,所以只能加载WireGuard-Go作为Fallback方案)将client.conf传回本地并导入客户端中(个人推荐使用TunSafe,下载地址自己找),即可完成配置工作。4. 一点点善后工作…配置WireGuard开机启动:systemctl enable wg-quick@wg0清理编译过程中产生的垃圾文件:rm -rf /tmp/gobuild/rm -rf /tmp/build/rm -f /tmp/golang.tar.gz