自动化与脚本
在 Linux 上用 EdgeSecurityAccess(…
适用于部署 ESA WireGuard VPN 服务器、创建/管理用户、生成客户端配置及排障的场景。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 Linux 上用 EdgeSecur特定
解决思路
- Linux 服务端安装 WireGuard:Debian/Ubuntu 执行 `apt install wireguard`;RHEL/CentOS 执行 `yum install wireguard-tools`。
- 安装 Go 1.26+:下载 `go1.26.linux-amd64.tar.gz`,`tar -C /usr/local -xzf go1.26.linux-amd64.tar.gz`,并 `export PATH=$PATH:/usr/local/go/bin`。
- Windows 客户端安装 WireGuard:执行 `winget install WireGuard.WireGuard`。
- Windows 客户端安装 ESA Desktop 所需 .NET Runtime:从 Microsoft 官网下载安装。
- 克隆 ESA 仓库:`git clone https://github.com/KochiyaSanaeNya/EdgeSecurityAccess.git`,然后 `cd EdgeSecurityAccess`。
- 构建 ESA 主服务端:`go build -o esa-server ./main.go`。
- 构建 ESA 工具包:`cd tools` 后分别执行 `gcc -o ESAusr ESAusr.c`、`gcc -o ESAProc ESAProc.c`、`gcc -o ESAInit ESAInit.c`,再 `cd ..`。
- 构建 ESA Desktop(Windows):在 Visual Studio 中打开解决方案并构建,或 `cd ESADesktop` 后执行 `dotnet build -c Release`。
- 生成服务端 WireGuard 密钥:`wg genkey | tee server_private.key | wg pubkey > server_public.key`。
- 客户端密钥由 ESAProc 自动生成;也可手动用 `wg genkey | tee client_private.key | wg pubkey > client_public.key`。
- 创建 `/etc/esa/esa.conf`,写入 `$servip`、`$subnet`、`$endpoint`、`$keeptime`、`$wgport`、`$httport`、`$servpriv`、`$servpub` 等配置项。
- 配置项含义:`$servip` 为虚拟网络内服务端 IP;`$subnet` 为虚拟网络子网;`$endpoint` 为外部 WireGuard 接入点;`$keeptime` 为 NAT 穿透保活间隔;`$wgport` 为 WireGuard UDP 端口;`$httport` 为 HTTP API 端口;`$servpriv` 为服务端 WireGuard 私钥;`$servpub` 为服务端 WireGuard 公钥。
- 用 ESAusr 添加用户:`cp tools/ESAusr /etc/esa/`,`cd /etc/esa`,运行 `./ESAusr`,按提示输入 username 和 password,用户写入 `users.txt`。
- 维护 `users.txt` 格式:`username:password:ip:client_public_key`,例如 `alice:secure_password:172.16.16.2/32:client_public_key_here`。
- 生成用户 WireGuard 配置:`cp tools/ESAProc /etc/esa/`,`cd /etc/esa`,运行 `./ESAProc`,输出 `usrwg.conf`。
- 初始化本地 WireGuard:`cp tools/ESAInit /etc/esa/`,`cd /etc/esa`,运行 `./ESAInit`,输出配置到 `/etc/wireguard/wg0.conf`。
- 启动 ESA 主服务端:`./esa-server -config /etc/esa/esa.conf`。
- 将 ESA 配置为 systemd 服务:创建 `/etc/systemd/system/esa.service`,设置 `ExecStart=/usr/local/bin/esa-server -config /etc/esa/esa.conf`、`Restart=on-failure`、`WantedBy=multi-user.target`,然后 `systemctl daemon-reload`、`systemctl enable esa`、`systemctl start esa`。
- 启动 WireGuard 接口:`wg-quick up wg0`。
- 检查 WireGuard 状态:`wg show`。
- 开机启用 WireGuard:`systemctl enable wg-quick@wg0`。
- 启用内核转发:`sysctl -w net.ipv4.ip_forward=1`,并写入 `/etc/sysctl.conf` 后 `sysctl -p` 持久化。
- 配置 NAT:`iptables -t nat -A POSTROUTING -s 172.16.16.0/24 -o eth0 -j MASQUERADE`(将 eth0 替换为实际网卡)。
- 配置转发规则:`iptables -A FORWARD -i wg0 -j ACCEPT` 和 `iptables -A FORWARD -o wg0 -j ACCEPT`。
- 通过 HTTP API 获取用户配置:`curl -X POST --data "username=alice&password=secure_password" http://127.0.0.1:50001`。
- API 返回内容为 WireGuard 配置,包含 `[Interface]` 的 PrivateKey/Address 和 `[Peer]` 的 PublicKey/AllowedIPs/Endpoint/PersistentKeepalive。
- Go 客户端获取配置:构造 `url.Values` 设置 username/password,用 `http.Post` 以 `application/x-www-form-urlencoded` 请求 API URL,读取响应体并返回。
- Python 客户端获取配置:用 `requests.post(api_url, data={'username': username, 'password': password})`,返回 `response.text`。
- Python 客户端保存并连接:将配置写入 `wg_client.conf`,再执行 `subprocess.run(['wg-quick', 'up', 'wg_client.conf'])`。
- Linux/macOS CLI 连接:`curl -X POST --data "username=alice&password=secure_password" http://127.0.0.1:50001 > esa_user.conf`,然后 `wg-quick up esa_user.conf`。
- 测试连接:`ping 172.16.16.1`。
- 断开连接:`wg-quick down esa_user.conf`。
- 使用 ESA Desktop(Windows):启动应用,输入 Username、Password、Server URL(如 `http://vpn.example.com:50001`),点击 Connect,WireGuard 隧道自动建立。
- 配置 Nginx HTTPS 反向代理:监听 443 ssl http2,设置 server_name、ssl_certificate、ssl_certificate_key,`location /` 中 `proxy_pass http://127.0.0.1:50001`,并转发 Host、X-Real-IP、X-Forwarded-For、X-Forwarded-Proto 头。
- 配置 Caddy HTTPS 反向代理:`vpn.example.com { reverse_proxy 127.0.0.1:50001 }`。
- 使用 HTTPS API:`curl -X POST --data "username=alice&password=secure_password" https://vpn.example.com`。
- 自动化为用户生成 WireGuard 密钥:用 `crypto/rand` 读取 32 字节私钥,用 `curve25519.X25519(private[:], curve25519.Basepoint)` 生成公钥,再 base64 编码。
- 自动添加用户到 `users.txt`:构造 `username:password:ip:pubKey` 行,以 `O_APPEND|O_WRONLY|O_CREATE` 和权限 `0600` 追加写入 `/etc/esa/users.txt`。
- 添加健康检查端点:创建 `/health` handler 返回 200 和 `OK`,监听 `:50002`。
- 备份用户配置:`backup_esa_users.sh` 中用 `tar -czf` 打包 `/etc/esa/users.txt`、`/etc/esa/usrwg.conf`、`/etc/esa/esa.conf` 到 `/var/backups/esa/esa_backup_$TIMESTAMP.tar.gz`。
- 备份保留策略:用 `ls -t` 加 `tail -n +8` 加 `xargs -r rm` 只保留最近 7 个备份。
- 排查服务端无法启动:用 `netstat -tuln | grep 50001`、`lsof -i :50001` 检查端口占用。
- 排查 WireGuard 端口:`netstat -tuln | grep 50000`。
- 验证配置语法:`cat /etc/esa/esa.conf | grep -E '^\$'`。
- 查看服务日志:`journalctl -u esa -f`。
- 排查认证失败:`grep "^username:" /etc/esa/users.txt` 确认用户存在。
- 检查 users.txt 格式:`cat /etc/esa/users.txt`,应为 `username:password:ip:pubkey`。
- 重新生成 usrwg.conf:`cd /etc/esa` 后运行 `./ESAProc`。
- 排查 WireGuard 连接问题:`wg show wg0`。
- 验证服务端 UDP 端口可达:`nc -u -v vpn.example.com 50000`。
- 检查路由:`ip route show`。
- 从客户端测试:`ping 172.16.16.1`。
- 检查服务端防火墙:`iptables -L -n -v` 和 `ufw status`。
- 放行 WireGuard 端口:`ufw allow 50000/udp`。
- 排查无法转发流量:`sysctl net.ipv4.ip_forward`,应输出 `net.ipv4.ip_forward = 1`。
- 检查 NAT 规则:`iptables -t nat -L -n -v`。
- 重新添加 NAT 规则:`iptables -t nat -A POSTROUTING -s 172.16.16.0/24 -o eth0 -j MASQUERADE`。
- 保存 iptables 规则:`iptables-save > /etc/iptables/rules.v4`。
- 排查 HTTP API 返回空:`curl -v -X POST --data "username=alice&password=secure_password" http://127.0.0.1:50001`。
- 检查 users.txt 格式:`cat /etc/esa/users.txt | sed 's/:/ : /g'`。
- 确认 usrwg.conf 已生成:`cat /etc/esa/usrwg.conf`。
- 重启 ESA 服务:`systemctl restart esa`。
- 处理密钥安全:`chmod 600 /etc/esa/users.txt`、`chmod 600 /etc/esa/usrwg.conf`、`chown root:root /etc/esa/*.txt /etc/esa/*.conf`。
- 注意 ESA 当前版本不加密 username/password,需用 HTTPS 反向代理保护凭据传输。
- 性能调优:必要时设置 `ip link set dev wg0 mtu 1420`。
- 优化 sysctl:向 `/etc/sysctl.conf` 追加 `net.core.rmem_max = 134217728`、`net.core.wmem_max = 134217728`、`net.ipv4.tcp_rmem = 4096 87380 67108864`、`net.ipv4.tcp_wmem = 4096 65536 67108864`,再 `sysctl -p`。
- 安全考虑:使用 HTTPS 反向代理(Nginx/Caddy)保护凭据。
- 安全考虑:users.txt 中密码为明文,需限制访问权限。
- 安全考虑:用户私钥存储在服务器上,属于架构限制。
- 安全考虑:无速率限制,需实现 fail2ban 或反向代理限速。
- 安全考虑:启用防火墙规则限制 HTTP API 访问。
- 安全考虑:定期备份配置文件。
- 安全考虑:监控日志中的认证尝试。
- 配置 fail2ban 过滤器:创建 `/etc/fail2ban/filter.d/esa.conf`,设置 `failregex = ^.*Authentication failed for user <HOST>.*$`,`ignoreregex =`。
- 配置 fail2ban jail:在 `/etc/fail2ban/jail.local` 中启用 `[esa]`,设置 `port = 50001`、`filter = esa`、`logpath = /var/log/esa/access.log`、`maxretry = 5`、`bantime = 3600`。
适用边界 · 注意事项
- 不适用于不想使用 EdgeSecurityAccess 这套专用工具、只需要通用 WireGuard 配置的场景。
- 不适用于要求用户私钥绝不落服务端、密码不得明文存储的高安全生产环境。
- 不适用于非 Linux 服务端或没有 Go/C 编译环境且无法安装依赖的场景。