tproxy 的规则如何理解?

104 天前
 8675bc86

参考 gost 的手册:

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

iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT

iptables -t mangle -N GOST
iptables -t mangle -A GOST -p tcp -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A GOST -p tcp -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A GOST -p tcp -m mark --mark 100 -j RETURN 
iptables -t mangle -A GOST -p tcp -j TPROXY --tproxy-mark 0x1/0x1 --on-ip 127.0.0.1 --on-port 12345 
iptables -t mangle -A PREROUTING -p tcp -j GOST

iptables -t mangle -N GOST_LOCAL
iptables -t mangle -A GOST_LOCAL -p tcp -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A GOST_LOCAL -p tcp -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A GOST_LOCAL -p tcp -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A GOST_LOCAL -p tcp -m mark --mark 100 -j RETURN 
iptables -t mangle -A GOST_LOCAL -p tcp -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp -j GOST_LOCAL

问题 1:

iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT

这几行看起来无用,删了的确可以工作。

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

这两条规则看起来前面一条应该会拦截后面一条的,因为内部已经 ACCEPT ,后面的规则就不会执行了。实际上并没有。

问题 2:

iptables -t mangle -A GOST -p tcp -j TPROXY --tproxy-mark 0x1/0x1 --on-ip 127.0.0.1 --on-port 12345 

这里已经将流量导入到 12345 端口,为什么还要再打 mark ?

问题 3: 策略路由跟 iptables 结合时,是如何工作的,作用在 PREROUTING 链后? LOCAL 发出的流量,看起来是:

  1. 打 mark 1 ;
  2. 走策略路由;
  3. 走 iptables mangle PREROUTING 链。(怎么走到这里的?)
1174 次点击
所在节点    宽带症候群
2 条回复
sduoduo233
103 天前
这几句是说给 OUTPUT 链的 TCP 和 UDP 打个标记 1(OUTPUT 应用 V2RAY_MASK 链)。由于 Netfilter 的特性,在 OUTPUT 链打标记会使相应的包重路由到 PREROUTING 链上,在已经配置好了 PREROUTING 相关的透明代理的情况下,OUTPUT 链也可以透明代理了,也就是网关对自身的 UDP 流量透明代理自身(当然 TCP 也不在话下)。因为这是 netfilter 本身的特性,Shadowsocks 应该也可以用同样的方法对本机的 UDP 透明代理,但我没有实际测试过效果。

https://guide.v2fly.org/app/tproxy.html#%E9%85%8D%E7%BD%AE%E9%80%8F%E6%98%8E%E4%BB%A3%E7%90%86%E8%A7%84%E5%88%99
delpo
103 天前
1. DIVERT 表是为了避免一部分流量二次进入 tproxy, 可以提高一部分性能, 所以删除了也可以正常工作.
具体来说, -m socket 模块将数据包匹配本机已有的 socket. 所以对于路由器设备而言, lan --> wan 的流量属于转发流量, 不存在 socket. 对路由器而言一般只有目的地址属于本机 ip 的流量可以匹配到 socket 模块.
所以, DIVERT 表和 GOST 表大部分情况下是不会冲突的, 转发流量经过的是 GOST 表.

2. TPROXY 模块不是把数据包传递到目标端口, 因为 TPROXY 模块不会修改数据包的目的地址, 而是修改 socket 的目的地址. 同样因为包的目的地址没有改变, 所以需要使用策略路由将包重新路由到本机.

3. prerouting 在路由决策之前. 所以顺序应该是 312

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1012261

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX