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.

234 lines
9.5 KiB

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