OSDN Git Service

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