Xray-core icon indicating copy to clipboard operation
Xray-core copied to clipboard

旁路由 tproxy 透明代理下 speedtest 无法测速

Open marcuslannister opened this issue 2 years ago • 10 comments

问题描述

  • speedtest 测试会使用 tcp 8080 端口(抓包显示为 http-alt)
  • 旁路由有收到 8080 的封包并发出请求但没有收到回应
❮ sudo tcpdump port 8080
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp1s0f0, link-type EN10MB (Ethernet), capture size 262144 bytes
19:08:24.688630 IP 192.168.1.42.52205 > 23.130.129.165.http-alt: Flags [S], seq 322383792, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 3235685452 e
cr 0,sackOK,eol], length 0
19:08:25.688491 IP 192.168.1.42.52205 > 23.130.129.165.http-alt: Flags [S], seq 322383792, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 3235686452 e
cr 0,sackOK,eol], length 0
19:08:26.688552 IP 192.168.1.42.52205 > 23.130.129.165.http-alt: Flags [S], seq 322383792, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 3235687452 e
cr 0,sackOK,eol], length 0
  • 我再用 nc 命令模拟
    • nc -vz 23.130.129.165 80 可以成功
    • nc -vz 23.130.129.165 8080 失败
      • 查看 log 发现 xray 有处理 23.130.129.165:80 ,但没有处理 23.130.129.165:8080

问题可能的原因是?

版本

Xray 1.5.5

配置

{
    "log": {
        "loglevel": "debug",
        "error": "/var/log/xray/error.log",
        "access": "/var/log/xray/access.log"
    },
    "inbounds": [
        {
            "port": 1300,
            "listen": "0.0.0.0",
            "protocol": "socks",
            "settings": {
                "udp": true
            }
        },
        {
      "tag": "all-in",
      "port": 8992,
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      },
      "streamSettings": {
        "sockopt": {
          "tproxy": "tproxy"
        }
      }
    }
    ],
    "outbounds": [
        {
            "protocol": "vless",
            "settings": {
                "vnext": [
                    {
                        "address": "x.x.x.x",
                        "port": 443,
                        "users": [
                            {
                                "id": "yyyyyy",
                                "flow": "xtls-rprx-direct",
                                "encryption": "none",
                                "level": 0
                            }
                        ]
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "xtls",
                "xtlsSettings": {
                    "serverName": "zzzz"
                }
            }
        }
    ]
}

marcuslannister avatar May 03 '22 11:05 marcuslannister

发一下 iptables 或者 nftables 的配置

yichya avatar May 05 '22 10:05 yichya

# Add any tproxy policy rules
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100

# restore china addresses ipset
ipset -R -exist < /etc/routing_table/chnroute.ipset

# --------------- Route Rules Begin ---------------------------
# Create a new chain for router
iptables -t mangle -N tproxy
# Ignore china addresses you'd like to bypass the proxy
iptables -t mangle -A tproxy -m set --match-set chnroute dst -j RETURN

# Ignore LANs and any other addresses you'd like to bypass the proxy
iptables -t mangle -A tproxy -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A tproxy -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A tproxy -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A tproxy -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A tproxy -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A tproxy -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A tproxy -d 240.0.0.0/4 -j RETURN

iptables -t mangle -A tproxy -d 192.168.0.0/16 -j RETURN

# Allow connection to the vps server
iptables -t mangle -A tproxy -d x.x.x.x -j RETURN

iptables -t mangle -A tproxy -p udp -j TPROXY --on-port 7892 --tproxy-mark 1
iptables -t mangle -A tproxy -p tcp -j TPROXY --on-port 7892 --tproxy-mark 1

# Apply the route rules
iptables -t mangle -A PREROUTING -j tproxy
# ---------------- Route Rules End -----------------------------

# ---------------- Local Rules Begin --------------------------
# Create new chain for localhost
iptables -t mangle -N tproxy_local

# Ignore packets sent from local host itself.
iptables -t mangle -A tproxy_local -j RETURN -m owner --uid-owner xxx
iptables -t mangle -A tproxy_local -j RETURN -m owner --uid-owner yyy

# Ignore Lans and any other address you'd like to bypass the proxy
iptables -t mangle -A tproxy_local -m set --match-set chnroute dst -j RETURN

# Ignore LANs and any other addresses you'd like to bypass the proxy
iptables -t mangle -A tproxy_local -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A tproxy_local -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A tproxy_local -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A tproxy_local -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A tproxy_local -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A tproxy_local -d 240.0.0.0/4 -j RETURN

iptables -t mangle -A tproxy_local -d 192.168.0.0/16 -j RETURN

# Allow connection to the vps server
iptables -t mangle -A tproxy_local -d x.x.x -j RETURN

# Mark tcp and udp to reroute.
iptables -t mangle -A tproxy_local -p udp -j MARK --set-mark 1
iptables -t mangle -A tproxy_local -p tcp -j MARK --set-mark 1

# Apply the local rules
iptables -t mangle -A OUTPUT -j tproxy_local

marcuslannister avatar May 05 '22 11:05 marcuslannister

想问问老哥你这iptable配置除了不能测速,udp透明代理成功了吗

Ch4mpa9ne avatar May 09 '22 04:05 Ch4mpa9ne

想问问老哥你这 iptable 配置除了不能测速,udp 透明代理成功了吗

要怎么验证 udp 代理是否成功?

marcuslannister avatar May 11 '22 12:05 marcuslannister

想问问老哥你这 iptable 配置除了不能测速,udp 透明代理成功了吗

要怎么验证 udp 代理是否成功?

路由器开启全局代理后下级路由做一些走udp流量的行为。一般即时游戏都是udp的。 比如老哥可以开个王者荣耀什么的看看能不能玩,如果延迟明显比热点高但是能正常游戏我想应该是udp代理成功了。

Ch4mpa9ne avatar May 15 '22 06:05 Ch4mpa9ne

路由器开启全局代理后下级路由做一些走 udp 流量的行为。一般即时游戏都是 udp 的。 比如老哥可以开个王者荣耀什么的看看能不能玩,如果延迟明显比热点高但是能正常游戏我想应该是 udp 代理成功了。

我不打游戏,但我试了下用 iperf3 测试 udp 转发是失败的。 所以我这个配置 udp 透明代理也不行。

marcuslannister avatar May 22 '22 12:05 marcuslannister

你的iptables规则怎么没有-j TPROXY --on-port #### --tproxy-mark 1这个呢?

#!/bin/sh

# 需要先去 /etc/config/firewall 添加项
# config include
#	option path '/etc/firewall.user'
#  -->	option reload '1'
# 以便在WAN拨号后自动更新 WAN_IP

##### SETTINGS #####
WAN_IP=$(ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address')
TUN_IP=x.x.x.x
VPS_IP=x.x.x.x

###### TPROXY ######

# 设置策略路由
ip rule del fwmark 1 table 100
ip rule add fwmark 1 table 100
ip route del local default dev lo table 100
ip route add local default dev lo table 100

# 新建 DIVERT 规则,避免已有连接的包二次通过 TPROXY,理论上有一定的性能提升
iptables -t mangle -N DIVERT
iptables -t mangle -F DIVERT

iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT

iptables -t mangle -D PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT

# 代理局域网设备
iptables -t mangle -N XRAY
iptables -t mangle -F XRAY
iptables -t mangle -A XRAY -j RETURN -m mark --mark 0xff
iptables -t mangle -A XRAY -j RETURN -m set --match-set private dst
iptables -t mangle -A XRAY -j RETURN -d $WAN_IP,$TUN_IP,$VPS_IP
iptables -t mangle -A XRAY -j RETURN -m set --match-set china dst
iptables -t mangle -A XRAY -p tcp -j TPROXY --on-port 8090 --tproxy-mark 1
iptables -t mangle -A XRAY -p udp -j TPROXY --on-port 8090 --tproxy-mark 1

iptables -t mangle -D PREROUTING -j XRAY
iptables -t mangle -A PREROUTING -j XRAY

# 代理网关本机
iptables -t mangle -N XRAY_MASK
iptables -t mangle -F XRAY_MASK
iptables -t mangle -A XRAY_MASK -j RETURN -m mark --mark 0xff
iptables -t mangle -A XRAY_MASK -j RETURN -m set --match-set private dst
iptables -t mangle -A XRAY_MASK -j RETURN -d $WAN_IP,$TUN_IP,$VPS_IP
iptables -t mangle -A XRAY_MASK -j RETURN -m set --match-set china dst
iptables -t mangle -A XRAY_MASK -p tcp -j MARK --set-mark 1
iptables -t mangle -A XRAY_MASK -p udp -j MARK --set-mark 1

iptables -t mangle -D OUTPUT -j XRAY_MASK
iptables -t mangle -A OUTPUT -j XRAY_MASK

azzvx avatar May 29 '22 02:05 azzvx

你的 iptables 规则怎么没有 -j TPROXY --on-port #### --tproxy-mark 1 这个呢?

有的,之前贴漏了,现在补上了。 谢谢。

marcuslannister avatar May 29 '22 03:05 marcuslannister

想问问udp透明代理的问题解决了吗,求贴一下配置

Ch4mpa9ne avatar Jul 08 '22 13:07 Ch4mpa9ne

想问问udp透明代理的问题解决了吗,求贴一下配置

并没有解决,目前发现透明代理的 host 无法收到 udp 转发或者是收到后 xray 没有转发给 vps 。 现在还没有思路。

marcuslannister avatar Jul 10 '22 07:07 marcuslannister

遇到了和你一样的问题,我单独设置了UDP代理8080端口,还是跑不了测速,fast.com的测速倒是正常

zycboss avatar Mar 17 '23 07:03 zycboss

感觉是个大范围的老问题,v2/xray好像都存在这个问题 现象是一旦打开udp相关的代理选项启动tproxy udp代理 udp相关的各种流量就开始间歇异常,包括但不限于udp,甚至iptv等

fo2w avatar Oct 21 '23 11:10 fo2w