OSDN Git Service

luci-app-unblockneteasemusic: rewrite func to add pub_access rule
[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         local music_source
76         config_get music_source "config" "music_source" "default"
77         [ "${music_source}" != "default" ] && append_param -o "${music_source}"
78
79         append_param_arg "config" "cnrelay" "-c"
80         append_param_arg "config" "endpoint_url" "-e" "https://music.163.com"
81         append_param_arg "config" "netease_server_ip" "-f"
82         append_param_arg "config" "proxy_server_ip" "-u"
83         is_enabled "config" "strict_mode" && append_param "-s"
84
85         procd_set_param env LOG_FILE="/tmp/$NAME.log"
86
87         append_param_env "config" "joox_cookie" "JOOX_COOKIE"
88         append_param_env "config" "migu_cookie" "MIGU_COOKIE"
89         append_param_env "config" "qq_cookie" "QQ_COOKIE"
90         append_param_env "config" "youtube_key" "YOUTUBE_KEY"
91         append_param_env "config" "self_issue_cert_crt" "SIGN_CERT" "/usr/share/$NAME/core/server.crt"
92         append_param_env "config" "self_issue_cert_key" "SIGN_KEY" "/usr/share/$NAME/core/server.key"
93
94         is_enabled "config" "enable_flac" && procd_append_param env ENABLE_FLAC="true"
95         is_enabled "config" "local_vip" && procd_append_param env ENABLE_LOCAL_VIP="true"
96         case "$(config_get "config" "replace_music_source")" in
97                 "lower_than_192kbps") procd_append_param env MIN_BR="192000" ;;
98                 "lower_than_320kbps") procd_append_param env MIN_BR="320000" ;;
99                 "lower_than_999kbps") procd_append_param env MIN_BR="600000" ;;
100                 "replace_all") procd_append_param env MIN_BR="9999999" ;;
101         esac
102         procd_append_param env JSON_LOG="true"
103
104         procd_set_param stdout 1
105         procd_set_param stderr 1
106         procd_set_param respawn
107
108         local lan_addr="$(uci -q get network.lan.ipaddr)"
109         if [ "${hijack_ways}" = "use_ipset" ]; then
110                 # TODO: wating for dnsmasq support nftset
111                 mkdir -p "/tmp/dnsmasq.d"
112                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
113                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
114                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
115                         ipset=/.music.163.com/neteasemusic
116                         ipset=/interface.music.163.com/neteasemusic
117                         ipset=/interface3.music.163.com/neteasemusic
118                         ipset=/apm.music.163.com/neteasemusic
119                         ipset=/apm3.music.163.com/neteasemusic
120                         ipset=/clientlog.music.163.com/neteasemusic
121                         ipset=/clientlog3.music.163.com/neteasemusic
122                 EOF
123                 /etc/init.d/dnsmasq reload
124
125                 local ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
126                 let ip_addr_num="ip_addr_num-1"
127                 local acl_http_addr acl_https_addr
128                 [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
129                 do
130                         ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
131                         filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
132
133                         case "${filter_mode}" in
134                         "disable_http")
135                                 acl_http_addr="${acl_http_addr}${ip_addr}\n"
136                                 ;;
137                         "disable_https")
138                                 acl_https_addr="${acl_https_addr}${ip_addr}\n"
139                                 ;;
140                         "disable_all")
141                                 acl_http_addr="${acl_http_addr}${ip_addr}\n"
142                                 acl_https_addr="${acl_https_addr}${ip_addr}\n"
143                                 ;;
144                         esac
145                 done
146
147                 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.*')"
148                 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"].*')"
149                 local tmp="/tmp/$NAME"
150                 local neteasemusic_addr="$(echo -e "${netease_music_ips}\n${netease_music_ips2}" |sort -u |awk '{print $1}')"
151                 json_init
152                 if is_enabled "config" "pub_access"; then
153                         json_add_int o_pub_access "1"
154                 else
155                         json_add_int o_pub_access "0"
156                 fi
157                 json_add_int o_http_port "${http_port}"
158                 json_add_int o_https_port "${https_port}"
159                 json_add_string o_acl_http_addr "$acl_http_addr"
160                 json_add_string o_acl_https_addr "$acl_https_addr"
161                 json_add_string o_neteasemusic_addr "$neteasemusic_addr"
162                 json_dump -i >"$tmp.json"
163
164                 if ucode -S -i "$RULES_UC" -E "$tmp.json" >"$tmp.nft" \
165                         && ! cmp -s "$tmp.nft" "$RULES_NFT"; then
166                         echo "table inet chk {include \"$tmp.nft\";}" >"$tmp.nft.chk"
167                         if nft -f "$tmp.nft.chk" -c; then
168                                 mv -f "$tmp.nft" "$RULES_NFT"
169                                 fw4 reload
170                         fi
171                         rm -f "$tmp.nft.chk"
172                 fi
173                 rm -f "$tmp.json" "$tmp.nft"
174         elif [ "${hijack_ways}" = "use_hosts" ]; then
175                 mkdir -p "/tmp/dnsmasq.d"
176                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
177                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
178                         dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
179                         address=/music.163.com/${lan_addr}
180                         address=/interface.music.163.com/${lan_addr}
181                         address=/interface3.music.163.com/${lan_addr}
182                         address=/apm.music.163.com/${lan_addr}
183                         address=/apm3.music.163.com/${lan_addr}
184                         address=/clientlog.music.163.com/${lan_addr}
185                         address=/clientlog3.music.163.com/${lan_addr}
186                         address=/music.httpdns.c.163.com/0.0.0.0
187                 EOF
188                 /etc/init.d/dnsmasq reload
189
190                 ip route add "223.252.199.10" dev lo
191         fi
192
193         procd_close_instance
194 } >"/dev/null" 2>&1
195
196 stop_service()
197 {
198         config_load "$NAME"
199
200         sed -i "/$NAME/d" "/etc/crontabs/root"
201         /etc/init.d/cron restart
202
203         rm -f "${UPGRADE_CONF}"
204         is_enabled "config" "keep_core_when_upgrade" && {
205                 echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"
206                 echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"
207         }
208
209         local self_issue_cert_crt self_issue_cert_key
210         config_get "self_issue_cert_crt" "config" "self_issue_cert_crt"
211         config_get "self_issue_cert_key" "config" "self_issue_cert_key"
212         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
213                 echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
214                 echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
215         }
216
217         [ ! -e "$RULES_NFT" ] || rm -f "$RULES_NFT"
218         fw4 reload
219
220         rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
221         /etc/init.d/dnsmasq reload
222         ip route del "223.252.199.10"
223
224         rm -f "/tmp/$NAME.log"
225 } >"/dev/null" 2>&1
226
227 reload_service() {
228         stop
229         start
230 }
231
232 service_triggers() {
233         procd_add_reload_trigger "$NAME"
234 }