重定向 & TProxy — Xray-core
Xray 没有 专门的 redirect 或 tproxy 入站类型。两种模式都 通过 Dokodemo-door 入站配合 streamSettings.sockopt 实现。
REDIRECT(Linux iptables NAT)
使用启用 followRedirect: true 的 Dokodemo 入站:
json
{
"inbounds": [{
"tag": "redir-in",
"listen": "127.0.0.1",
"port": 12345,
"protocol": "dokodemo-door",
"settings": {
"network": "tcp",
"followRedirect": true
}
}]
}然后安装对应的 iptables REDIRECT 规则:
sh
iptables -t nat -N XRAY
iptables -t nat -A XRAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A XRAY -d 10.0.0.0/8 -j RETURN
iptables -t nat -A XRAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A XRAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A XRAY -p tcp -j REDIRECT --to-ports 12345
iptables -t nat -A OUTPUT -p tcp -j XRAYREDIRECT 仅支持 TCP。UDP 透明代理需要 TPROXY。
TPROXY(Linux iptables mangle)
json
{
"inbounds": [{
"tag": "tproxy-in",
"listen": "0.0.0.0",
"port": 12345,
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
},
"streamSettings": {
"sockopt": {
"tproxy": "tproxy",
"mark": 255
}
}
}]
}对应的 iptables 规则(TCP + UDP,并带基于 mark 的旁路):
sh
# 出站流量的自定义链
iptables -t mangle -N XRAY
iptables -t mangle -A XRAY -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A XRAY -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A XRAY -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A XRAY -m mark --mark 255 -j RETURN
iptables -t mangle -A XRAY -p tcp -j TPROXY --on-port 12345 --tproxy-mark 0x1
iptables -t mangle -A XRAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 0x1
iptables -t mangle -A PREROUTING -j XRAY
# 标记包的路由规则
ip rule add fwmark 0x1 table 100
ip route add local 0.0.0.0/0 dev lo table 100sockopt 中的 mark: 255 应用于 Xray 自身的出站套接字 —— 配合 -m mark --mark 255 -j RETURN 规则可避免 Xray 自身出口流量 再次进入 TPROXY 管线(防止回环)。
说明
followRedirect: true是两种模式都必需的 —— 它让 Xray 从套接字 读取 NAT 前的原始目的地(REDIRECT 用SO_ORIGINAL_DST,TPROXY 用IP_RECVORIGDSTADDR)。- TPROXY 要求
CAP_NET_ADMIN(以 root 运行,或附带该 capability)。 sockopt.tproxy值可取"redirect"(同 REDIRECT 模式)、"tproxy"或"off"。- Linux 上完整的透明代理指南可参考 Xray 文档维护的脚本模板 —— 搜索 "xray transparent proxy"。
跨内核说明
- sing-box 提供专用的
redirect与tproxy入站类型,内核侧 要求相同。参见 Redirect/TProxy — sing-box。 - mihomo 同时提供顶层快捷字段(
redir-port与tproxy-port—— 见 mihomo 基础)和listeners:条目 (type: redir、type: tproxy)。参见 Redirect/TProxy — mihomo。
