Skip to content

Commit 48b61e4

Browse files
committed
[adjust] net.inotify script
1 parent 62916fa commit 48b61e4

File tree

3 files changed

+45
-80
lines changed

3 files changed

+45
-80
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#### Changelog v1.7.1
2+
+ [adjust] net.inotify script
13
#### Changelog v1.7.0
2-
34
+ feat: Insert rules when the network changes [CHIZI-0618/box4magisk@a8a85e1](https://github.com/CHIZI-0618/box4magisk/commit/a8a85e1dad6322626a9f314fa6e600f95e4c9ff0)
45
+ add option to download sing-box "Pre-release" and "Latest release"
56
+ fix: 修复某些情况下clash dns端口匹配不到

box/scripts/net.inotify

+27-70
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,48 @@ events=$1
55
# monitor_file=$3
66

77
export PATH="/data/adb/magisk:/data/adb/ksu/bin:/data/adb/ap/bin:$PATH:/system/bin"
8+
89
iptables_version=$(iptables --version | busybox awk '/^iptables/ {print $2}')
910
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"
1315
else
1416
IPV="iptables"
1517
IP6V="ip6tables"
1618
fi
17-
iptables=$IPV
18-
ip6tables=$IP6V
1919

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"
3923

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}"
5526
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
7137
done
7238

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
8345
done
8446
}
8547

8648
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
8950
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
9451
fi
9552
fi

box/scripts/start.sh

+16-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ enable_iptables() {
3636
fi
3737
}
3838

39-
start_inotifyd() {
40-
PIDs=($($busybox pidof inotifyd))
41-
for PID in "${PIDs[@]}"; do
42-
if grep -q "box.inotify" "/proc/$PID/cmdline"; then
43-
kill -9 "$PID"
44-
fi
45-
done
46-
inotifyd "${scripts_dir}/box.inotify" "${moddir}" > "/dev/null" 2>&1 &
47-
39+
net_inotifyd() {
4840
while [ ! -f /data/misc/net/rt_tables ] ; do
4941
sleep 3
5042
done
@@ -54,8 +46,23 @@ start_inotifyd() {
5446
inotifyd "${scripts_dir}/net.inotify" "${net_dir}" > "/dev/null" 2>&1 &
5547
}
5648

49+
start_inotifyd() {
50+
PIDs=($($busybox pidof inotifyd))
51+
for PID in "${PIDs[@]}"; do
52+
if grep -q -e "box.inotify" -e "net.inotify" "/proc/$PID/cmdline"; then
53+
kill -9 "$PID"
54+
fi
55+
# if grep -q "box.inotify" "/proc/$PID/cmdline"; then
56+
# kill -9 "$PID"
57+
# fi
58+
done
59+
inotifyd "${scripts_dir}/box.inotify" "${moddir}" > "/dev/null" 2>&1 &
60+
net_inotifyd
61+
}
62+
5763
mkdir -p /data/adb/box/run/
5864
if [ -f "/data/adb/box/manual" ]; then
65+
net_inotifyd
5966
exit 1
6067
fi
6168

0 commit comments

Comments
 (0)