WireGuard — Xray-core
Xray-core 自带用户态 WireGuard 实现,可同时充当协议两端。隧道内的 TCP/IP 流量运行在 Xray 内置的轻量 TCP/IP 栈上;在 Linux 上,具备 CAP_NET_ADMIN 的出站会改用内核 TUN 接口 —— 设置 noKernelTun: true 可保持使用内置栈。
设置
"protocol": "wireguard" 出站或入站的 settings:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
noKernelTun | bool | false | true | false | 仅出站(Linux)。不使用内核 TUN 接口,改在 Xray 内置的轻量 TCP/IP 栈上运行隧道。没有 CAP_NET_ADMIN 时 Xray 本就会自动回落到内置栈。 |
secretKey | string | (required) | <key> | 本地私钥。接受 hex(64 字符)、base64(带或不带 padding)以及 URL-safe base64 —— 见 infra/conf/wireguard.go:134 的 ParseWireGuardKey。 |
address | []string | [10.0.0.1, fd59:...:0001] | [<CIDR or IP>] | 本地接口地址。省略时使用两个 bogon IP(一个 IPv4、一个 IPv6)。 |
peers | []*WireGuardPeerConfig | [] | [WireGuardPeerConfig] | 远端 peer。标准的单 peer 场景为一元素列表。 |
mtu | int32 | 1420 | <bytes> | 隧道 MTU。0 会被替换为 1420(WireGuard 协议默认值)。 |
reserved | []byte | (empty) | <3 bytes> | 三字节 reserved 字段覆盖(部分商业 WG 实现使用)。必须为空或恰好 3 字节。 |
remoteDNS | []string | [1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001] | [<IP>, …] | 仅出站。目标为域名时通过隧道查询的 DNS 服务器 —— 按 address 中包含的地址族发起 A 和/或 AAAA 查询;address 含 IPv6 地址时优先尝试 IPv6 应答。条目必须是 IP 字面量 —— 其他任何值(如 "local")都会使 Xray 在启动时中止。应答按其 TTL 缓存,最长 300 秒。 |
源码: infra/conf/wireguard.go:59-69 · 锚定版本 v26.11.9 (676eaf6)
peers[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
publicKey | string | (required) | <key> | peer 公钥。可接受的格式与 secretKey 相同。 |
preSharedKey | string | (unset) | <key> | 可选 PSK,为握手提供额外的安全性。 |
endpoint | string | (required for outbound) | <host:port> | 联系 peer 的目标地址。主机名会在出站第一次建立连接时用 Xray 的 DNS 解析一次(见“说明”)。 |
keepAlive | uint32 | 0 | <seconds> | WireGuard persistent-keepalive 间隔。0 表示禁用 keepalive。 |
allowedIPs | []string | ["0.0.0.0/0", "::0/0"] | [<CIDR>] | 路由到该 peer 的源 / 目的 CIDR。省略时默认全路由。 |
level | uint32 | 0 | <uint32> | 该 peer 的策略等级。仅入站(服务端)生效 —— 每个 peer 都会注册为一个用户。 |
email | string | (unset) | <string> | 统计 / 日志中显示的 peer 标识。仅入站(服务端)生效。 |
源码: infra/conf/wireguard.go:17-26 · 锚定版本 v26.11.9 (676eaf6)
示例
到单个 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
}
]
}
}
]
}带 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:141-156)。 - 若省略
address,会使用 bogon IP —— 对仅出站场景没问题,但任何使用 WireGuard 接口作为源地址的流量都会失败。在任何非平凡配置中都应当显式设置该字段。 mtu: 0在构建时静默改写为 1420(infra/conf/wireguard.go:116-120)。reserved必须 为空或恰好 3 字节。长度为 2 或 4 的数组会构建失败(infra/conf/wireguard.go:122-124)。- 内核 TUN 路径仅适用于 Linux 上的出站,且需要
CAP_NET_ADMIN;noKernelTun: true可禁用它。其他情况 —— 其他平台、无权限运行以及所有入站 —— WireGuard 都运行在 Xray 内置的轻量 TCP/IP 栈上。 - peer 的
endpoint主机名由 Xray 自身的 DNS 模块解析;设置了streamSettings.sockopt时,其domainStrategy会限定地址族(例如forceipv4只保留 IPv4 应答)。 - 在入站(服务端)块中,每个
peers[]条目都会注册为一个用户:其email与level会进入统计 / 日志与策略(infra/conf/wireguard.go:97-113)。在出站中这两个字段会被忽略。
跨内核说明
- sing-box 把 WireGuard 配置为 端点 —— 字段形态(
private_key、address、peers[]、udp_timeout)见 WireGuard — sing-box。 - mihomo 接受简化的单 peer 形式(顶层
server/port/public-key/allowed-ips)或完整的peers:列表,并提供amnezia-wg-option块用于 AmneziaWG 兼容。参见 WireGuard — mihomo。
源码: infra/conf/wireguard.go:17-69 · v26.11.9 (676eaf6)
