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