You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
8.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. PKG_VERSION=
  3. export START=94
  4. export USE_PROCD=1
  5. #PROCD_DEBUG=1
  6. readonly A_TMP='/var/hosts.allowed.tmp'
  7. readonly B_TMP='/var/hosts.blocked.tmp'
  8. readonly T_TMP='/var/simple-adblock.hosts'
  9. readonly dl='wget --no-check-certificate -qO-'
  10. readonly h_filter='/localhost/d;/^#/d;/^[^0-9]/d;s/^0\.0\.0\.0.//;s/^127\.0\.0\.1.//;s/[[:space:]]*#.*$//;s/[[:cntrl:]]$//;s/[[:space:]]//g;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;'
  11. readonly d_filter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;'
  12. readonly f_filter='s|^|local=/|;s|$|/|'
  13. readonly _ok_='\033[0;32m\xe2\x9c\x93\033[0m'
  14. readonly _fail_='\033[0;31m\xe2\x9c\x97\033[0m'
  15. readonly __ok__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  16. readonly __fail__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  17. readonly _error_='\033[0;31mERROR\033[0m'
  18. export verbosity=2 force_dns=1 bgrun=0 debug=0 hosts_file='/var/dnsmasq.d/simple-adblock' led wan_if wan_gw wanphysdev
  19. ok() { case $verbosity in 1) output "$_ok_";; 2) output "$__ok__\n";; esac; }
  20. okn() { case $verbosity in 1) output "$_ok_\n";; 2) output "$__ok__\n";; esac; }
  21. fail() { case $verbosity in 1) output "$_fail_";; 2) output "$__fail__\n";; esac; }
  22. failn() { case $verbosity in 1) output "$_fail_\n";; 2) output "$__fail__\n";; esac; }
  23. output() { [[ $# -ne 1 ]] && { [[ ! $((verbosity & $1)) -gt 0 ]] && return 0 || shift; }; local msg; msg=$(echo -n "${1/$p_name /service }" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g'); [[ -t 1 ]] && echo -e -n "$1"; [[ $(echo -e -n "$msg" | wc -l) -gt 0 ]] && logger -t "${PKG_NAME:-service} [$$]" "$(echo -e -n ${logmsg}${msg})" && logmsg='' || logmsg=${logmsg}${msg}; }
  24. PKG_NAME="${PKG_NAME:-simple-adblock}"; p_name="${PKG_NAME} ${PKG_VERSION}"
  25. led_on(){ [[ -n "$led" && -e "$led/trigger" ]] && echo "default-on" > "$led/trigger"; }
  26. led_off(){ [[ -n "$led" && -e "$led/trigger" ]] && echo "none" > "$led/trigger"; }
  27. is_enabled () {
  28. local c=1 enabled
  29. config_load $PKG_NAME
  30. config_get_bool enabled 'config' 'enabled' 1
  31. config_get_bool bgrun 'config' 'run_in_background' 0
  32. config_get_bool force_dns 'config' 'force_dns' 1
  33. config_get_bool debug 'config' 'debug' 0
  34. config_get verbosity 'config' 'verbosity' '2'
  35. config_get hosts_file 'config' 'hosts_file' '/var/dnsmasq.d/simple-adblock'
  36. config_get led 'config' 'led'
  37. if [ "$debug" -ne 0 ]; then
  38. exec 1>>/tmp/simple-adblock.log
  39. exec 2>&1
  40. set -x
  41. fi
  42. led="${led:+/sys/class/leds/$led}"
  43. [[ $enabled -gt 0 ]] || { output "$_error_: $p_name is not enabled.\n"; return 1; }
  44. source /lib/functions/network.sh
  45. while : ; do
  46. network_find_wan wan_if; [ -n "$wan_if" ] && network_get_gateway wan_gw $wan_if;
  47. [[ $c -ge 25 || -n "$wan_gw" ]] && break
  48. output "$p_name waiting for wan gateway...\n"; sleep 2; network_flush_cache; let "c+=1";
  49. done
  50. [ -n "$wan_gw" ] && return 0 || { output "$_error_: $p_name failed to discover WAN gateway.\n"; return 1; }
  51. }
  52. reset_iptables() {
  53. [[ $force_dns -eq 0 ]] && return 0
  54. [ -z "$PKG_NAME" ] && return 1
  55. iptables-save | grep -Fv -- "$PKG_NAME" | iptables-restore
  56. lsmod | grep -q ip6table_nat && ip6tables-save | grep -Fv -- "$PKG_NAME" | ip6tables-restore
  57. [ ! "$1" == "quiet" ] && output 'No longer forcing local DNS server.\n'
  58. }
  59. set_iptables() {
  60. local ip ipv6 label ipv6wan brname
  61. network_get_ipaddr ip lan; network_get_ipaddr6 ipv6 lan; network_get_device brname lan; network_get_physdev wanphysdev wan;
  62. ipv6wan=$(ifconfig $wanphysdev | grep inet6 | awk '{print $3}')
  63. if [[ $force_dns -ne 0 ]]; then
  64. [ -n "$ip" ] && iptables -t nat -A prerouting_rule -i $brname -p tcp --dport 53 -j DNAT --to $ip -m comment --comment "$PKG_NAME"
  65. [ -n "$ip" ] && iptables -t nat -A prerouting_rule -i $brname -p udp --dport 53 -j DNAT --to $ip -m comment --comment "$PKG_NAME"
  66. if [[ -n "$ipv6" && -n "$ipv6wan" ]] && lsmod | grep -q ip6table_nat; then
  67. ip6tables -t nat -A PREROUTING -i $brname -p tcp --dport 53 -j DNAT --to-destination [$ipv6] -m comment --comment "$PKG_NAME"
  68. ip6tables -t nat -A PREROUTING -i $brname -p udp --dport 53 -j DNAT --to-destination [$ipv6] -m comment --comment "$PKG_NAME"
  69. label="$ip/$ipv6"
  70. else
  71. label="$ip"
  72. fi
  73. [ -n "$label" ] && output "Forcing local DNS server: $label.\n" || output "$_error_: $p_name failed to obtain LAN IP address for DNS forcing!\n"
  74. fi
  75. }
  76. stop_adblocking () {
  77. [ -f $hosts_file ] && mv $hosts_file $T_TMP
  78. output 3 "Restarting dnsmasq "
  79. led_off
  80. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  81. [[ $? -eq 0 ]] && { okn; output "$p_name stopped.\n"; } || { failn; output "$_error_: $p_name failed to reload dnsmasq!\n"; }
  82. }
  83. process_url() {
  84. local label type D_TMP R_TMP
  85. [[ -n "$1" && -n "$2" && -n "$3" ]] || return 1
  86. local url=$1
  87. [ "$2" == "hosts" ] && label="Hosts: $(echo $1 | cut -d'/' -f3)" filter="$h_filter" || label="Domains: $(echo $1 | cut -d'/' -f3)" filter="$d_filter"
  88. [ "$3" == "blocked" ] && { type='Blocked'; D_TMP="$B_TMP"; } || { type='Allowed'; D_TMP="$A_TMP"; }
  89. R_TMP="/var/simple-adblock_$(head /dev/urandom | tr -dc 'A-Za-z0-9' | head -c10)"
  90. while [ -e "$R_TMP" ]; do R_TMP="/var/simple-adblock_$(head /dev/urandom | tr -dc 'A-Za-z0-9' | head -c10)"; done
  91. touch "$R_TMP"
  92. output 2 "[DL] $type $label "
  93. $dl "${url}" > "$R_TMP" 2>/dev/null && ok || fail
  94. { sed -i "$filter" "$R_TMP"; cat "$R_TMP" >> "$D_TMP"; rm -f "$R_TMP"; } &
  95. }
  96. start_adblocking () {
  97. local whitelist_domains blacklist_domains whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
  98. config_get whitelist_domains 'config' 'whitelist_domain'
  99. config_get blacklist_domains 'config' 'blacklist_domain'
  100. config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
  101. config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
  102. config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
  103. local hf w_filter
  104. [ ! -d ${hosts_file%/*} ] && mkdir -p ${hosts_file%/*}
  105. if [[ -s $T_TMP && ! "$1" == "reload" ]]; then
  106. output 3 'Found existing data file, reusing it '
  107. mv $T_TMP $hosts_file && okn || failn
  108. else
  109. [ -f $A_TMP ] && rm -f $A_TMP; [ -f $B_TMP ] && rm -f $B_TMP; [ -f $T_TMP ] && rm -f $T_TMP; [ -f $hosts_file ] && rm -f $hosts_file
  110. touch $A_TMP; touch $B_TMP; touch $T_TMP;
  111. if [ -n "$blacklist_hosts_urls" ]; then
  112. output 1 '[DL] Blocked Hosts '
  113. for hf in ${blacklist_hosts_urls}; do process_url "$hf" 'hosts' 'blocked'; done
  114. output 1 '\n'
  115. fi
  116. if [ -n "$blacklist_domains_urls" ]; then
  117. output 1 '[DL] Blocked Domains '
  118. for hf in ${blacklist_domains_urls}; do process_url "$hf" 'domains' 'blocked'; done
  119. output 1 '\n'
  120. fi
  121. if [ -n "$whitelist_domains_urls" ]; then
  122. output 1 '[DL] Allowed Domains '
  123. for hf in ${whitelist_domains_urls}; do process_url "$hf" 'domains' 'allowed'; done
  124. output 1 '\n'
  125. fi
  126. output 3 'Waiting for background processes '
  127. wait && okn
  128. [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$d_filter" >> $B_TMP; done
  129. whitelist_domains="${whitelist_domains}"$'\n'"$(cat $A_TMP)"
  130. [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf=$(echo $hf | sed 's/\./\\./g'); w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
  131. if [ -s $B_TMP ]; then
  132. output 1 'Processing downloads '
  133. output 2 'Sorting merged file '; sort $B_TMP | uniq > $T_TMP && ok || fail
  134. output 2 'Whitelisting domains '; sed -i "$w_filter" $T_TMP && ok || fail
  135. output 2 'Formatting merged file '; sed "$f_filter" $T_TMP > $B_TMP && mv $B_TMP $hosts_file && ok || fail
  136. output 1 '\n'
  137. output 3 'Removing temporary files '
  138. [ -f $A_TMP ] && rm -f $A_TMP; [ -f $B_TMP ] && rm -f $B_TMP; [ -f $T_TMP ] && rm -f $T_TMP;
  139. okn
  140. fi
  141. fi
  142. if [ -s $hosts_file ]; then
  143. output 3 'Restarting dnsmasq '
  144. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  145. if [[ $? -eq 0 ]]; then
  146. led_on; okn;
  147. output "$p_name blocking $(wc -l < $hosts_file) domains $_ok_\n"
  148. else
  149. failn; output "$_error_: $p_name failed to reload dnsmasq!\n";
  150. exit 1
  151. fi
  152. else
  153. output "$_error_: $p_name failed to create its data file!\n"
  154. exit 1
  155. fi
  156. }
  157. boot() { sleep 10; rc_procd start_service; rc_procd service_triggers; }
  158. reload_service () {
  159. is_enabled || return 1
  160. [[ -t 1 && $bgrun -eq 1 ]] && (start_adblocking 'reload' | cat &) || start_adblocking 'reload'
  161. }
  162. start_service () {
  163. is_enabled || return 1
  164. reset_iptables quiet
  165. set_iptables
  166. [[ -t 1 && $bgrun -eq 1 ]] && (start_adblocking $1 | cat &) || start_adblocking $1
  167. }
  168. stop_service () {
  169. is_enabled || return 1
  170. reset_iptables
  171. [[ -t 1 && $bgrun -eq 1 ]] && (stop_adblocking | cat &) || stop_adblocking
  172. }
  173. service_triggers () {
  174. procd_add_reload_trigger 'simple-adblock'
  175. }