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 # Created By ImmortalWrt
3 # https://github.com/immortalwrt
4
5 START=92
6 STOP=10
7
8 NAME="unblockneteasemusic"
9 UPGRADE_CONF="/lib/upgrade/keep.d/$NAME"
10
11 uci_get_by_type() {
12         local "ret"
13         ret="$(uci get "$NAME".@"$1"[0]."$2" 2>/dev/null)"
14         echo "${ret:=$3}"
15 }
16
17 uci_get_by_name() {
18         local "index"
19         index=0
20         if [ -n "$4" ]; then
21                 index="$4"
22         fi
23         ret="$(uci get "$NAME".@"$1"["${index}"]."$2" 2>/dev/null)"
24         echo "${ret:=$3}"
25 }
26
27 lan_addr="$(uci get network.lan.ipaddr)"
28
29 enable="$(uci_get_by_type "$NAME" "enable" "0")"
30
31 music_source="$(uci_get_by_type "$NAME" "music_source" "default")"
32
33 enable_flac="$(uci_get_by_type "$NAME" "enable_flac" "0")"
34 [ "${enable_flac}" -eq "1" ] && export ENABLE_FLAC="true"
35 replace_music_source="$(uci_get_by_type "$NAME" "replace_music_source" "dont_replace")"
36
37 use_remote_qq_server="$(uci_get_by_type "$NAME" "use_remote_qq_server")"
38
39 auto_update="$(uci_get_by_type "$NAME" "auto_update" "1")"
40 update_time="$(uci_get_by_type "$NAME" "update_time" "3")"
41
42 http_port="$(uci_get_by_type "$NAME" "http_port" "5200")"
43 https_port="$(uci_get_by_type "$NAME" "https_port" "5201")"
44
45 endpoint_url="$(uci_get_by_type "$NAME" "endpoint_url" "http://music.163.com")"
46 hijack_ways="$(uci_get_by_type "$NAME" "hijack_ways" "use_ipset")"
47
48 migu_cookie="$(uci_get_by_type "$NAME" "migu_cookie")"
49 [ -n "${migu_cookie}" ] && export MIGU_COOKIE="${migu_cookie}"
50 qq_cookie="$(uci_get_by_type "$NAME" "qq_cookie")"
51 [ -n "${qq_cookie}" ] && export QQ_COOKIE="${qq_cookie}"
52 youtube_key="$(uci_get_by_type "$NAME" "youtube_key")"
53 [ -n "${youtube_key}" ] && export YOUTUBE_KEY="${youtube_key}"
54
55 keep_core_when_upgrade="$(uci_get_by_type "$NAME" "keep_core_when_upgrade")"
56 [ "$(uci_get_by_type "$NAME" "pub_access")" = "1" ] && addr="0.0.0.0" || addr="${lan_addr}"
57 [ "$(uci_get_by_type "$NAME" "strict_mode")" = "1" ] && strict_mode="-s"
58
59 netease_server_ip="$(uci_get_by_type "$NAME" "netease_server_ip")"
60 [ -n "${netease_server_ip}" ] && netease_server_ip="-f ${netease_server_ip}"
61 proxy_server_ip="$(uci_get_by_type "$NAME" "proxy_server_ip")"
62 [ -n "${proxy_server_ip}" ] && proxy_server_ip="-u ${proxy_server_ip}"
63
64 self_issue_cert_crt="$(uci_get_by_type "$NAME" "self_issue_cert_crt")"
65 self_issue_cert_key="$(uci_get_by_type "$NAME" "self_issue_cert_key")"
66
67 set_ipset()
68 {
69         if [ "${set_type}" = "start" ]; then
70                 mkdir -p "/tmp/dnsmasq.d"
71                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
72                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
73 dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
74 ipset=/.music.163.com/neteasemusic
75 ipset=/interface.music.163.com/neteasemusic
76 ipset=/interface3.music.163.com/neteasemusic
77 ipset=/apm.music.163.com/neteasemusic
78 ipset=/apm3.music.163.com/neteasemusic
79 ipset=/clientlog.music.163.com/neteasemusic
80 ipset=/clientlog3.music.163.com/neteasemusic
81                 EOF
82                 /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
83
84                 if ! ipset list "acl_neteasemusic_http" > "/dev/null"; then ipset create "acl_neteasemusic_http" hash:ip; fi
85                 if ! ipset list "acl_neteasemusic_https" > "/dev/null"; then ipset create "acl_neteasemusic_https" hash:ip; fi
86                 ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
87                 let ip_addr_num="ip_addr_num-1"
88                 [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
89                 do
90                         ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
91                         filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
92                         case "${filter_mode}" in
93                         "disable_http")
94                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
95                                 ;;
96                         "disable_https")
97                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
98                                 ;;
99                         "disable_all")
100                                 ipset -! add "acl_neteasemusic_http" "${ip_addr}"
101                                 ipset -! add "acl_neteasemusic_https" "${ip_addr}"
102                                 ;;
103                         esac
104                 done
105
106                 if ! ipset list "neteasemusic" > "/dev/null"; then ipset create "neteasemusic" hash:ip; fi
107                 curl --retry "5" --retry-delay "3" -s "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" |grep -Eo '[0-9]+?\.[0-9]+?\.[0-9]+?\.[0-9]+?' |sort |uniq |awk '{print "ipset add neteasemusic "$1}' |bash > "/dev/null" 2>&1
108                 iptables -t "nat" -N "netease_cloud_music"
109                 iptables -t "nat" -A "netease_cloud_music" -d "0.0.0.0/8" -j "RETURN"
110                 iptables -t "nat" -A "netease_cloud_music" -d "10.0.0.0/8" -j "RETURN"
111                 iptables -t "nat" -A "netease_cloud_music" -d "127.0.0.0/8" -j "RETURN"
112                 iptables -t "nat" -A "netease_cloud_music" -d "169.254.0.0/16" -j "RETURN"
113                 iptables -t "nat" -A "netease_cloud_music" -d "172.16.0.0/12" -j "RETURN"
114                 iptables -t "nat" -A "netease_cloud_music" -d "192.168.0.0/16" -j "RETURN"
115                 iptables -t "nat" -A "netease_cloud_music" -d "224.0.0.0/4" -j "RETURN"
116                 iptables -t "nat" -A "netease_cloud_music" -d "240.0.0.0/4" -j "RETURN"
117                 iptables -t "nat" -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_http" "src" --dport "80" -j "REDIRECT" --to-ports "${http_port}"
118                 iptables -t "nat" -A "netease_cloud_music" -p "tcp" -m "set" ! --match-set "acl_neteasemusic_https" "src" --dport "443" -j "REDIRECT" --to-ports "${https_port}"
119                 iptables -t "nat" -I "PREROUTING" -p "tcp" -m "set" --match-set "neteasemusic" "dst" -j "netease_cloud_music"
120                 [ -z "$(iptables -t "nat" -L "KOOLPROXY" | grep "UnblockMusic" | sed 's/\/.*//')" ] && iptables -t "nat" -I "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
121
122                 mkdir -p "/var/etc/"
123                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
124         elif [ "${set_type}" = "stop" ]; then
125                 iptables -t "nat" -D "PREROUTING" -p "tcp" -m set --match-set "neteasemusic" "dst" -j "netease_cloud_music"
126                 iptables -t "nat" -D "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
127                 iptables -t "nat" -F "netease_cloud_music"
128                 iptables -t "nat" -X "netease_cloud_music"
129                 ipset destroy "neteasemusic"
130                 ipset destroy "acl_neteasemusic_http"
131                 ipset destroy "acl_neteasemusic_https"
132
133                 echo "" > "/var/etc/$NAME.include"
134                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
135                 /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
136         fi
137 }
138
139 set_hosts()
140 {
141         if [ "${set_type}" = "start" ]; then
142                 mkdir -p "/tmp/dnsmasq.d"
143                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
144                 cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
145 dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
146 address=/music.163.com/${lan_addr}
147 address=/interface.music.163.com/${lan_addr}
148 address=/interface3.music.163.com/${lan_addr}
149 address=/apm.music.163.com/${lan_addr}
150 address=/apm3.music.163.com/${lan_addr}
151 address=/clientlog.music.163.com/${lan_addr}
152 address=/clientlog3.music.163.com/${lan_addr}
153 address=/music.httpdns.c.163.com/0.0.0.0
154                 EOF
155                 /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
156
157                 ip route add "223.252.199.10" dev lo
158         elif [ "${set_type}" = "stop" ]; then
159                 rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
160                 /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
161
162                 ip route del "223.252.199.10"
163         fi
164 }
165
166 set_ports()
167 {
168         if [ "${set_type}" = "start" ]; then
169                 iptables -I "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
170                 iptables -I "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
171
172                 mkdir -p "/var/etc/"
173                 echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
174         elif [ "${set_type}" = "stop" ]; then
175                 iptables -D "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
176                 iptables -D "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
177
178                 echo "" > "/var/etc/$NAME.include"
179         fi
180 }
181
182 start()
183 {
184         stop
185
186         [ "${enable}" -ne "1" ] && exit 0
187
188         sed -i "/$NAME/d" /etc/crontabs/root
189         [ "${auto_update}" -eq "1" ] && echo "0 ${update_time} * * * /usr/share/$NAME/update.sh update_core" >> "/etc/crontabs/root"
190         echo "*/5 * * * * /usr/share/$NAME/log_check.sh" >> "/etc/crontabs/root"
191         /etc/init.d/cron restart > "/dev/null" 2>&1
192
193         [ ! -e "/usr/share/$NAME/core/app.js" ] && { rm -f "/usr/share/$NAME/local_ver"; bash "/usr/share/$NAME/update.sh" "update_core_non_restart"; }
194         [ ! -e "/usr/share/$NAME/core/app.js" ] && { echo "Core Not Found, please download it before starting." >> "/tmp/$NAME.log"; exit 1; }
195
196         quality_check_line="$(awk "/target == 0 \|\| item.id == target/{print NR}" "/usr/share/$NAME/core/src/hook.js")"
197         sed -i "${quality_check_line}d" "/usr/share/$NAME/core/src/hook.js"
198         if [ "${replace_music_source}" = "dont_replace" ]; then
199                 sed -i -e "${quality_check_line}i \\\t\\tif ((item.code != 200 || item.freeTrialInfo) && (target == 0 || item.id == target)) {" "/usr/share/$NAME/core/src/hook.js"
200         elif [ "${replace_music_source}" = "lower_than_192kbps" ]; then
201                 sed -i -e "${quality_check_line}i \\\t\\tif ((item.code != 200 || item.freeTrialInfo || item.br < 192000) && (target == 0 || item.id == target)) {" "/usr/share/$NAME/core/src/hook.js"
202         elif [ "${replace_music_source}" = "lower_than_320kbps" ]; then
203                 sed -i -e "${quality_check_line}i \\\t\\tif ((item.code != 200 || item.freeTrialInfo || item.br < 320000) && (target == 0 || item.id == target)) {" "/usr/share/$NAME/core/src/hook.js"
204         elif [ "${replace_music_source}" = "lower_than_999kbps" ]; then
205                 sed -i -e "${quality_check_line}i \\\t\\tif ((item.code != 200 || item.freeTrialInfo || item.br < 999000) && (target == 0 || item.id == target)) {" "/usr/share/$NAME/core/src/hook.js"
206         elif [ "${replace_music_source}" = "replace_all" ]; then
207                 sed -i -e "${quality_check_line}i \\\t\\tif (target == 0 || item.id == target) {" "/usr/share/$NAME/core/src/hook.js"
208         fi
209
210         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
211                 { [ "${self_issue_cert_crt}" != "/usr/share/$NAME/core/server.crt" ] && [ "${self_issue_cert_key}" != "/usr/share/$NAME/core/server.key" ]; } && {
212                         ln -sf "${self_issue_cert_crt}" "/usr/share/$NAME/core/server.crt"
213                         ln -sf "${self_issue_cert_key}" "/usr/share/$NAME/core/server.key"
214                 }
215         }
216
217         [ "${hijack_ways}" = "use_hosts" ] && { http_port="80"; https_port="443"; }
218         [ "${music_source}" = "default" ] && music_source="" || music_source="-o ${music_source}"
219         nohup node "/usr/share/$NAME/core/app.js" -a "${addr}" -p "${http_port}":"${https_port}" ${music_source} -e "${endpoint_url}" ${netease_server_ip} ${proxy_server_ip} ${strict_mode} >> "/tmp/$NAME.log" 2>&1 &
220
221         set_type="start"
222         if [ "${hijack_ways}" = "use_ipset" ]; then
223                 set_ipset > "/dev/null" 2>&1
224         elif [ "${hijack_ways}" = "use_hosts" ]; then
225                 set_hosts > "/dev/null" 2>&1
226         fi
227         [ "$(uci_get_by_type "$NAME" pub_access)" = "1" ] && set_ports > "/dev/null" 2>&1
228 }
229
230 stop()
231 {
232         { ps |grep "$NAME" |grep "app.js" |grep -v "grep" |awk '{print $1}' |xargs kill -9; } > "/dev/null" 2>&1
233
234         sed -i "/$NAME/d" "/etc/crontabs/root"
235         /etc/init.d/cron restart > "/dev/null" 2>&1
236
237         [ ! -f "${UPGRADE_CONF}" ] && touch "${UPGRADE_CONF}"
238         sed -i "/$NAME\/core/d;/$NAME\/local_ver/d" "${UPGRADE_CONF}"
239         [ "${keep_core_when_upgrade}" -eq "1" ] && { echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"; echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"; }
240
241         { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
242                 { [ "${self_issue_cert_crt}" != "/usr/share/$NAME/core/server.crt" ] && [ "${self_issue_cert_key}" != "/usr/share/$NAME/core/server.key" ]; } && {
243                         sed -i "/${self_issue_cert_crt//\//\\/}/d" "${UPGRADE_CONF}"
244                         echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
245
246                         sed -i "/${self_issue_cert_key//\//\\/}/d" "${UPGRADE_CONF}"
247                         echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
248                 }
249         }
250
251         rm -f "/tmp/$NAME.log"
252
253         set_type="stop"
254         set_ipset > "/dev/null" 2>&1
255         set_hosts > "/dev/null" 2>&1
256         set_ports > "/dev/null" 2>&1
257 }