1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039
4 # shellcheck disable=SC1091
11 export EXTRA_COMMANDS
='check dl killcache status'
12 export EXTRA_HELP
=' check Checks if specified domain is found in current blacklist
13 dl Force-redownloads all the list
14 status Shows the service last-run status'
16 readonly packageName
='simple-adblock'
17 readonly serviceName
="$packageName $PKG_VERSION"
18 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
19 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
20 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
21 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
22 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
23 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
24 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
25 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
26 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
27 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
28 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
29 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
30 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
31 readonly serversFile
="/var/run/${packageName}.servers"
32 readonly serversCache
="/var/run/${packageName}.servers.cache"
33 readonly serversGzip
="/etc/${packageName}.servers.gz"
34 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
35 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
36 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
37 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
38 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
39 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
40 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
41 readonly PIDFile
="/var/run/${packageName}.pid"
42 readonly jsonFile
="/var/run/${packageName}.json"
43 readonly hostsFilter
='/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;/[^[:alnum:]_.-]/d;'
44 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
45 readonly checkmark
='\xe2\x9c\x93'
46 readonly xmark
='\xe2\x9c\x97'
47 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
48 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
49 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
50 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
51 readonly _ERROR_
='\033[0;31mERROR\033[0m'
52 readonly statusSuccess
='Success'
53 readonly statusFail
='Fail'
54 readonly statusDownloading
='Downloading'
55 readonly statusReloading
='Reloading'
56 readonly statusRestarting
='Restarting'
57 readonly statusStarting
='Starting'
58 readonly statusForceReloading
='Force-Reloading'
59 readonly statusProcessing
='Processing'
60 readonly statusStopped
='Stopped'
61 readonly sharedMemoryError
="/dev/shm/$packageName-error"
62 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
64 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
65 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
67 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
68 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
69 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
70 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
71 str_replace
() { echo "$1" |
sed -e "s/$2/$3/g"; }
72 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
73 compare_versions
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
74 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
75 is_ipset_procd
() { compare_versions
"$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
76 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
77 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
78 dnsmasq_kill
() { killall
-q -HUP dnsmasq
; }
79 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
80 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
83 # Can take a single parameter (text) to be output at any verbosity
84 # Or target verbosity level and text to be output at specifc verbosity
87 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
89 [ -t 1 ] && printf "%b" "$1"
90 msg
="$(printf "%s
" "$
(str_replace
"$1" "$serviceName " "service ")" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g')";
91 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
92 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
93 logger
-t "${packageName:-service} [$$]" "$(printf "%b" "${memmsg}${msg}")"
94 rm -f "$sharedMemoryOutput"
96 printf "%b
" "$msg" >> "$sharedMemoryOutput"
100 export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache
101 export targetDNS bootDelay dlTimeout curlRetry verbosity led dnsInstance
102 export whitelist_domains blacklist_domains
103 export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
104 export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
105 export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
107 load_package_config() {
108 config_load "$packageName"
109 config_get_bool serviceEnabled 'config' 'enabled' 1
110 config_get_bool forceDNS 'config' 'force_dns' 1
111 config_get_bool parallelDL 'config' 'parallel_downloads' 1
112 config_get_bool debug 'config' 'debug' 0
113 config_get_bool compressedCache 'config' 'compressed_cache' 0
114 config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
115 config_get bootDelay 'config' 'boot_delay' '120'
116 config_get dlTimeout 'config' 'download_timeout' '20'
117 config_get curlRetry 'config' 'curl_retry' '3'
118 config_get verbosity 'config' 'verbosity' '2'
119 config_get led 'config' 'led'
120 config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
121 config_get dnsInstance 'config' 'dns_instance' '0'
122 config_get whitelist_domains 'config' 'whitelist_domain'
123 config_get blacklist_domains 'config' 'blacklist_domain'
124 config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
125 config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
126 config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
128 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
129 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
130 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
131 targetDNS='dnsmasq.servers'
136 outputFilter="$addnhostsOutputFilter"
137 outputFile="$addnhostsFile"
138 outputCache="$addnhostsCache"
139 outputGzip="$addnhostsGzip"
140 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
141 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
142 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
143 rm -f "$serversFile" "$serversCache" "$serversGzip"
144 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
147 outputFilter="$dnsmasqOutputFilter"
148 outputFile="$dnsmasqFile"
149 outputCache="$dnsmasqCache"
150 outputGzip="$dnsmasqGzip"
151 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
152 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
153 rm -f "$serversFile" "$serversCache" "$serversGzip"
154 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
157 outputFilter="$ipsetOutputFilter"
158 outputFile="$ipsetFile"
159 outputCache="$ipsetCache"
160 outputGzip="$ipsetGzip"
161 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
162 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
163 rm -f "$serversFile" "$serversCache" "$serversGzip"
164 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
167 outputFilter="$serversOutputFilter"
168 outputFile="$serversFile"
169 outputCache="$serversCache"
170 outputGzip="$serversGzip"
171 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
172 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
173 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
174 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
177 outputFilter="$unboundOutputFilter"
178 outputFile="$unboundFile"
179 outputCache="$unboundCache"
180 outputGzip="$unboundGzip"
181 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
182 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
183 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
184 rm -f "$serversFile" "$serversCache" "$serversGzip"
187 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
190 . /lib/functions/network.sh
191 . /usr/share/libubox/jshn.sh
192 # Prefer curl because it supports the file: scheme.
193 if [ -x /usr/bin/curl ] ; then
194 dl_command="curl
--insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
197 dl_command="wget
--no-check-certificate --timeout $dlTimeout -q"
200 led="${led:+/sys/class/leds/$led}"
206 if [ "$debug" -ne 0 ]; then
207 exec 1>>/tmp/simple-adblock.log
212 if [ "$serviceEnabled" -eq 0 ]; then
215 output "$packageName is currently disabled.
\\n
"
216 output "Run the following commands before starting service again
:\\n
"
217 output "uci
set ${packageName}.config.enabled
='1'; uci commit
$packageName;\\n
"
224 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
225 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
237 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
238 output "$_ERROR_: DNSMASQ ipset support is enabled
in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets
!\\n
"
239 targetDNS='dnsmasq.servers'
241 if ! ipset help hash:net >/dev/null 2>&1; then
242 output "$_ERROR_: DNSMASQ ipset support is enabled
in $packageName, but ipset is either not installed or installed ipset does not support
'hash:net' type!\\n
"
243 targetDNS='dnsmasq.servers'
248 [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
249 [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
250 [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
251 cacheOps 'testGzip' && return 0
252 network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
253 [ -n "$wan_gw" ] && return 0
254 output "$_ERROR_: $serviceName failed to discover WAN gateway.
\\n
"; return 1;
258 local param output_text
261 if [ ! -s "$outputFile" ]; then
262 tmpfs set status "$statusFail"
263 tmpfs add error "Error
: Failed to create
$outputFile file.
"
264 output "$_ERROR_: $serviceName failed to create its data
file!\\n
"
269 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
270 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
272 if ! uci -q get dhcp.@dnsmasq["$dnsInstance"].addnhosts | grep -q "$addnhostsFile"; then
273 uci add_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
276 output_text='Reloading DNSMASQ'
279 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
280 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
281 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
283 param=dnsmasq_restart
284 output_text='Restarting DNSMASQ'
287 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
288 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
289 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
291 param=dnsmasq_restart
292 output_text='Restarting DNSMASQ'
295 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
296 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" != "$serversFile" ]; then
297 uci set dhcp.@dnsmasq["$dnsInstance"].serversfile="$serversFile"
300 output_text='Reloading DNSMASQ'
303 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
304 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
305 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
307 param=unbound_restart
308 output_text='Restarting Unbound'
311 if [ -n "$
(uci changes dhcp
)" ]; then
313 if [ "$param" = 'unbound_restart' ]; then
314 param='dnsmasq_restart; unbound_restart;'
315 output_text='Restarting Unbound/DNSMASQ'
317 param=dnsmasq_restart
318 output_text='Restarting DNSMASQ'
321 output 1 "$output_text "
322 output 2 "$output_text "
323 tmpfs set message "$output_text"
324 if eval "$param"; then
325 tmpfs set status "$statusSuccess"
330 tmpfs set status "$statusFail"
331 tmpfs add error "Error
: $output_text error.
"
332 output "$_ERROR_: $serviceName $output_text error
!\\n
"
339 dnsmasq.addnhosts | dnsmasq.servers)
342 dnsmasq.conf | dnsmasq.ipset)
343 param=dnsmasq_restart
346 param=unbound_restart
349 if [ -n "$
(uci changes dhcp
)" ]; then
351 if [ "$param" = 'unbound_restart' ]; then
352 param='dnsmasq_restart; unbound_restart;'
354 param=dnsmasq_restart
362 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
363 param=dnsmasq_restart
366 param=unbound_restart
376 local action="$1" instance="$2" value="$3"
377 local status message error stats
378 local readReload readRestart curReload curRestart ret
379 if [ -s "$jsonFile" ]; then
380 status="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['status']")"
381 message="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['message']")"
382 error="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['error']")"
383 stats="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['stats']")"
384 readReload="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['reload']")"
385 readRestart="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['restart']")"
391 echo "$status"; return;;
393 echo "$message"; return;;
395 echo "$error"; return;;
397 echo "$stats"; return;;
399 curReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
400 curRestart="$compressedCache $forceDNS $led"
401 if [ "$curReload" != "$readReload" ]; then
403 elif [ "$curRestart" != "$readRestart" ]; then
413 [ -n "$status" ] && status="$status $value" || status="$value";;
415 [ -n "$message" ] && message="${message} ${value}" || message="$value";;
417 [ -n "$error" ] && error="$error $value" || error="$value";;
419 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
439 unset readReload; unset readRestart;;
453 readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
454 readRestart="$compressedCache $forceDNS $led"
460 json_add_object 'data'
461 json_add_string version "$PKG_VERSION"
462 json_add_string status "$status"
463 json_add_string message "$message"
464 json_add_string error "$error"
465 json_add_string stats "$stats"
466 json_add_string reload "$readReload"
467 json_add_string restart "$readRestart"
469 json_dump > "$jsonFile"
477 [ -f "$outputFile" ] && mv "$outputFile" "$outputCache" >/dev/null 2>/dev/null
481 [ -f "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
485 [ -s "$outputCache" ]
489 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
493 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
494 if gzip < "$outputFile" > "$R_TMP"; then
495 if mv "$R_TMP" "$outputGzip"; then
506 expand|unpack|expandGzip|unpackGzip)
507 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
514 local action="$1" param="$2" _restart
516 reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
517 restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
520 dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
521 ipset) uci -q del firewall.simple_adblock_ipset
522 uci -q del firewall.simple_adblock_ipset_rule;;
524 uci -q del firewall.simple_adblock_dns_redirect
525 uci -q del firewall.simple_adblock_ipset
526 uci -q del firewall.simple_adblock_ipset_rule
533 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
534 uci -q set firewall.simple_adblock_dns_redirect=redirect
535 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
536 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
537 uci -q set firewall.simple_adblock_dns_redirect.src=lan
538 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
539 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
540 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
544 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
545 uci -q set firewall.simple_adblock_ipset=ipset
546 uci -q set firewall.simple_adblock_ipset.name=adb
547 uci -q set firewall.simple_adblock_ipset.match=dest_net
548 uci -q set firewall.simple_adblock_ipset.storage=hash
549 uci -q set firewall.simple_adblock_ipset.enabled=1
552 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
553 uci -q set firewall.simple_adblock_ipset_rule=rule
554 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
555 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
556 uci -q set firewall.simple_adblock_ipset_rule.src=lan
557 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
558 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
559 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
560 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
564 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
565 uci -q set firewall.simple_adblock_dns_redirect=redirect
566 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
567 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
568 uci -q set firewall.simple_adblock_dns_redirect.src=lan
569 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
570 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
571 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
573 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
574 uci -q set firewall.simple_adblock_ipset=ipset
575 uci -q set firewall.simple_adblock_ipset.name=adb
576 uci -q set firewall.simple_adblock_ipset.match=dest_net
577 uci -q set firewall.simple_adblock_ipset.storage=hash
578 uci -q set firewall.simple_adblock_ipset.enabled=1
581 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
582 uci -q set firewall.simple_adblock_ipset_rule=rule
583 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
584 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
585 uci -q set firewall.simple_adblock_ipset_rule.src=lan
586 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
587 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
588 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
589 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
594 if [ -n "$
(uci changes firewall
)" ]; then
595 uci -q commit firewall
596 if [ -z "$_restart" ]; then
605 local label type D_TMP R_TMP
606 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
607 label="${1##*//}"; label="${label%%/*}";
608 if [ "$2" = 'hosts' ]; then
609 label="Hosts
: $label"; filter="$hostsFilter";
611 label="Domains
: $label"; filter="$domainsFilter";
613 if [ "$3" = 'blocked' ]; then
614 type='Blocked'; D_TMP="$B_TMP";
616 type='Allowed'; D_TMP="$A_TMP";
618 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
619 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
621 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
623 output 2 "[DL
] $type $label $__FAIL__\\n
"
624 echo "Error
: downloading
'${1}'.
" >> "$sharedMemoryError"
626 sed -i "$filter" "$R_TMP"
627 if [ ! -s "$R_TMP" ]; then
629 output 2 "[DL
] $type $label $__FAIL__\\n
"
630 echo "Error
: parsing
'${1}'.
" >> "$sharedMemoryError"
632 cat "${R_TMP}" >> "$D_TMP"
634 output 2 "[DL
] $type $label $__OK__\\n
"
642 local hf w_filter j=0 R_TMP
644 tmpfs set message "${statusDownloading}...
"
645 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
646 if [ "$
(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
647 output 3 'Low free memory, restarting resolver... '
648 if reload_resolver 'quiet'; then
654 touch $A_TMP; touch $B_TMP;
655 output 1 'Downloading lists '
656 rm -f "$sharedMemoryError"
657 if [ -n "$blacklist_hosts_urls" ]; then
658 for hf in ${blacklist_hosts_urls}; do
659 if [ "$parallelDL" -gt 0 ]; then
660 process_url "$hf" 'hosts' 'blocked' &
662 process_url "$hf" 'hosts' 'blocked'
666 if [ -n "$blacklist_domains_urls" ]; then
667 for hf in ${blacklist_domains_urls}; do
668 if [ "$parallelDL" -gt 0 ]; then
669 process_url "$hf" 'domains' 'blocked' &
671 process_url "$hf" 'domains' 'blocked'
675 if [ -n "$whitelist_domains_urls" ]; then
676 for hf in ${whitelist_domains_urls}; do
677 if [ "$parallelDL" -gt 0 ]; then
678 process_url "$hf" 'domains' 'allowed' &
680 process_url "$hf" 'domains' 'allowed'
685 [ -s "$sharedMemoryError" ] && tmpfs add error "$
(cat "$sharedMemoryError")"
686 rm -f "$sharedMemoryError"
689 [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
690 whitelist_domains="${whitelist_domains}
692 [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
694 [ ! -s "$B_TMP" ] && return 1
696 output
1 'Processing downloads '
697 output
2 'Sorting combined list '
698 tmpfs
set message
"$statusProcessing: sorting combined list"
699 if [ "$allowIDN" -gt 0 ]; then
700 if sort -u "$B_TMP" > "$A_TMP"; then
704 tmpfs add error
'Error: Sorting error.'
707 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
711 tmpfs add error
'Error: Sorting error.'
715 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
716 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
717 [ "$targetDNS" = 'dnsmasq.servers' ] || \
718 [ "$targetDNS" = 'unbound.adb_list' ]; then
719 # TLD optimization written by Dirk Brenken (dev@brenken.org)
720 output
2 'Optimizing combined list '
721 tmpfs
set message
"$statusProcessing: optimizing combined list"
722 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
723 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
724 if sort "$B_TMP" > "$A_TMP"; then
725 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
726 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
727 if sort -u "$A_TMP" > "$B_TMP"; then
731 tmpfs add error
'Error: Data file optimization.'
736 tmpfs add error
'Error: Data file optimization.'
740 tmpfs add error
'Error: Data file optimization.'
745 tmpfs add error
'Error: Data file optimization.'
749 tmpfs add error
'Error: Data file optimization.'
756 output
2 'Whitelisting domains '
757 tmpfs
set message
"$statusProcessing: whitelisting domains"
758 if sed -i "$w_filter" "$B_TMP"; then
762 tmpfs add error
'Error: Whitelist processing.'
765 output
2 'Formatting merged file '
766 tmpfs
set message
"$statusProcessing: formatting merged file"
767 if [ -z "$outputFilterIPv6" ]; then
768 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
772 tmpfs add error
'Error: Data file formatting.'
777 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
778 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
782 tmpfs add error
'Error: Data file formatting.'
790 output
2 'Creating DNSMASQ addnhosts file '
791 tmpfs
set message
"$statusProcessing: creating DNSMASQ addnhosts file"
794 output
2 'Creating DNSMASQ config file '
795 tmpfs
set message
"$statusProcessing: creating DNSMASQ config file"
798 output
2 'Creating DNSMASQ ipset file '
799 tmpfs
set message
"$statusProcessing: creating DNSMASQ ipset file"
802 output
2 'Creating DNSMASQ servers file '
803 tmpfs
set message
"$statusProcessing: creating DNSMASQ servers file"
806 output
2 'Creating Unbound adb_list file '
807 tmpfs
set message
"$statusProcessing: creating Unbound adb_list file"
810 if mv "$A_TMP" "$outputFile"; then
814 tmpfs add error
"Error: moving data file '${A_TMP}' to '${outputFile}'."
816 if [ "$compressedCache" -gt 0 ]; then
817 output
2 'Creating compressed cache '
818 tmpfs
set message
"$statusProcessing: creating compressed cache"
819 if cacheOps
'createGzip'; then
823 tmpfs add error
'Error: creating compressed cache.'
828 output
2 'Removing temporary files '
829 tmpfs
set message
"$statusProcessing: removing temporary files"
830 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
831 if [ $j -eq 0 ]; then
835 tmpfs add error
'Error: removing temporary files.'
845 rc_procd start_service
&& rc_procd service_triggers
850 is_enabled
'on_start' ||
return 1
851 local action status error message stats
853 if is_chaos_calmer ||
! is_ipset_procd
; then
854 if [ "$forceDNS" -ne 0 ]; then
855 fw3Ops
'insert' 'dns_redirect'
857 fw3Ops
'remove' 'dns_redirect'
859 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
860 fw3Ops
'insert' 'ipset'
862 fw3Ops
'remove' 'ipset'
864 procd_open_instance
'main'
865 procd_set_param
command /bin
/true
866 procd_set_param stdout
1
867 procd_set_param stderr
1
870 procd_open_instance
'main'
871 procd_set_param
command /bin
/true
872 procd_set_param stdout
1
873 procd_set_param stderr
1
875 json_add_array firewall
876 if [ "$forceDNS" -ne 0 ]; then
878 json_add_string
type redirect
879 json_add_string name simple_adblock_dns_redirect
880 json_add_string target DNAT
881 json_add_string src lan
882 json_add_string proto tcpudp
883 json_add_string src_dport
53
884 json_add_string dest_port
53
885 json_add_string reflection
0
888 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
890 json_add_string
type ipset
891 json_add_string name adb
892 json_add_string match dest_net
893 json_add_string storage
hash
894 json_add_string enabled
1
897 json_add_string
type rule
898 json_add_string name simple_adblock_ipset_rule
899 json_add_string ipset adb
900 json_add_string src lan
901 json_add_string dest
'*'
902 json_add_string proto tcpudp
903 json_add_string target REJECT
904 json_add_string enabled
1
912 status
="$(tmpfs get status)"
913 error
="$(tmpfs get error)"
914 message
="$(tmpfs get message)"
915 stats
="$(tmpfs get stats)"
916 action
="$(tmpfs get triggers)"
919 download
) action
='download';;
921 if [ -s "$outputFile" ] && [ -n "$status" ]; then
924 elif [ ! -s "$outputFile" ] && ! cacheOps
'test' && ! cacheOps
'testGzip'; then
926 elif cacheOps
'test' || cacheOps
'testGzip'; then
929 if [ -n "$error" ]; then
932 action
="${action:-$1}"
941 if [ -s "$outputFile" ] || cacheOps
'test' || cacheOps
'testGzip'; then
942 output
0 "Force-reloading $serviceName... "
943 output
3 "Force-reloading $serviceName...\\n"
944 tmpfs
set status
"$statusForceReloading"
946 output
0 "Starting $serviceName... "
947 output
3 "Starting $serviceName...\\n"
948 tmpfs
set status
"$statusStarting"
951 reload_resolver
'on_start'
954 if [ "$action" = 'restart' ]; then
955 output
0 "Restarting $serviceName... "
956 output
3 "Restarting $serviceName...\\n"
957 tmpfs
set status
"$statusRestarting"
959 output
0 "Starting $serviceName... "
960 output
3 "Starting $serviceName...\\n"
961 tmpfs
set status
"$statusStarting"
963 if cacheOps
'testGzip' && ! cacheOps
'test' && [ ! -s "$outputFile" ]; then
964 output
3 'Found compressed cache file, unpacking it '
965 tmpfs
set message
'found compressed cache file, unpacking it.'
966 if cacheOps
'unpackGzip'; then
970 output
"$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
971 tmpfs add error
'Error: Failed to unpack compressed cache.'
975 if cacheOps
'test' && [ ! -s "$outputFile" ]; then
976 output
3 'Found cache file, reusing it '
977 tmpfs
set message
'found cache file, reusing it.'
978 if cacheOps
'restore'; then
982 tmpfs add error
"Error: moving '$outputCache' to '$outputFile'."
985 reload_resolver
'on_start'
989 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "$statusFail" ]; then
990 output
0 "$__OK__\\n";
991 c
="$(wc -l < "$outputFile")"
992 output
3 "$serviceName is blocking $c domains (with ${targetDNS}) "; output_okn
994 tmpfs
set status
"$statusSuccess: $c domains blocked (with ${targetDNS})."
995 error
="$(tmpfs get error)"
996 if [ -n "$error" ]; then
997 output
"$(str_replace "$error" "Error
:" "$_ERROR_:")\\n"
1000 output
0 "$__FAIL__\\n";
1001 tmpfs
set status
"$statusFail"
1002 tmpfs add error
'Error: Failed to create blocklist.'
1006 output
3 "$serviceName: another instance is starting up "; output_fail
1011 service_started
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1012 service_stopped
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1013 restart_service
() { rc_procd start_service
'restart'; }
1014 reload_service
() { restart_service
; }
1015 restart
() { restart_service
; }
1016 reload
() { restart_service
; }
1017 dl
() { rc_procd start_service
'download'; }
1020 rm -f "$addnhostsCache" "$addnhostsGzip"
1021 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1022 rm -f "$ipsetCache" "$ipsetGzip"
1023 rm -f "$serversCache" "$serversGzip"
1024 rm -f "$unboundCache" "$unboundGzip"
1029 local status
="$(tmpfs get status)" error
="$(tmpfs get error)" message
="$(tmpfs get message)"
1030 if [ -n "$status" ] && [ -n "$message" ]; then
1031 status
="${status}: $message"
1033 [ -n "$status" ] && output
"$serviceName $status\\n"
1034 [ -n "$error" ] && output
"$error\\n"
1039 fw3Ops
'remove' 'all'
1040 if [ -s "$outputFile" ]; then
1041 output
"Stopping $serviceName... "
1043 if reload_resolver
'on_stop'; then
1045 output
0 "$__OK__\\n"; output_okn
1046 tmpfs
set status
"$statusStopped"
1049 output
0 "$__FAIL__\\n"; output_fail
1050 tmpfs
set status
"$statusFail"
1051 tmpfs add error
"Error: error stopping $serviceName."
1052 output
"$_ERROR_: error stopping $serviceName!\\n"
1060 local c
="$(grep -c "$string" "$outputFile")"
1061 if [ ! -f "$outputFile" ]; then
1062 echo "No blacklist ('$outputFile') found."
1063 elif [ -z "$string" ]; then
1064 echo "Usage: /etc/init.d/${packageName} check string"
1065 elif [ "$c" -gt 0 ]; then
1066 if [ "$c" -gt 1 ]; then
1067 echo "Found $c matches for '$string' in '$outputFile':"
1069 echo "Found 1 match for '$string' in '$outputFile':"
1071 case "$targetDNS" in
1073 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1075 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1077 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1079 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1081 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1084 echo "The $string is not found in current blacklist ('$outputFile')."