OSDN Git Service

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