官网:https://www.wireguard.com/
WireGuard® is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.
WireGuard是一个非常简单但利用最先进的加密技术快速和现代的VPN。它的目标是比IPsec更快、更简单、更精简和更有用,同时避免大量令人头痛的问题。它打算比OpenVPN性能更好。WireGuard被设计为一种通用的VPN,可以在嵌入式接口和超级计算机上运行,适合许多不同的情况。它最初是为Linux内核发布的,现在是跨平台的(Windows, macOS, BSD, iOS, Android),并且可以广泛部署。它目前正在大力开发,但它可能已经被认为是业界最安全、最容易使用和最简单的VPN解决方案。
内网穿透(frp)和虚拟专用网(wireguard):
部署环境:阿里云(Ubuntu20.04)
sudo apt install wireguard
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
Wireguard网络中每一个接入设备都一个对应密钥对(私钥由各端自己保留,公钥需要添加到服务端配置文件中)。
wg genkey | tee server.key | wg pubkey > server.key.pub
wg genkey | tee client_01.key | wg pubkey > client_01.key.pub
wg genkey | tee client_02.key | wg pubkey > client_02.key.pub
wg genkey | tee client_xx.key | wg pubkey > client_xx.key.pub
注:密钥对可以使用服务器创建再分配,也可以各个设备端使用相同的方法生成。
创建配置文件:
touch /etc/wireguard/wg0.conf
修改配置文件:
配置文件示例:
[Interface]
PrivateKey = KB16EseuTq7Ax1zeuEob5PctLm/v3w7N+BoD28eAOks=
Address = 10.0.8.1/24
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 9000
MTU = 1420
[Peer]
PublicKey = cAHcJbh4Pl3CNeZ5/eZRP5RslpeiOCzgfFcKwkM0eUw=
AllowedIPs = 10.0.8.10/32
[Peer]
PublicKey = gOqWWD7dFEn2GoTfOYthvZgWX08Eoc8B1sVgUmBQUWo=
AllowedIPs = 10.0.8.11/32
[Peer]
PublicKey = 8LWZxQ4quXQ16jGS6vnv+uvqXN87altNCtsugrc9ons=
AllowedIPs = 10.0.8.12/32
安装wireguard后,会创建[email protected]文件,我们可以通过systemd对wireguard功能进行管理,服务名:wg-quick@
# 启动服务
sudo systemctl start wg-quick@wg0
# 停止服务
sudo systemctl stop wg-quick@wg0
# 重启服务
sudo systemctl restart wg-quick@wg0
# 查看状态
sudo systemctl status wg-quick@wg0
# 使能自启动
sudo systemctl enable wg-quick@wg0
# 关闭自启动
sudo systemctl disable wg-quick@wg0
sudo apt install wireguard
创建配置文件:
touch /etc/wireguard/wg0.conf
配置文件示例:
[Interface]
PrivateKey = 0CxPRLJiY26DLS2+Rtu8JooBeLdKnY+VkrkvhiTRC3s=
Address = 10.0.8.13/24
[Peer]
PublicKey = WLAVpXsK5+BMYwfqbY/Bt7G14psHJE6TwMkXoZ7tJEo=
AllowedIPs = 10.0.8.0/24
Endpoint = 100.200.200.200:9000
PersistentKeepalive = 60
注:如果拦截所有流量走VPN,可以配置“AllowedIPs = 0.0.0.0/1、128.0.0.0/1”。
# 启动服务
sudo systemctl start wg-quick@wg0
# 停止服务
sudo systemctl stop wg-quick@wg0
# 重启服务
sudo systemctl restart wg-quick@wg0
# 查看状态
sudo systemctl status wg-quick@wg0
# 使能自启动
sudo systemctl enable wg-quick@wg0
# 关闭自启动
sudo systemctl disable wg-quick@wg0
Wireguard处理Linux支持外,也开发了Windows/Android等设备端的应用,支持这类系统设备使用UI界面的方式配置连接到Wireguard网络。下载链接:https://www.wireguard.com/install/
配置项与Linux设备一致,Windows端支持“新建隧道->从文件导入隧道”的方式直接导入.conf文件建立隧道(连接)。
移动端除了导入文件的方式外,也可以使用**“qrencode -t ansiutf8
interface: wg0
public key: iPU3HCbreBTMrpox+Ku6WU5xf5Rb3lEc9O9knGpNHVM=
private key: (hidden)
listening port: 9000
peer: DkqRdBMW9m6VvlfXEacRVajA3NLujXVtV9OvSEZo4RU=
allowed ips: 10.0.8.10/32
peer: IOoa6sdwBMgno9rYMvsVZxbgvCfbfN/XEUBKw6Irjlg=
allowed ips: 10.0.8.11/32
peer: 1Oruos/kMsVoy7lrf7SIy/2cVTNcH5VWJaccYpKtZz0=
allowed ips: 10.0.8.12/32
peer: Qss20/1vcwm1Ir71vGxXsAq02l+zk1h4heBwpviq5Co=
allowed ips: 10.0.8.13/32
客户端设备连接后执行“sudo wg”:
interface: wg0
public key: iPU3HCbreBTMrpox+Ku6WU5xf5Rb3lEc9O9knGpNHVM=
private key: (hidden)
listening port: 9000
peer: Qss20/1vcwm1Ir71vGxXsAq02l+zk1h4heBwpviq5Co=
endpoint: 222.70.222.174:50867
allowed ips: 10.0.8.13/32
latest handshake: 14 seconds ago
transfer: 180 B received, 92 B sent
peer: DkqRdBMW9m6VvlfXEacRVajA3NLujXVtV9OvSEZo4RU=
allowed ips: 10.0.8.10/32
peer: x/uU+r9+Z9WFgMYJBVyzi8swUUf6kOW1f9bYi7kv/A0=
allowed ips: 10.0.8.11/32
peer: 1Oruos/kMsVoy7lrf7SIy/2cVTNcH5VWJaccYpKtZz0=
allowed ips: 10.0.8.12/32
PING 10.0.8.13 (10.0.8.13) 56(84) bytes of data.
64 bytes from 10.0.8.13: icmp_seq=1 ttl=64 time=116 ms
64 bytes from 10.0.8.13: icmp_seq=2 ttl=64 time=11.9 ms
64 bytes from 10.0.8.13: icmp_seq=3 ttl=64 time=11.5 ms
64 bytes from 10.0.8.13: icmp_seq=4 ttl=64 time=10.1 ms
64 bytes from 10.0.8.13: icmp_seq=5 ttl=64 time=11.2 ms
64 bytes from 10.0.8.13: icmp_seq=6 ttl=64 time=15.1 ms
64 bytes from 10.0.8.13: icmp_seq=7 ttl=64 time=10.2 ms
64 bytes from 10.0.8.13: icmp_seq=8 ttl=64 time=10.8 ms
64 bytes from 10.0.8.13: icmp_seq=9 ttl=64 time=10.8 ms