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.

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