2025-03-14  2025-03-21    1285 字  3 分钟

有时候服务器之间需要加密传输数据(例如NFS挂载),为了平衡延迟和速度,可以采用Wireguard来组建一个内网,这样业务数据可以通过内网IP高速和安全的传输

注意

  • Wireguard有加密但无混淆,特征较为明显,谨慎用于其他用途
  • Wireguard使用UDP传输,部分云服务商限制了UDP会导致性能较差
  • 由于海外和国内网络互联较差, 使用了一个带优化线路的中转节点来中转国内和海外服务器, 降低连接延迟, 提高传输速度

特性

ListenPort 监听和发送端口的问题

当给Interface配置ListenPort之后,这个端口既承担了监听端口的功能,又承担了对外发送端口的功能,这会导致一些问题

当我给家里的各个设备(经过NAT后由同一个公网IP发送)分配好IP, 并且每台设备ListenPort都相同。然后就出问题了

家里的设备互相能访问,那些配置了IP和端口的Peer也能直连访问,但是中转服务器和那些需要中转的服务器就访问不了了(有时候家里部分设备能访问,但我猜可能是他们提前把端口抢过来了)

然后通过wg show查看中转,发现他们连接到中转服务器的端口就是ListenPort监听的端口,也就是说他们接收和发送都用的同一个UDP端口,这可能会导致端口冲突

因此,要么NAT下设备不配置ListenPort, 要么配置不同的ListenPort

安装

1apt install -y wireguard

生成密钥

1wg genkey | tee privatekey | wg pubkey > publickey

启动

1# 启动
2wg-quick up wg0
3# 开机启动
4systemctl enable wg-quick@wg0
5# 查看状态
6wg show

开放防火墙(如果没有启用防火墙则跳过)

如果想通过内网IP来访问, 那么需要在防火墙中配置允许这个内网IP段访问

1# 通过ufw开放
2ufw allow from 10.0.0.0/16 to any comment wg0

测试

1ping 10.0.0.1
2ping 10.0.0.2
3ping 10.0.0.100
4ping 10.0.0.3
5ping 10.0.0.4

配置文件

/etc/wireguard/wg0.conf

示例服务器

  • ABC在公网
  • DE在某个NAT下的内网(如家里的设备
  • AB互相直连访问
  • DE互相直连访问
  • AB和DE互相通过C中转访问
服务器 公网/NAT IP 内网 WireGuard IP
A X.X.X.A 10.0.0.1
B X.X.X.B 10.0.0.2
C X.X.X.C 10.0.0.100
D X.X.X.D 10.0.0.3
E X.X.X.E 10.0.0.4

A - 公网服务器

1vim /etc/wireguard/wg0.conf
  • 直连C, 同时将10.0.0.0/16网段的请求转发到C
  • 直连B
  • 通过C中转连接DE
 1[Interface]
 2PrivateKey = <A>
 3Address = 10.0.0.1/16
 4ListenPort = 51820
 5
 6#############
 7# center    #
 8#############
 9
10[Peer]
11PublicKey = <C>
12Endpoint = X.X.X.C:51820
13AllowedIPs = 10.0.0.0/16
14PersistentKeepalive = 15
15
16#############
17# direct    #
18#############
19
20[Peer]
21PublicKey = <B>
22Endpoint = X.X.X.B:51820
23AllowedIPs = 10.0.0.2/32
24PersistentKeepalive = 15

B - 公网服务器

1vim /etc/wireguard/wg0.conf
  • 直连C, 同时将10.0.0.0/16网段的请求转发到C
  • 直连A
  • 通过C中转连接DE
 1[Interface]
 2PrivateKey = <B>
 3Address = 10.0.0.2/16
 4ListenPort = 51820
 5
 6#############
 7# center    #
 8#############
 9
10[Peer]
11PublicKey = <C>
12Endpoint = X.X.X.C:51820
13AllowedIPs = 10.0.0.0/16
14PersistentKeepalive = 15
15
16#############
17# direct    #
18#############
19
20[Peer]
21PublicKey = <A>
22Endpoint = X.X.X.A:51820
23AllowedIPs = 10.0.0.1/32
24PersistentKeepalive = 15

C - 公网服务器 - 中转节点

1vim /etc/wireguard/wg0.conf
  • 中转ABDE节点
 1#############
 2# center    #
 3#############
 4
 5# C
 6[Interface]
 7PrivateKey = <C>
 8Address = 10.0.0.100/16
 9ListenPort = 51820
10
11PostUp = sysctl -w net.ipv4.ip_forward=1; iptables -A FORWARD -i wg0 -j ACCEPT
12PostDown = sysctl -w net.ipv4.ip_forward=0; iptables -D FORWARD -i wg0 -j ACCEPT
13
14#############
15# direct    #
16#############
17
18#############
19# redir     #
20#############
21
22# A
23[Peer]
24PublicKey = <A>
25AllowedIPs = 10.0.0.1/32
26
27# B
28[Peer]
29PublicKey = <B>
30AllowedIPs = 10.0.0.2/32
31
32# D
33[Peer]
34PublicKey = <D>
35AllowedIPs = 10.0.0.3/32
36
37# E
38[Peer]
39PublicKey = <E>
40AllowedIPs = 10.0.0.4/32

D - NAT下服务器

1vim /etc/wireguard/wg0.conf
  • 直连C, 同时将10.0.0.0/16网段的请求转发到C
  • 直连E
  • 通过C中转连接AB
 1[Interface]
 2PrivateKey = <D>
 3Address = 10.0.0.3/16
 4ListenPort = 51820
 5
 6#############
 7# center    #
 8#############
 9
10[Peer]
11PublicKey = <C>
12Endpoint = X.X.X.C:51820
13AllowedIPs = 10.0.0.0/16
14PersistentKeepalive = 15
15
16#############
17# direct    #
18#############
19
20[Peer]
21PublicKey = <E>
22Endpoint = X.X.X.E:51821
23AllowedIPs = 10.0.0.4/32
24PersistentKeepalive = 15

E - NAT下服务器

1vim /etc/wireguard/wg0.conf
  • 直连C, 同时将10.0.0.0/16网段的请求转发到C
  • 直连D
  • 通过C中转连接AB
 1[Interface]
 2PrivateKey = <E>
 3Address = 10.0.0.4/16
 4ListenPort = 51821
 5
 6#############
 7# center    #
 8#############
 9
10[Peer]
11PublicKey = <C>
12Endpoint = X.X.X.C:51820
13AllowedIPs = 10.0.0.0/16
14PersistentKeepalive = 15
15
16#############
17# direct    #
18#############
19
20[Peer]
21PublicKey = <D>
22Endpoint = X.X.X.D:51820
23AllowedIPs = 10.0.0.3/32
24PersistentKeepalive = 15

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处