跳转到内容

云服务器 + WireGuard

与 Tailscale 方案一样,网关不会在公网上监听:它只监听服务器的 WireGuard 地址,iPhone 通过隧道访问它。区别在于隧道由你自己运行,不依赖第三方协调服务。服务器只为 WireGuard 开放一个 UDP 端口,不为 hmob 开放任何端口。

示例使用以下地址,请替换为你自己的地址:

地址
WireGuard 网络 10.8.0.0/24
服务器(WireGuard 端点) 10.8.0.1,监听 UDP 51820
iPhone 10.8.0.2
  • 一台 Linux 服务器(x86_64 或 aarch64),已安装 herdr,有 root 权限,并已安装 wireguard-tools、nftables 和 qrencode(例如 sudo apt install wireguard-tools nftables qrencode)。
  • iPhone 上已安装官方 WireGuard App 和 Hmob App。

为服务器和手机生成密钥:

终端窗口
sudo sh -c 'umask 077; cd /etc/wireguard &&
wg genkey | tee server.key | wg pubkey > server.pub &&
wg genkey | tee phone.key | wg pubkey > phone.pub'

创建 /etc/wireguard/hmobwg.conf:

[Interface]
Address = 10.8.0.1/32
ListenPort = 51820
PostUp = wg set %i private-key /etc/wireguard/server.key
PreUp = nft -f /etc/wireguard/hmobwg.nft
PostDown = nft delete table inet hmobwg
[Peer]
# iPhone: the contents of /etc/wireguard/phone.pub
PublicKey = <phone public key>
AllowedIPs = 10.8.0.2/32

默认情况下,WireGuard 对端可以访问服务器上的所有服务。只放行 hmob,丢弃来自隧道的其他所有流量。创建 /etc/wireguard/hmobwg.nft:

table inet hmobwg {
chain input {
type filter hook input priority filter - 5; policy accept;
iifname "hmobwg" ip saddr 10.8.0.2 ip daddr 10.8.0.1 tcp dport 8443 accept comment "hmob"
iifname "hmobwg" drop
}
}

启动隧道:

终端窗口
sudo systemctl enable --now wg-quick@hmobwg
sudo wg show hmobwg

在云防火墙或安全组中放行入站 UDP 51820(如果可以,将来源限制为你使用的网络)。不要开放 TCP 8443。

在服务器上生成手机的配置,并以二维码形式显示:

终端窗口
SERVER_PUBLIC_IP=203.0.113.10 # 服务器的公网地址
sudo sh -c "umask 077; cat > /etc/wireguard/phone.conf" <<EOF
[Interface]
PrivateKey = $(sudo cat /etc/wireguard/phone.key)
Address = 10.8.0.2/32
[Peer]
PublicKey = $(sudo cat /etc/wireguard/server.pub)
Endpoint = $SERVER_PUBLIC_IP:51820
AllowedIPs = 10.8.0.1/32
PersistentKeepalive = 25
EOF
sudo qrencode -t ansiutf8 < /etc/wireguard/phone.conf

在 WireGuard App 中点击 + → 通过二维码创建,扫描二维码并打开隧道。然后从服务器上删除手机的密钥材料:

终端窗口
sudo rm /etc/wireguard/phone.conf /etc/wireguard/phone.key

AllowedIPs = 10.8.0.1/32 只让发往服务器的流量走隧道,手机的其他流量不受影响。

以运行 herdr 的用户(而非 root)执行:

终端窗口
curl -fsSL https://hmob-app.github.io/install.sh | sh
export PATH="$HOME/.local/bin:$PATH" # 如果脚本提示它不在 PATH 中
hmob install-service --bind 10.8.0.1
ss -ltn | grep 8443 # 只有 10.8.0.1:8443

这会安装一个 systemd 用户服务并启用 lingering,因此无需登录即可开机运行。

终端窗口
hmob pair --url https://10.8.0.1:8443

打开 WireGuard 隧道后,打开 Hmob App,点击 扫描配对二维码 并扫描二维码。请始终通过扫码配对:手动输入不包含证书指纹,在网关的自签名证书下会失败。

基于规则的 VPN 客户端(Stash、Clash 等)

Section titled “基于规则的 VPN 客户端(Stash、Clash 等)”

基于规则的客户端默认会让 10.0.0.0/8 等私有地址段绕过隧道,因此连接 10.8.0.1 会超时。请改用私有主机名:

  • 选择以 .test 或 .internal 结尾的名称,例如 hmob.wg.test。App 仅对这些保留后缀放宽 iOS 的系统信任检查,是否信任仍由二维码中的指纹决定。
  • 在服务器的 WireGuard 地址上运行一个小型 DNS 服务器,将该名称解析为 10.8.0.1,其他查询全部转发(例如 dnsmasq --host-record=hmob.wg.test,10.8.0.1 --listen-address=10.8.0.1 --bind-interfaces --server=1.1.1.1),并在 hmobwg.nft 的 drop 规则之前放行来自手机的 TCP/UDP 53。
  • 在客户端中使用 fake-ip DNS,将 WireGuard 出站的 dns 设为 10.8.0.1,并把 DOMAIN,hmob.wg.test,<your WireGuard outbound> 放在所有局域网直连规则之前。
  • 用 hmob pair --url https://hmob.wg.test:8443 配对。
现象 检查项
配对超时 WireGuard App 中隧道已打开,且 sudo wg show hmobwg 显示最近有握手;UDP 51820 已开放;sudo tcpdump -ni hmobwg port 8443 能看到数据包。
数据包到达但无法连接 运行 sudo nft list table inet hmobwg:accept 规则位于 drop 规则之前;ss -ltn 显示 hmob 在 10.8.0.1:8443 上监听。
“A TLS error caused the secure connection to fail” 你手动输入了地址,或使用了不以 .test / .internal 结尾的主机名。请重新扫码配对。
大屏幕内容卡住 将隧道 MTU 降到 1280–1380(在手机配置的 [Interface] 中设置 MTU = 1380)。