18

个人办公用 wireguard 组网笔记

 3 years ago
source link: https://zhangguanzhang.github.io/2020/08/05/wireguard-for-personal/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

个人办公用 wireguard 组网笔记



字数统计: 1.3k阅读时长: 5 min
 2020/08/05  279  Share

作为 IT 人员,经常需要连到办公网工作,并不是每个公司都有 vpn,自己搭建的话 openvpn 之类的配置麻烦啰嗦。这里写下 wireguard 的简单搭建。它比 IPSec 更快,更简单,更精简,更有用。它比 OpenVPN 更高效。WireGuard 设计为通用 VPN,适用于多种不同情况。它是跨平台的,可大规模部署。

通常如下图的部署: 一台 ECS 主机,得有公网 IP,下图就是 pc ----> ECS <------ 公司的 pc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
            +----------+
| |
+------->+ ECS +<-----+
| +----------+ |
| |
| |
| |
| | company
| +---+------------------+
+--++ | |
|PC | | +---+ |
+---+ | |PC | |
| +---+ |
| |
+----------------------+

当然,如果你会折腾的话 pc 可以是软路由,有兴趣和条件的可以看我博客 proxmox x86软路由笔记

登录到 ECS 上

得益于 wireguard 中没有 client/server 的概念,只要所有 nat 中的某台机器能够和 gateway 主机建立连接,即可实现共享所有节点的网络资源。这里 ECS 有公网ip,所以担当 gateway

安装 wireguard

官方安装文档 ,ECS是 linux 系统的话内核要5.x以上,没有就升级下内核,其他个人 pc 电脑则下载客户端,当然软路由的话则去找个带 wireguard的固件。

1
sysctl -w net.ipv4.ip_forward=1
1
cd /etc/wireguard

生成密钥对

wg 的每个互相之间要一对密钥,例如 A 连 gateway, A 需要 gateway的公钥,gateway 需要 A 的公钥,不能共用一套密钥对。

生成 gateway 的密钥对

1
wg genkey | tee gw-privatekey | wg pubkey > gw-publickey

生成个人电脑的密钥对

1
wg genkey | tee pc-privatekey | wg pubkey > pc-publickey

生成公司电脑的密钥对

1
wg genkey | tee cm-pc-privatekey | wg pubkey > cm-pc-publickey

wg 的组网定义一个网段,例如我的是 10.1.0.1/24,ecs 上配置文件为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cat > wg0.conf <<EOF
[Interface]
ListenPort = 16000 # 客户端连过来填写的端口,安全组的tcp和udp都要放行
Address = 10.1.0.1/24 #组网的内网ip和段
PrivateKey = $(cat gw-privatekey) # 使用 shell 读取gateway的私钥到这里
# 下面两条是放行的iptables和MASQUERADE
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

# pc
[Peer]
PublicKey = $(cat pc-publickey)
AllowedIPs = 10.1.0.2/32

# company router
[Peer]
PublicKey = $(cat cm-pc-publickey)
AllowedIPs = 10.1.0.3/32, 192.168.2.0/24, 10.243.0.0/16, 10.0.6.0/24, 172.13.0.0/16

EOF

然后是每个客户端的配置文件,下面是我笔记本 wg 的客户端软件配置文件内容。

1
2
3
4
5
6
7
8
9
10
11
12
cat > pc.conf <<EOF
[Interface]
PrivateKey = $(cat pc-privatekey)
Address = 10.1.0.2/24 #组网的内网ip和段,主机位每个得不一样
# DNS = 192.168.2.3

[Peer]
PublicKey = $(cat gw-publickey) # gateway的公钥
AllowedIPs = 10.1.0.0/24, 192.168.2.0/24, 10.243.0.0/16, 10.0.6.0/24, 172.13.0.0/16
Endpoint = $(curl -s ip.sb):16000 #gateway 公网ip和端口
PersistentKeepalive = 10 # 心跳时间
EOF

公司的电脑 wg 配置文件

1
2
3
4
5
6
7
8
9
10
11
cat > cm-pc.conf <<EOF
[Interface]
PrivateKey = $(cat cm-pc-privatekey)
Address = 10.1.0.3/24 #组网的内网ip和段,主机位每个得不一样

[Peer]
PublicKey = $(cat gw-publickey) # gateway的公钥
AllowedIPs = 10.1.0.0/24
Endpoint = $(curl -s ip.sb):16000 # gateway 公网ip和端口
PersistentKeepalive = 10 # 心跳时间
EOF

然后把 pc.confcm-pc.conf 的内容拷贝到对应的 wg 客户端软件里。

讲解下配置文件,我办公室是台式机proxmox里的软路由,并不是上面我说的办公室pc,这样我接入的设备也可以访问。我个人是推荐办公室搞个proxmox整虚拟机和软路由。

办公网的路由器网段是 192.168.2.0/24192.168.2.3是软路由,主要是上面有dns server(adguard home),办公网内添加hosts我是直接在dns server上添加的。所以我个人PC那里写了 DNS = 192.168.2.3,这样dns 解析都走到办公网的软路由上,家里不需要本地配置hosts。

10.243.0.0/16, 10.0.6.0/24, 172.13.0.0/16的网段都是办公网的内网网段。AllowedIPs意思就是把请求目的IP是这些网段的,都发到 wg0 这个接口上,也就是添加路由表。这样我在家里,我个人 pc 打开 wg后,就能访问办公网了。

ECS上启动 wg 和停止 wg

1
2
3
wg-quick up wg0 #默认取 /etc/wireguard/$name.conf
# 指定配置文件启动 wg-quick up /etc/wireguard/wg0.conf
wg-quick down wg0

PostUp 和 PostDown 就是启动后和停止后的命令,是Linux的话就推荐写iptables放行转发和做 NAT。如果是软路由,开了 passwall 代理之类的,记得把 ECS 的 公网IP 设置为不走代理。

查看组网状态,shell上wg回车即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ wg
interface: wg0
public key: FZcFhf0eq2yFgXPNBqYnpoZHnzmgFI7JCLp/5vn1DG0=
private key: (hidden)
listening port: 16000

peer: OtydRPJDt+H8upZDz5zJueRjUQ0tS4tr9P6w4BL2+w0=
endpoint: xxxxxxxxxxx:53956
allowed ips: 10.1.0.3/32, 192.168.2.0/24, 10.243.0.0/16, 10.0.6.0/24, 172.13.0.0/16
latest handshake: 1 minute, 2 seconds ago
transfer: 485.48 MiB received, 55.88 MiB sent

peer: VkhLdmaPS2KmhlSOrPk1XS1MWZrhb+00BdsC0swUBhk=
endpoint: xxxxxxxxxx:13545
allowed ips: 10.1.0.2/32
latest handshake: 21 minutes, 25 seconds ago
transfer: 56.11 MiB received, 476.83 MiB sent

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK