不见春山
骑马倚斜桥,满楼红袖招。
Home
Categories
Archives
Tags
About
Home
WireGuard 搭建组网教程
WireGuard 搭建组网教程
取消
WireGuard 搭建组网教程
由
ctaoist
发布于 2022-04-11
·
最后更新:2024-02-29
1
IOS 只支持一个 vpn 连接,如果想科学上网和连上家里的局域网,就需要频繁切换 vpn。之前使用了 tailscale 组网,choc 支持 tailscale,但作者疑似跑路。 Stash 和 Surge 支持 wireguard 协议,这样出国和回家就能在同一个 vpn 中实现,不同来回切换,索性再用 wireguard 再组一个虚拟网络。 >shadowrocket 也支持 wireguard,但我尝试了一下,和服务端握手后仍然 ping 不通。 网络拓扑图如下: ![](https://blog.qiniu.ctaoist.cn/vpn_wireguard_网络拓扑图.png) 用一个有公网服务器做 WireGuard 服务端,OpenWrt 和 IOS 做客户端。WireGuard vpn 的网段为:`10.1.1.0/24`,OpenWrt 的网段为 `192.168.1.0/24`,网段可以根据自己的需求修改。 ## WireGuard 简介 WireGuard 是一个利用现有社会最先进的加密技术而产生的非常简单和快捷的 VPN 工具。它的目标是比 IPsec 更快,更简单,更精简,更易用,同时避免大规模配置 IPsec 的麻烦事。同时 WireGuard 也打算比 OpenVPN 更高效。WireGuard 设计为通用 VPN,可在嵌入式设备和常见计算机上运行,适用于多种不同情况。WireGuard 最初是为 Linux 内核发布的,而现在 WireGuard 已经可广泛部署并且跨平台支持。WireGuard 目前正在大力发展,但 WireGuard 已经被认为是业内最安全,最易用和最简单的 VPN 解决方案。 - [官网链接](https://www.wireguard.com) - [Arch Wiki](https://wiki.archlinux.org/index.php/WireGuard_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)) 5.6 及以上内核则已经集成了 WireGuard,如果内核版本低于5.6,则有如下几个安装方法可供选择: - 安装版本高于 5.6 的内核 - 安装 wireguard 内核模块 - 安装 wireguard-go 理论网络性能:**内核集成** ≥ **内核模块** > **wireguard-go** 安装没什么好说的,[官网链接](https://www.wireguard.com/install/) 已非常详细。 ```sh apt install wireguard # 可能需要重启 modprobe wireguard ``` ## 搭建服务端 先生成公钥和私钥: ```sh cd /etc/wireguard wg genkey | tee privatekey | wg pubkey > publickey ``` 配置文件使用 INI 语法作为其配置文件格式,可以放在任何路径下,但必须通过绝对路径引用。默认路径是 `/etc/wireguard/wg0.conf`,命名形式必须为 `${WireGuard 接口的名称}.conf`,常情况下 WireGuard 接口名称以 `wg` 为前缀,并从 `0` 开始编号,但也可以使用其他名称,只要符合正则表达式 `^[a-zA-Z0-9_=+.-]{1,15}$` 就行。 ```ini [Interface] # Name = node1.example.tld Address = 10.1.1.1/32 ListenPort = 51820 PrivateKey = serverPrivateKeyAbcAbcAbc= #DNS = 1.1.1.1,8.8.8.8 #Table = 12345 #MTU = 1500 #PreUp = /bin/example arg1 arg2 %i PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE #PreDown = /bin/example arg1 arg2 %i PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE # Openwrt [Peer] AllowedIPs = 10.1.1.2/32, 192.168.1.0/24 PublicKey = clientPublicKeyAbcAbcAbc= PersistentKeepalive = 25 # IOS [Peer] AllowedIPs = 10.1.1.3/32 PublicKey = client2PublicKeyAbcAbcAbc= PersistentKeepalive = 25 ``` >防火墙记得放行对应 **UDP 端口**。 >所有 Peer 的 `AllowedIPs` 最好不要有重叠部分,容易出现问题。我之前就是,两个 Peer 的 `AllowedIPs` 都是 `10.1.1.0/24`,OpenWrt 那边能成功和服务端握手,但就是 **ping 不通**。 PostUp 钩子启用 iptables 伪装。这允许流量离开服务器,使VPN客户端可以访问互联网。 PostUp 和 PostDown 中的 `eth0` 需要替换成自己的网口,不太确定可以用以下方法确定: ```sh ip -o -4 route show to default | awk '{print $5}' ``` PostDown 钩子:我们在关闭接口之前删除iptables伪装。一旦接口关闭,iptablesnat转发规则将被删除。 ### 启用 Wireguard 接口 ```sh wg-quick up wg0 # 开机自启动 systemctl enable wg-quick@wg0 # 查看状态 wg show ``` 并开启 ipv4 转发,编辑 `/etc/sysctl.conf`:`net.ipv4.ip_forward=1` ## OpenWrt 配置 ```sh opkg install wireguard luci-app-wireguard luci-proto-wireguard luci-i18n-wireguard-zh-cn ``` 生成公钥和私钥随便保存在哪里:`wg genkey | tee privatekey | wg pubkey > publickey`。 在 **网络**->**接口** 页面新增一个接口,协议选择 `WireGuardVPN`,配置好私钥、监听端口、本机的 vpn 地址等。 在接口编辑页面添加 Peer,填上服务器端的公钥,允许的 ip:`10.1.1.0/24`,端点主机填服务端的公网ip或对应的域名,端口填服务端监听的端口,本例中是 `51820`。 ## IOS 配置 Stash 和 Surge 的 WireGuard 都能正常工作,迫于 Surge 太贵,只能使用 Stash 了。 生成IOS端的公钥和私钥随便保存在哪里:`wg genkey | tee privatekey | wg pubkey > publickey`。 Stash 添加一个 wireguard 类型的代理,乏善可陈,就是**选项**里面的私钥填IOS端的私钥,公钥填服务端的公钥,MTU留空或者1500。 **遇到的奇怪问题**:就是IOS端启动 vpn 后,能 ping 通 ios 这边,但是 ios 却访问不了 openwrt 局域网里面的资源,苦苦尝试,搜索枯肠,最终发现在 Stash 的 **设置**->**网络设置** 里面有**跳过代理**和**跳过路由**两个选项,点进去看里面网段都有 `10.0.0.0/8` 和 `192.168.0.0/16` 等,Bypass 的网段覆盖了我们自己的 vpn 网段 `10.1.1.0/24` 和 openwrt 网段 `192.168.1.0/24`,**删掉这些或者使用另外不跳过的网段**就正常了。 ## Mac OSX 应用商店中的 APP 对系统版本有要求,能直接用最好,如果不能直接使用商店里面的 APP,可以使用 brew 安装: ```sh brew install wireguard-tools ``` `wireguard-tools` 依赖 `wireguard-go` 包,`wireguard-go` 又依赖 `go`,而 brew 会去下载 `go1.17`,这个版本太老,下载链接已经失效,可以考虑自己编译 `wireguard-go`,然后 brew 中编辑 wireguard-tools 的安装文件,去掉相应的依赖项。剩下的操作和其他客户端一样。 ## 其他客户端 配置是类似的,以 Windows 客户端为例,`AllowedIPs` 填 `10.1.1.0/24, 192.168.1.0/24`,这样 windows 电脑也能直接访问 OpenWrt 的局域网资源。 ```ini [Interface] PrivateKey = PrivateKey= ListenPort = 51820 Address = 10.1.1.x/32 [Peer] PublicKey = PublicKey= AllowedIPs = 10.1.1.0/24, 192.168.1.0/24 Endpoint = ip:51820 ``` ## 参考 - [WireGuard 教程:WireGuard 的搭建使用与配置详解](https://icloudnative.io/posts/wireguard-docs-practice/) - [如何在Debian 11安装WireGuard VPN](https://www.myfreax.com/how-to-set-up-wireguard-vpn-on-debian-11/) - [Stash wiki](https://stash.wiki/proxy-protocols/proxy-types#wireguard) - [Wireguard笔记(二) 命令行操作](https://www.cnblogs.com/milton/p/15339898.html)
VPN
该博客文章由作者通过
CC BY 4.0
进行授权。
分享
最近更新
WireGuard 搭建组网教程
LVM 管理
HK1 RBOX X4 电视盒子折腾笔记
使用usbip网络转发usb设备到远程主机
Altera 烧写双镜像
热门标签
VPN
Router
Tensorflow
Linux
ROS
虚拟组网
机器学习
Latex
DNS
pandoc
文章目录
N2N 搭建教程
Leanote 改造笔记