本文有地方存在错误和不准的地方,请移步到新的文章:在Sing-box上使用WireGuard
目标:
实现clash中配置wireguard,能够只开mihomo就能实现异地组网
设备: 服务器:Ubuntu22.04 PC:Windows 11 平板:matepad pro 11 冤种2022款 手机:p30pro
登录服务器,更新源,开放防火墙端口
apt update
apt install wireguard ufw
ufw allow 51820/tcp
ufw allow 51820/udp生成WireGuard公钥和私钥:
wg genkey > priv_key && wg pubkey < priv_key > pub_key && echo"" && echo "here is priv_key:" && cat priv_key && echo "" && echo "here is pub_key:" && cat pub_key && rm -f priv_key pub_key编辑wg0配置文件
vi /etc/wireguard/wg0.conf
示例配置文件:
[Interface]
Address = 10.0.0.1/32 #我的服务器的虚拟局域网的IP /32对应255.255.255.255的子网掩码
ListenPort = 51820
PrivateKey = 1234567890VKjsfhlkwefhliiwehfliwehfwe=
PostUp = iptables -I FORWARD -i wg0 -j ACCEPT
PostUp = iptables -I FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
PostUp = iptables -t nat -A POSTROUTING -s 192.168.6.0/24 -o eth0 -j MASQUERADE
PostUp = iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 192.168.6.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT
[Peer]
PublicKey = oooooooshfiashfefjerieroijgtnbsdoferrigo=
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = 8282882828282sfrfewrfewrfoitrgkbfodgbhiegrhr=
AllowedIPs = 10.0.0.3/32
[Peer]
PublicKey = pppppppppioetjhgtibtenbpetjbboptrjbirtbijbt=
AllowedIPs = 10.0.0.4/32按下ESC,输入:wq保存并退出配置文件
按照wg0配置快速启动WireGuard命令:
wg-quick up wg0
添加WireGuard的wg0配置文件的开机自动启动命令:
systemctl enable wg-quick@wg0.service
如果你修改了配置你可以这样快速重启WireGuard
wg-quick down wg0 && wg-quick up wg0
在mihomo中的配置:
merge file:
prepend-proxies:
- name: "wg"
type: wireguard
private-key: 8282882828282sfrfewrfewrfoitrgkbfodgbhiegrhr
server: 8.138.130.196
port: 51820
ip: 10.0.0.2
public-key: 8282882828282sfrfewrfewrfoitrgkbfodgbhiegrhr
allowed-ips: ['0.0.0.0/0']
udp: true
dns: [1.1.1.1, 8.8.8.8]Windows WireGuard终端配置:
[Interface]
PrivateKey = hruiufuehrfheriferhfiwjffjlwrwjgffwrhfr=
Address = 10.0.0.2/32
[Peer]
PublicKey = iwfhiwrfiwjrfljhjgoerjogjeroigjoerjgojwrg=
AllowedIPs = 10.0.0.0/24, 192.168.6.0/24
Endpoint = 8.138.130.196:51820你以为结束了?并没有,接下来是踩坑历史和详细的说明:
在刚开始的时候,我天真地以为,只要配置成这样就好了:
[Interface]
Address = 10.0.0.1/32
ListenPort = 51820 #WireGuard端口号
PrivateKey = 1111111hfrhflihewrilfhilerhfliehrf= #服务器的WireGuard私钥
PostUp = iptables -I FORWARD -s 10.0.0.0/24 -i wg0 -d 10.0.0.0/24 -j ACCEPT
PostUp = iptables -I FORWARD -s 10.0.0.0/24 -i wg0 -d 192.168.6.0/24 -j ACCEPT
PostUP = iptables -I FORWARD -s 192.168.6.0/24 -i wg0 -d 10.0.0.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.0.0.0/24 -i wg0 -d 10.0.0.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.0.0.0/24 -i wg0 -d 192.168.6.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 192.168.6.0/24 -i wg0 -d 10.0.0.0/24 -j ACCEPT
[Peer]
PublicKey = oooooooooojdlsgjitgjtgicWmwhkfrehfherfher= #客户端1的公钥 在客户端1需要填上你公钥对应的私钥
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = mmmmmmhiroghjetgetoigjeitjgioejtiijeg= #客户端2的公钥 在客户端2需要填上你公钥对应的私钥
AllowedIPs = 10.0.0.3/32
[Peer]
PublicKey = kkkkkkkkkkhfrhfoerhfoierhferfreghogh= #客户端3的公钥 在客户端3需要填上你公钥对应的私钥
AllowedIPs = 10.0.0.4/32客户端配置保持不变。
启动WireGuard,配置好客户端,一切看上去非常顺利,WireGuard客户端也连上了服务器,也成功的握手了
而且我在10.0.0.1 、10.0.0.2 、10.0.0.3互相ping对方,也非常的顺利,没有任何问题。
but,当我启动parsec的时候 parsec 800 报错
为什么?
fail to request /web/json
看起来它获取不到这个文件,但是我都是可以ping通的
在经过了不断地排错后,我意识到,会不会我的虚拟局域网根本就访问不了外部网络?
有道理
于是将配置的路由表进行修改,使用NAT转发流量,这样就可以实现访问外部网络了。
路由表配置部分:
PostUp = iptables -I FORWARD -i wg0 -j ACCEPT
#作用: 允许所有通过 wg0 接口进入的流量进行转发。
PostUp = iptables -I FORWARD -o wg0 -j ACCEPT
#作用: 允许所有通过 wg0 接口发出的流量进行转发。
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
#作用: 为源地址为 10.0.0.0/24 的流量在通过 eth0 接口时进行地址伪装(NAT)。它确保来自 10.0.0.0/24 子网的流量在通过外部接口 eth0 时使用NAT。
PostUp = iptables -t nat -A POSTROUTING -s 192.168.6.0/24 -o eth0 -j MASQUERADE
#作用: 为源地址为 192.168.6.0/24 的流量在通过 eth0 接口时进行地址伪装(NAT)。它确保来自 192.168.6.0/24 子网的流量在通过外部接口 eth0 时使用NAT。
PostUp = iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT
#作用: 允许所有发往UDP端口 51820 的流量。它确保WireGuard所使用的UDP端口 51820 的流量被允许进入。
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
#删除Postup对应的规则
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
#删除Postup对应的规则
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
#删除Postup对应的规则
PostDown = iptables -t nat -D POSTROUTING -s 192.168.6.0/24 -o eth0 -j MASQUERADE
#删除Postup对应的规则
PostDown = iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT
#删除Postup对应的规则修改完成后,将WireGuard重新启动。
这次它十分符合预期,一切正常。
parsec正常工作,我可以远程连接我的Windows电脑,而且不用开启WireGuard客户端,单单开启clash即可。