Skip to content

Commit a05e0b0

Browse files
committed
improvement: independent local IP anti-loopback rules
1 parent ae31f19 commit a05e0b0

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

box/scripts/box.tproxy

+21-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ start_redirect() {
6262
if [ "${bin_name}" = "clash" ] ; then
6363
${iptables} -t nat -A BOX_EXTERNAL -p udp --dport 53 -j REDIRECT --to-ports ${clash_dns_port}
6464
${iptables} -t nat -A BOX_LOCAL -p udp --dport 53 -j REDIRECT --to-ports ${clash_dns_port}
65-
${iptables} -t nat -A BOX_EXTERNAL -d ${clash_fake_ip_range} -p icmp -j DNAT --to-destination 127.0.0.1
66-
${iptables} -t nat -A BOX_LOCAL -d ${clash_fake_ip_range} -p icmp -j DNAT --to-destination 127.0.0.1
65+
${iptables} -t nat -A BOX_EXTERNAL -d ${fake_ip_range_v4} -p icmp -j DNAT --to-destination 127.0.0.1
66+
${iptables} -t nat -A BOX_LOCAL -d ${fake_ip_range_v4} -p icmp -j DNAT --to-destination 127.0.0.1
6767
# else
6868
# Other types of inbound should be added here to receive DNS traffic instead of sniffing
6969
# ${iptables} -t nat -A BOX_EXTERNAL -p udp --dport 53 -j REDIRECT --to-ports ${redir_port}
@@ -73,6 +73,10 @@ start_redirect() {
7373
for subnet in ${intranet[@]} ; do
7474
${iptables} -t nat -A BOX_EXTERNAL -d ${subnet} -j RETURN
7575
${iptables} -t nat -A BOX_LOCAL -d ${subnet} -j RETURN
76+
${iptables} -t nat -N LOCAL_IP_V4
77+
${iptables} -t nat -F LOCAL_IP_V4
78+
${iptables} -t nat -A BOX_EXTERNAL -j LOCAL_IP_V4
79+
${iptables} -t nat -A BOX_LOCAL -j LOCAL_IP_V4
7680
done
7781

7882
${iptables} -t nat -A BOX_EXTERNAL -p tcp -i lo -j REDIRECT --to-ports ${redir_port}
@@ -159,6 +163,8 @@ stop_redirect() {
159163
${iptables} -t nat -X BOX_EXTERNAL
160164
${iptables} -t nat -F BOX_LOCAL
161165
${iptables} -t nat -X BOX_LOCAL
166+
${iptables} -t nat -F LOCAL_IP_V4
167+
${iptables} -t nat -X LOCAL_IP_V4
162168
}
163169

164170
start_tproxy() {
@@ -203,10 +209,16 @@ start_tproxy() {
203209
for subnet6 in ${intranet6[@]} ; do
204210
${iptables} -t mangle -A BOX_EXTERNAL -d ${subnet6} -j RETURN
205211
done
212+
${iptables} -t mangle -N LOCAL_IP_V6
213+
${iptables} -t mangle -F LOCAL_IP_V6
214+
${iptables} -t mangle -A BOX_EXTERNAL -j LOCAL_IP_V6
206215
else
207216
for subnet in ${intranet[@]} ; do
208217
${iptables} -t mangle -A BOX_EXTERNAL -d ${subnet} -j RETURN
209218
done
219+
${iptables} -t mangle -N LOCAL_IP_V4
220+
${iptables} -t mangle -F LOCAL_IP_V4
221+
${iptables} -t mangle -A BOX_EXTERNAL -j LOCAL_IP_V4
210222
fi
211223

212224
${iptables} -t mangle -A BOX_EXTERNAL -p tcp -i lo -j TPROXY --on-port ${tproxy_port} --tproxy-mark ${mark_id}
@@ -255,10 +267,12 @@ start_tproxy() {
255267
for subnet6 in ${intranet6[@]} ; do
256268
${iptables} -t mangle -A BOX_LOCAL -d ${subnet6} -j RETURN
257269
done
270+
${iptables} -t mangle -A BOX_LOCAL -j LOCAL_IP_V6
258271
else
259272
for subnet in ${intranet[@]} ; do
260273
${iptables} -t mangle -A BOX_LOCAL -d ${subnet} -j RETURN
261274
done
275+
${iptables} -t mangle -A BOX_LOCAL -j LOCAL_IP_V4
262276
fi
263277

264278
# Disable kernel
@@ -385,6 +399,11 @@ stop_tproxy() {
385399
${iptables} -t mangle -F BOX_LOCAL
386400
${iptables} -t mangle -X BOX_LOCAL
387401

402+
iptables -t mangle -F LOCAL_IP_V4
403+
iptables -t mangle -X LOCAL_IP_V4
404+
ip6tables -t mangle -F LOCAL_IP_V6
405+
ip6tables -t mangle -X LOCAL_IP_V6
406+
388407
${iptables} -t mangle -F DIVERT
389408
${iptables} -t mangle -X DIVERT
390409

box/scripts/net.inotify

+5-12
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ events=$1
55
# monitor_file=$3
66

77
rules_add() {
8+
iptables -t mangle -F LOCAL_IP_V4
89
ip -4 a | awk '/inet/ {print $2}' | grep -vE "^127.0.0.1" | while read -r local_ipv4 ; do
9-
if ! iptables -t mangle -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
10-
iptables -w 100 -t mangle -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
11-
iptables -w 100 -t mangle -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
12-
fi
13-
if ! iptables -t nat -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
14-
iptables -w 100 -t nat -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
15-
iptables -w 100 -t nat -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
16-
fi
10+
iptables -w 100 -t mangle -A LOCAL_IP_V4 -d $local_ipv4 -j RETURN
11+
iptables -w 100 -t nat -A LOCAL_IP_V4 -d $local_ipv4 -j RETURN
1712
done
1813

14+
ip6tables -t mangle -F LOCAL_IP_V6
1915
ip -6 a | awk '/inet6/ {print $2}' | grep -vE "^fe80|^::1" | while read -r local_ipv6 ; do
20-
if ! ip6tables -t mangle -nL BOX_LOCAL | grep -q $local_ipv6 > /dev/null 2>&1 ; then
21-
ip6tables -w 100 -t mangle -I BOX_EXTERNAL 3 -d $local_ipv6 -j RETURN
22-
ip6tables -w 100 -t mangle -I BOX_LOCAL 4 -d $local_ipv6 -j RETURN
23-
fi
16+
ip6tables -w 100 -t mangle -A LOCAL_IP_V6 -d $local_ipv6 -j RETURN
2417
done
2518
}
2619

0 commit comments

Comments
 (0)