AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1169137
Accepted
iopq
iopq
Asked: 2024-12-21 02:26:18 +0800 CST2024-12-21 02:26:18 +0800 CST 2024-12-21 02:26:18 +0800 CST

如何通过 tproxy 发送所有 TCP 和 UDP 流量而不产生循环?

  • 772

我在使用连接的同一台 Linux 机器上运行代理客户端。在配置中,我可以告诉它使用 tproxy 到端口 2500。代理在端口 443 上运行,使用 TLS(尽管这部分由代理接管管理)

因此我需要使用 iptables 或 nftables 将所有数据包路由到代理。但我也需要标记它们,以免造成循环。

我最初的尝试没有成功,好像我的 UDP 数据包没有通过(我位于 NAT 后面,但我不知道这是不是问题,因为我无论如何都在使用代理)。我想知道它如何更好地工作,以便我可以自己调试它。

这是我的尝试:

ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100

#Setup a chain DIVERT to mark packets
iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT

#Use DIVERT to prevent existing connections going through TPROXY twice:
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A PREROUTING -p udp -m socket -j DIVERT

#Mark all other (new) packets and use TPROXY to pass into xray
iptables -t mangle -A PREROUTING -p tcp -j TPROXY --tproxy-mark 0x1/0x1 --on-port 2500
iptables -t mangle -A PREROUTING -p udp -j TPROXY --tproxy-mark 0x1/0x1 --on-port 2500

#disable rp_filter and enable ip_forward
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv4.conf.default.rp_filter=0
sysctl -w  net.ipv4.conf.all.rp_filter=0
sysctl -w net.ipv4.conf.wlp38s0.rp_filter=0

数据包只是按照我的规则传递,就像它们不存在一样

iptables
  • 1 1 个回答
  • 104 Views

1 个回答

  • Voted
  1. Best Answer
    iopq
    2024-12-23T16:17:10+08:002024-12-23T16:17:10+08:00

    因此我想出了一个方法,即以不同的用户身份运行 xray 可执行文件:

    sudo -u xray_tproxy xray -c config.json其中 GID 为 23333

    接下来我创建了这些规则

    ip rule add fwmark 1 table 100
    ip route add local 0.0.0.0/0 dev lo table 100
    
    # new table
    iptables -t mangle -N XRAY
    #  "网关所在ipv4网段" 通过运行命令"ip address | grep -w inet | awk '{print $2}'"获得,一般有多个
    iptables -t mangle -A XRAY -d 192.168.2.211/24 -j RETURN
    
    
    # multicast address
    iptables -t mangle -A XRAY -d 224.0.0.0/3 -j RETURN
    
    
    #Get this IP range by doing "ip address | grep -w "inet" | awk '{print $2}'"
    iptables -t mangle -A XRAY ! -s 192.168.2.211/24 -j RETURN
    
    # Set the mark to 1 and tproxy port 2500
    iptables -t mangle -A XRAY -p tcp -j TPROXY --on-port 2500 --tproxy-mark 1
    iptables -t mangle -A XRAY -p udp -j TPROXY --on-port 2500 --tproxy-mark 1
    # 应用规则
    iptables -t mangle -A PREROUTING -j XRAY
    
    # don't proxy gateway or the user with that GUID we created earlier
    iptables -t mangle -N XRAY_MASK
    iptables -t mangle -A XRAY_MASK -m owner --gid-owner 23333 -j RETURN
    iptables -t mangle -A XRAY_MASK -d 192.168.2.211/24 -j RETURN
    
    iptables -t mangle -A XRAY_MASK -d 224.0.0.0/3 -j RETURN
    iptables -t mangle -A XRAY_MASK -j MARK --set-mark 1
    iptables -t mangle -A OUTPUT -p tcp -j XRAY_MASK
    iptables -t mangle -A OUTPUT -p udp -j XRAY_MASK 
    

    我运行它们sudo

    参见: https://xtls.github.io/document/level-2/iptables_gid.html#_3-%E9%85%8D%E7%BD%AE%E6%9C%80%E5%A4%A7%E6%96%87%E4%BB%B6%E6%89%93%E5%BC%80%E6%95%B0-%E8%BF%90%E8%A1%8C-xray-%E5%AE%A2%E6%88%B7%E7%AB%AF

    • 0

相关问题

  • OpenVPN 的 Linux IP 转发 - 正确的防火墙设置?

  • iptables 单个规则中的多个源 IP

  • 存储 iptables 规则的规范方法是什么

  • 使用 iptables 和 dhcpd 进行端口转发

  • 根据 Apache 日志数据自动修改 iptables 以阻止行为不良的客户端

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve