OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.13
authorTianling Shen <cnsztl@immortalwrt.org>
Wed, 6 Jul 2022 04:25:33 +0000 (12:25 +0800)
committerTianling Shen <cnsztl@immortalwrt.org>
Wed, 6 Jul 2022 04:25:33 +0000 (12:25 +0800)
Signed-off-by: Tianling Shen <cnsztl@immortalwrt.org>
applications/luci-app-unblockneteasemusic/Makefile
applications/luci-app-unblockneteasemusic/luasrc/controller/unblockneteasemusic.lua
applications/luci-app-unblockneteasemusic/luasrc/model/cbi/unblockneteasemusic/unblockneteasemusic.lua
applications/luci-app-unblockneteasemusic/root/etc/init.d/unblockneteasemusic
applications/luci-app-unblockneteasemusic/root/usr/bin/unm-debug
applications/luci-app-unblockneteasemusic/root/usr/share/unblockneteasemusic/debugging.sh
applications/luci-app-unblockneteasemusic/root/usr/share/unblockneteasemusic/log_check.sh
applications/luci-app-unblockneteasemusic/root/usr/share/unblockneteasemusic/update.sh

index 35d98c0..b7596cf 100644 (file)
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-3.0-only
 #
-# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 include $(TOPDIR)/rules.mk
 
@@ -10,8 +10,8 @@ LUCI_DEPENDS:=+busybox +dnsmasq-full +ipset +jsonfilter +node +uclient-fetch +uc
 LUCI_PKGARCH:=all
 
 PKG_NAME:=luci-app-unblockneteasemusic
-PKG_VERSION:=2.12
-PKG_RELEASE:=3
+PKG_VERSION:=2.13
+PKG_RELEASE:=1
 
 PKG_MAINTAINER:=Tianling Shen <cnsztl@immortalwrt.org>
 
index 62c06af..a4e2646 100644 (file)
@@ -1,5 +1,5 @@
 -- SPDX-License-Identifer: GPL-3.0-only
--- Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+-- Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 module("luci.controller.unblockneteasemusic", package.seeall)
 
@@ -23,8 +23,10 @@ function index()
 end
 
 function act_status()
-       local e = {}
-       e.running = luci.sys.call("ps -w |grep unblockneteasemusic |grep app.js |grep -v grep >/dev/null") == 0
+       local stat = luci.util.ubus("service", "list", { name = "unblockneteasemusic" })
+       local running = next(stat) and stat.unblockneteasemusic.instances.unblockneteasemusic.running or false
+
+       local e = { running = running }
        luci.http.prepare_content("application/json")
        luci.http.write_json(e)
 end
index 2847a7c..3a17798 100644 (file)
@@ -95,13 +95,13 @@ o.write = function()
 end
 
 function act_download_cert()
-       local t,e
-       t=nixio.open("/usr/share/unblockneteasemusic/core/ca.crt","r")
-       luci.http.header('Content-Disposition','attachment; filename="ca.crt"')
+       local t, e
+       t = nixio.open("/usr/share/unblockneteasemusic/core/ca.crt","r")
+       luci.http.header('Content-Disposition', 'attachment; filename="ca.crt"')
        luci.http.prepare_content("application/octet-stream")
        while true do
-               e=t:read(nixio.const.buffersize)
-               if(not e)or(#e==0)then
+               e = t:read(nixio.const.buffersize)
+               if (not e) or (#e == 0) then
                        break
                else
                        luci.http.write(e)
index edc4112..31fa891 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh /etc/rc.common
 # SPDX-License-Identifier: GPL-3.0-only
 #
-# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 USE_PROCD=1
 
@@ -40,14 +40,29 @@ append_param_env() {
        [ -n "$value" ] && procd_append_param env "$3"="$value"
 }
 
-uci_get_by_name() {
-       local ret
-       ret="$(uci -q get "$NAME".@"$1"["${4:-0}"]."$2")"
-       echo "${ret:-$3}"
+append_filter_client() {
+       local cfg="$1"
+
+       local ip_addr filter_mode
+       config_get ip_addr "$cfg" "ip_addr"
+       config_get filter_mode "$cfg" "filter_mode"
+       [ -n "$ip_addr" -a -n "$filter_mode" ] || return 1
+
+       case "${filter_mode}" in
+       "disable_http")
+               acl_http_addr="${acl_http_addr:+$acl_http_addr\n}${ip_addr}"
+               ;;
+       "disable_https")
+               acl_https_addr="${acl_https_addr:+$acl_https_addr\n}${ip_addr}"
+               ;;
+       "disable_all")
+               acl_http_addr="${acl_http_addr:+$acl_http_addr\n}${ip_addr}"
+               acl_https_addr="${acl_https_addr:+$acl_https_addr\n}${ip_addr}"
+               ;;
+       esac
 }
 
-start_service()
-{
+start_service() {
        config_load "$NAME"
        is_enabled "config" "enable" || exit 1
 
@@ -93,6 +108,7 @@ start_service()
        is_enabled "config" "strict_mode" && append_param "-s"
 
        procd_set_param env LOG_FILE="/tmp/$NAME.log"
+       procd_append_param env JSON_LOG="true"
 
        append_param_env "config" "joox_cookie" "JOOX_COOKIE"
        append_param_env "config" "qq_cookie" "QQ_COOKIE"
@@ -108,7 +124,6 @@ start_service()
                "lower_than_999kbps") procd_append_param env MIN_BR="600000" ;;
                "replace_all") procd_append_param env MIN_BR="9999999" ;;
        esac
-       procd_append_param env JSON_LOG="true"
 
        procd_set_param stdout 1
        procd_set_param stderr 1
@@ -117,54 +132,35 @@ start_service()
        local lan_addr="$(uci -q get network.lan.ipaddr)"
        local tmp="/tmp/$NAME"
        if [ "${hijack_ways}" = "use_ipset" ]; then
-               local ipset="ipset"
-               local nftflag=""
-               [ -n "$(dnsmasq --version | grep -m1 'Compile time options:' | cut -d: -f2 | grep ' nftset')" ] && {
-                       ipset="nftset"
+               local settype nftflag
+               if dnsmasq --version | grep -q 'Compile time options:.* nftset'; then
+                       settype="nftset"
                        nftflag="inet#fw4#"
-               }
+               else
+                       settype="ipset"
+               fi
                mkdir -p "/tmp/dnsmasq.d"
                rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
                cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
                        dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
-                       ${ipset}=/.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/interface.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/interface3.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/apm.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/apm3.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/clientlog.music.163.com/${nftflag}neteasemusic
-                       ${ipset}=/clientlog3.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/interface.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/interface3.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/apm.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/apm3.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/clientlog.music.163.com/${nftflag}neteasemusic
+                       ${settype}=/clientlog3.music.163.com/${nftflag}neteasemusic
                EOF
                /etc/init.d/dnsmasq reload
 
-               local ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
-               let ip_addr_num="ip_addr_num-1"
-               local acl_http_addr acl_https_addr
-               [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
-               do
-                       ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
-                       filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
-
-                       case "${filter_mode}" in
-                       "disable_http")
-                               acl_http_addr="${acl_http_addr}${ip_addr}\n"
-                               ;;
-                       "disable_https")
-                               acl_https_addr="${acl_https_addr}${ip_addr}\n"
-                               ;;
-                       "disable_all")
-                               acl_http_addr="${acl_http_addr}${ip_addr}\n"
-                               acl_https_addr="${acl_https_addr}${ip_addr}\n"
-                               ;;
-                       esac
-               done
+               config_foreach append_filter_client "acl_rule"
 
                local netease_music_ips="$(uclient-fetch -qO- "http://httpdns.n.netease.com/httpdns/v2/d?domain=music.163.com,interface.music.163.com,interface3.music.163.com,apm.music.163.com,apm3.music.163.com,clientlog.music.163.com,clientlog3.music.163.com" |jsonfilter -e '@.data.*.ip.*')"
                local netease_music_ips2="$(uclient-fetch -qO- "https://music.httpdns.c.163.com/d" --post-data="music.163.com,interface.music.163.com,interface3.music.163.com,apm.music.163.com,apm3.music.163.com,clientlog.music.163.com,clientlog3.music.163.com" |jsonfilter -e '@.dns.*["ips"].*')"
-               local neteasemusic_addr="$(echo -e "${netease_music_ips}\n${netease_music_ips2}" |sort -u |awk '{print $1}')"
+               local neteasemusic_addr="$(echo -e "${netease_music_ips}\n${netease_music_ips2}" | sort -u | awk '{print $1}')"
 
-               json_add_string o_acl_http_addr "$(echo -e "${acl_http_addr}" |sort -u |awk '{print $1}')"
-               json_add_string o_acl_https_addr "$(echo -e "${acl_https_addr}" |sort -u |awk '{print $1}')"
+               json_add_string o_acl_http_addr "$(echo -e "${acl_http_addr}" | sort -u | awk '{print $1}')"
+               json_add_string o_acl_https_addr "$(echo -e "${acl_https_addr}" | sort -u | awk '{print $1}')"
                json_add_string o_neteasemusic_addr "$neteasemusic_addr"
        elif [ "${hijack_ways}" = "use_hosts" ]; then
                mkdir -p "/tmp/dnsmasq.d"
@@ -186,22 +182,16 @@ start_service()
        fi
 
        json_dump -i >"$tmp.json"
-       if utpl -F "$tmp.json" -S "$RULES_UC" >"$tmp.nft" \
-               && ! cmp -s "$tmp.nft" "$RULES_NFT"; then
-               echo "table inet chk {include \"$tmp.nft\";}" >"$tmp.nft.chk"
-               if nft -f "$tmp.nft.chk" -c; then
-                       mv -f "$tmp.nft" "$RULES_NFT"
-                       fw4 reload
-               fi
-               rm -f "$tmp.nft.chk"
+       if utpl -F "$tmp.json" -S "$RULES_UC" > "$tmp.nft" && ! cmp -s "$tmp.nft" "$RULES_NFT"; then
+               echo "table inet chk {include \"$tmp.nft\";}" > "$tmp.nft.chk"
+               ! nft -f "$tmp.nft.chk" -c || { mv -f "$tmp.nft" "$RULES_NFT"; fw4 reload; }
        fi
-       rm -f "$tmp.json" "$tmp.nft"
+       rm -f "$tmp.json" "$tmp.nft" "$tmp.nft.chk"
 
        procd_close_instance
 } >"/dev/null" 2>&1
 
-stop_service()
-{
+stop_service() {
        config_load "$NAME"
 
        sed -i "/$NAME/d" "/etc/crontabs/root"
@@ -221,25 +211,20 @@ stop_service()
                echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
        }
 
-       [ ! -e "$RULES_NFT" ] || {
-               rm -f "$RULES_NFT"
-               for chain in "netease_cloud_music_redir" "netease_cloud_music"; do
-                       nft --check list chain inet fw4 $chain > /dev/null 2>&1
-                       if [ $? -eq "0" ]; then
-                               nft flush chain inet fw4 $chain
-                       fi
-               done
-               for set in "acl_neteasemusic_http" "acl_neteasemusic_https" "local_addr" "neteasemusic"; do
-                       nft --check list set inet fw4 $set > /dev/null 2>&1
-                       if [ $? -eq "0" ]; then
-                               nft flush set inet fw4 $set
-                       fi
-               done
-               fw4 reload
-       }
+       local chain settable
+       for chain in "netease_cloud_music_redir" "netease_cloud_music"; do
+               nft flush chain inet fw4 "$chain"
+       done
+       for settable in "acl_neteasemusic_http" "acl_neteasemusic_https" "local_addr" "neteasemusic"; do
+               nft flush set inet fw4 "$set"
+       done
+
+       rm -f "$RULES_NFT"
+       fw4 reload
 
        rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
        /etc/init.d/dnsmasq reload
+
        ip route del "223.252.199.10"
 
        rm -f "/tmp/$NAME.log"
index 11cb264..b70bfd1 100755 (executable)
@@ -1,5 +1,7 @@
 #!/bin/sh
-# Copyright (C) 2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2021-2022 Tianling Shen <cnsztl@immortalwrt.org>
+
+command -v "curl" >"/dev/null" || { echo -e "curl is not found."; exit 1; }
 
 mkdir -p "/tmp"
 /usr/share/unblockneteasemusic/debugging.sh 2>&1 | tee "/tmp/unm-debugging-output.txt"
index e30d0a8..19457df 100755 (executable)
@@ -1,15 +1,17 @@
 #!/bin/sh
-# Copyright (C) 2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2021-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 . /lib/functions.sh
 
+NAME="unblockneteasemusic"
+
 command -v "curl" >"/dev/null" || { echo -e "curl is not found."; exit 1; }
 
 echo -e "Launching luci-app-unblockneteasmusic Debugging Tool..."
 echo -e "\n"
 
 echo -e "OpenWrt info:"
-cat "/etc/openwrt_release"
+ubus call system board || cat "/etc/openwrt_release"
 echo -e "\n"
 
 echo -e "uclient-fetch info:"
@@ -26,23 +28,23 @@ echo -e "\n"
 
 echo -e "luci-app-unblockneteasmusic info:"
 opkg info "luci-app-unblockneteasemusic"
-ls -lh "/etc/config/unblockneteasemusic" "/etc/init.d/unblockneteasemusic" "/usr/share/unblockneteasemusic"
-cat "/etc/config/unblockneteasemusic" | sed -e "s,joox_cookie .*,joox_cookie 'set',g" \
+ls -lh "/etc/config/$NAME" "/etc/init.d/$NAME" "/usr/share/$NAME"
+cat "/etc/config/$NAME" | sed -e "s,joox_cookie .*,joox_cookie 'set',g" \
        -e "s,qq_cookie .*,qq_cookie 'set',g" \
        -e "s,youtube_key .*,youtube_key 'set',g" \
        -e "s,proxy_server_ip .*,proxy_server_ip 'set',g"
 echo -e "\n"
 
 echo -e "UnblockNeteaseMusic Node.js info:"
-echo -e "Git HEAD version: $(cat "/usr/share/unblockneteasemusic/core_local_ver" 2>"/dev/null" || echo "Not Found")"
-echo -e "Core version: $(node "/usr/share/unblockneteasemusic/core/app.js" -v 2>"/dev/null" || echo "Not Found")"
-ls -lh "/usr/share/unblockneteasemusic/core" 2>"/dev/null"
+echo -e "Git HEAD version: $(cat "/usr/share/$NAME/core_local_ver" 2>"/dev/null" || echo "Not Found")"
+echo -e "Core version: $(node "/usr/share/$NAME/core/app.js" -v 2>"/dev/null" || echo "Not Found")"
+ls -lh "/usr/share/$NAME/core" 2>"/dev/null"
 echo -e "\n"
 
 echo -e "Netease networking info:"
 curl -fsv "http://music.163.com/song/media/outer/url?id=641644.mp3" 2>&1 | grep "Location" || echo -e "Cannot connect to NeteaseMusic."
 curl -sSL "http://httpdns.n.netease.com/httpdns/v2/d?domain=music.163.com" || echo -e "Cannot connect to Netease HTTPDNS."
-config_load "unblockneteasemusic"
+config_load "$NAME"
 config_get custom_proxy "config" "proxy_server_ip"
 [ -n "$custom_proxy" ] && { curl -sL -x "$custom_proxy" "http://music.163.com/song/media/outer/url?id=641644.mp3" 2>&1 | grep "Location" || echo -e "Cannot connect to NeteaseMusic via proxy."; }
 echo -e "\n"
@@ -50,20 +52,22 @@ echo -e "\n"
 echo -e "Port status:"
 config_get unm_port "config" "http_port" "5200"
 config_get unm_ports "config" "https_port" "5201"
-[ "x$(config_get "config" "hijack_ways")" = "xuse_hosts" ] && { unm_port="80"; unm_ports="443"; }
+[ "$(config_get "config" "hijack_ways")" = "use_hosts" ] && { unm_port="80"; unm_ports="443"; }
 netstat -tlpen | grep "$unm_port" || echo -e "No instance found on port $unm_port."
 netstat -tlpen | grep "$unm_ports" || echo -e "No instance found on port $unm_ports."
 echo -e "\n"
 
-echo -e "Running info:"
-procd_running_status="$(/etc/init.d/unblockneteasemusic status)"
-echo -e "PROCD running status: $procd_running_status"
-[ "$procd_running_status" = "running" ] && { ps -w | grep "unblockneteasemusic" | grep "app\.js" || echo -e "Thread is not found."; }
+echo -e "PROCD running info:"
+running_stat="$(ubus call service list '{"name": "unblockneteasemusic", "verbose": true}' | sed -e 's,"YOUTUBE_KEY".*","YOUTUBE_KEY": "set",g' -e 's,"QQ_COOKIE".*","QQ_COOKIE": "set",g')"
+[ "$(echo -e "$running_stat" | jsonfilter -e "@.$NAME.instances.$NAME.running")" == "true" ] || is_stopped=1
+echo -e "$running_stat"
+
 echo -e "\n"
 
-[ "$procd_running_status" != "running" ] || {
+[ -n "$is_stopped" ] || {
        echo -e "Firewall info:"
-       [ -e "/etc/nftables.d/90-unblockneteasemusic-rules.nft" ] || echo -e 'netease_cloud_music nft rule file not found.'
+
+       [ -e "/etc/nftables.d/90-$NAME-rules.nft" ] || echo -e 'netease_cloud_music nft rule file not found.'
        echo -e ""
        nft list set inet fw4 "acl_neteasemusic_http" 2>&1
        echo -e ""
@@ -81,7 +85,7 @@ echo -e "\n"
        echo -e ""
        nft list chain inet fw4 "netease_cloud_music_redir" 2>&1
        echo -e ""
-       cat "/tmp/dnsmasq.d/dnsmasq-unblockneteasemusic.conf"
+       cat "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
        echo -e "\n"
 
        echo -e "Testing source replacing..."
@@ -92,5 +96,6 @@ echo -e "\n"
        echo -e ""
        curl -ksSL -X "POST" -x "http://$lan_ip:$unm_port" "https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=" --data "params=bf3kf%2BOyalbxNS%2FeHAXquH8D2nt2YrhBzww4zy5rj2H%2BeAhdOIaGh4HHHzcoREFcu9Ve35LUgc%2BGE1YJD1HxrJ87ucm5zK%2FFn1lLvHFv1A8ZAuyU1afjG28s2Xja6zpfg00T0EcCeqkK61OpTfAaqw%3D%3D&encSecKey=6bab0dfa7ee3b292f9263a7af466636731cdbbd1d8747c9178c17477e70be899b7788c4a4e315c9fdb8c6e787603db6f9dff62c356f164d35b16b7f2d9ad5ede3cc7336130605521a8f916d308ce86b15c32b81c883ae2ba9c244444d91e1683be93fa0ea3e2a85207c9d693b86b5bb31adb002dd56c0bbcce9c73ec3bf5c105"
        echo -e ""
-       cat "/tmp/unblockneteasemusic.log"
 }
+
+cat "/tmp/$NAME.log" 2>"/dev/null" || echo -e "Log is not avaiable."
index 40e921c..b236138 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-3.0-only
-# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 NAME="unblockneteasemusic"
 
index e7f1193..806f9ba 100755 (executable)
@@ -1,21 +1,26 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-3.0-only
-# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 NAME="unblockneteasemusic"
+LOCK="/tmp/$NAME.update_core.lock"
 
-check_core_if_already_running(){
-       running_tasks="$(ps -w |grep "$NAME" |grep "update.sh" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
-       [ "${running_tasks}" -gt "2" ] && { echo -e "\nA task is already running." >> "/tmp/$NAME.log"; exit 2; }
+check_core_if_already_running() {
+       if [ -e "$LOCK" ]; then
+               echo -e "\nA task is already running." >> "/tmp/$NAME.log"
+               exit 2
+       else
+               touch "$LOCK"
+       fi
 }
 
 clean_log(){
        echo "" > "/tmp/$NAME.log"
 }
 
-check_core_latest_version(){
+check_core_latest_version() {
        core_latest_ver="$(uclient-fetch -qO- 'https://api.github.com/repos/UnblockNeteaseMusic/server/commits?sha=enhanced&path=precompiled' | jsonfilter -e '@[0].sha')"
-       [ -z "${core_latest_ver}" ] && { echo -e "\nFailed to check latest core version, please try again later." >> "/tmp/$NAME.log"; exit 1; }
+       [ -n "${core_latest_ver}" ] || { echo -e "\nFailed to check latest core version, please try again later." >> "/tmp/$NAME.log"; rm -f "$LOCK"; exit 1; }
        if [ ! -e "/usr/share/$NAME/core_local_ver" ]; then
                clean_log
                echo -e "Local version: NOT FOUND, latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
@@ -28,22 +33,24 @@ check_core_latest_version(){
                else
                        echo -e "\nLocal version: $(cat /usr/share/$NAME/core_local_ver 2>"/dev/null"), latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
                        echo -e "You're already using the latest version." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 3
                fi
        fi
 }
 
-update_core(){
+update_core() {
        echo -e "Updating core..." >> "/tmp/$NAME.log"
 
-       mkdir -p "/usr/share/$NAME/core" > "/dev/null" 2>&1
-       rm -rf /usr/share/$NAME/core/* > "/dev/null" 2>&1
+       mkdir -p "/usr/share/$NAME/core"
+       rm -rf "/usr/share/$NAME/core"/*
 
        for url in $(uclient-fetch -qO- "https://api.github.com/repos/UnblockNeteaseMusic/server/contents/precompiled" |jsonfilter -e '@[*].download_url')
        do
                uclient-fetch "${url}" -qO "/usr/share/$NAME/core/${url##*/}"
                [ -s "/usr/share/$NAME/core/${url##*/}" ] || {
                        echo -e "Failed to download ${url##*/}." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 1
                }
        done
@@ -53,16 +60,18 @@ update_core(){
                uclient-fetch "https://raw.githubusercontent.com/UnblockNeteaseMusic/server/enhanced/${cert}" -qO "/usr/share/$NAME/core/${cert}"
                [ -s "/usr/share/$NAME/core/${cert}" ] || {
                        echo -e "Failed to download ${cert}." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 1
                }
        done
 
-       [ -n "${update_core_from_luci}" ] && touch "/usr/share/$NAME/update_core_successfully"
+       [ -z "${update_core_from_luci}" ] || touch "/usr/share/$NAME/update_core_successfully"
        echo -e "${core_latest_ver}" > "/usr/share/$NAME/core_local_ver"
-       [ -z "${non_restart}" ] && /etc/init.d/$NAME restart
+       [ -n "${non_restart}" ] || /etc/init.d/"$NAME" restart
 
        echo -e "Succeeded in updating core." > "/tmp/$NAME.log"
        echo -e "Current core version: ${core_latest_ver}.\n" >> "/tmp/$NAME.log"
+       rm -f "$LOCK"
 }
 
 case "$1" in
@@ -81,6 +90,6 @@ case "$1" in
                check_core_latest_version
                ;;
        *)
-               echo -e "Usage: ./update.sh update_core"
+               echo -e "Usage: $0/update.sh update_core"
                ;;
 esac