@@ -279,13 +279,10 @@ refresh_subscription_dir() {
279279 # 登记到兜底清理列表
280280 SUB_TMP_DIRS=" $SUB_TMP_DIRS $tmp_dir "
281281
282- # 设置请求参数供 import_sub -> run_proxylink 读取
283- SUB_UA=" $ua "
284- SUB_HWID=" $hwid_val "
285-
286282 # 先拉取到临时目录,失败则保留旧节点
283+ # UA/HWID 仅在子 shell 内生效,避免污染全局 (update-all 跨订阅复用)
287284 ensure_dir " $tmp_dir " " 无法创建临时目录: $tmp_dir "
288- if ! ( import_sub " $url " " $tmp_dir " ); then
285+ if ! ( SUB_UA= " $ua " ; SUB_HWID= " $hwid_val " ; import_sub " $url " " $tmp_dir " ); then
289286 log " ERROR" " 订阅拉取失败,保留旧节点: $name "
290287 rm -rf " $tmp_dir "
291288 return 1
@@ -300,6 +297,35 @@ refresh_subscription_dir() {
300297 write_subscription_meta " $sub_dir " " $name " " $url " " $ua " " $hwid_val "
301298}
302299
300+ # ######################################
301+ # 从订阅目录的元数据读取信息并刷新该订阅
302+ # 读取 name/url 及历史 ua/hwid,命令行 SUB_UA/SUB_HWID 优先。
303+ # 参数:
304+ # $1 订阅目录
305+ # 返回: 刷新成功返回 0;元数据缺失或拉取失败返回 1
306+ # ######################################
307+ refresh_subscription_from_meta () {
308+ local sub_dir=" $1 "
309+ local meta_file=" $sub_dir /_meta.json"
310+ local name url saved_ua saved_hwid use_ua use_hwid
311+
312+ # 元数据缺失则跳过该订阅
313+ [ -f " $meta_file " ] || return 1
314+
315+ name=" $( read_subscription_meta_value " $meta_file " " name" || true) "
316+ url=" $( read_subscription_meta_value " $meta_file " " url" || true) "
317+ [ -n " $url " ] || return 1
318+ [ -n " $name " ] || name=" ${sub_dir##*/ } "
319+
320+ # 命令行参数优先,缺省时回退到各订阅持久化值
321+ saved_ua=" $( read_subscription_meta_value " $meta_file " " ua" || true) "
322+ saved_hwid=" $( read_subscription_meta_value " $meta_file " " hwid" || true) "
323+ use_ua=" ${SUB_UA:- $saved_ua } "
324+ use_hwid=" ${SUB_HWID:- $saved_hwid } "
325+
326+ refresh_subscription_dir " $name " " $url " " $sub_dir " " $use_ua " " $use_hwid "
327+ }
328+
303329# ######################################
304330# 添加订阅并首次导入节点
305331# 参数:
@@ -337,61 +363,42 @@ add_subscription() {
337363# ######################################
338364update_subscription () {
339365 local name=" $1 "
340- local sub_dir meta_file url saved_name saved_ua saved_hwid
366+ local sub_dir
341367
342368 [ -n " $name " ] || die " 用法: $( basename " $0 " ) update <名称> [-ua <UA>] [-hwid <HWID>]"
343369
344370 sub_dir=" $( subscription_dir_from_name " $OUTBOUNDS_DIR " " $name " ) "
345- meta_file=" $sub_dir /_meta.json"
346-
347- # 从元数据读取订阅链接与历史请求参数
348- require_file " $meta_file " " 订阅不存在: $name "
349- saved_name=" $( read_subscription_meta_value " $meta_file " " name" || true) "
350- url=" $( read_subscription_meta_value " $meta_file " " url" || true) "
351- saved_ua=" $( read_subscription_meta_value " $meta_file " " ua" || true) "
352- saved_hwid=" $( read_subscription_meta_value " $meta_file " " hwid" || true) "
353-
354- [ -n " $url " ] || die " 无法读取订阅链接: $meta_file "
355- [ -n " $saved_name " ] || saved_name=" $name "
371+ require_file " $sub_dir /_meta.json" " 订阅不存在: $name "
356372
357- # 命令行参数优先,缺省时回退到持久化值
358- [ -n " $SUB_UA " ] || SUB_UA=" $saved_ua "
359- [ -n " $SUB_HWID " ] || SUB_HWID=" $saved_hwid "
360-
361- refresh_subscription_dir " $saved_name " " $url " " $sub_dir " " $SUB_UA " " $SUB_HWID "
362- log " INFO" " 订阅更新完成: $saved_name "
373+ refresh_subscription_from_meta " $sub_dir " || die " 订阅更新失败: $name "
374+ log " INFO" " 订阅更新完成: $name "
363375}
364376
365377# ######################################
366378# 更新全部订阅
379+ # 单个订阅失败时记 WARN 并跳过,继续更新其余订阅。
367380# 参数: 无
368- # 返回: 无 (逐个更新所有订阅目录 )
381+ # 返回: 无 (汇总成功/失败数 )
369382# ######################################
370383update_all_subscriptions () {
371- local sub_dir meta_file name url saved_ua saved_hwid count =0
384+ local sub_dir name ok=0 failed =0
372385
373386 # 遍历所有订阅目录
374387 for sub_dir in " $OUTBOUNDS_DIR " /sub_* ; do
375388 [ -d " $sub_dir " ] || continue
376- meta_file=" $sub_dir /_meta.json"
377- [ -f " $meta_file " ] || continue
378-
379- name=" $( read_subscription_meta_value " $meta_file " " name" || true) "
380- url=" $( read_subscription_meta_value " $meta_file " " url" || true) "
381- [ -n " $url " ] || continue
382- [ -n " $name " ] || name=" ${sub_dir##*/ } "
383-
384- # 命令行参数优先,否则使用各订阅各自的持久化值
385- saved_ua=" $( read_subscription_meta_value " $meta_file " " ua" || true) "
386- saved_hwid=" $( read_subscription_meta_value " $meta_file " " hwid" || true) "
387- local use_ua=" ${SUB_UA:- $saved_ua } "
388- local use_hwid=" ${SUB_HWID:- $saved_hwid } "
389-
390- refresh_subscription_dir " $name " " $url " " $sub_dir " " $use_ua " " $use_hwid "
391- count=$(( count + 1 ))
389+ [ -f " $sub_dir /_meta.json" ] || continue
390+
391+ name=" ${sub_dir##*/ } "
392+ # 容错:单订阅失败不中断整体
393+ if refresh_subscription_from_meta " $sub_dir " ; then
394+ ok=$(( ok + 1 ))
395+ else
396+ failed=$(( failed + 1 ))
397+ log " WARN" " 订阅更新失败,已跳过: $name "
398+ fi
392399 done
393400
394- log " INFO" " 全部订阅更新完成,共 $count 个"
401+ log " INFO" " 全部订阅更新完成,成功 $ok 个,失败 $failed 个"
395402}
396403
397404# ######################################
0 commit comments