OSDN Git Service

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