OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.10-3
[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 # Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
4
5 START=99
6 STOP=10
7
8 NAME="unblockneteasemusic"
9 UPGRADE_CONF="/lib/upgrade/keep.d/$NAME"
10
11 IPT_N="iptables -t nat"
12
13 is_enabled() {
14         local enabled
15         config_get enabled "$1" "$2" "0"
16         if [ "$enabled" -eq "1" ]; then
17                 return 0
18         else
19                 return 1
20         fi
21 }
22
23 config_padding_arg() {
24         local value
25         config_get value "$1" "$2" $4
26         [ -n "$value" ] && export "$2"="$3 $value"
27 }
28
29 append_param_env() {
30         local value
31         config_get value "$1" "$2"
32         [ -n "$value" ] && export "$3"="$value"
33 }
34
35 uci_get_by_name() {
36         local "index"
37         index=0
38         if [ -n "$4" ]; then
39                 index="$4"
40         fi
41         ret="$(uci get "$NAME".@"$1"["${index}"]."$2" 2>/dev/null)"
42         echo "${ret:=$3}"
43 }
44
45 start()
46 {
47         stop
48
49         config_load "$NAME"
50         is_enabled "config" "enable" || exit 1
51
52         local update_time
53         config_get update_time "config" "update_time" "3"
54         sed -i "/$NAME/d" /etc/crontabs/root
55         is_enabled "config" "auto_update" && echo "0 ${update_time} * * * /usr/share/$NAME/update.sh update_core" >> "/etc/crontabs/root"
56         echo "*/5 * * * * /usr/share/$NAME/log_check.sh" >> "/etc/crontabs/root"
57         /etc/init.d/cron restart > "/dev/null" 2>&1
58
59         [ ! -e "/usr/share/$NAME/core/app.js" ] && { rm -f "/usr/share/$NAME/local_ver"; sh "/usr/share/$NAME/update.sh" "update_core_non_restart"; }
60         [ ! -e "/usr/share/$NAME/core/app.js" ] && { echo "Core Not Found, please download it before starting." >> "/tmp/$NAME.log"; exit 1; }
61
62         local http_port https_port hijack_ways
63         config_get http_port "config" "http_port" "5200"
64         config_get https_port "config" "https_port" "5201"
65         config_get hijack_ways "config" "hijack_ways" "use_ipset"
66         [ "${hijack_ways}" = "use_hosts" ] && { http_port="80"; https_port="443"; }
67
68         local lan_addr
69         lan_addr="$(uci get network.lan.ipaddr)"
70         if is_enabled "config" "pub_access"; then
71                 listen_addr="0.0.0.0"
72
73                 iptables -I "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
74                 iptables -I "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
75                 echo "${http_port}:${https_port}" > "/tmp/$NAME.ports"
76
77                 mkdir -p "/var/etc/"
78                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
79         else
80                 listen_addr="${lan_addr}"
81         fi
82
83         local music_source
84         config_get music_source "config" "music_source" "default"
85         if [ "${music_source}" = "default" ]; then
86                unset music_source
87         else
88                music_source="-o ${music_source}"
89         fi
90
91         is_enabled "config" "strict_mode" && strict_mode="-s"
92         config_padding_arg "config" "endpoint_url" "-e" "https://music.163.com"
93         config_padding_arg "config" "netease_server_ip" "-f"
94         config_padding_arg "config" "proxy_server_ip" "-u"
95
96         append_param_env "config" "migu_cookie" "MIGU_COOKIE"
97         append_param_env "config" "qq_cookie" "QQ_COOKIE"
98         append_param_env "config" "youtube_key" "YOUTUBE_KEY"
99         append_param_env "config" "self_issue_cert_crt" "SIGN_CERT"
100         append_param_env "config" "self_issue_cert_key" "SIGN_KEY"
101
102         is_enabled "config" "enable_flac" && export ENABLE_FLAC="true"
103         case "$(config_get "config" "replace_music_source")" in
104                 "lower_than_192kbps") export MIN_BR="192000" ;;
105                 "lower_than_320kbps") export MIN_BR="320000" ;;
106                 "lower_than_999kbps") export MIN_BR="999000" ;;
107                 "replace_all") export MIN_BR="999999" ;;
108         esac
109
110         node "/usr/share/$NAME/core/app.js" -a "${listen_addr}" -p "${http_port}":"${https_port}" ${music_source} ${endpoint_url} ${netease_server_ip} ${proxy_server_ip} ${strict_mode} >"/tmp/$NAME.log" 2>&1 &
111
112         if [ "${hijack_ways}" = "use_ipset" ]; then
113                 mkdir -p "/tmp/dnsmasq.d"
114                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
115                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
116                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
117                         ipset=/.music.163.com/neteasemusic
118                         ipset=/interface.music.163.com/neteasemusic
119                         ipset=/interface3.music.163.com/neteasemusic
120                         ipset=/apm.music.163.com/neteasemusic
121                         ipset=/apm3.music.163.com/neteasemusic
122                         ipset=/clientlog.music.163.com/neteasemusic
123                         ipset=/clientlog3.music.163.com/neteasemusic
124                 EOF
125                 /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
126
127                 if ! ipset list "acl_neteasemusic_http" > "/dev/null"; then ipset create "acl_neteasemusic_http" hash:ip; fi
128                 if ! ipset list "acl_neteasemusic_https" > "/dev/null"; then ipset create "acl_neteasemusic_https" hash:ip; fi
129                 ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
130                 let ip_addr_num="ip_addr_num-1"
131                 [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
132                 do
133                         ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
134                         filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
135                         case "${filter_mode}" in
136                         "disable_http")
137                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
138                                 ;;
139                         "disable_https")
140                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
141                                 ;;
142                         "disable_all")
143                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
144                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
145                                 ;;
146                         esac
147                 done
148
149                 ipset list "neteasemusic" > "/dev/null" || ipset create "neteasemusic" hash:ip
150                 uclient-fetch -q -O- "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.*' |sort -u |awk '{print "ipset add neteasemusic "$1}' |sh > "/dev/null" 2>&1
151                 $IPT_N -N "netease_cloud_music"
152                 for local_addr in "0.0.0.0/8" "10.0.0.0/8" "127.0.0.0/8" "169.254.0.0/16" "172.16.0.0/12" "192.168.0.0/16" "224.0.0.0/4" "240.0.0.0/4"; do
153                         $IPT_N -A "netease_cloud_music" -d "${local_addr}" -j "RETURN"
154                 done
155                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_http" "src" --dport "80" -j "REDIRECT" --to-ports "${http_port}"
156                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_https" "src" --dport "443" -j "REDIRECT" --to-ports "${https_port}"
157                 $IPT_N -I "PREROUTING" -p "tcp" -m "set" --match-set "neteasemusic" "dst" -j "netease_cloud_music"
158                 $IPT_N -I "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
159
160                 mkdir -p "/var/etc/"
161                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
162         elif [ "${hijack_ways}" = "use_hosts" ]; then
163                 mkdir -p "/tmp/dnsmasq.d"
164                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
165                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
166                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
167                         address=/music.163.com/${lan_addr}
168                         address=/interface.music.163.com/${lan_addr}
169                         address=/interface3.music.163.com/${lan_addr}
170                         address=/apm.music.163.com/${lan_addr}
171                         address=/apm3.music.163.com/${lan_addr}
172                         address=/clientlog.music.163.com/${lan_addr}
173                         address=/clientlog3.music.163.com/${lan_addr}
174                         address=/music.httpdns.c.163.com/0.0.0.0
175                 EOF
176                 /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
177
178                 ip route add "223.252.199.10" dev lo
179         fi
180 } >"/dev/null" 2>&1
181
182 stop()
183 {
184         { ps |grep "$NAME" |grep "app.js" |grep -v "grep" |awk '{print $1}' |xargs kill -9; } > "/dev/null" 2>&1
185
186         config_load "$NAME"
187
188         sed -i "/$NAME/d" "/etc/crontabs/root"
189         /etc/init.d/cron restart > "/dev/null" 2>&1
190
191         [ -f "${UPGRADE_CONF}" ] && sed -i "/$NAME\/core/d;/$NAME\/local_ver/d" "${UPGRADE_CONF}"
192         is_enabled "config" "keep_core_when_upgrade" && { echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"; echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"; }
193
194         local self_issue_cert_crt self_issue_cert_key
195         config_get "self_issue_cert_crt" "config" "self_issue_cert_crt"
196         config_get "self_issue_cert_key" "config" "self_issue_cert_key"
197         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
198                 sed -i "/${self_issue_cert_crt//\//\\/}/d" "${UPGRADE_CONF}"
199                 sed -i "/${self_issue_cert_key//\//\\/}/d" "${UPGRADE_CONF}"
200
201                 echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
202                 echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
203         }
204
205         http_port="$(cat "/tmp/$NAME.ports" | awk -F ':' 'print $1')"
206         https_port="$(cat "/tmp/$NAME.ports" | awk -F ':' 'print $2')"
207         iptables -D "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
208         iptables -D "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
209
210         $IPT_N -D "PREROUTING" -p "tcp" -m set --match-set "neteasemusic" "dst" -j "netease_cloud_music"
211         $IPT_N -D "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
212         $IPT_N -F "netease_cloud_music"
213         $IPT_N -X "netease_cloud_music"
214         ipset destroy "neteasemusic"
215         ipset destroy "acl_neteasemusic_http"
216         ipset destroy "acl_neteasemusic_https"
217         echo "" > "/var/etc/$NAME.include"
218
219         rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
220         /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
221         ip route del "223.252.199.10"
222
223         rm -f "/tmp/$NAME.log"
224 } >"/dev/null" 2>&1