luci-app-xray icon indicating copy to clipboard operation
luci-app-xray copied to clipboard

Completely rewrite iptables for readability and better performance

Open ttimasdf opened this issue 2 years ago • 5 comments

主要更改:

  1. 参考 Shadowsocks 文档 Transparent proxy (pure tproxy) - shadowsocks-libev/README.md at master · shadowsocks/shadowsocks-libev 对 iptables 规则进行重写。只对 UDP 首包和 TCP SYN 包进行标记,后续使用 CONNMARK 跟踪标记状态,保证稳定性。
  2. Fix #50
  3. 原版 iptables 应该是参考了 luci-app-shadowsocks。重写后保留了黑白名单功能,合并为两个链 XRAY_RULESXRAY_PROXY
  4. 添加注释,增强可读性
  5. 不再依赖本机 IP 地址和 网关地址,不需要将任何固定地址添加到白名单。也就是说,可以删掉 root/etc/hotplug.d/iface/01-transparent-proxy-ipset.fw3 了

新增依赖的用途:

  1. iptables-mod-conntrack-extra 为了 -j CONNMARK
  2. iptables-mod-extra 为了 -m addrtype

ttimasdf avatar Jul 07 '22 02:07 ttimasdf

需不需要把已建立的链接也 bypass 掉?反正改不改都行,但估计性能瓶颈会在 xray 占用的 CPU 和服务器的网速上,晚上我 benchmark 下看看有没有区别吧。

diff --git a/root/usr/share/xray/firewall_include.lua b/root/usr/share/xray/firewall_include.lua
index ecc0d7b..7de3f7f 100644
--- a/root/usr/share/xray/firewall_include.lua
+++ b/root/usr/share/xray/firewall_include.lua
@@ -20,6 +20,8 @@ local rules = [[
 -A XRAY_RULES -m mark --mark 0x%x -j RETURN
 # connection-mark -> packet-mark
 -A XRAY_RULES -j CONNMARK --restore-mark
+# ignore established connections
+-A XRAY_RULES -m mark --mark 0x2333 -j RETURN
 
 # ignore traffic sent to reserved addresses
 -A XRAY_RULES -m set --match-set tp_spec_dst_sp dst -j RETURN

ttimasdf avatar Jul 07 '22 07:07 ttimasdf

3. 合并为两个链 XRAY_RULESXRAY_PROXY

这个是必须要改嘛,我觉得原来 luci-app-shadowsocks 的那几个链的分工还挺明确的,以及顺便看一下 nftables 的实现是否可以一起做了吧

yichya avatar Jul 11 '22 07:07 yichya

这个是必须要改嘛,我觉得原来 luci-app-shadowsocks 的那几个链的分工还挺明确的,以及顺便看一下 nftables 的实现是否可以一起做了吧

xray 有 XTLS 和 v2ray 的 routing 机制,其实这几个链都没啥太大的必要了,原来那几个链,最古早 ipset 还没用起来的时候,是用来放 chnroute 的,现在只留一个 XRAY_RULES 专门做 ipset matching,就相当于保留 xray 和 ss 的所有优点了。

ttimasdf avatar Jul 12 '22 02:07 ttimasdf

nftables 的实现等 22.03 stable 了再看看(还没开始学 🤣)

ttimasdf avatar Jul 13 '22 06:07 ttimasdf

22.02 stable了,有写的意向么

iusearch avatar Sep 13 '22 08:09 iusearch

22.02 stable了,有写的意向么

最近开始搞了

ttimasdf avatar Dec 02 '22 07:12 ttimasdf

搞完了!

除了 uid 和 gid 那块没测,剩下的路由规则基本都没问题了。里面还保留了注释,还有几个 counter 做流量统计,在 luci 的 Firewall 那里能看到几个规则的流量总计和每条规则的用途说明。

ttimasdf avatar Dec 12 '22 09:12 ttimasdf

测试 ipk 可从下面的 Github Actions 下载,所有cpu架构都是一个包,只需要找对应的 OpenWrt 版本即可。目前只编译了 22.03.2, 21.02.5 两个版本。

Add package build GH actions · ttimasdf/luci-app-xray@a1cb5b8

ttimasdf avatar Dec 13 '22 05:12 ttimasdf

维护个人分支,不再更新上游

ttimasdf avatar Jan 03 '23 09:01 ttimasdf