Skip to content

Commit 70445c9

Browse files
committed
refactor(core): 优化服务启动流程并修复节点测速 group 问题
1. 修复 Auto-Fastest 节点组逻辑:在扫描节点时自动过滤 `default` 标签,防止直连节点进入测速组导致代理失效。 2. 架构重构:将散乱的环境检查与变量加载逻辑整合至 `initialize_runtime_context`, 提升代码的可读性与维护性。
1 parent 8621d32 commit 70445c9

2 files changed

Lines changed: 77 additions & 57 deletions

File tree

src/module/scripts/core/runtime.sh

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,86 @@ append_selector_tag() {
4848
fi
4949
}
5050

51+
# 运行时上下文(由 initialize_runtime_context 填充)
52+
CUR_OUTBOUND_CONFIG=""
53+
CUR_OUTBOUND_DIR=""
54+
CUR_OUTBOUND_MODE=""
55+
CUR_SELECTOR_MODE=""
56+
57+
# 节点扫描结果(由 write_runtime_outbounds 填充)
58+
SCAN_NODE_ARGS=""
59+
SCAN_NODE_COUNT=0
60+
SCAN_SKIPPED_COUNT=0
61+
62+
#######################################
63+
# 初始化启动环境与基础配置
64+
#######################################
65+
initialize_runtime_context() {
66+
# 基础环境检查
67+
[ -x "${SING_BOX_BIN:-}" ] || die "sing-box 二进制不存在或不可执行"
68+
[ -f "${MODULE_CONF:-}" ] || die "模块配置文件不存在"
69+
[ -f "${TPROXY_CONF_DIR:-}/tproxy.conf" ] || die "透明代理配置文件不存在"
70+
71+
# 加载模块与透明代理配置
72+
. "$MODULE_CONF"
73+
. "$TPROXY_CONF_DIR/tproxy.conf"
74+
75+
# 提取并验证当前节点路径
76+
CUR_OUTBOUND_CONFIG="$(strip_quotes "${CURRENT_CONFIG:-}")"
77+
[ -n "$CUR_OUTBOUND_CONFIG" ] || die "CURRENT_CONFIG 未定义,请先选择节点"
78+
[ -f "$CUR_OUTBOUND_CONFIG" ] || die "指定的节点配置文件不存在: $CUR_OUTBOUND_CONFIG"
79+
80+
# 确定运行模式与选择器模式
81+
CUR_OUTBOUND_MODE="${OUTBOUND_MODE:-rule}"
82+
CUR_SELECTOR_MODE="${SELECTOR_MODE:-urltest}"
83+
84+
# 获取当前节点目录
85+
CUR_OUTBOUND_DIR="$(get_current_outbounds_dir "$CUR_OUTBOUND_CONFIG")" || return 1
86+
}
87+
5188
#######################################
52-
# 写入运行时出站配置
89+
# 扫描节点并生成运行时出站配置
5390
#######################################
5491
write_runtime_outbounds() {
55-
local current_config="$1"
56-
local selector_mode="${2:-urltest}"
57-
local output="$RUNTIME_DIR/outbounds.json"
58-
local current_dir current_tag current_tag_json tags="" f tag
92+
local output="${RUNTIME_DIR:?RUNTIME_DIR 未定义}/outbounds.json"
93+
local current_config="${1:-$CUR_OUTBOUND_CONFIG}"
94+
local current_dir="${CUR_OUTBOUND_DIR:-$(get_current_outbounds_dir "$current_config")}"
95+
local selector_mode="${2:-$CUR_SELECTOR_MODE}"
96+
local current_tag current_tag_json tags="" f tag
97+
98+
SCAN_NODE_ARGS=""
99+
SCAN_NODE_COUNT=0
100+
SCAN_SKIPPED_COUNT=0
59101

60-
current_dir="$(get_current_outbounds_dir "$current_config")"
61102
current_tag="$(detect_outbound_tag "$current_config")"
62103
[ -n "$current_tag" ] || die "无法从当前出站配置读取标签: $current_config"
63104
current_tag_json="$(json_escape "$current_tag")"
64105

65106
mkdir -p "$RUNTIME_DIR" || die "无法创建运行时配置目录: $RUNTIME_DIR"
66107

67-
# 扫描当前节点目录,收集可切换的出站标签
108+
log "INFO" "正在扫描节点目录: $current_dir"
109+
110+
# 扫描当前节点目录
68111
for f in "$current_dir"/*.json; do
69112
is_node_config_file "$f" || continue
70113
tag="$(detect_outbound_tag "$f")"
71-
[ -n "$tag" ] && [ "$tag" != "default" ] || continue
72-
tags="$(append_selector_tag "$tags" "$tag")"
114+
115+
if [ -z "$tag" ]; then
116+
SCAN_SKIPPED_COUNT=$((SCAN_SKIPPED_COUNT + 1))
117+
continue
118+
fi
119+
120+
# 收集启动参数(所有节点都加载)
121+
SCAN_NODE_ARGS="$SCAN_NODE_ARGS -c \"$f\""
122+
SCAN_NODE_COUNT=$((SCAN_NODE_COUNT + 1))
123+
124+
# 收集可切换标签(过滤掉 default 以防止抢占测速组)
125+
if [ "$tag" != "default" ]; then
126+
tags="$(append_selector_tag "$tags" "$tag")"
127+
fi
73128
done
74129

75-
# 未发现节点时,至少保留当前节点
130+
# 未发现可切换节点时,至少保留当前节点供测速/选择
76131
[ -n "$tags" ] || tags="$(append_selector_tag "" "$current_tag")"
77132

78133
case "$selector_mode" in

src/module/scripts/core/service.sh

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,8 @@ readonly KILL_TIMEOUT=5
2121

2222
export PATH="$MODDIR/bin:$PATH"
2323

24-
2524
readonly BUSYBOX="$(detect_busybox)"
2625

27-
#######################################
28-
# 环境与配置校验
29-
#######################################
30-
verify_environment() {
31-
[ -x "$SING_BOX_BIN" ] || die "sing-box 二进制不存在或不可执行: $SING_BOX_BIN"
32-
[ -f "$MODULE_CONF" ] || die "模块配置文件不存在: $MODULE_CONF"
33-
[ -f "$TPROXY_CONF_DIR/tproxy.conf" ] || die "透明代理配置文件不存在: $TPROXY_CONF_DIR/tproxy.conf"
34-
35-
. "$MODULE_CONF"
36-
. "$TPROXY_CONF_DIR/tproxy.conf"
37-
38-
local outbound_config
39-
outbound_config="$(strip_quotes "${CURRENT_CONFIG:-}")"
40-
[ -n "$outbound_config" ] || die "CURRENT_CONFIG 未定义,请先选择节点"
41-
[ -f "$outbound_config" ] || die "指定的节点配置文件不存在: $outbound_config"
42-
43-
echo "$outbound_config"
44-
}
4526

4627
#######################################
4728
# 启动服务
@@ -57,37 +38,21 @@ do_start() {
5738
return 0
5839
fi
5940

60-
# 准备节点与运行时出站配置
61-
local outbound_config outbound_dir outbound_mode selector_mode runtime_outbounds
62-
outbound_config="$(verify_environment)" || exit 1
63-
. "$MODULE_CONF"
64-
outbound_mode="${OUTBOUND_MODE:-rule}"
65-
selector_mode="${SELECTOR_MODE:-urltest}"
66-
outbound_dir="$(get_current_outbounds_dir "$outbound_config")" || exit 1
67-
runtime_outbounds="$(write_runtime_outbounds "$outbound_config" "$selector_mode")" || exit 1
41+
# 准备启动环境与配置
42+
initialize_runtime_context || exit 1
43+
write_runtime_outbounds || exit 1
44+
local runtime_outbounds="$RUNTIME_DIR/outbounds.json"
6845

69-
log "INFO" "路由模式: $outbound_mode"
46+
log "INFO" "路由模式: $CUR_OUTBOUND_MODE"
47+
log "INFO" "节点目录: $CUR_OUTBOUND_DIR"
7048

71-
# 构造启动参数
72-
local f tag node_count=0 skipped_count=0
49+
# 构造最终启动参数
7350
set -- run -C "$CONFDIR"
74-
75-
for f in "$outbound_dir"/*.json; do
76-
is_node_config_file "$f" || continue
77-
tag="$(detect_outbound_tag "$f")"
78-
if [ -z "$tag" ]; then
79-
skipped_count=$((skipped_count + 1))
80-
continue
81-
fi
82-
83-
set -- "$@" -c "$f"
84-
node_count=$((node_count + 1))
85-
done
86-
87-
[ "$node_count" -gt 0 ] || die "当前节点目录没有可加载的节点配置: $outbound_dir"
88-
log "INFO" "节点目录: $outbound_dir"
89-
log "INFO" "已加载节点: $node_count,跳过无效节点: $skipped_count"
9051
[ -n "$runtime_outbounds" ] && set -- "$@" -c "$runtime_outbounds"
52+
eval "set -- \"\$@\" $SCAN_NODE_ARGS"
53+
54+
[ "$SCAN_NODE_COUNT" -gt 0 ] || die "当前节点目录没有可加载的节点配置: $CUR_OUTBOUND_DIR"
55+
log "INFO" "已加载节点: $SCAN_NODE_COUNT,跳过节点: $SCAN_SKIPPED_COUNT"
9156

9257
# 启动 sing-box 进程
9358
log "INFO" "正在启动 sing-box 进程..."
@@ -105,7 +70,7 @@ do_start() {
10570
fi
10671

10772
# 同步运行模式并载入透明代理规则
108-
sh "$MODDIR/scripts/core/switch.sh" mode "$outbound_mode" >> "$LOG_FILE" 2>&1 || log "WARN" "控制接口失败"
73+
sh "$MODDIR/scripts/core/switch.sh" mode "$CUR_OUTBOUND_MODE" >> "$LOG_FILE" 2>&1 || log "WARN" "控制接口失败"
10974
log "INFO" "载入透明代理规则..."
11075
"$MODDIR/scripts/network/tproxy.sh" start -d "$TPROXY_CONF_DIR" >> "$LOG_FILE" 2>&1
11176

0 commit comments

Comments
 (0)