@@ -5,91 +5,48 @@ events=$1
5
5
# monitor_file=$3
6
6
7
7
export PATH=" /data/adb/magisk:/data/adb/ksu/bin:/data/adb/ap/bin:$PATH :/system/bin"
8
+
8
9
iptables_version=$( iptables --version | busybox awk ' /^iptables/ {print $2}' )
9
10
required_version=" v1.6.1"
10
- if [ " $( printf ' %s\n' " $required_version " " $iptables_version " | sort -V | head -n1) " = " $required_version " ]; then
11
- IPV=" iptables -w 64"
12
- IP6V=" ip6tables -w 64"
11
+
12
+ if [ " $( printf ' %s\n' " ${required_version} " " ${iptables_version} " | sort -V | head -n1) " = " ${required_version} " ]; then
13
+ IPV=" iptables -w 100"
14
+ IP6V=" ip6tables -w 100"
13
15
else
14
16
IPV=" iptables"
15
17
IP6V=" ip6tables"
16
18
fi
17
- iptables=$IPV
18
- ip6tables=$IP6V
19
19
20
- rules_add () {
21
- # Retrieve all local IPv4 addresses except loopback (127.0.0.1)
22
- ip -4 a | busybox awk ' /inet/ {print $2}' | grep -vE " ^127.0.0.1" | while read -r local_ipv4 ; do
23
- echo " Checking IPv4 address: $local_ipv4 " >> /data/adb/box/run/net.inotify.log
24
- # Check if a rule for the local IPv4 address already exists in the mangle table
25
- if ! iptables -t mangle -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
26
- echo " Adding IPv4 address $local_ipv4 to mangle tables." >> /data/adb/box/run/net.inotify.log
27
- # If not, add a rule to prevent traffic to the local IPv4 address from going external
28
- ${iptables} -t mangle -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
29
- ${iptables} -t mangle -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
30
- fi
31
- # Check if a rule for the local IPv4 address already exists in the nat table
32
- if ! iptables -t nat -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
33
- echo " Adding IPv4 address $local_ipv4 to nat tables." >> /data/adb/box/run/net.inotify.log
34
- # If not, add a rule to prevent NAT on the local IPv4 address
35
- ${iptables} -t nat -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
36
- ${iptables} -t nat -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
37
- fi
38
- done
20
+ iptables=" ${IPV} "
21
+ ip6tables=" ${IP6V} "
22
+ logs=" /data/adb/box/run/net.log"
39
23
40
- # Retrieve all local IPv6 addresses except link-local (fe80::) and loopback (::1)
41
- ip -6 a | busybox awk ' /inet6/ {print $2}' | grep -vE " ^fe80|^::1" | while read -r local_ipv6 ; do
42
- echo " Checking IPv6 address: $local_ipv6 " >> /data/adb/box/run/net.inotify.log
43
- # Check if a rule for the local IPv6 address already exists in the mangle table
44
- if ! ip6tables -t mangle -nL BOX_LOCAL | grep -q $local_ipv6 > /dev/null 2>&1 ; then
45
- echo " Adding IPv6 address $local_ipv6 to mangle tables." >> /data/adb/box/run/net.inotify.log
46
- # If not, add a rule to prevent traffic to the local IPv6 address from going external
47
- ${ip6tables} -t mangle -I BOX_EXTERNAL 3 -d $local_ipv6 -j RETURN
48
- ${ip6tables} -t mangle -I BOX_LOCAL 4 -d $local_ipv6 -j RETURN
49
- fi
50
- done
51
- }
52
-
53
- rules_delete () {
54
- # Retrieve all local IPv4 addresses except loopback (127.0.0.1)
24
+ rules_add () {
25
+ date > " ${logs} "
55
26
ip -4 a | busybox awk ' /inet/ {print $2}' | grep -vE " ^127.0.0.1" | while read -r local_ipv4 ; do
56
- echo " Checking IPv4 address for deletion: $local_ipv4 " >> /data/adb/box/run/net.inotify.log
57
- # Check if a rule for the local IPv4 address exists in the mangle table
58
- if iptables -t mangle -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
59
- echo " Deleting IPv4 address $local_ipv4 from mangle tables." >> /data/adb/box/run/net.inotify.log
60
- # If exists, delete the rule
61
- ${iptables} -t mangle -D BOX_EXTERNAL -d $local_ipv4 -j RETURN
62
- ${iptables} -t mangle -D BOX_LOCAL -d $local_ipv4 -j RETURN
63
- fi
64
- # Check if a rule for the local IPv4 address exists in the nat table
65
- if iptables -t nat -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
66
- echo " Deleting IPv4 address $local_ipv4 from nat tables." >> /data/adb/box/run/net.inotify.log
67
- # If exists, delete the rule
68
- ${iptables} -t nat -D BOX_EXTERNAL -d $local_ipv4 -j RETURN
69
- ${iptables} -t nat -D BOX_LOCAL -d $local_ipv4 -j RETURN
70
- fi
27
+ if ! iptables -t mangle -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
28
+ echo " adding IPv4 address $local_ipv4 to mangle tables." >> " ${logs} "
29
+ ${iptables} -t mangle -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
30
+ ${iptables} -t mangle -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
31
+ fi
32
+ if ! iptables -t nat -nL BOX_LOCAL | grep -q $local_ipv4 > /dev/null 2>&1 ; then
33
+ echo " adding IPv4 address $local_ipv4 to nat tables." >> " ${logs} "
34
+ ${iptables} -t nat -I BOX_EXTERNAL 3 -d $local_ipv4 -j RETURN
35
+ ${iptables} -t nat -I BOX_LOCAL 4 -d $local_ipv4 -j RETURN
36
+ fi
71
37
done
72
38
73
- # Retrieve all local IPv6 addresses except link-local (fe80::) and loopback (::1)
74
- ip -6 a | busybox awk ' /inet6/ {print $2}' | grep -vE " ^fe80|^::1" | while read -r local_ipv6 ; do
75
- echo " Checking IPv6 address for deletion: $local_ipv6 " >> /data/adb/box/run/net.inotify.log
76
- # Check if a rule for the local IPv6 address exists in the mangle table
77
- if ip6tables -t mangle -nL BOX_LOCAL | grep -q $local_ipv6 > /dev/null 2>&1 ; then
78
- echo " Deleting IPv6 address $local_ipv6 from mangle tables." >> /data/adb/box/run/net.inotify.log
79
- # If exists, delete the rule
80
- ${ip6tables} -t mangle -D BOX_EXTERNAL -d $local_ipv6 -j RETURN
81
- ${ip6tables} -t mangle -D BOX_LOCAL -d $local_ipv6 -j RETURN
82
- fi
39
+ ip -6 a | busybox awk ' /inet6/ {print $2}' | grep -vE " ^fe80|^::1|^fd00" | while read -r local_ipv6 ; do
40
+ if ! ip6tables -t mangle -nL BOX_LOCAL | grep -q $local_ipv6 > /dev/null 2>&1 ; then
41
+ echo " adding IPv6 address $local_ipv6 to mangle tables." >> " ${logs} "
42
+ ${ip6tables} -t mangle -I BOX_EXTERNAL 3 -d $local_ipv6 -j RETURN
43
+ ${ip6tables} -t mangle -I BOX_LOCAL 4 -d $local_ipv6 -j RETURN
44
+ fi
83
45
done
84
46
}
85
47
86
48
if [ " $events " = " w" ]; then
87
- date > /data/adb/box/run/net.inotify.log
88
- if [ -f /data/adb/box/run/box.pid ] ; then
49
+ if [ -f " /data/adb/box/run/box.pid" ]; then
89
50
rules_add
90
- rm -f /data/adb/box/run/net
91
- elif [ ! -f /data/adb/box/run/net ]; then
92
- rules_delete
93
- touch /data/adb/box/run/net
94
51
fi
95
52
fi
0 commit comments