Skip to content

Commit fc0aa0a

Browse files
feat(inotify): optimize network change triggers and enhance firewall chain safety
1 parent a0160f0 commit fc0aa0a

1 file changed

Lines changed: 40 additions & 29 deletions

File tree

box_bll/scripts/net.inotify

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ events=$1
55
scripts=$(realpath "$0")
66
scripts_dir=$(dirname "$scripts")
77

8-
source ${scripts_dir}/box.config
9-
source ${scripts_dir}/box.iptables
8+
. "${scripts_dir}/box.config"
9+
. "${scripts_dir}/box.iptables"
1010

1111
CLASH_CONFIG="/data/adb/box_bll/clash/config.yaml"
1212
LINKCLEAR_LOG="/data/adb/box_bll/run/linkclear.log"
1313

14-
TZ=Asia/Shanghai
15-
export TZ
14+
export TZ=Asia/Shanghai
1615

1716
TMP_DIR="/dev/tmp"
1817
LINKCLEAR_LOCK="${TMP_DIR}/linkclear.lock"
@@ -21,36 +20,48 @@ BOOT_STABLE_FLAG="${TMP_DIR}/boot_stable.flag"
2120
uptime_sec=$(awk '{print int($1)}' /proc/uptime)
2221

2322
clear_links() {
24-
[ ! -f "$BOOT_STABLE_FLAG" ] && return
23+
[ ! -f "$BOOT_STABLE_FLAG" ] && return
2524

26-
now_ts=$(date +%s)
27-
if [ -f "$LINKCLEAR_LOCK" ]; then
28-
last=$(cat "$LINKCLEAR_LOCK")
29-
[ $((now_ts - last)) -lt 15 ] && return
30-
fi
31-
echo "$now_ts" > "$LINKCLEAR_LOCK"
32-
33-
local PORT=$(grep -E '^[[:space:]]*external-controller:' "$CLASH_CONFIG" | head -n1 | awk -F ':' '{print $NF}' | cut -d'#' -f1 | tr -d ' "\r\n')
34-
local RAW_SECRET=$(grep -E '^[[:space:]]*secret:' "$CLASH_CONFIG" | head -n1 | cut -d':' -f2- | cut -d'#' -f1 | tr -d ' "' | tr -d '\r\n')
25+
now_ts=$(date +%s)
26+
if [ -f "$LINKCLEAR_LOCK" ]; then
27+
last=$(cat "$LINKCLEAR_LOCK")
28+
[ $((now_ts - last)) -lt 15 ] && return
29+
fi
30+
echo "$now_ts" > "$LINKCLEAR_LOCK"
3531

36-
local AUTH_HEADER=""
37-
[ -n "$RAW_SECRET" ] && AUTH_HEADER="-H \"Authorization: Bearer $RAW_SECRET\""
32+
local PORT=$(grep -E '^[[:space:]]*external-controller:' "$CLASH_CONFIG" | head -n1 | awk -F ':' '{print $NF}' | cut -d'#' -f1 | tr -d ' "\r\n')
33+
local RAW_SECRET=$(grep -E '^[[:space:]]*secret:' "$CLASH_CONFIG" | head -n1 | cut -d':' -f2- | cut -d'#' -f1 | tr -d ' "' | tr -d '\r\n')
3834

39-
local URL_CONN="http://127.0.0.1:${PORT}/connections"
40-
eval "curl -s $AUTH_HEADER -X DELETE \"$URL_CONN\"" >/dev/null 2>&1
35+
{
36+
if [ -n "$RAW_SECRET" ]; then
37+
curl -s -H "Authorization: Bearer $RAW_SECRET" -X DELETE "http://127.0.0.1:${PORT}/connections"
38+
else
39+
curl -s -X DELETE "http://127.0.0.1:${PORT}/connections"
40+
fi
41+
} >/dev/null 2>&1
4142
}
4243

4344
rules_add() {
44-
iptables -t mangle -F LOCAL_IP_V4
45-
ip -4 a | awk '/inet/ {print $2}' | grep -vE "^127\.0\.0\.1" | while read -r local_ipv4; do
46-
iptables -w 100 -t mangle -A LOCAL_IP_V4 -d "$local_ipv4" -j ACCEPT
47-
iptables -w 100 -t nat -A LOCAL_IP_V4 -d "$local_ipv4" -j ACCEPT
48-
done
49-
50-
ip6tables -t mangle -F LOCAL_IP_V6
51-
ip -6 a | awk '/inet6/ {print $2}' | grep -vE "^fe80|^::1" | while read -r local_ipv6; do
52-
ip6tables -w 100 -t mangle -A LOCAL_IP_V6 -d "$local_ipv6" -j ACCEPT
53-
done
45+
local ip4_list=$(ip -4 a | awk '/inet/ {print $2}' | grep -vE "^127\.0\.0\.1")
46+
local ip6_list=$(ip -6 a | awk '/inet6/ {print $2}' | grep -vE "^fe80|^::1")
47+
if iptables -w 100 -t mangle -L LOCAL_IP_V4 >/dev/null 2>&1; then
48+
iptables -w 100 -t mangle -F LOCAL_IP_V4
49+
for ip in $ip4_list; do
50+
iptables -w 100 -t mangle -A LOCAL_IP_V4 -d "$ip" -j ACCEPT
51+
done
52+
fi
53+
if iptables -w 100 -t nat -L LOCAL_IP_V4 >/dev/null 2>&1; then
54+
iptables -w 100 -t nat -F LOCAL_IP_V4
55+
for ip in $ip4_list; do
56+
iptables -w 100 -t nat -A LOCAL_IP_V4 -d "$ip" -j ACCEPT
57+
done
58+
fi
59+
if ip6tables -w 100 -t mangle -L LOCAL_IP_V6 >/dev/null 2>&1; then
60+
ip6tables -w 100 -t mangle -F LOCAL_IP_V6
61+
for ip in $ip6_list; do
62+
ip6tables -w 100 -t mangle -A LOCAL_IP_V6 -d "$ip" -j ACCEPT
63+
done
64+
fi
5465
clear_links
5566
}
5667

@@ -66,4 +77,4 @@ if [ "$uptime_sec" -lt 360 ] && [ ! -f "$BOOT_STABLE_FLAG" ]; then
6677
sleep 60
6778
rules_add
6879
touch "$BOOT_STABLE_FLAG"
69-
fi
80+
fi

0 commit comments

Comments
 (0)