OSDN Git Service

Merge Official Source
[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" "joox_cookie" "JOOX_COOKIE"
97         append_param_env "config" "migu_cookie" "MIGU_COOKIE"
98         append_param_env "config" "qq_cookie" "QQ_COOKIE"
99         append_param_env "config" "youtube_key" "YOUTUBE_KEY"
100         append_param_env "config" "self_issue_cert_crt" "SIGN_CERT"
101         append_param_env "config" "self_issue_cert_key" "SIGN_KEY"
102
103         is_enabled "config" "enable_flac" && export ENABLE_FLAC="true"
104         case "$(config_get "config" "replace_music_source")" in
105                 "lower_than_192kbps") export MIN_BR="192000" ;;
106                 "lower_than_320kbps") export MIN_BR="320000" ;;
107                 "lower_than_999kbps") export MIN_BR="999000" ;;
108                 "replace_all") export MIN_BR="999999" ;;
109         esac
110
111         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 &
112
113         if [ "${hijack_ways}" = "use_ipset" ]; then
114                 mkdir -p "/tmp/dnsmasq.d"
115                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
116                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
117                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
118                         ipset=/.music.163.com/neteasemusic
119                         ipset=/interface.music.163.com/neteasemusic
120                         ipset=/interface3.music.163.com/neteasemusic
121                         ipset=/apm.music.163.com/neteasemusic
122                         ipset=/apm3.music.163.com/neteasemusic
123                         ipset=/clientlog.music.163.com/neteasemusic
124                         ipset=/clientlog3.music.163.com/neteasemusic
125                 EOF
126                 /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
127
128                 if ! ipset list "acl_neteasemusic_http" > "/dev/null"; then ipset create "acl_neteasemusic_http" hash:ip; fi
129                 if ! ipset list "acl_neteasemusic_https" > "/dev/null"; then ipset create "acl_neteasemusic_https" hash:ip; fi
130                 ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
131                 let ip_addr_num="ip_addr_num-1"
132                 [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
133                 do
134                         ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
135                         filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
136                         case "${filter_mode}" in
137                         "disable_http")
138                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
139                                 ;;
140                         "disable_https")
141                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
142                                 ;;
143                         "disable_all")
144                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
145                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
146                                 ;;
147                         esac
148                 done
149
150                 ipset list "neteasemusic" > "/dev/null" || ipset create "neteasemusic" hash:ip
151                 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
152                 $IPT_N -N "netease_cloud_music"
153                 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
154                         $IPT_N -A "netease_cloud_music" -d "${local_addr}" -j "RETURN"
155                 done
156                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_http" "src" --dport "80" -j "REDIRECT" --to-ports "${http_port}"
157                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_https" "src" --dport "443" -j "REDIRECT" --to-ports "${https_port}"
158                 $IPT_N -I "PREROUTING" -p "tcp" -m "set" --match-set "neteasemusic" "dst" -j "netease_cloud_music"
159                 $IPT_N -I "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
160
161                 mkdir -p "/var/etc/"
162                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
163         elif [ "${hijack_ways}" = "use_hosts" ]; then
164                 mkdir -p "/tmp/dnsmasq.d"
165                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
166                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
167                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
168                         address=/music.163.com/${lan_addr}
169                         address=/interface.music.163.com/${lan_addr}
170                         address=/interface3.music.163.com/${lan_addr}
171                         address=/apm.music.163.com/${lan_addr}
172                         address=/apm3.music.163.com/${lan_addr}
173                         address=/clientlog.music.163.com/${lan_addr}
174                         address=/clientlog3.music.163.com/${lan_addr}
175                         address=/music.httpdns.c.163.com/0.0.0.0
176                 EOF
177                 /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
178
179                 ip route add "223.252.199.10" dev lo
180         fi
181 } >"/dev/null" 2>&1
182
183 stop()
184 {
185         { ps |grep "$NAME" |grep "app.js" |grep -v "grep" |awk '{print $1}' |xargs kill -9; } > "/dev/null" 2>&1
186
187         config_load "$NAME"
188
189         sed -i "/$NAME/d" "/etc/crontabs/root"
190         /etc/init.d/cron restart > "/dev/null" 2>&1
191
192         [ -f "${UPGRADE_CONF}" ] && sed -i "/$NAME\/core/d;/$NAME\/local_ver/d" "${UPGRADE_CONF}"
193         is_enabled "config" "keep_core_when_upgrade" && { echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"; echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"; }
194
195         local self_issue_cert_crt self_issue_cert_key
196         config_get "self_issue_cert_crt" "config" "self_issue_cert_crt"
197         config_get "self_issue_cert_key" "config" "self_issue_cert_key"
198         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
199                 sed -i "/${self_issue_cert_crt//\//\\/}/d" "${UPGRADE_CONF}"
200                 sed -i "/${self_issue_cert_key//\//\\/}/d" "${UPGRADE_CONF}"
201
202                 echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
203                 echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
204         }
205
206         http_port="$(cat "/tmp/$NAME.ports" | awk -F ':' 'print $1')"
207         https_port="$(cat "/tmp/$NAME.ports" | awk -F ':' 'print $2')"
208         iptables -D "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
209         iptables -D "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
210
211         $IPT_N -D "PREROUTING" -p "tcp" -m set --match-set "neteasemusic" "dst" -j "netease_cloud_music"
212         $IPT_N -D "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
213         $IPT_N -F "netease_cloud_music"
214         $IPT_N -X "netease_cloud_music"
215         ipset destroy "neteasemusic"
216         ipset destroy "acl_neteasemusic_http"
217         ipset destroy "acl_neteasemusic_https"
218         echo "" > "/var/etc/$NAME.include"
219
220         rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
221         /etc/init.d/dnsmasq reload >"/dev/null" 2>&1
222         ip route del "223.252.199.10"
223
224         rm -f "/tmp/$NAME.log"
225 } >"/dev/null" 2>&1