From c67d652dda7bac482fc0db673ada46d0e924ef27 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sat, 19 Dec 2020 22:04:32 +0800 Subject: [PATCH] optimize: ipv6 performance --- service/core/iptables/redirect.go | 15 +++++- service/core/iptables/tproxy.go | 54 ++++++++++++---------- service/core/v2ray/transparent.go | 1 - service/global/systemServiceControlMode.go | 2 +- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/service/core/iptables/redirect.go b/service/core/iptables/redirect.go index cb672a9f..05918d3e 100644 --- a/service/core/iptables/redirect.go +++ b/service/core/iptables/redirect.go @@ -26,7 +26,20 @@ func (r *redirect) GetSetupCommands() SetupCommands { iptables -t nat -N V2RAY # 出方向白名单端口 iptables -t nat -A V2RAY -p tcp -m multiport --sports {{TCP_PORTS}} -j RETURN -# 白名单自动插入 +iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN +iptables -t nat -A V2RAY -d 100.64.0.0/10 -j RETURN +iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN +iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN +iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN +iptables -t nat -A V2RAY -d 192.0.0.0/24 -j RETURN +iptables -t nat -A V2RAY -d 192.0.2.0/24 -j RETURN +iptables -t nat -A V2RAY -d 192.88.99.0/24 -j RETURN +iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN +iptables -t nat -A V2RAY -d 198.18.0.0/15 -j RETURN +iptables -t nat -A V2RAY -d 198.51.100.0/24 -j RETURN +iptables -t nat -A V2RAY -d 203.0.113.0/24 -j RETURN +iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN +iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN iptables -t nat -A V2RAY -m mark --mark 0xff -j RETURN iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 32345 diff --git a/service/core/iptables/tproxy.go b/service/core/iptables/tproxy.go index 8bb479fd..519ba7d7 100644 --- a/service/core/iptables/tproxy.go +++ b/service/core/iptables/tproxy.go @@ -13,6 +13,8 @@ type tproxy struct { var Tproxy tproxy func (t *tproxy) AddIPWhitelist(cidr string) { + // avoid duplication + t.RemoveIPWhitelist(cidr) var commands string commands = fmt.Sprintf(`iptables -t mangle -I SETMARK 6 -d %s -j RETURN`, cidr) if !strings.Contains(cidr, ".") { @@ -41,11 +43,8 @@ ip route add local 0.0.0.0/0 dev lo table 100 # 建链 iptables -t mangle -N TP_OUT iptables -t mangle -N TP_PRE -iptables -t mangle -N TP_ONCE -iptables -t mangle -N TP_LOCALWHITELIST iptables -t mangle -I OUTPUT -j TP_OUT iptables -t mangle -I PREROUTING -j TP_PRE -iptables -t mangle -I PREROUTING -m socket --transparent -j TP_ONCE # 打上 iptables 标记,mark 了的会走代理 iptables -t mangle -N SETMARK @@ -56,7 +55,21 @@ iptables -t mangle -A SETMARK -i br-+ -j RETURN iptables -t mangle -A SETMARK -p udp --dport 53 -j MARK --set-mark 1 iptables -t mangle -A SETMARK -p tcp --dport 53 -j MARK --set-mark 1 # 注意,如果要调整位置,记得调整func AddIPWhitelist的插入位置 -# 白名单自动插入 +iptables -t mangle -A SETMARK -d 0.0.0.0/32 -j RETURN +iptables -t mangle -A SETMARK -d 10.0.0.0/8 -j RETURN +iptables -t mangle -A SETMARK -d 100.64.0.0/10 -j RETURN +iptables -t mangle -A SETMARK -d 127.0.0.0/8 -j RETURN +iptables -t mangle -A SETMARK -d 169.254.0.0/16 -j RETURN +iptables -t mangle -A SETMARK -d 172.16.0.0/12 -j RETURN +iptables -t mangle -A SETMARK -d 192.0.0.0/24 -j RETURN +iptables -t mangle -A SETMARK -d 192.0.2.0/24 -j RETURN +iptables -t mangle -A SETMARK -d 192.88.99.0/24 -j RETURN +iptables -t mangle -A SETMARK -d 192.168.0.0/16 -j RETURN +iptables -t mangle -A SETMARK -d 198.18.0.0/15 -j RETURN +iptables -t mangle -A SETMARK -d 198.51.100.0/24 -j RETURN +iptables -t mangle -A SETMARK -d 203.0.113.0/24 -j RETURN +iptables -t mangle -A SETMARK -d 224.0.0.0/4 -j RETURN +iptables -t mangle -A SETMARK -d 240.0.0.0/4 -j RETURN iptables -t mangle -A SETMARK -p tcp -m multiport --sports {{TCP_PORTS}} -j RETURN iptables -t mangle -A SETMARK -p udp -m multiport --sports {{UDP_PORTS}} -j RETURN iptables -t mangle -A SETMARK -p tcp -j MARK --set-mark 1 @@ -77,10 +90,6 @@ iptables -t mangle -A TP_PRE -p udp -m mark ! --mark 1 -j SETMARK iptables -t mangle -A TP_PRE -m mark --mark 1 -p tcp -j TPROXY --on-port 32345 --tproxy-mark 1 iptables -t mangle -A TP_PRE -m mark --mark 1 -p udp -j TPROXY --on-port 32345 --tproxy-mark 1 -# 略过已建立且被tproxy标记的的socket -iptables -t mangle -A TP_ONCE -j MARK --set-mark 0xff -iptables -t mangle -A TP_ONCE -j ACCEPT - ` if cmds.IsCommandValid("ip6tables") { commands += ` @@ -91,10 +100,8 @@ ip -6 route add local ::/0 dev lo table 100 # 建链 ip6tables -t mangle -N TP_OUT ip6tables -t mangle -N TP_PRE -ip6tables -t mangle -N TP_ONCE ip6tables -t mangle -I OUTPUT -j TP_OUT ip6tables -t mangle -I PREROUTING -j TP_PRE -ip6tables -t mangle -I PREROUTING -m socket --transparent -j TP_ONCE # 打上 iptables 标记,mark 了的会走代理 ip6tables -t mangle -N SETMARK @@ -105,7 +112,19 @@ ip6tables -t mangle -A SETMARK -i br-+ -j RETURN ip6tables -t mangle -A SETMARK -p udp --dport 53 -j MARK --set-mark 1 ip6tables -t mangle -A SETMARK -p tcp --dport 53 -j MARK --set-mark 1 # 注意,如果要调整位置,记得调整func AddIPWhitelist的插入位置 -# 白名单自动插入 +ip6tables -t mangle -A SETMARK -d ::/128 -j RETURN +ip6tables -t mangle -A SETMARK -d ::1/128 -j RETURN +ip6tables -t mangle -A SETMARK -d ::ffff:0:0/96 -j RETURN +ip6tables -t mangle -A SETMARK -d ::ffff:0:0:0/96 -j RETURN +ip6tables -t mangle -A SETMARK -d 64:ff9b::/96 -j RETURN +ip6tables -t mangle -A SETMARK -d 100::/64 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001::/32 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001:20::/28 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001:db8::/32 -j RETURN +ip6tables -t mangle -A SETMARK -d 2002::/16 -j RETURN +ip6tables -t mangle -A SETMARK -d fc00::/7 -j RETURN +ip6tables -t mangle -A SETMARK -d fe80::/10 -j RETURN +ip6tables -t mangle -A SETMARK -d ff00::/8 -j RETURN ip6tables -t mangle -A SETMARK -p tcp -m multiport --sports {{TCP_PORTS}} -j RETURN ip6tables -t mangle -A SETMARK -p udp -m multiport --sports {{UDP_PORTS}} -j RETURN ip6tables -t mangle -A SETMARK -p tcp -j MARK --set-mark 1 @@ -125,10 +144,6 @@ ip6tables -t mangle -A TP_PRE -p udp -m mark ! --mark 1 -j SETMARK # 将所有打了标记的 TCP 和 UDP 包透明地转发到代理的监听端口 ip6tables -t mangle -A TP_PRE -m mark --mark 1 -p tcp -j TPROXY --on-port 32345 --tproxy-mark 1 ip6tables -t mangle -A TP_PRE -m mark --mark 1 -p udp -j TPROXY --on-port 32345 --tproxy-mark 1 - -# 略过已建立且被tproxy标记的的socket -ip6tables -t mangle -A TP_ONCE -j MARK --set-mark 0xff -ip6tables -t mangle -A TP_ONCE -j ACCEPT ` } return SetupCommands(commands) @@ -139,9 +154,6 @@ func (t *tproxy) GetCleanCommands() CleanCommands { ip rule del fwmark 1 table 100 ip route del local 0.0.0.0/0 dev lo table 100 -iptables -t mangle -F TP_ONCE -iptables -t mangle -D PREROUTING -m socket --transparent -j TP_ONCE -iptables -t mangle -X TP_ONCE iptables -t mangle -F TP_OUT iptables -t mangle -D OUTPUT -j TP_OUT iptables -t mangle -X TP_OUT @@ -150,18 +162,12 @@ iptables -t mangle -D PREROUTING -j TP_PRE iptables -t mangle -X TP_PRE iptables -t mangle -F SETMARK iptables -t mangle -X SETMARK -iptables -t mangle -F TP_LOCALWHITELIST -iptables -t mangle -D SETMASK -j TP_LOCALWHITELIST -iptables -t mangle -X TP_LOCALWHITELIST ` if cmds.IsCommandValid("ip6tables") { commands += ` ip -6 rule del fwmark 1 table 100 ip -6 route del local ::/0 dev lo table 100 -ip6tables -t mangle -F TP_ONCE -ip6tables -t mangle -D PREROUTING -m socket --transparent -j TP_ONCE -ip6tables -t mangle -X TP_ONCE ip6tables -t mangle -F TP_OUT ip6tables -t mangle -D OUTPUT -j TP_OUT ip6tables -t mangle -X TP_OUT diff --git a/service/core/v2ray/transparent.go b/service/core/v2ray/transparent.go index fcad3704..e9908099 100644 --- a/service/core/v2ray/transparent.go +++ b/service/core/v2ray/transparent.go @@ -55,7 +55,6 @@ func (h *DnsHijacker) HijackDNS() error { func resetDnsHijacker() { if hijacker != nil { hijacker.Close() - hijacker = nil } hijacker = NewDnsHijacker() } diff --git a/service/global/systemServiceControlMode.go b/service/global/systemServiceControlMode.go index 358071e9..f3013386 100644 --- a/service/global/systemServiceControlMode.go +++ b/service/global/systemServiceControlMode.go @@ -8,7 +8,7 @@ import ( type SystemServiceControlMode int const ( - SystemctlMode = SystemServiceControlMode(iota) + SystemctlMode SystemServiceControlMode = iota ServiceMode UniversalMode )