OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.13
[immortalwrt/luci.git] / applications / luci-app-unblockneteasemusic / root / etc / init.d / unblockneteasemusic
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"