Skip to content

WireGuard — Xray-core

Xray-core 自带用户态 WireGuard 实现,可同时充当协议两端。在 Linux 上 还可选启用内核 TUN 快路径 —— 通过 noKernelTun: true 重新切回用户态 wireguard-go 设备。

设置

"protocol": "wireguard" 出站或入站的 settings

字段类型默认值允许值描述
noKernelTunboolfalsetrue | false禁用内核 TUN 快路径(仅 Linux),改用用户态 wireguard-go 设备。在无权限运行时有用。
secretKeystring(required)<key>本地私钥。接受 hex(64 字符)、base64(带或不带 padding)以及 URL-safe base64 —— 见 `infra/conf/wireguard.go:126` 的 ParseWireGuardKey。
address[]string[10.0.0.1, fd59:...:0001][<CIDR or IP>]本地接口地址。省略时使用两个 bogon IP(一个 IPv4、一个 IPv6)。
peers[]*WireGuardPeerConfig[][WireGuardPeerConfig]远端 peer。标准的单 peer 场景为一元素列表。
mtuint321420<bytes>隧道 MTU。0 会被替换为 1420(WireGuard 协议默认值)。
workersint320 (CPU-based)<int>加密管线的 worker goroutine 数。0 表示使用 runtime.NumCPU()。
reserved[]byte(empty)<3 bytes>三字节 reserved 字段覆盖(部分商业 WG 实现使用)。必须为空或恰好 3 字节。
domainStrategystringforceipforceip | forceipv4 | forceipv6 | forceipv4v6 | forceipv6v4peer endpoint 主机名的解析方式。四字母后缀控制首选哪个家族(v4v6 = 先试 IPv4 再回落 IPv6)。

源码: infra/conf/wireguard.go:51-62 · 锚定版本 v26.6.1 (94ffd50)

peers[]

字段类型默认值允许值描述
publicKeystring(required)<key>peer 公钥。可接受的格式与 `secretKey` 相同。
preSharedKeystring(unset)<key>可选 PSK,为握手提供额外的安全性。
endpointstring(required for outbound)<host:port>联系 peer 的目标地址。允许使用主机名,握手时会重新解析。
keepAliveuint320<seconds>WireGuard persistent-keepalive 间隔。0 表示禁用 keepalive。
allowedIPs[]string["0.0.0.0/0", "::0/0"][<CIDR>]路由到该 peer 的源 / 目的 CIDR。省略时默认全路由。

源码: infra/conf/wireguard.go:13-19 · 锚定版本 v26.6.1 (94ffd50)

示例

到单个 WireGuard peer 的出站:

json
{
  "outbounds": [
    {
      "tag": "wg-out",
      "protocol": "wireguard",
      "settings": {
        "secretKey": "<base64-private-key>",
        "address": ["10.0.0.2/32", "fd00:dead:beef::2/128"],
        "mtu": 1420,
        "peers": [
          {
            "publicKey": "<base64-peer-public-key>",
            "endpoint": "wg.example.com:51820",
            "allowedIPs": ["0.0.0.0/0", "::/0"],
            "keepAlive": 25
          }
        ],
        "domainStrategy": "forceipv4v6"
      }
    }
  ]
}

带 3 字节 reserved 字段的出站(部分商业服务):

json
{
  "outbounds": [
    {
      "tag": "wg-cloak",
      "protocol": "wireguard",
      "settings": {
        "secretKey": "<base64>",
        "address": ["172.16.0.2/32"],
        "reserved": [123, 45, 67],
        "peers": [
          { "publicKey": "<base64>", "endpoint": "engage.cloudflareclient.com:2408" }
        ]
      }
    }
  ]
}

说明

  • 内部一律把密钥归一化为 hex。输入端可接受 hex(64 字符)、 带 padding base64 与 URL-safe base64(infra/conf/wireguard.go:133-148)。
  • 若省略 address,会使用 bogon IP —— 对仅出站场景没问题,但任何使用 WireGuard 接口作为源地址的流量都会失败。在任何非平凡配置中都应当 显式设置该字段。
  • mtu: 0 在构建时静默改写为 1420 (infra/conf/wireguard.go:91-95)。
  • reserved 必须 为空或恰好 3 字节。长度为 2 或 4 的数组会构建 失败(infra/conf/wireguard.go:100-102)。
  • noKernelTun 是 Linux 上禁用内核 TUN 快路径的唯一方式。其他平台 始终使用用户态 wireguard-go。

跨内核说明

  • sing-box 在 1.11+ 中把 WireGuard 迁到了 endpoints 模型 —— 新字段形态(private_keyaddresspeers[]udp_timeout) 见 WireGuard — sing-box
  • mihomo 接受简化的单 peer 形式(顶层 server / port / public-key / allowed-ips)或完整的 peers: 列表,并提供 amnezia-wg-option 块用于 AmneziaWG 兼容。参见 WireGuard — mihomo

源码: infra/conf/wireguard.go:13-62 · v26.6.1 (94ffd50)

由 Argsment 出品的 Core Tutorial