OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.13
[immortalwrt/luci.git] / applications / luci-app-unblockneteasemusic / root / etc / init.d / unblockneteasemusic
1 #!/bin/sh /etc/rc.common
2 # SPDX-License-Identifier: GPL-3.0-only
3 #
4 # Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
5
6 USE_PROCD=1
7
8 START=99
9 STOP=10
10
11 NAME="unblockneteasemusic"
12 UPGRADE_CONF="/lib/upgrade/keep.d/$NAME"
13
14 RULES_UC="/usr/share/$NAME/rules/default.uc"
15 RULES_NFT="/etc/nftables.d/90-$NAME-rules.nft"
16
17 is_enabled() {
18         local enabled
19         config_get_bool enabled "$1" "$2" "${3:-0}"
20         if [ "$enabled" -eq "1" ]; then
21                 return 0
22         else
23                 return 1
24         fi
25 }
26
27 append_param() {
28         procd_append_param command "$1" $2
29 }
30
31 append_param_arg() {
32         local value
33         config_get value "$1" "$2" $4
34         [ -n "$value" ] && append_param "$3" "$value"
35 }
36
37 append_param_env() {
38         local value
39         config_get value "$1" "$2" $4
40         [ -n "$value" ] && procd_append_param env "$3"="$value"
41 }
42
43 append_filter_client() {
44         local cfg="$1"
45
46         local ip_addr filter_mode
47         config_get ip_addr "$cfg" "ip_addr"
48         config_get filter_mode "$cfg" "filter_mode"
49         [ -n "$ip_addr" -a -n "$filter_mode" ] || return 1
50
51         case "${filter_mode}" in
52         "disable_http")
53                 acl_http_addr="${acl_http_addr:+$acl_http_addr\n}${ip_addr}"
54                 ;;
55         "disable_https")
56                 acl_https_addr="${acl_https_addr:+$acl_https_addr\n}${ip_addr}"
57                 ;;
58         "disable_all")
59                 acl_http_addr="${acl_http_addr:+$acl_http_addr\n}${ip_addr}"
60                 acl_https_addr="${acl_https_addr:+$acl_https_addr\n}${ip_addr}"
61                 ;;
62         esac
63 }
64
65 start_service() {
66         config_load "$NAME"
67         is_enabled "config" "enable" || exit 1
68
69         local update_time
70         config_get update_time "config" "update_time" "3"
71         sed -i "/$NAME/d" /etc/crontabs/root
72         is_enabled "config" "auto_update" && echo "0 ${update_time} * * * /usr/share/$NAME/update.sh update_core" >> "/etc/crontabs/root"
73         echo "*/5 * * * * /usr/share/$NAME/log_check.sh" >> "/etc/crontabs/root"
74         /etc/init.d/cron restart
75
76         [ ! -s "/usr/share/$NAME/core/app.js" ] && { rm -f "/usr/share/$NAME/local_ver"; sh "/usr/share/$NAME/update.sh" "update_core_non_restart"; }
77         [ ! -s "/usr/share/$NAME/core/app.js" ] && { echo "Core Not Found, please download it before starting." >> "/tmp/$NAME.log"; exit 1; }
78
79         procd_open_instance "$NAME"
80         procd_set_param command node "/usr/share/$NAME/core/app.js"
81         append_param "-a" "0.0.0.0"
82
83         local http_port https_port hijack_ways
84         config_get http_port "config" "http_port" "5200"
85         config_get https_port "config" "https_port" "5201"
86         config_get hijack_ways "config" "hijack_ways" "use_ipset"
87         [ "${hijack_ways}" = "use_hosts" ] && { http_port="80"; https_port="443"; }
88         append_param "-p" "${http_port}":"${https_port}"
89
90         json_init
91         if is_enabled "config" "pub_access"; then
92                 json_add_int o_pub_access "1"
93         else
94                 json_add_int o_pub_access "0"
95         fi
96         json_add_int o_http_port "${http_port}"
97         json_add_int o_https_port "${https_port}"
98         json_add_string o_hijack_ways "${hijack_ways}"
99
100         local music_source
101         config_get music_source "config" "music_source" "default"
102         [ "${music_source}" != "default" ] && append_param -o "${music_source}"
103
104         append_param_arg "config" "cnrelay" "-c"
105         append_param_arg "config" "endpoint_url" "-e" "https://music.163.com"
106         append_param_arg "config" "netease_server_ip" "-f"
107         append_param_arg "config" "proxy_server_ip" "-u"
108         is_enabled "config" "strict_mode" && append_param "-s"
109
110         procd_set_param env LOG_FILE="/tmp/$NAME.log"
111         procd_append_param env JSON_LOG="true"
112
113         append_param_env "config" "joox_cookie" "JOOX_COOKIE"
114         append_param_env "config" "qq_cookie" "QQ_COOKIE"
115         append_param_env "config" "youtube_key" "YOUTUBE_KEY"
116         append_param_env "config" "self_issue_cert_crt" "SIGN_CERT" "/usr/share/$NAME/core/server.crt"
117         append_param_env "config" "self_issue_cert_key" "SIGN_KEY" "/usr/share/$NAME/core/server.key"
118
119         is_enabled "config" "enable_flac" && procd_append_param env ENABLE_FLAC="true"
120         is_enabled "config" "local_vip" && procd_append_param env ENABLE_LOCAL_VIP="true"
121         case "$(config_get "config" "replace_music_source")" in
122                 "lower_than_192kbps") procd_append_param env MIN_BR="192000" ;;
123                 "lower_than_320kbps") procd_append_param env MIN_BR="320000" ;;
124                 "lower_than_999kbps") procd_append_param env MIN_BR="600000" ;;
125                 "replace_all") procd_append_param env MIN_BR="9999999" ;;
126         esac
127
128         procd_set_param stdout 1
129         procd_set_param stderr 1
130         procd_set_param respawn
131
132         local lan_addr="$(uci -q get network.lan.ipaddr)"
133         local tmp="/tmp/$NAME"
134         if [ "${hijack_ways}" = "use_ipset" ]; then
135                 local settype nftflag
136                 if dnsmasq --version | grep -q 'Compile time options:.* nftset'; then
137                         settype="nftset"
138                         nftflag="inet#fw4#"
139                 else
140                         settype="ipset"
141                 fi
142                 mkdir -p "/tmp/dnsmasq.d"
143                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
144                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
145                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
146                         ${settype}=/.music.163.com/${nftflag}neteasemusic
147                         ${settype}=/interface.music.163.com/${nftflag}neteasemusic
148                         ${settype}=/interface3.music.163.com/${nftflag}neteasemusic
149                         ${settype}=/apm.music.163.com/${nftflag}neteasemusic
150                         ${settype}=/apm3.music.163.com/${nftflag}neteasemusic
151                         ${settype}=/clientlog.music.163.com/${nftflag}neteasemusic
152                         ${settype}=/clientlog3.music.163.com/${nftflag}neteasemusic
153                 EOF
154                 /etc/init.d/dnsmasq reload
155
156                 config_foreach append_filter_client "acl_rule"
157
158                 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.*')"
159                 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"].*')"
160                 local neteasemusic_addr="$(echo -e "${netease_music_ips}\n${netease_music_ips2}" | sort -u | awk '{print $1}')"
161
162                 json_add_string o_acl_http_addr "$(echo -e "${acl_http_addr}" | sort -u | awk '{print $1}')"
163                 json_add_string o_acl_https_addr "$(echo -e "${acl_https_addr}" | sort -u | awk '{print $1}')"
164                 json_add_string o_neteasemusic_addr "$neteasemusic_addr"
165         elif [ "${hijack_ways}" = "use_hosts" ]; then
166                 mkdir -p "/tmp/dnsmasq.d"
167                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
168                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
169                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
170                         address=/music.163.com/${lan_addr}
171                         address=/interface.music.163.com/${lan_addr}
172                         address=/interface3.music.163.com/${lan_addr}
173                         address=/apm.music.163.com/${lan_addr}
174                         address=/apm3.music.163.com/${lan_addr}
175                         address=/clientlog.music.163.com/${lan_addr}
176                         address=/clientlog3.music.163.com/${lan_addr}
177                         address=/music.httpdns.c.163.com/0.0.0.0
178                 EOF
179                 /etc/init.d/dnsmasq reload
180
181                 ip route add "223.252.199.10" dev lo
182         fi
183
184         json_dump -i >"$tmp.json"
185         if utpl -F "$tmp.json" -S "$RULES_UC" > "$tmp.nft" && ! cmp -s "$tmp.nft" "$RULES_NFT"; then
186                 echo "table inet chk {include \"$tmp.nft\";}" > "$tmp.nft.chk"
187                 ! nft -f "$tmp.nft.chk" -c || { mv -f "$tmp.nft" "$RULES_NFT"; fw4 reload; }
188         fi
189         rm -f "$tmp.json" "$tmp.nft" "$tmp.nft.chk"
190
191         procd_close_instance
192 } >"/dev/null" 2>&1
193
194 stop_service() {
195         config_load "$NAME"
196
197         sed -i "/$NAME/d" "/etc/crontabs/root"
198         /etc/init.d/cron restart
199
200         rm -f "${UPGRADE_CONF}"
201         is_enabled "config" "keep_core_when_upgrade" && {
202                 echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"
203                 echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"
204         }
205
206         local self_issue_cert_crt self_issue_cert_key
207         config_get "self_issue_cert_crt" "config" "self_issue_cert_crt"
208         config_get "self_issue_cert_key" "config" "self_issue_cert_key"
209         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
210                 echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
211                 echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
212         }
213
214         local chain settable
215         for chain in "netease_cloud_music_redir" "netease_cloud_music"; do
216                 nft flush chain inet fw4 "$chain"
217         done
218         for settable in "acl_neteasemusic_http" "acl_neteasemusic_https" "local_addr" "neteasemusic"; do
219                 nft flush set inet fw4 "$set"
220         done
221
222         rm -f "$RULES_NFT"
223         fw4 reload
224
225         rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
226         /etc/init.d/dnsmasq reload
227
228         ip route del "223.252.199.10"
229
230         rm -f "/tmp/$NAME.log"
231 } >"/dev/null" 2>&1
232
233 reload_service() {
234         stop
235         start
236 }
237
238 service_triggers() {
239         procd_add_reload_trigger "$NAME"
240 }