在使用 sing-box 作為代理伺服器時,將出站流量透過 Cloudflare WARP 代理是一個非常實用的技巧,不僅能解鎖流媒體,還能隱藏伺服器真實 IP。然而,如果你參考的是網路上的舊教學,在更新到 sing-box 1.12 或更高版本後,你可能會遇到滿滿的報錯。

這篇文章將帶你避開 SSH 貼上大段代碼的「吞字坑」,並正確適應 sing-box 1.12+ 版本的破壞性更新,實現支援 IPv6 優先的 WARP 雙棧出站。

為什麼舊版配置會報錯?

在配置過程中,你可能會遇到類似以下的報錯:

  • unknown field "server"
  • unknown field "local_address"
  • detour to an empty direct outbound makes no sense
  • missing route.default_domain_resolver

這些都不是你的錯,而是 sing-box 在近期版本中對架構進行了大幅度調整:

  1. WireGuard 獨立為 endpoints:新版不再將 WireGuard 寫在 outbounds 裡,而是獨立成一個頂層區塊 endpoints
  2. DNS 邏輯更嚴格:如果 direct 出站只是一個空殼,DNS 裡就不能亂加 "detour": "direct";且路由規則強制要求加上 default_domain_resolver
  3. 陣列結構改變:對端節點資訊必須寫在 peers 陣列中,欄位名稱也簡化為 addressport

準備工作

在開始前,請確認你已經取得 Cloudflare WARP 的 WireGuard 配置資訊(包含 IPv4/IPv6 雙棧地址、PrivateKeyPublicKey,以及用於避開 Cloudflare 偵測的 reserved 值)。

本文以 Debian/Ubuntu 環境為例,設定檔路徑預設為 /usr/local/etc/sing-box/config.json


最終完美配置與說明

為了避免在終端機 (Terminal) 中貼上多行代碼時發生緩衝區溢出 (Buffer Overflow) 導致 JSON 損毀,建議直接使用下方提供的單行壓縮寫入命令。

以下是完整的設定檔結構解析:

json
{
  "ntp": {
    "enabled": true,
    "server": "time.apple.com",
    "server_port": 123,
    "interval": "30m"
  },
  "dns": {
    "servers": [
      {
        "tag": "dns-cloudflare",
        "type": "https",
        "server": "1.1.1.1",
        "path": "/dns-query"
      },
      {
        "tag": "dns-aliyun",
        "type": "https",
        "server": "223.5.5.5",
        "path": "/dns-query"
      }
    ],
    "strategy": "prefer_ipv6" 
  },
  "inbounds": [
    {
      "type": "vless",
      "tag": "你的節點名稱",
      "listen": "127.0.0.1",
      "listen_port": 58992,
      "users": [
        {
          "uuid": "你的-UUID",
          "flow": ""
        }
      ],
      "transport": {
        "type": "ws",
        "path": "/你的路徑"
      }
    }
  ],
  "endpoints": [
    {
      "type": "wireguard",
      "tag": "warp-out",
      "address": [
        "100.96.0.58/32",
        "2606:4700:cf1:1000::3a/128"
      ],
      "private_key": "你的私鑰=",
      "peers": [
        {
          "address": "162.159.193.10",
          "port": 2408,
          "public_key": "Cloudflare的公鑰=",
          "allowed_ips": [
            "0.0.0.0/0",
            "::/0"
          ],
          "reserved": [11, 212, 165]
        }
      ],
      "mtu": 1280
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "inbound": ["你的節點名稱"],
        "outbound": "warp-out"
      }
    ],
    "final": "warp-out",
    "default_domain_resolver": "dns-cloudflare"
  }
}