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.

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