Tunnel — mihomo
mihomo 提供两种等价的方式声明静态端口转发:顶层 tunnels: 块 (旧式,支持紧凑字符串形态)或 listeners: 下 type: tunnel 条目。
Listener 形态
listeners: 下 type: tunnel 条目。内嵌 BaseOption(listen、 port)。
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
network | []string | (required) | tcp | udp | 接受的传输。同时支持时传入 `[tcp, udp]`,可在两种协议上暴露同一个转发器。 |
target | string | (required) | <host:port> | 转发连接的目的地。 |
源码: listener/inbound/tunnel.go:13-17 · 锚定版本 v1.19.27 (5184081)
顶层 tunnels: 块
顶层形态简洁,早于统一的 listeners 模型。每条要么是 对象, 要么是 紧凑字符串:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
network | []string | (required) | tcp | udp | 接受的传输。对象形态取列表。 |
address | string | (required) | <host:port> | 监听地址。 |
target | string | (required) | <host:port> | 目的地。 |
proxy | string | (routing default) | <proxy or group name> | 把该 tunnel 的流量强制走指定的代理或组,绕过 rules 列表。 |
源码: listener/config/tunnel.go:11-16 · 锚定版本 v1.19.27 (5184081)
紧凑字符串形态为 network,address,target[,proxy],其中 network 取 tcp、udp 或 tcp/udp:
yaml
tunnels:
- tcp,127.0.0.1:5353,1.1.1.1:53 # 仅 TCP
- udp,127.0.0.1:5353,1.1.1.1:53 # 仅 UDP
- tcp/udp,127.0.0.1:5353,1.1.1.1:53 # 两者
- tcp,0.0.0.0:8080,10.0.0.10:80,VPN # 强制走代理 "VPN"对象形态把同样的内容按键展开:
yaml
tunnels:
- network: [tcp, udp]
address: 127.0.0.1:5353
target: 1.1.1.1:53
proxy: VPN示例
Listener 形态的 DNS 转发:
yaml
listeners:
- name: dns-fwd
type: tunnel
listen: 0.0.0.0
port: 5353
network: [tcp, udp]
target: 1.1.1.1:53顶层形态:经命名代理把 HTTP 转发到内网主机:
yaml
proxies:
- name: VPN
type: trojan
server: vpn.example.com
port: 443
password: <password>
tunnels:
- network: [tcp]
address: 0.0.0.0:8080
target: 10.0.0.10:80
proxy: VPN说明
- 紧凑字符串形态的
address与target都会在解析时按host:port字面量校验 (listener/config/tunnel.go:53-58)。缺端口会给出明确错误。 network中未知取值会在启动时被静默跳过并发出警告 (listener/inbound/tunnel.go:96-99)。仅接受tcp与udp。proxy字段会绕过 rules 列表 —— tunnel 的流量无视mode与任何 匹配的rules:条目,直接走该命名代理。需要固定且不受常规路由 影响的路径时使用。- listener 形态与
tunnels:形态可在同一配置中共存 —— 互不影响。
跨内核说明
- Xray-core 使用
dokodemo-door协议,单一入站内即可通过portMap字段做多端口转发。参见 Dokodemo — Xray-core。 - sing-box 没有专用 tunnel 类型 —— 请使用 Direct 入站 配合
override_address与override_port。
源码: listener/inbound/tunnel.go:13-17 · v1.19.27 (5184081)
