Skip to content

Commit 1efe021

Browse files
feat(inotify): upgrade network switch logic from module start stop to iptables hot switch
1 parent fc0aa0a commit 1efe021

1 file changed

Lines changed: 89 additions & 70 deletions

File tree

box_bll/scripts/ctr.inotify

Lines changed: 89 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export PATH="/data/adb/magisk:/data/adb/ksu/bin:/data/adb/ap/bin:$PATH:/data/dat
55
variab_dir="/dev/tmp/"
66
module_dir="/data/adb/modules/Surfing"
77
base_dir="/data/adb/box_bll"
8-
log_file="${base_dir}/run/Networkswitch.log"
8+
log_file="${base_dir}/run/debugnet.log"
99
temp_log_file="${variab_dir}debug.log.tmp"
1010
last_check_file="${variab_dir}/last_check_time"
1111
last_wifi_state_file="${variab_dir}/last_wifi_state"
@@ -82,12 +82,53 @@ get_current_ip() {
8282
ip addr show wlan0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1
8383
}
8484

85+
apply_wifi_bypass() {
86+
local state="$1"
87+
88+
{
89+
if [ "$state" = "enable" ]; then
90+
for cmd in iptables "ip6tables -w 100"; do
91+
for table in mangle nat filter; do
92+
[ "$table" = "filter" ] && t_arg="" || t_arg="-t $table"
93+
$cmd $t_arg -N WIFI_BYPASS
94+
$cmd $t_arg -F WIFI_BYPASS
95+
$cmd $t_arg -A WIFI_BYPASS -j ACCEPT
96+
done
97+
done
98+
99+
for chain in BOX_LOCAL BOX_EXTERNAL CLASH_DNS_LOCAL CLASH_DNS_EXTERNAL; do
100+
for cmd in iptables "ip6tables -w 100"; do
101+
$cmd -t mangle -I $chain 1 -j WIFI_BYPASS
102+
$cmd -t nat -I $chain 1 -j WIFI_BYPASS
103+
done
104+
done
105+
106+
iptables -w 100 -I BOX_DNS 1 -j WIFI_BYPASS
107+
ip6tables -w 100 -I BOX_DNS 1 -j WIFI_BYPASS
108+
109+
touch "${variab_dir}/wifi_bypassed"
110+
111+
elif [ "$state" = "disable" ]; then
112+
for chain in BOX_LOCAL BOX_EXTERNAL CLASH_DNS_LOCAL CLASH_DNS_EXTERNAL; do
113+
for cmd in iptables "ip6tables -w 100"; do
114+
while $cmd -t mangle -D $chain -j WIFI_BYPASS; do :; done
115+
while $cmd -t nat -D $chain -j WIFI_BYPASS; do :; done
116+
done
117+
done
118+
while iptables -w 100 -D BOX_DNS -j WIFI_BYPASS; do :; done
119+
while ip6tables -w 100 -D BOX_DNS -j WIFI_BYPASS; do :; done
120+
121+
rm "${variab_dir}/wifi_bypassed"
122+
fi
123+
} >/dev/null 2>&1
124+
}
125+
85126
check_module_service() {
86127
if [ "$enable_network_service_control" = "true" ]; then
87-
if [ -f "${module_dir}/disable" ]; then
88-
current_state="disabled"
128+
if [ -f "${variab_dir}/wifi_bypassed" ]; then
129+
current_state="bypassed"
89130
else
90-
current_state="enabled"
131+
current_state="proxying"
91132
fi
92133

93134
last_ssid=""
@@ -108,121 +149,99 @@ check_module_service() {
108149

109150
if [ -n "$current_ip" ]; then
110151
log_msg ""
111-
log_msg "网络波动?网络发生变化!"
112-
log_msg "WiFi已连接: ${ssid}"
113-
log_msg "IP地址:${current_ip}"
152+
log_msg "网络发生变化!"
153+
log_msg "WiFi已连接: ${ssid}, IP地址:${current_ip}"
114154
echo "ssid:${ssid}" > "$last_wifi_state_file"
115155
echo "ip:${current_ip}" >> "$last_wifi_state_file"
116156

117157
if [ "$use_module_on_wifi" = "false" ]; then
118-
if [ "$current_state" != "disabled" ]; then
119-
touch "${module_dir}/disable"
120-
log_msg "当前配置:禁用"
121-
log_msg "模块服务已禁用"
158+
if [ "$current_state" != "bypassed" ]; then
159+
apply_wifi_bypass "enable"
160+
log_msg "当前配置:WiFi下不代理"
161+
log_msg "已执行热切换 -> 全局直连"
122162
else
123-
log_msg "当前配置:禁用"
124-
log_msg "当前服务已是禁用,无需操作"
163+
log_msg "当前配置:WiFi下不代理"
164+
log_msg "当前已是直连状态,无需操作"
125165
fi
126166
else
127167
if [ "$use_ssid_matching" = "true" ]; then
128168
if [ "$use_wifi_list_mode" = "blacklist" ]; then
129-
log_msg "黑名单模式:启用"
130-
log_msg "黑名单列表: ${blacklist_wifi_ssids}"
169+
log_msg "黑名单模式:启用. 黑名单列表: ${blacklist_wifi_ssids}"
131170
if is_allowed_wifi "$ssid" "$blacklist_wifi_ssids"; then
132-
if [ "$current_state" != "disabled" ]; then
133-
touch "${module_dir}/disable"
134-
log_msg "当前配置:禁用"
135-
log_msg "已命中列表,模块服务已禁用"
136-
171+
if [ "$current_state" != "bypassed" ]; then
172+
apply_wifi_bypass "enable"
173+
log_msg "已命中黑名单,执行热切换 -> 全局直连"
137174
else
138-
log_msg "当前配置:禁用"
139-
log_msg "已命中列表,当前模块服务已是禁用,无需操作"
175+
log_msg "已命中黑名单,当前已是直连状态,无需操作"
140176
fi
141177
else
142-
if [ "$current_state" != "enabled" ]; then
143-
rm "${module_dir}/disable" 2>/dev/null
144-
log_msg "当前配置:启用"
145-
log_msg "未命中列表,模块服务已启用"
178+
if [ "$current_state" != "proxying" ]; then
179+
apply_wifi_bypass "disable"
180+
log_msg "未命中黑名单,执行热切换 -> 恢复代理"
146181
else
147-
log_msg "当前配置:启用"
148-
log_msg "未命中列表,模块当前已是启用,无需操作"
182+
log_msg "未命中黑名单,当前已是代理状态,无需操作"
149183
fi
150184
fi
151185
elif [ "$use_wifi_list_mode" = "whitelist" ]; then
152-
log_msg "白名单模式:启用"
153-
log_msg "白名单列表: ${whitelist_wifi_ssids}"
186+
log_msg "白名单模式:启用. 白名单列表: ${whitelist_wifi_ssids}"
154187
if is_allowed_wifi "$ssid" "$whitelist_wifi_ssids"; then
155-
if [ "$current_state" != "enabled" ]; then
156-
rm "${module_dir}/disable" 2>/dev/null
157-
log_msg "当前配置:启用"
158-
log_msg "已命中列表,模块服务已启用"
188+
if [ "$current_state" != "proxying" ]; then
189+
apply_wifi_bypass "disable"
190+
log_msg "已命中白名单,执行热切换 -> 恢复代理"
159191
else
160-
log_msg "当前配置:启用"
161-
log_msg "已命中列表,模块当前已是启用,无需操作"
192+
log_msg "已命中白名单,当前已是代理状态,无需操作"
162193
fi
163194
else
164-
if [ "$current_state" != "disabled" ]; then
165-
touch "${module_dir}/disable"
166-
log_msg "当前配置:禁用"
167-
log_msg "未命中列表,已禁用模块服务"
195+
if [ "$current_state" != "bypassed" ]; then
196+
apply_wifi_bypass "enable"
197+
log_msg "未命中白名单,执行热切换 -> 全局直连"
168198
else
169-
log_msg "当前配置:禁用"
170-
log_msg "未命中列表,模块当前已是禁用,无需操作"
199+
log_msg "未命中白名单,当前已是直连状态,无需操作"
171200
fi
172201
fi
173202
else
174203
log_msg "警告:当前黑白名单模式选择为空"
175-
if [ "$current_state" != "enabled" ]; then
176-
rm "${module_dir}/disable" 2>/dev/null
177-
log_msg "当前配置:启用"
178-
log_msg "模块服务已启用(默认逻辑)"
204+
if [ "$current_state" != "proxying" ]; then
205+
apply_wifi_bypass "disable"
206+
log_msg "执行热切换 -> [恢复代理(默认逻辑)]"
179207
else
180-
log_msg "当前配置:启用"
181-
log_msg "模块服务已是启用,无需操作(默认逻辑)"
208+
log_msg "模块当前已是代理状态,无需操作(默认逻辑)"
182209
fi
183210
fi
184211
else
185212
log_msg "使用常规WiFi模块设置"
186-
if [ "$current_state" != "enabled" ]; then
187-
rm "${module_dir}/disable" 2>/dev/null
188-
log_msg "当前配置:启用"
189-
log_msg "模块服务已启用"
213+
if [ "$current_state" != "proxying" ]; then
214+
apply_wifi_bypass "disable"
215+
log_msg "执行热切换 -> 恢复代理"
190216
else
191-
log_msg "当前配置:启用"
192-
log_msg "模块服务已是启用,无需操作"
217+
log_msg "模块当前已是代理状态,无需操作"
193218
fi
194219
fi
195220
fi
196221
else
197222
log_msg "未获取到有效的IP地址,视为WiFi连接失败"
198223
fi
199-
200224
elif [ "$wifi_status" = "not_wifi" ]; then
201225
log_msg ""
202-
log_msg "网络波动?网络发生变化!"
203-
log_msg "当前WiFi已断开!"
226+
log_msg "网络发生变化!当前WiFi已断开!"
204227
if [ "$use_module_on_wifi_disconnect" = "true" ]; then
205-
if [ "$current_state" != "enabled" ]; then
206-
rm "${module_dir}/disable" 2>/dev/null
207-
log_msg "当前配置:启用"
208-
log_msg "模块服务已启用"
228+
if [ "$current_state" != "proxying" ]; then
229+
apply_wifi_bypass "disable"
230+
log_msg "断开WiFi后:执行热切换 -> 恢复代理"
209231
else
210-
log_msg "当前配置:启用"
211-
log_msg "模块服务已是启用,无需操作"
232+
log_msg "断开WiFi后:当前已是代理状态,无需操作"
212233
fi
213234
else
214-
if [ "$current_state" != "disabled" ]; then
215-
touch "${module_dir}/disable"
216-
log_msg "当前配置:禁用"
217-
log_msg "模块服务已禁用"
235+
if [ "$current_state" != "bypassed" ]; then
236+
apply_wifi_bypass "enable"
237+
log_msg "断开WiFi后:执行热切换 -> 全局直连"
218238
else
219-
log_msg "当前配置:禁用"
220-
log_msg "当前模块服务已是禁用,无需操作"
239+
log_msg "断开WiFi后:当前已是直连状态,无需操作"
221240
fi
222241
fi
223242
fi
224243
else
225-
log_msg "网络波动?网络发生变化!"
244+
log_msg "网络发生变化!"
226245
log_msg "网络控制模块服务已停用!"
227246
return
228247
fi

0 commit comments

Comments
 (0)