OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.12-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 #
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" "cnrelay" "-c"
93         append_param_arg "config" "endpoint_url" "-e" "https://music.163.com"
94         append_param_arg "config" "netease_server_ip" "-f"
95         append_param_arg "config" "proxy_server_ip" "-u"
96         is_enabled "config" "strict_mode" && append_param "-s"
97
98         procd_set_param env LOG_FILE="/tmp/$NAME.log"
99
100         append_param_env "config" "joox_cookie" "JOOX_COOKIE"
101         append_param_env "config" "migu_cookie" "MIGU_COOKIE"
102         append_param_env "config" "qq_cookie" "QQ_COOKIE"
103         append_param_env "config" "youtube_key" "YOUTUBE_KEY"
104         append_param_env "config" "self_issue_cert_crt" "SIGN_CERT" "/usr/share/$NAME/core/server.crt"
105         append_param_env "config" "self_issue_cert_key" "SIGN_KEY" "/usr/share/$NAME/core/server.key"
106
107         is_enabled "config" "enable_flac" && procd_append_param env ENABLE_FLAC="true"
108         is_enabled "config" "local_vip" && procd_append_param env ENABLE_LOCAL_VIP="true"
109         case "$(config_get "config" "replace_music_source")" in
110                 "lower_than_192kbps") procd_append_param env MIN_BR="192000" ;;
111                 "lower_than_320kbps") procd_append_param env MIN_BR="320000" ;;
112                 "lower_than_999kbps") procd_append_param env MIN_BR="600000" ;;
113                 "replace_all") procd_append_param env MIN_BR="9999999" ;;
114         esac
115
116         procd_set_param stdout 1
117         procd_set_param stderr 1
118         procd_set_param respawn
119
120         if [ "${hijack_ways}" = "use_ipset" ]; then
121                 mkdir -p "/tmp/dnsmasq.d"
122                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
123                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
124                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
125                         ipset=/.music.163.com/neteasemusic
126                         ipset=/interface.music.163.com/neteasemusic
127                         ipset=/interface3.music.163.com/neteasemusic
128                         ipset=/apm.music.163.com/neteasemusic
129                         ipset=/apm3.music.163.com/neteasemusic
130                         ipset=/clientlog.music.163.com/neteasemusic
131                         ipset=/clientlog3.music.163.com/neteasemusic
132                 EOF
133                 /etc/init.d/dnsmasq reload
134
135                 ipset create "acl_neteasemusic_http" hash:ip
136                 ipset create "acl_neteasemusic_https" hash:ip
137                 ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
138                 let ip_addr_num="ip_addr_num-1"
139                 [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
140                 do
141                         ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
142                         filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
143                         case "${filter_mode}" in
144                         "disable_http")
145                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
146                                 ;;
147                         "disable_https")
148                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
149                                 ;;
150                         "disable_all")
151                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
152                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
153                                 ;;
154                         esac
155                 done
156
157                 ipset create "neteasemusic" hash:ip
158                 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                 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                 echo -e "${netease_music_ips}\n${netease_music_ips2}" |sort -u |awk '{print "ipset add neteasemusic "$1}' |sh
161                 $IPT_N -N "netease_cloud_music"
162                 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
163                         $IPT_N -A "netease_cloud_music" -d "${local_addr}" -j "RETURN"
164                 done
165                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_http" "src" --dport "80" -j "REDIRECT" --to-ports "${http_port}"
166                 $IPT_N -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_https" "src" --dport "443" -j "REDIRECT" --to-ports "${https_port}"
167                 $IPT_N -I "PREROUTING" -p "tcp" -m "set" --match-set "neteasemusic" "dst" -j "netease_cloud_music"
168
169                 mkdir -p "/var/etc/"
170                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
171         elif [ "${hijack_ways}" = "use_hosts" ]; then
172                 mkdir -p "/tmp/dnsmasq.d"
173                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
174                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
175                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
176                         address=/music.163.com/${lan_addr}
177                         address=/interface.music.163.com/${lan_addr}
178                         address=/interface3.music.163.com/${lan_addr}
179                         address=/apm.music.163.com/${lan_addr}
180                         address=/apm3.music.163.com/${lan_addr}
181                         address=/clientlog.music.163.com/${lan_addr}
182                         address=/clientlog3.music.163.com/${lan_addr}
183                         address=/music.httpdns.c.163.com/0.0.0.0
184                 EOF
185                 /etc/init.d/dnsmasq reload
186
187                 ip route add "223.252.199.10" dev lo
188         fi
189
190         procd_close_instance
191 } >"/dev/null" 2>&1
192
193 stop_service()
194 {
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         local self_issue_cert_crt self_issue_cert_key
206         config_get "self_issue_cert_crt" "config" "self_issue_cert_crt"
207         config_get "self_issue_cert_key" "config" "self_issue_cert_key"
208         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
209                 echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
210                 echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
211         }
212
213         [ -f "/tmp/$NAME.ports" ] && {
214                 iptables -D "INPUT" -p "tcp" --dport "$(awk -F ':' 'print $1' "/tmp/$NAME.ports")" -j "ACCEPT"
215                 iptables -D "INPUT" -p "tcp" --dport "$(awk -F ':' 'print $2' "/tmp/$NAME.ports")" -j "ACCEPT"
216                 rm -f "/tmp/$NAME.ports"
217         }
218
219         $IPT_N -D "PREROUTING" -p "tcp" -m set --match-set "neteasemusic" "dst" -j "netease_cloud_music"
220         $IPT_N -F "netease_cloud_music"
221         $IPT_N -X "netease_cloud_music"
222         ipset destroy "neteasemusic"
223         ipset destroy "acl_neteasemusic_http"
224         ipset destroy "acl_neteasemusic_https"
225         echo "" > "/var/etc/$NAME.include"
226
227         rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
228         /etc/init.d/dnsmasq reload
229         ip route del "223.252.199.10"
230
231         rm -f "/tmp/$NAME.log"
232 } >"/dev/null" 2>&1
233
234 reload_service() {
235         stop
236         start
237 }
238
239 service_triggers() {
240         procd_add_reload_trigger "$NAME"
241 }