| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #!/bin/sh /etc/rc.common
- # SPDX-License-Identifier: GPL-3.0-only
- # Copyright (C) 2021 Tianling Shen <[email protected]>
- START=92
- STOP=10
- NAME="unblockneteasemusic"
- UPGRADE_CONF="/lib/upgrade/keep.d/$NAME"
- uci_get_by_type() {
- local "ret"
- ret="$(uci get "$NAME".@"$1"[0]."$2" 2>/dev/null)"
- echo "${ret:=$3}"
- }
- uci_get_by_name() {
- local "index"
- index=0
- if [ -n "$4" ]; then
- index="$4"
- fi
- ret="$(uci get "$NAME".@"$1"["${index}"]."$2" 2>/dev/null)"
- echo "${ret:=$3}"
- }
- lan_addr="$(uci get network.lan.ipaddr)"
- enable="$(uci_get_by_type "$NAME" "enable" "0")"
- music_source="$(uci_get_by_type "$NAME" "music_source" "default")"
- enable_flac="$(uci_get_by_type "$NAME" "enable_flac" "0")"
- [ "${enable_flac}" -eq "1" ] && export ENABLE_FLAC="true"
- replace_music_source="$(uci_get_by_type "$NAME" "replace_music_source" "dont_replace")"
- use_remote_qq_server="$(uci_get_by_type "$NAME" "use_remote_qq_server")"
- auto_update="$(uci_get_by_type "$NAME" "auto_update" "1")"
- update_time="$(uci_get_by_type "$NAME" "update_time" "3")"
- http_port="$(uci_get_by_type "$NAME" "http_port" "5200")"
- https_port="$(uci_get_by_type "$NAME" "https_port" "5201")"
- endpoint_url="$(uci_get_by_type "$NAME" "endpoint_url" "http://music.163.com")"
- hijack_ways="$(uci_get_by_type "$NAME" "hijack_ways" "use_ipset")"
- migu_cookie="$(uci_get_by_type "$NAME" "migu_cookie")"
- [ -n "${migu_cookie}" ] && export MIGU_COOKIE="${migu_cookie}"
- qq_cookie="$(uci_get_by_type "$NAME" "qq_cookie")"
- [ -n "${qq_cookie}" ] && export QQ_COOKIE="${qq_cookie}"
- youtube_key="$(uci_get_by_type "$NAME" "youtube_key")"
- [ -n "${youtube_key}" ] && export YOUTUBE_KEY="${youtube_key}"
- keep_core_when_upgrade="$(uci_get_by_type "$NAME" "keep_core_when_upgrade")"
- [ "$(uci_get_by_type "$NAME" "pub_access")" = "1" ] && addr="0.0.0.0" || addr="${lan_addr}"
- [ "$(uci_get_by_type "$NAME" "strict_mode")" = "1" ] && strict_mode="-s"
- netease_server_ip="$(uci_get_by_type "$NAME" "netease_server_ip")"
- [ -n "${netease_server_ip}" ] && netease_server_ip="-f ${netease_server_ip}"
- proxy_server_ip="$(uci_get_by_type "$NAME" "proxy_server_ip")"
- [ -n "${proxy_server_ip}" ] && proxy_server_ip="-u ${proxy_server_ip}"
- self_issue_cert_crt="$(uci_get_by_type "$NAME" "self_issue_cert_crt")"
- self_issue_cert_key="$(uci_get_by_type "$NAME" "self_issue_cert_key")"
- { [ -n "{$self_issue_cert_crt}" ] && [ -n "${self_issue_cert_key}" ]; } && {
- export SIGN_CERT="${self_issue_cert_crt}"
- export SIGN_KEY="${self_issue_cert_key}"
- }
- set_ipset()
- {
- if [ "${set_type}" = "start" ]; then
- mkdir -p "/tmp/dnsmasq.d"
- rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
- ipset=/.music.163.com/neteasemusic
- ipset=/interface.music.163.com/neteasemusic
- ipset=/interface3.music.163.com/neteasemusic
- ipset=/apm.music.163.com/neteasemusic
- ipset=/apm3.music.163.com/neteasemusic
- ipset=/clientlog.music.163.com/neteasemusic
- ipset=/clientlog3.music.163.com/neteasemusic
- EOF
- /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
- if ! ipset list "acl_neteasemusic_http" > "/dev/null"; then ipset create "acl_neteasemusic_http" hash:ip; fi
- if ! ipset list "acl_neteasemusic_https" > "/dev/null"; then ipset create "acl_neteasemusic_https" hash:ip; fi
- ip_addr_num="$(uci show "$NAME" | grep -c "filter_mode")"
- let ip_addr_num="ip_addr_num-1"
- [ "${ip_addr_num}" -ge "0" ] && for i in $(seq 0 "${ip_addr_num}")
- do
- ip_addr="$(uci_get_by_name "acl_rule" "ip_addr" "" "$i")"
- filter_mode="$(uci_get_by_name "acl_rule" "filter_mode" "" "$i")"
- case "${filter_mode}" in
- "disable_http")
- ipset -! add "acl_neteasemusic_http" "${ip_addr}"
- ;;
- "disable_https")
- ipset -! add "acl_neteasemusic_https" "${ip_addr}"
- ;;
- "disable_all")
- ipset -! add "acl_neteasemusic_http" "${ip_addr}"
- ipset -! add "acl_neteasemusic_https" "${ip_addr}"
- ;;
- esac
- done
- if ! ipset list "neteasemusic" > "/dev/null"; then ipset create "neteasemusic" hash:ip; fi
- uclient-fetch -q -O- "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.*' |sort -u |awk '{print "ipset add neteasemusic "$1}' |sh > "/dev/null" 2>&1
- iptables -t "nat" -N "netease_cloud_music"
- iptables -t "nat" -A "netease_cloud_music" -d "0.0.0.0/8" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "10.0.0.0/8" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "127.0.0.0/8" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "169.254.0.0/16" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "172.16.0.0/12" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "192.168.0.0/16" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "224.0.0.0/4" -j "RETURN"
- iptables -t "nat" -A "netease_cloud_music" -d "240.0.0.0/4" -j "RETURN"
- 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}"
- 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}"
- iptables -t "nat" -I "PREROUTING" -p "tcp" -m "set" --match-set "neteasemusic" "dst" -j "netease_cloud_music"
- [ -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"
- mkdir -p "/var/etc/"
- echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
- elif [ "${set_type}" = "stop" ]; then
- iptables -t "nat" -D "PREROUTING" -p "tcp" -m set --match-set "neteasemusic" "dst" -j "netease_cloud_music"
- iptables -t "nat" -D "KOOLPROXY" -m "set" --match-set "neteasemusic" "dst" -j "RETURN" -m "comment" --comment "KP for UnblockMusic"
- iptables -t "nat" -F "netease_cloud_music"
- iptables -t "nat" -X "netease_cloud_music"
- ipset destroy "neteasemusic"
- ipset destroy "acl_neteasemusic_http"
- ipset destroy "acl_neteasemusic_https"
- echo "" > "/var/etc/$NAME.include"
- rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
- fi
- }
- set_hosts()
- {
- if [ "${set_type}" = "start" ]; then
- mkdir -p "/tmp/dnsmasq.d"
- rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- cat <<-EOF > "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- dhcp-option=252,http://${lan_addr}:${http_port}/proxy.pac
- address=/music.163.com/${lan_addr}
- address=/interface.music.163.com/${lan_addr}
- address=/interface3.music.163.com/${lan_addr}
- address=/apm.music.163.com/${lan_addr}
- address=/apm3.music.163.com/${lan_addr}
- address=/clientlog.music.163.com/${lan_addr}
- address=/clientlog3.music.163.com/${lan_addr}
- address=/music.httpdns.c.163.com/0.0.0.0
- EOF
- /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
- ip route add "223.252.199.10" dev lo
- elif [ "${set_type}" = "stop" ]; then
- rm -f "/tmp/dnsmasq.d/dnsmasq-$NAME.conf"
- /etc/init.d/dnsmasq reload > "/dev/null" 2>&1
- ip route del "223.252.199.10"
- fi
- }
- set_ports()
- {
- if [ "${set_type}" = "start" ]; then
- iptables -I "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
- iptables -I "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
- mkdir -p "/var/etc/"
- echo "/etc/init.d/$NAME restart" > "/var/etc/$NAME.include"
- elif [ "${set_type}" = "stop" ]; then
- iptables -D "INPUT" -p "tcp" --dport "${http_port}" -j "ACCEPT"
- iptables -D "INPUT" -p "tcp" --dport "${https_port}" -j "ACCEPT"
- echo "" > "/var/etc/$NAME.include"
- fi
- }
- start()
- {
- stop
- [ "${enable}" -ne "1" ] && exit 0
- sed -i "/$NAME/d" /etc/crontabs/root
- [ "${auto_update}" -eq "1" ] && echo "0 ${update_time} * * * /usr/share/$NAME/update.sh update_core" >> "/etc/crontabs/root"
- echo "*/5 * * * * /usr/share/$NAME/log_check.sh" >> "/etc/crontabs/root"
- /etc/init.d/cron restart > "/dev/null" 2>&1
- [ ! -e "/usr/share/$NAME/core/app.js" ] && { rm -f "/usr/share/$NAME/local_ver"; sh "/usr/share/$NAME/update.sh" "update_core_non_restart"; }
- [ ! -e "/usr/share/$NAME/core/app.js" ] && { echo "Core Not Found, please download it before starting." >> "/tmp/$NAME.log"; exit 1; }
- quality_check_line="$(awk "/target == 0 \|\| item.id == target/{print NR}" "/usr/share/$NAME/core/src/hook.js")"
- sed -i "${quality_check_line}d" "/usr/share/$NAME/core/src/hook.js"
- if [ "${replace_music_source}" = "dont_replace" ]; then
- 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"
- elif [ "${replace_music_source}" = "lower_than_192kbps" ]; then
- 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"
- elif [ "${replace_music_source}" = "lower_than_320kbps" ]; then
- 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"
- elif [ "${replace_music_source}" = "lower_than_999kbps" ]; then
- 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"
- elif [ "${replace_music_source}" = "replace_all" ]; then
- sed -i -e "${quality_check_line}i \\\t\\tif (target == 0 || item.id == target) {" "/usr/share/$NAME/core/src/hook.js"
- fi
- [ "${hijack_ways}" = "use_hosts" ] && { http_port="80"; https_port="443"; }
- [ "${music_source}" = "default" ] && music_source="" || music_source="-o ${music_source}"
- 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 &
- set_type="start"
- if [ "${hijack_ways}" = "use_ipset" ]; then
- set_ipset > "/dev/null" 2>&1
- elif [ "${hijack_ways}" = "use_hosts" ]; then
- set_hosts > "/dev/null" 2>&1
- fi
- [ "$(uci_get_by_type "$NAME" pub_access)" = "1" ] && set_ports > "/dev/null" 2>&1
- }
- stop()
- {
- { ps |grep "$NAME" |grep "app.js" |grep -v "grep" |awk '{print $1}' |xargs kill -9; } > "/dev/null" 2>&1
- sed -i "/$NAME/d" "/etc/crontabs/root"
- /etc/init.d/cron restart > "/dev/null" 2>&1
- [ ! -f "${UPGRADE_CONF}" ] && touch "${UPGRADE_CONF}"
- sed -i "/$NAME\/core/d;/$NAME\/local_ver/d" "${UPGRADE_CONF}"
- [ "${keep_core_when_upgrade}" -eq "1" ] && { echo "/usr/share/$NAME/core/" >> "${UPGRADE_CONF}"; echo "/usr/share/$NAME/local_ver" >> "${UPGRADE_CONF}"; }
- { [ -f "${self_issue_cert_crt}" ] && [ -f "${self_issue_cert_key}" ]; } && {
- sed -i "/${self_issue_cert_crt//\//\\/}/d" "${UPGRADE_CONF}"
- sed -i "/${self_issue_cert_key//\//\\/}/d" "${UPGRADE_CONF}"
- echo "${self_issue_cert_crt}" >> "${UPGRADE_CONF}"
- echo "${self_issue_cert_key}" >> "${UPGRADE_CONF}"
- }
- rm -f "/tmp/$NAME.log"
- set_type="stop"
- set_ipset > "/dev/null" 2>&1
- set_hosts > "/dev/null" 2>&1
- set_ports > "/dev/null" 2>&1
- }
|