跳到內容

雲端伺服器 + 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
  • 一台已安裝 herdr 的 Linux 伺服器(x86_64 或 aarch64),具備 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。

在伺服器上產生手機的設定,並以 QR 碼顯示:

終端機視窗
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 中點選 + → Create from QR code,掃描後開啟通道。接著從伺服器刪除手機的金鑰資料:

終端機視窗
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,點選 掃描配對 QR 碼 並掃描 QR 碼。請務必以掃描方式配對:手動輸入沒有憑證指紋,連線到閘道的自簽憑證時會失敗。

規則式 VPN 用戶端(Stash、Clash 等)

Section titled “規則式 VPN 用戶端(Stash、Clash 等)”

規則式用戶端預設會將 10.0.0.0/8 這類私人位址範圍導向通道之外,因此連線到 10.8.0.1 會逾時。請改用私人主機名稱:

  • 選擇以 .test 或 .internal 結尾的名稱,例如 hmob.wg.test。App 只對這些保留後綴放寬 iOS 的系統信任檢查;是否信任仍由 QR 碼中的指紋決定。
  • 在伺服器的 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)。