diff --git a/router/mihomo-kit.sh b/router/mihomo-kit.sh index 407eafa..6bbea13 100755 --- a/router/mihomo-kit.sh +++ b/router/mihomo-kit.sh @@ -644,21 +644,22 @@ core_ok() { fi } -core_update() { +# Core change in two steps around the nikki restart (cmd_fix): core_fetch while +# nikki still runs - the router may reach the download host only through mihomo - +# then core_install with nikki stopped. CORE_PENDING: "" (nothing), stock or new. +core_fetch() { + CORE_PENDING="" cur=$(mihomo -v 2>/dev/null | awk '{print $3; exit}') if core_ok; then log "mihomo core is $cur" return 0 fi if [ "$MIHOMO_VERSION" = stock ]; then - # back to the package's core: /usr/bin/mihomo becomes a symlink to it again if [ ! -x "$STOCK_CORE" ]; then log "warning: no package core at $STOCK_CORE, keeping $cur" return 1 fi - rm -f /usr/bin/mihomo /usr/bin/mihomo.new /usr/bin/mihomo.bak - ln -s "$STOCK_CORE" /usr/bin/mihomo - log "core: the package's $("$STOCK_CORE" -v 2>/dev/null | awk '{print $3; exit}')" + CORE_PENDING=stock return 0 fi case "$(uname -m)" in @@ -688,8 +689,23 @@ core_update() { "free space in ${bin%/*}: $(df -h "${bin%/*}" 2>/dev/null | awk 'NR==2 {print $4}')), keeping ${cur:-the current one}" return 1 fi - mv "$new" "$bin" - log "core: $(mihomo -v | head -n 1)" + CORE_PENDING=new +} + +core_install() { + case "${CORE_PENDING:-}" in + stock) + # back to the package's core: /usr/bin/mihomo becomes a symlink to it again + rm -f /usr/bin/mihomo /usr/bin/mihomo.new /usr/bin/mihomo.bak + ln -s "$STOCK_CORE" /usr/bin/mihomo + log "core: the package's $("$STOCK_CORE" -v 2>/dev/null | awk '{print $3; exit}')" + ;; + new) + mv "$bin.new" "$bin" + log "core: $(mihomo -v | head -n 1)" + ;; + esac + CORE_PENDING="" } # Tailscale control over port 80 gets broken by the ISP: force 443. @@ -849,8 +865,9 @@ cmd_install() { } cmd_fix() { + core_fetch || true /etc/init.d/nikki stop 2>/dev/null - core_update || true + core_install patch_init fix_reserved wan_metrics