docker部署wireguard

在这里插入图片描述

wireguard无所谓server与client端,支持漫游。

实验环境:

主机公网地址内网地址私有网段vpn地址
server113.204.133.126:51820192.168.100.11/24192.168.11.0/2410.0.0.1/24
clientADSL192.168.100.12/24192.168.12.0/2410.0.0.2/24
server:
1: lo: inet 127.0.0.1/8 scope host lo
2: ens33: inet 192.168.100.11/24 brd 192.168.100.255 scope global ens33
3: ens37: inet 192.168.11.11/24 brd 192.168.11.255 scope global ens37

client:
1: lo: inet 127.0.0.1/8 scope host lo
2: ens33: inet 192.168.100.12/24 brd 192.168.100.255 scope global ens33
3: ens37: inet 192.168.11.12/24 brd 192.168.11.255 scope global ens37

server

#目录:/etc/wireguard
#生成server公私钥
mkdir peer1
umask 077;wg genkey | tee ./peer1/privatekey | wg pubkey > ./peer1/publickey
#生成client公私钥
mkdir peer2
umask 077;wg genkey | tee ./peer2/privatekey | wg pubkey > ./peer2/publickey

#目录:/etc/wireguard
#创建网卡
ip link add wg0 type wireguard
#设置地址
ip address add 10.0.0.1/24 dev wg0
#监听端口
wg set wg0 listen-port 51820 private-key ./peer1/privatekey
#设置参数
wg set wg0 peer $(cat ./peer2/publickey) allowed-ips 10.0.0.2/32,192.168.12.0/24
#启动网卡
ip link set wg0 up
#添加路由
ip route add 192.168.12.0/24 dev wg0 scope link
#保存配置
wg showconf wg0 > /etc/wireguard/wg0.conf 		#注意无地址
#查看配置
root@debian:/etc/wireguard# cat wg0.conf
[Interface]
Address = 10.0.0.1
ListenPort = 51820
PrivateKey = gIm3ivihh3R2Jqs9yB3XLcusFw1nUmGJ9izDNf0oW3c=

[Peer]
PublicKey = kJeFpOnu4rUULayVacp1l+9XkTO0kq7BS0VhPf9Vvi4=
AllowedIPs = 10.0.0.2/32, 192.168.12.0/24
#查看路由
root@debian:/etc/wireguard# ip route show
default via 192.168.100.2 dev ens33 onlink
10.0.0.2 dev wg0 scope link
192.168.11.0/24 dev ens37 proto kernel scope link src 192.168.11.11
192.168.12.0/24 dev wg0 scope link
192.168.100.0/24 dev ens33 proto kernel scope link src 192.168.100.11

client

#拉取密钥
scp -r root@192.168.100.11:/etc/wireguard/peer* /etc/wireguard

#目录:/etc/wireguard
#创建网卡
ip link add wg0 type wireguard
#设置地址
ip address add 10.0.0.2/24 dev wg0
#监听端口
wg set wg0 listen-port 51820 private-key ./peer2/privatekey
#设置参数
wg set wg0 peer $(cat ./peer1/publickey) allowed-ips 10.0.0.1/32,192.168.11.0/24 persistent-keepalive 25 endpoint 192.168.100.11:51820
#启动网卡
ip link set wg0 up
#添加路由
ip route add 192.168.11.0/24 dev wg0 scope link
#保存配置
wg showconf wg0 > /etc/wireguard/wg0.conf 
#查看配置
root@debian:/etc/wireguard# cat wg0.conf
[Interface]
Address = 10.0.0.2
ListenPort = 51820
PrivateKey = CK84IYu0pbDKe9inykdQqPEOH3Z6WsPJsUKUIa3Fmk0=

[Peer]
PublicKey = fH5dpb2PSQqqDAt6BE5If+j8hGXqYXOfMsIzEHj5M3o=
AllowedIPs = 10.0.0.1/32, 192.168.11.0/24
Endpoint = 192.168.100.11:51820
PersistentKeepalive = 25
#查看路由
root@debian:/etc/wireguard# ip route show
default via 192.168.100.2 dev ens33 onlink
10.0.0.0/24 dev wg0 proto kernel scope link src 10.0.0.2
192.168.11.0/24 dev wg0 scope link
192.168.12.0/24 dev ens37 proto kernel scope link src 192.168.12.12
192.168.100.0/24 dev ens33 proto kernel scope link src 192.168.100.12

sessiion

#server
root@debian:/etc/wireguard# wg
interface: wg0
  public key: fH5dpb2PSQqqDAt6BE5If+j8hGXqYXOfMsIzEHj5M3o=
  private key: (hidden)
  listening port: 51820

peer: kJeFpOnu4rUULayVacp1l+9XkTO0kq7BS0VhPf9Vvi4=
  endpoint: 192.168.100.12:51820
  allowed ips: 10.0.0.2/32, 192.168.12.0/24
  latest handshake: 1 minute, 46 seconds ago
  transfer: 756 B received, 668 B sent

#client
root@debian:/etc/wireguard# wg
interface: wg0
  public key: kJeFpOnu4rUULayVacp1l+9XkTO0kq7BS0VhPf9Vvi4=
  private key: (hidden)
  listening port: 51820

peer: fH5dpb2PSQqqDAt6BE5If+j8hGXqYXOfMsIzEHj5M3o=
  endpoint: 192.168.100.11:51820
  allowed ips: 10.0.0.1/32, 192.168.11.0/24
  latest handshake: 1 minute, 56 seconds ago
  transfer: 668 B received, 756 B sent
  persistent keepalive: every 25 seconds

test

#可以看见去10.0.0.0/24和192.168.12.0/24的路由均由wg0转发

ping -I 10.0.0.1 10.0.0.2		#通
ping -I 10.0.0.1 192.168.12.12	#通

#添加回城路由
ping -I 10.0.0.1 192.168.12.1	#不通,因通过vmware,主机无回程路由
#windows添加路由
route add 10.0.0.1 mask 255.255.255.255 192.168.12.12
#再次ping
ping -I 10.0.0.1 192.168.12.1
PING 192.168.12.1 (192.168.12.1) from 10.0.0.1 : 56(84) bytes of data.
64 bytes from 192.168.12.1: icmp_seq=1 ttl=127 time=1.50 ms

tcpdump

server操作:
ping -I 10.0.0.1 192.168.12.1

client抓包:
root@debian:/etc/wireguard# tcpdump -i ens33 port 51820
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), snapshot length 262144 bytes
14:11:23.329695 IP 192.168.100.11.51820 > 192.168.100.12.51820: UDP, length 128
14:11:23.330203 IP 192.168.100.12.51820 > 192.168.100.11.51820: UDP, length 128
14:11:38.447320 IP 192.168.100.11.51820 > 192.168.100.12.51820: UDP, length 32

root@debian:/etc/wireguard# tcpdump -i wg0 icmp
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on wg0, link-type RAW (Raw IP), snapshot length 262144 bytes
14:09:29.574750 IP 10.0.0.1 > 192.168.12.1: ICMP echo request, id 57861, seq 1, length 64
14:09:29.575050 IP 192.168.12.1 > 10.0.0.1: ICMP echo reply, id 57861, seq 1, length 64

分析:
ens33抓包因加密不可判断包内容,但从长度可判断ping包为128字节,wireguard会话保持包为32字节。证明从wg0转发经出口ens33进行外层地址封装。
wg0抓包可以看见原目地址解封装均为内网地址。

配置文件

# ==================== begin ===================
[Interface]
PrivateKey  = +JSj15aqL6VXxige5luKj3Ix5wA7Rx30QwbKHuAFGE4=
Address = 10.0.0.1
ListenPort = 51820
DNS = 114.114.114.114
# 规则说明:接受来源为%i的转发;接受出口为%i的转发;增加对出口为本机网卡的NAT映射
PostUp   = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ens33 -j MASQUERADE

[Peer]
PublicKey = U3wOXdy8t36jJUHRoXKEW5VGtjl+1aMTFwtDD1+rJws=
# 允许客户端使用的IP段,对于peer的隧道IP, 建议使用/32的固定IP, 以避免和其他peer重复导致无法连接
AllowedIPs = 192.168.11.0/24,172.16.0.0/24
Endpoint = 192.168.100.11:51820
PersistentKeepalive = 25
# ===================== end ====================

#通过配置文件启动执行过程
root@debian:/etc/wireguard# wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.0.0.1 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 10.0.0.2/32 dev wg0
[#] ip -4 route add 192.168.12.0/24 dev wg0

#设置自启
systemctl enable wg-quick@wg0.service

docker部署

docker run -d \
  --name=wireguard \
  --cap-add=NET_ADMIN \
  --cap-add=SYS_MODULE \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=Europe/London \
  -e SERVERURL=wireguard.domain.com `#optional` \
  -e SERVERPORT=51820 `#optional` \
  -e PEERS=3 `#optional` \
  -e PEERDNS=auto `#optional` \
  -e INTERNAL_SUBNET=10.13.13.0 `#optional` \
  -e ALLOWEDIPS=0.0.0.0/0 `#optional` \
  -p 51820:51820/udp \
  -v /path/to/appdata/config:/config \
  -v /lib/modules:/lib/modules \
  --sysctl="net.ipv4.conf.all.src_valid_mark=1" \
  --restart unless-stopped \
  lscr.io/linuxserver/wireguard
来源:https://blog.csdn.net/alucardXXOO/article/details/120763149

评论

此博客中的热门博文

近期折腾 tailscale 的一些心得

高可用用户中心设计

群晖硬软件的的各种坑及解决方案

打造一个可国内访问的Blogger(Blogspot)方法

星际蜗牛安装黑裙(群晖)制作家用nas

Cloudflare免费版设置说明

N1 PT下载小钢炮固件下载及安装说明

分析redis key大小的几种方法

Windows7系统目录迁移:Users,Program Files,ProgramData

个性化推荐从入门到精通