Skip to content

WireGuard — mihomo

mihomo 的 WireGuard 出站在用户态运行(基于 gVisor)。schema 同时提供 简化的单 peer 形态(peer 字段位于代理根部)与通过 peers: 的完整 多 peer 形态。可选的 amnezia-wg-option 块用于与 AmneziaWG 风格的 服务器互通。

出站

proxies:type: wireguard 条目。内嵌 BasicOption,并组合 WireGuardPeerOption 的简化 peer 字段。

字段类型默认值允许值描述
namestring(required)<string>唯一的代理名。
ipstring(unset)<IPv4 CIDR>本地隧道 IPv4 地址(如 `10.0.0.2/32`)。
ipv6string(unset)<IPv6 CIDR>本地隧道 IPv6 地址。
private-keystring(required)<base64 key>本地私钥。
workersint(CPU-based)<int>加密管线 worker 数。
mtuint1408<bytes>隧道 MTU。
udpboolfalsetrue | false启用 UDP 转发。
persistent-keepaliveint0<seconds>persistent-keepalive 间隔。0 表示禁用。
amnezia-wg-option*AmneziaWGOption(unset)AmneziaWGOptionAmneziaWG 混淆参数(junk 包、头部掩码)。
peers[]WireGuardPeerOption(use simplified shape)[WireGuardPeerOption]完整的多 peer 列表。设置后内嵌的简化字段被忽略。
remote-dns-resolveboolfalsetrue | false使用 peer 的解析器在 WireGuard 隧道内解析 DNS。
dns[]string[][<DNS server>]`remote-dns-resolve` 为 true 时隧道内使用的解析器。
refresh-server-ip-intervalint0<seconds>每 N 秒重新解析 peer 主机名。0 禁用周期性重解析。

源码: adapter/outbound/wireguard.go:57-77 · 锚定版本 v1.19.27 (5184081)

peers[] —— 多 peer 形态

字段类型默认值允许值描述
serverstring(required)<host>peer 主机名或 IP。
portint(required)<port>peer UDP 端口。
public-keystring(required)<base64 key>peer 公钥。
pre-shared-keystring(unset)<base64 key>可选 PSK。
reserved[]uint8(empty)<3 bytes>3 字节 reserved 字段覆盖。
allowed-ips[]string[][<CIDR>]该 peer 的 Allowed-IPs。

源码: adapter/outbound/wireguard.go:79-86 · 锚定版本 v1.19.27 (5184081)

amnezia-wg-option

字段类型默认值允许值描述
jcint0<int>每次握手的 junk 包数量。
jminint0<bytes>junk 包最小尺寸。
jmaxint0<bytes>junk 包最大尺寸。
s1int0<bytes>init 包前的 padding 长度。
s2int0<bytes>response 包前的 padding 长度。
s3int0<bytes>AmneziaWG v1.5/v2 —— cookie 包前的 padding 长度。
s4int0<bytes>AmneziaWG v1.5/v2 —— data 包前的 padding 长度。
h1string(unset)<int or hex>init 包的 header magic。数值或 hex。
h2string(unset)<int or hex>response 包的 header magic。
h3string(unset)<int or hex>cookie 包的 header magic。
h4string(unset)<int or hex>data 包的 header magic。
i1string(unset)<hex>AmneziaWG v1.5/v2 —— 特殊包 1 的载荷。
i2string(unset)<hex>特殊包 2 载荷。
i3string(unset)<hex>特殊包 3 载荷。
i4string(unset)<hex>特殊包 4 载荷。
i5string(unset)<hex>特殊包 5 载荷。
j1string(unset)<hex>仅 AmneziaWG v1.5 —— junk 包 1 载荷。
j2string(unset)<hex>仅 AmneziaWG v1.5 —— junk 包 2 载荷。
j3string(unset)<hex>仅 AmneziaWG v1.5 —— junk 包 3 载荷。
itimeint640<seconds>仅 AmneziaWG v1.5 —— junk 包发送节奏。

源码: adapter/outbound/wireguard.go:88-109 · 锚定版本 v1.19.27 (5184081)

示例

简化的单 peer 出站:

yaml
proxies:
  - name: wg-simple
    type: wireguard
    server: wg.example.com
    port: 51820
    private-key: <base64>
    public-key: <base64 peer key>
    ip: 10.0.0.2/32
    ipv6: fd00::2/128
    allowed-ips: ['0.0.0.0/0', '::/0']
    udp: true
    persistent-keepalive: 25

多 peer 出站(如 hub-and-spoke 拓扑):

yaml
proxies:
  - name: wg-multi
    type: wireguard
    private-key: <base64>
    ip: 10.0.0.2/32
    udp: true
    peers:
      - server: spoke1.example.com
        port: 51820
        public-key: <base64-spoke1>
        allowed-ips: ['10.0.1.0/24']
      - server: spoke2.example.com
        port: 51820
        public-key: <base64-spoke2>
        allowed-ips: ['10.0.2.0/24']

AmneziaWG 兼容的出站:

yaml
proxies:
  - name: awg
    type: wireguard
    server: awg.example.com
    port: 12345
    private-key: <base64>
    public-key: <base64>
    ip: 10.13.13.2/32
    allowed-ips: ['0.0.0.0/0']
    udp: true
    amnezia-wg-option:
      jc: 4
      jmin: 40
      jmax: 80
      s1: 0
      s2: 0
      h1: '0x12345678'
      h2: '0x87654321'
      h3: '0xabcdef01'
      h4: '0x10fedcba'

说明

  • mihomo 同时接受简化形态(顶层 server / port / public-key / allowed-ips)与完整的 peers: 列表。设置 peers 时内嵌的简化字段被忽略。
  • remote-dns-resolve: true 让 WireGuard 通过 dns: 列出的解析器 在隧道内查询 DNS(而不使用本地解析器)。当本地 DNS 不能到达目标 时有用。
  • refresh-server-ip-interval 仅在 peer 的 server 是主机名时 有意义;按固定间隔重新解析,适合动态 DNS 端点。
  • amnezia-wg-option 字段有版本之分:s3 / s4 / i1-i5 属于 AmneziaWG v1.5+;j1 / j2 / j3 / itime 仅 v1.5 (v2.0 已移除)。见源码注释 adapter/outbound/wireguard.go:94-108

跨内核说明

  • Xray-core 始终使用 peers:(没有简化形态),并暴露 Linux 快路 径相关的 noKernelTun 字段,字段名为 camelCase(secretKeyaddresspublicKey)。参见 WireGuard — Xray-core
  • sing-box 已把 WireGuard 迁到 endpoints 模型 —— 不再位于 outbounds[]。字段名为 snake_case(private_keyallowed_ips)。 参见 WireGuard — sing-box

源码: adapter/outbound/wireguard.go:57-109 · v1.19.27 (5184081)

由 Argsment 出品的 Core Tutorial