seud0nym/openwrt-wireguard-go
GitHub: seud0nym/openwrt-wireguard-go
为不支持内核 WireGuard 的 OpenWRT 设备提供轻量级用户态 WireGuard VPN 实现方案。
Stars: 27 | Forks: 6
# openwrt-wireguard-go
这是一个用 Golang 编写的完整用户空间 WireGuard 实现,专为不支持内核 WireGuard 的基于 OpenWRT 的设备设计。如果你的设备*确实*支持内核 WireGuard,则不应使用此项目。
该实现针对最小内存使用进行了优化。每个 WireGuard 接口应仅消耗约 20Mb 的 RAM。
有关 WireGuard 的更多信息,请参阅 https://www.wireguard.com/。
## 前置条件
设备固件**必须**具备内核 TUN 支持。你可以使用以下命令验证你的内核是否配置了 TUN:
```
zcat /proc/config.gz | grep CONFIG_TUN
```
如果此命令返回 `CONFIG_TUN=y`,则你*可以*使用此实现。如果返回 `# CONFIG_TUN is not set`,则无法使用。
## 安装
### 软件包安装
针对 **arm-cortex-a9** 架构的设备提供了一个 `opkg` 仓库。
#### opkg 配置
运行以下命令以正确配置 `opkg`:
```
grep -qE 'arch\s*\barm_cortex-a9\b' /etc/opkg.conf || echo 'arch arm_cortex-a9 10' >> /etc/opkg.conf
grep -q '/openwrt-wireguard-go/' /etc/opkg/customfeeds.conf || echo 'src/gz wg_go https://raw.githubusercontent.com/seud0nym/openwrt-wireguard-go/master/repository/arm_cortex-a9/base' >> /etc/opkg/customfeeds.conf
```
要安装或升级,请运行以下命令:
```
opkg update
opkg install wireguard-go
```
### 手动安装
#### 具有 ARMv5/6/7 处理器的设备
运行以下命令以下载并安装适合你设备的正确版本:
```
curl -skL https://raw.githubusercontent.com/seud0nym/openwrt-wireguard-go/master/install_arm.sh | sh -s --
```
该脚本将确定适合你处理器的正确版本。例如,这可能意味着即使你的设备具有 ARMv7 处理器,也可能会选择 ARMv5 版本,因为某些版本的 ARMv7 芯片没有浮点运算单元(FPU),这会导致 ARMv7 版本发生核心转储(core dump)。
如果在 `/etc/opkg.conf` 文件中正确配置了架构为 **arm-cortex-a9** 的 `opkg`,则将使用 `opkg` 命令下载并安装最新发布的 .ipk 文件。否则,相应的发布 tar 文件将被下载并解压到正确的位置。
#### 手动下载并执行安装脚本
如果你在不先查看脚本的情况下运行它感到不放心,只需下载并手动执行它:
```
curl -skLO https://raw.githubusercontent.com/seud0nym/openwrt-wireguard-go/master/install_arm.sh
chmod +x install_arm.sh
./install_arm.sh
```
### 为其他设备构建
这需要安装 go ≥ 1.16。
```
git clone https://github.com/seud0nym/openwrt-wireguard-go.git
cd openwrt-wireguard-go
./build.sh [ ...|all]
```
`build.sh` 脚本默认构建 ARM 架构的发布 .tgz 文件。你可以指定一个或多个有效的 [GOARCH](https://golang.org/doc/install/source#environment) 架构(用空格分隔)或 `all` 来构建所有版本。
要在设备上安装该版本,请执行:
```
tar -zxvf openwrt-wireguard-go_.tgz -C /
sysctl -e -p /etc/sysctl.d/99-wireguard.conf
```
请注意,只有 ARMv5 版本经过了测试。
## 用法
安装完成后,你可以使用 https://openwrt.org/docs/guide-user/services/vpn/wireguard/start 上的官方 OpenWRT 指南将 WireGuard 配置为[服务器](https://openwrt.org/docs/guide-user/services/vpn/wireguard/server)和/或[客户端](https://openwrt.org/docs/guide-user/services/vpn/wireguard/client)。
*但是*,**不要**安装指定的 opkg 软件包(`wireguard` 和 `wireguard-tools`)。如果你的设备不支持内核 WireGuard,那么 `wireguard` 软件包的安装可能会失败。`wireguard-tools` 软件包*可能*会工作,但它是没有必要的,因为本项目包含所有必需的文件,而 `wireguard-tools` 软件包会替换其中的部分或全部文件,从而导致一切崩溃。
如果你的设备*确实*支持内核 WireGuard,则不应使用此项目。如果你只需要一个 WireGuard [wg(8)](https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8) 工具的实现,请查看 [wg-go](https://github.com/seud0nym/wg-go)。
## 卸载
删除你创建的任何网络配置,然后:
### 软件包安装
```
opkg remove wireguard-go
```
### 手动安装
```
wg --uninstall
```
## 致谢
如果没有官方的 WireGuard 跨平台仓库,这个项目是不可能实现的:
- https://git.zx2c4.com/wireguard-go/about/
- https://github.com/WireGuard/wgctrl-go
标签:arm架构, Golang, OpenWRT, opkg包管理, TUN模块, VPN, WireGuard, 二进制发布, 代理工具, 内存优化, 加密通信, 安全编程, 嵌入式设备, 开源工具, 日志审计, 点对点隧道, 用户态实现, 网络安全, 网络安全, 翻墙, 路由器, 远程访问, 隐私保护, 隐私保护