使用FreeBSD + WireGuard为Android创建VPN隧道

2 min

language: ja bn en es hi pt ru zh-cn zh-tw

大家好,我是无能。

在上一篇文章中,我只发布了速度结果,但这里是创建隧道的步骤。

由于没有找到其他日语文章,特此记录。

WireGuard

手机端 (客户端)

您需要在PC端生成WireGuard密钥。

wg genkey | tee private.key
wg genpsk | tee psk.key

只要生成了私钥,公钥就会在WireGuard的手机版中显示,所以您不需要在这里生成它。

当然,您也可以同时使用wg genkey | tee private.key | wg pubkey > pub.key来生成。

将此密钥传递给手机后,在WireGuard手机应用中输入以下内容:

私钥 = private.key的内容  
预共享密钥 = psk.key的内容  
地址 = 想要通信的IP, 本次设置为`10.1.0.4/32`
端点 = WireGuard服务器的全局IP:51820
Allowed IPs = 0.0.0.0/0 , ::/0 # 所有流量通过VPN

输入这些信息后,确保此时生成的公钥可以发送或粘贴到服务器端。

PC端 (服务器端)

首先,假设您已经安装了WireGuard,请在配置文件末尾添加一个Peer。

[Peer]
PublicKey = 手机端输入private.key后显示的公钥,或者pub.key的内容
PreSharedKey = psk.key的内容
AllowedIPs = 10.1.0.4/32
PersistentKeepalive = 25

暂时执行wg quick up wg0

如果在手机端打开VPN后数据传输正常,则表示成功。但是,在这种状态下,通信只能在WireGuard隧道内部进行,因此需要创建NAT规则。

FreeBSD

首先,启用IPv4和IPv6的端口转发。

vi /etc/sysctl.conf

然后添加以下内容:

net.inet.ip.forwarding=1
net.inet6.ip6.forwarding=1

然后重启系统。

接下来,使用pf创建规则。

vi /etc/pf.conf

然后添加以下内容:

wireguard_clients=10.1.0.4
wanint="vtnet0"
wg_ports="{51820}"

nat on $wanint inet from $wireguard_clients to any -> $wanint
pass in on $wanint proto udp from any to $wanint port $wg_ports
pass out quick

之后启用它。

pfctl -nf /etc/pf.conf

如果没有错误,则执行:

pfctl -f /etc/pf.conf

这将应用规则。

请访问Takashi的网站,这是一个简单方便的工具,检查您的IP地址是否已变为WireGuard服务器的IP地址。

bibi.moe/ip
到此结束。

如果完成这些步骤,并且您将本地环境中运行的服务通过VPN连接到WireGuard网络,那么您就可以从智能手机浏览器访问家中的网络,这非常方便。

那么,下次再见。请多关照。

Related Posts