Skip to content

Commit 78c6e15

Browse files
committed
feat(core): 修复改进一些问题
- `box.tool`: 改进renew=true时的log显示 - `box.iptables`: 同步上游,去掉tun_forward - `box.service`: 优化核心启动的指令
1 parent 854bc4e commit 78c6e15

4 files changed

Lines changed: 19 additions & 106 deletions

File tree

box/mihomo/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dns:
8383
ipv6: true
8484
listen: 0.0.0.0:1053
8585
enhanced-mode: fake-ip
86-
fake-ip-range: 192.18.0.0/15
86+
fake-ip-range: 28.0.0.1/8
8787
fake-ip-filter:
8888
- "rule-set:Fake-IP-Filter"
8989
proxy-server-nameserver:

box/scripts/box.iptables

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ table="2024"
88
pref="100"
99
# 使用 iptables 规则禁用或启用 QUIC。注意,这可能导致部分网站无法访问。
1010
quic="enable"
11-
tun_forward="enable"
1211
mihomo_dns_forward="enable"
1312
fake_ip_range=""
1413

@@ -46,7 +45,6 @@ case "${bin_name}" in
4645
fake_ip6_range=$(busybox awk -F'"' '/inet6_range/ {print $4}' "${sing_config}")
4746
;;
4847
"hysteria")
49-
# 验证 hysteria 的 network_mode
5048
case "${network_mode}" in
5149
redirect|tproxy|enhance)
5250
# 支持的模式,无需操作
@@ -192,73 +190,10 @@ probe_tun_device() {
192190
busybox ifconfig | grep -q "${tun_device}" || return 1
193191
}
194192

195-
probe_tun_index() {
196-
while [ ! -f "/data/misc/net/rt_tables" ]; do
197-
sleep 1
198-
done
199-
200-
while read -r index name; do
201-
if [ "${name}" = "${tun_device}" ]; then
202-
tun_table_index=${index}
203-
return 0
204-
fi
205-
done < /data/misc/net/rt_tables
206-
207-
return 1
208-
}
209-
210-
tun_forward_ip_rules() {
211-
local action=$1
212-
ipv4_rules=(
213-
"iif lo goto 6000 pref 5000"
214-
"iif ${tun_device} lookup main suppress_prefixlength 0 pref 5010"
215-
"iif ${tun_device} goto 6000 pref 5020"
216-
"from 10.0.0.0/8 lookup ${tun_table_index} pref 5030"
217-
"from 172.16.0.0/12 lookup ${tun_table_index} pref 5040"
218-
"from 192.168.0.0/16 lookup ${tun_table_index} pref 5050"
219-
"nop pref 6000"
220-
)
221-
222-
ipv6_rules=(
223-
"iif lo goto 6000 pref 5000"
224-
"iif ${tun_device} lookup main suppress_prefixlength 0 pref 5010"
225-
"iif ${tun_device} goto 6000 pref 5020"
226-
"from fc00::/7 lookup ${tun_table_index} pref 5030" # ULA
227-
"from fd00::/8 lookup ${tun_table_index} pref 5040" # ULA 子集
228-
"from fe80::/10 lookup ${tun_table_index} pref 5050" # 链路本地
229-
# "from 2000::/3 lookup ${tun_table_index} pref 5060"
230-
"nop pref 6000"
231-
)
232-
233-
if [ "${iptables}" = "$IPV" ]; then
234-
for rule in "${ipv4_rules[@]}"; do
235-
ip -4 rule "${action}" ${rule}
236-
done
237-
else
238-
for rule in "${ipv6_rules[@]}"; do
239-
ip -6 rule "${action}" ${rule}
240-
done
241-
fi
242-
}
243-
244-
tun_forward_ip_rules_del() {
245-
for pref in 5000 5010 5020 5030 5040 5050 6000; do
246-
ip -4 rule del pref $pref >/dev/null 2>&1
247-
ip -6 rule del pref $pref >/dev/null 2>&1
248-
done
249-
}
250-
251-
sing_tun_ip_rules() {
252-
ip -4 rule $1 from all iif ${tun_device} lookup main suppress_prefixlength 0 pref 8000
253-
ip -4 rule $1 lookup main pref 7000
254-
ip -6 rule $1 from all iif ${tun_device} lookup main suppress_prefixlength 0 pref 8000
255-
ip -6 rule $1 lookup main pref 7000
256-
}
257-
258193
forward() {
259194
local action=$1
260195

261-
${iptables} -t nat "${action}" POSTROUTING -o ${tun_device} -j MASQUERADE
196+
# ${iptables} -t nat "${action}" POSTROUTING -o ${tun_device} -j MASQUERADE
262197

263198
${iptables} "${action}" FORWARD -i "${tun_device}" -j ACCEPT
264199
${iptables} "${action}" FORWARD -o "${tun_device}" -j ACCEPT
@@ -267,28 +202,8 @@ forward() {
267202
sysctl -w net.ipv4.conf.default.rp_filter=2
268203
sysctl -w net.ipv4.conf.all.rp_filter=2
269204

270-
probe_tun_index
271-
272-
if [ "${tun_forward}" = "enable" ]; then
273-
if probe_tun_device; then
274-
tun_forward_ip_rules_del
275-
tun_forward_ip_rules "${action}"
276-
if [ "${action}" = "-I" ]; then
277-
sing_tun_ip_rules "add"
278-
else
279-
sing_tun_ip_rules "del"
280-
fi
281-
return 0
282-
else
283-
tun_forward_ip_rules_del
284-
tun_forward_ip_rules -D
285-
sing_tun_ip_rules "del"
286-
return 1
287-
fi
288-
fi
289205
} >/dev/null 2>&1
290206

291-
# 下面所有日志和注释均已汉化
292207
start_redirect() {
293208
if [ "${iptables}" = "$IPV" ]; then
294209
${iptables} -t nat -N BOX_EXTERNAL
@@ -329,6 +244,9 @@ start_redirect() {
329244
${iptables} -t nat -A BOX_EXTERNAL -p tcp -i lo -j REDIRECT --to-ports "${redir_port}"
330245

331246
if [ "${ap_list}" != "" ]; then
247+
for ap in "${ap_list[@]}"; do
248+
${iptables} -t nat -A BOX_EXTERNAL -p tcp -i "${ap}" -j REDIRECT --to-ports "${redir_port}"
249+
done
332250
[ ${network_mode} = "enhance" ] || log Info "${ap_list[*]} 透明代理。"
333251
fi
334252

@@ -458,9 +376,9 @@ start_tproxy() {
458376
fi
459377

460378
# 跳过已被 TProxy 处理的流量,若默认路由接口有公网 IP,省略这些规则会导致本地流量代理异常,可能拖慢全网
461-
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_EXTERNAL -p tcp -m socket --transparent -j MARK --set-xmark ${fwmark}
462-
${iptables} -t mangle -A BOX_EXTERNAL -p udp -m socket --transparent -j MARK --set-xmark ${fwmark}
463-
${iptables} -t mangle -A BOX_EXTERNAL -m socket -j RETURN
379+
# [ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_EXTERNAL -p tcp -m socket --transparent -j MARK --set-xmark ${fwmark}
380+
# ${iptables} -t mangle -A BOX_EXTERNAL -p udp -m socket --transparent -j MARK --set-xmark ${fwmark}
381+
# ${iptables} -t mangle -A BOX_EXTERNAL -m socket -j RETURN
464382

465383
# 跳过内网,兼容性可用 su -c 'zcat /proc/config.gz | grep -i addrtype' 检查
466384
# ${iptables} -t mangle -A BOX_EXTERNAL -m addrtype --dst-type LOCAL -j RETURN
@@ -496,10 +414,6 @@ start_tproxy() {
496414
${iptables} -t mangle -N BOX_LOCAL
497415
${iptables} -t mangle -F BOX_LOCAL
498416

499-
if [ "${iptables}" = "$IP6V" ]; then
500-
${iptables} -t mangle -A BOX_LOCAL -o lo -j RETURN
501-
${iptables} -t mangle -A BOX_LOCAL -d ::1/128 -j RETURN
502-
fi
503417
${iptables} -t mangle -A BOX_LOCAL -m owner --uid-owner ${box_user} --gid-owner ${box_group} -j RETURN
504418
# ${iptables} -t mangle -A BOX_LOCAL -m mark --mark ${routing_mark} -j RETURN
505419

@@ -587,11 +501,11 @@ start_tproxy() {
587501

588502
${iptables} -t mangle -I OUTPUT -j BOX_LOCAL
589503

590-
# ${iptables} -t mangle -N DIVERT
591-
# ${iptables} -t mangle -F DIVERT
592-
# ${iptables} -t mangle -A DIVERT -j MARK --set-xmark "${fwmark}"
593-
# ${iptables} -t mangle -A DIVERT -j ACCEPT
594-
# [ ${network_mode} = "enhance" ] || ${iptables} -t mangle -I PREROUTING -p tcp -m socket -j DIVERT
504+
${iptables} -t mangle -N DIVERT
505+
${iptables} -t mangle -F DIVERT
506+
${iptables} -t mangle -A DIVERT -j MARK --set-xmark "${fwmark}"
507+
${iptables} -t mangle -A DIVERT -j ACCEPT
508+
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -I PREROUTING -p tcp -m socket -j DIVERT
595509

596510
# 禁用 QUIC
597511
if [ "${quic}" = "disable" ]; then
@@ -941,7 +855,6 @@ else
941855
disable_ipv6
942856
log Warning "已禁用 IPv6。"
943857
fi
944-
[ "${tun_forward}" = "enable" ] && log Info "TUN 热点支持已启用。" || log Warning "TUN 热点支持已禁用。"
945858
[ $1 = "renew" ] && log Info "重启 iptables TUN 规则完成。"
946859
bin_alive && log Info "${bin_name} 已连接。"
947860
;;

box/scripts/box.service

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ box_run_bin() {
487487
prepare_singbox
488488
fi
489489
if ${bin_path} check -c "${sing_config}" -D "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1; then
490-
${RUN_CMD} "${bin_path}" run -c "${sing_config}" -D "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
490+
nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -D "${sing_config}" -C "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
491491
PID=$!
492492
echo -n $PID > "${box_pid}"
493493
else
@@ -502,7 +502,7 @@ box_run_bin() {
502502
prepare_mihomo
503503
fi
504504
if ${bin_path} -t -d "${box_dir}/${bin_name}" -f "${mihomo_config}" > "${box_run}/${bin_name}.log" 2>&1; then
505-
${RUN_CMD} "${bin_path}" -d "${box_dir}/${bin_name}" -f "${mihomo_config}" > "${bin_log}" 2>&1 &
505+
nohup busybox setuidgid "${box_user_group}" "${bin_path}" -d "${box_dir}/${bin_name}" -f "${mihomo_config}" > "${bin_log}" 2>&1 &
506506
PID=$!
507507
echo -n $PID > "${box_pid}"
508508
else
@@ -530,7 +530,7 @@ box_run_bin() {
530530
fi
531531
export XRAY_LOCATION_ASSET="${box_dir}/${bin_name}"
532532
if ${bin_path} -test -confdir "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1; then
533-
${RUN_CMD} ${bin_path} run -confdir "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
533+
nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -confdir "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
534534
PID=$!
535535
echo -n $PID > "${box_pid}"
536536
else
@@ -558,7 +558,7 @@ box_run_bin() {
558558
fi
559559
export V2RAY_LOCATION_ASSET="${box_dir}/${bin_name}"
560560
if ${bin_path} test -d "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1; then
561-
${RUN_CMD} ${bin_path} run -d "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
561+
nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -d "${box_dir}/${bin_name}" > "${bin_log}" 2>&1 &
562562
PID=$!
563563
echo -n $PID > "${box_pid}"
564564
else

box/scripts/box.tool

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,15 @@ upsubs() {
387387
if [ "${renew}" = "true" ] && [ "$i" -eq 0 ]; then
388388
log Info "检测到 renew=true, 仅使用第一个订阅链接更新"
389389
if upfile "${mihomo_config}" "${url}" "ClashMeta"; then
390-
log Info "config.yaml 更新成功"
390+
log Info "${mihomo_config} 更新成功"
391391
if [ -f "${box_pid}" ]; then
392392
kill -0 "$(<"${box_pid}" 2>/dev/null)" && \
393393
$scripts_dir/box.service restart 2>/dev/null
394394
fi
395395
log Info "${bin_name} 订阅更新完成 → $(date)"
396396
exit 0
397397
else
398-
log Error "config.yaml 更新失败"
398+
log Error "${mihomo_config} 更新失败"
399399
exit 1
400400
fi
401401
fi

0 commit comments

Comments
 (0)