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 sizes status'
12 export EXTRA_HELP
=' check Checks if specified domain is found in current blacklist
13 dl Force-redownloads all the list
14 sizes Shows the file-sizes of enabled block-lists (by downloading them one by one)
15 status Shows the service last-run status'
17 readonly packageName
='simple-adblock'
18 readonly serviceName
="$packageName $PKG_VERSION"
19 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
20 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
21 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
22 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
23 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
24 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
25 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
26 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
27 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
28 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
29 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
30 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
31 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
32 readonly serversFile
="/var/run/${packageName}.servers"
33 readonly serversCache
="/var/run/${packageName}.servers.cache"
34 readonly serversGzip
="/etc/${packageName}.servers.gz"
35 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
36 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
37 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
38 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
39 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
40 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
41 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
42 readonly PIDFile
="/var/run/${packageName}.pid"
43 readonly jsonFile
="/var/run/${packageName}.json"
44 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;'
45 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
46 readonly checkmark
='\xe2\x9c\x93'
47 readonly xmark
='\xe2\x9c\x97'
48 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
49 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
50 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
51 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
52 readonly _ERROR_
='\033[0;31mERROR\033[0m'
53 readonly statusSuccess
='Success'
54 readonly statusFail
='Fail'
55 readonly statusDownloading
='Downloading'
56 readonly statusReloading
='Reloading'
57 readonly statusRestarting
='Restarting'
58 readonly statusStarting
='Starting'
59 readonly statusForceReloading
='Force-Reloading'
60 readonly statusProcessing
='Processing'
61 readonly statusStopped
='Stopped'
62 readonly sharedMemoryError
="/dev/shm/$packageName-error"
63 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
65 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
66 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
68 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
69 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
70 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
71 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
72 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
73 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
74 compare_versions
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
75 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
76 is_ipset_procd
() { compare_versions
"$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
77 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
78 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
79 dnsmasq_hup
() { killall
-q -HUP dnsmasq
; }
80 dnsmasq_kill
() { killall
-q -KILL dnsmasq
; }
81 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
82 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
85 # Can take a single parameter (text) to be output at any verbosity
86 # Or target verbosity level and text to be output at specifc verbosity
89 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
91 [ -t 1 ] && printf "%b" "$1"
92 msg
="$(printf "%s
" "$
(str_replace
"$1" "$serviceName " "service ")" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g')";
93 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
94 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
95 logger
-t "${packageName:-service} [$$]" "$(printf "%b" "${memmsg}${msg}")"
96 rm -f "$sharedMemoryOutput"
98 printf "%b
" "$msg" >> "$sharedMemoryOutput"
102 export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache
103 export targetDNS bootDelay dlTimeout curlRetry verbosity led dnsInstance
104 export whitelist_domains blacklist_domains
105 export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
106 export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
107 export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
109 load_package_config() {
110 config_load "$packageName"
111 config_get_bool serviceEnabled 'config' 'enabled' 1
112 config_get_bool forceDNS 'config' 'force_dns' 1
113 config_get_bool parallelDL 'config' 'parallel_downloads' 1
114 config_get_bool debug 'config' 'debug' 0
115 config_get_bool compressedCache 'config' 'compressed_cache' 0
116 config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
117 config_get bootDelay 'config' 'boot_delay' '120'
118 config_get dlTimeout 'config' 'download_timeout' '20'
119 config_get curlRetry 'config' 'curl_retry' '3'
120 config_get verbosity 'config' 'verbosity' '2'
121 config_get led 'config' 'led'
122 config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
123 config_get dnsInstance 'config' 'dns_instance' '0'
124 config_get whitelist_domains 'config' 'whitelist_domain'
125 config_get blacklist_domains 'config' 'blacklist_domain'
126 config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
127 config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
128 config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
130 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
131 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
132 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
133 targetDNS='dnsmasq.servers'
138 outputFilter="$addnhostsOutputFilter"
139 outputFile="$addnhostsFile"
140 outputCache="$addnhostsCache"
141 outputGzip="$addnhostsGzip"
142 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
143 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
144 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
145 rm -f "$serversFile" "$serversCache" "$serversGzip"
146 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
149 outputFilter="$dnsmasqOutputFilter"
150 outputFile="$dnsmasqFile"
151 outputCache="$dnsmasqCache"
152 outputGzip="$dnsmasqGzip"
153 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
154 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
155 rm -f "$serversFile" "$serversCache" "$serversGzip"
156 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
159 outputFilter="$ipsetOutputFilter"
160 outputFile="$ipsetFile"
161 outputCache="$ipsetCache"
162 outputGzip="$ipsetGzip"
163 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
164 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
165 rm -f "$serversFile" "$serversCache" "$serversGzip"
166 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
169 outputFilter="$serversOutputFilter"
170 outputFile="$serversFile"
171 outputCache="$serversCache"
172 outputGzip="$serversGzip"
173 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
174 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
175 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
176 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
179 outputFilter="$unboundOutputFilter"
180 outputFile="$unboundFile"
181 outputCache="$unboundCache"
182 outputGzip="$unboundGzip"
183 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
184 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
185 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
186 rm -f "$serversFile" "$serversCache" "$serversGzip"
189 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
192 . /lib/functions/network.sh
193 . /usr/share/libubox/jshn.sh
194 # Prefer curl because it supports the file: scheme.
195 if [ -x /usr/bin/curl ] ; then
196 dl_command="curl
--insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
199 dl_command="wget
--no-check-certificate --timeout $dlTimeout -q"
202 led="${led:+/sys/class/leds/$led}"
208 if [ "$debug" -ne 0 ]; then
209 exec 1>>/tmp/simple-adblock.log
214 if [ "$serviceEnabled" -eq 0 ]; then
217 output "$packageName is currently disabled.
\\n
"
218 output "Run the following commands before starting service again
:\\n
"
219 output "uci
set ${packageName}.config.enabled
='1'; uci commit
$packageName;\\n
"
226 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
227 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
239 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
240 output "$_ERROR_: DNSMASQ ipset support is enabled
in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets
!\\n
"
241 targetDNS='dnsmasq.servers'
243 if ! ipset help hash:net >/dev/null 2>&1; then
244 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
"
245 targetDNS='dnsmasq.servers'
250 [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
251 [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
252 [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
253 cacheOps 'testGzip' && return 0
254 network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
255 [ -n "$wan_gw" ] && return 0
256 output "$_ERROR_: $serviceName failed to discover WAN gateway.
\\n
"; return 1;
260 local param output_text
263 if [ ! -s "$outputFile" ]; then
264 tmpfs set status "$statusFail"
265 tmpfs add error "Error
: Failed to create
$outputFile file.
"
266 output "$_ERROR_: $serviceName failed to create its data
file!\\n
"
271 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
272 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
274 if ! uci -q get dhcp.@dnsmasq["$dnsInstance"].addnhosts | grep -q "$addnhostsFile"; then
275 uci add_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
278 output_text='Reloading DNSMASQ'
281 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
282 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
283 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
285 param=dnsmasq_restart
286 output_text='Restarting DNSMASQ'
289 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
290 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
291 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
293 param=dnsmasq_restart
294 output_text='Restarting DNSMASQ'
297 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
298 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" != "$serversFile" ]; then
299 uci set dhcp.@dnsmasq["$dnsInstance"].serversfile="$serversFile"
302 output_text='Reloading DNSMASQ'
305 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
306 if [ "$
(uci
-q get dhcp.@dnsmasq
["$dnsInstance"].serversfile
)" = "$serversFile" ]; then
307 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
309 param=unbound_restart
310 output_text='Restarting Unbound'
313 if [ -n "$
(uci changes dhcp
)" ]; then
315 if [ "$param" = 'unbound_restart' ]; then
316 param='dnsmasq_restart; unbound_restart;'
317 output_text='Restarting Unbound/DNSMASQ'
319 param=dnsmasq_restart
320 output_text='Restarting DNSMASQ'
323 output 1 "$output_text "
324 output 2 "$output_text "
325 tmpfs set message "$output_text"
326 if eval "$param"; then
327 tmpfs set status "$statusSuccess"
332 tmpfs set status "$statusFail"
333 tmpfs add error "Error
: $output_text error.
"
334 output "$_ERROR_: $serviceName $output_text error
!\\n
"
340 dnsmasq.addnhosts | dnsmasq.servers)
343 dnsmasq.conf | dnsmasq.ipset)
344 param=dnsmasq_restart
347 param=unbound_restart
350 if [ -n "$
(uci changes dhcp
)" ]; then
352 if [ "$param" = 'unbound_restart' ]; then
353 param='dnsmasq_restart; unbound_restart;'
355 param=dnsmasq_restart
363 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
364 param=dnsmasq_restart
367 param=unbound_restart
377 local action="$1" instance="$2" value="$3"
378 local status message error stats
379 local readReload readRestart curReload curRestart ret
380 if [ -s "$jsonFile" ]; then
381 status="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['status']")"
382 message="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['message']")"
383 error="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['error']")"
384 stats="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['stats']")"
385 readReload="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['reload']")"
386 readRestart="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['restart']")"
392 echo "$status"; return;;
394 echo "$message"; return;;
396 echo "$error"; return;;
398 echo "$stats"; return;;
400 curReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
401 curRestart="$compressedCache $forceDNS $led"
402 if [ "$curReload" != "$readReload" ]; then
404 elif [ "$curRestart" != "$readRestart" ]; then
414 [ -n "$status" ] && status="$status $value" || status="$value";;
416 [ -n "$message" ] && message="${message} ${value}" || message="$value";;
418 [ -n "$error" ] && error="$error $value" || error="$value";;
420 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
440 unset readReload; unset readRestart;;
454 readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
455 readRestart="$compressedCache $forceDNS $led"
461 json_add_object 'data'
462 json_add_string version "$PKG_VERSION"
463 json_add_string status "$status"
464 json_add_string message "$message"
465 json_add_string error "$error"
466 json_add_string stats "$stats"
467 json_add_string reload "$readReload"
468 json_add_string restart "$readRestart"
470 json_dump > "$jsonFile"
478 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
482 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
486 [ -s "$outputCache" ]
490 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
494 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
495 if gzip < "$outputFile" > "$R_TMP"; then
496 if mv "$R_TMP" "$outputGzip"; then
507 expand|unpack|expandGzip|unpackGzip)
508 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
515 local action="$1" param="$2" _restart
517 reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
518 restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
521 dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
522 ipset) uci -q del firewall.simple_adblock_ipset
523 uci -q del firewall.simple_adblock_ipset_rule;;
525 uci -q del firewall.simple_adblock_dns_redirect
526 uci -q del firewall.simple_adblock_ipset
527 uci -q del firewall.simple_adblock_ipset_rule
534 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
535 uci -q set firewall.simple_adblock_dns_redirect=redirect
536 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
537 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
538 uci -q set firewall.simple_adblock_dns_redirect.src=lan
539 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
540 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
541 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
545 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
546 uci -q set firewall.simple_adblock_ipset=ipset
547 uci -q set firewall.simple_adblock_ipset.name=adb
548 uci -q set firewall.simple_adblock_ipset.match=dest_net
549 uci -q set firewall.simple_adblock_ipset.storage=hash
550 uci -q set firewall.simple_adblock_ipset.enabled=1
553 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
554 uci -q set firewall.simple_adblock_ipset_rule=rule
555 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
556 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
557 uci -q set firewall.simple_adblock_ipset_rule.src=lan
558 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
559 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
560 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
561 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
565 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
566 uci -q set firewall.simple_adblock_dns_redirect=redirect
567 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
568 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
569 uci -q set firewall.simple_adblock_dns_redirect.src=lan
570 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
571 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
572 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
574 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
575 uci -q set firewall.simple_adblock_ipset=ipset
576 uci -q set firewall.simple_adblock_ipset.name=adb
577 uci -q set firewall.simple_adblock_ipset.match=dest_net
578 uci -q set firewall.simple_adblock_ipset.storage=hash
579 uci -q set firewall.simple_adblock_ipset.enabled=1
582 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
583 uci -q set firewall.simple_adblock_ipset_rule=rule
584 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
585 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
586 uci -q set firewall.simple_adblock_ipset_rule.src=lan
587 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
588 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
589 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
590 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
595 if [ -n "$
(uci changes firewall
)" ]; then
596 uci -q commit firewall
597 if [ -z "$_restart" ]; then
606 local label type D_TMP R_TMP
607 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
608 label="${1##*//}"; label="${label%%/*}";
609 if [ "$2" = 'hosts' ]; then
610 label="Hosts
: $label"; filter="$hostsFilter";
612 label="Domains
: $label"; filter="$domainsFilter";
614 if [ "$3" = 'blocked' ]; then
615 type='Blocked'; D_TMP="$B_TMP";
617 type='Allowed'; D_TMP="$A_TMP";
619 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
620 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
622 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
624 output 2 "[DL
] $type $label $__FAIL__\\n
"
625 echo "Error
: downloading
'${1}'.
" >> "$sharedMemoryError"
627 sed -i "$filter" "$R_TMP"
628 if [ ! -s "$R_TMP" ]; then
630 output 2 "[DL
] $type $label $__FAIL__\\n
"
631 echo "Error
: parsing
'${1}'.
" >> "$sharedMemoryError"
633 cat "${R_TMP}" >> "$D_TMP"
635 output 2 "[DL
] $type $label $__OK__\\n
"
643 local hf w_filter j=0 R_TMP
645 tmpfs set message "${statusDownloading}...
"
646 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
647 if [ "$
(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
648 output 3 'Low free memory, restarting resolver... '
649 if dnsOps 'quiet'; then
655 touch $A_TMP; touch $B_TMP;
656 output 1 'Downloading lists '
657 rm -f "$sharedMemoryError"
658 if [ -n "$blacklist_hosts_urls" ]; then
659 for hf in ${blacklist_hosts_urls}; do
660 if [ "$parallelDL" -gt 0 ]; then
661 process_url "$hf" 'hosts' 'blocked' &
663 process_url "$hf" 'hosts' 'blocked'
667 if [ -n "$blacklist_domains_urls" ]; then
668 for hf in ${blacklist_domains_urls}; do
669 if [ "$parallelDL" -gt 0 ]; then
670 process_url "$hf" 'domains' 'blocked' &
672 process_url "$hf" 'domains' 'blocked'
676 if [ -n "$whitelist_domains_urls" ]; then
677 for hf in ${whitelist_domains_urls}; do
678 if [ "$parallelDL" -gt 0 ]; then
679 process_url "$hf" 'domains' 'allowed' &
681 process_url "$hf" 'domains' 'allowed'
686 [ -s "$sharedMemoryError" ] && tmpfs add error "$
(cat "$sharedMemoryError")"
687 rm -f "$sharedMemoryError"
690 [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
691 whitelist_domains="${whitelist_domains}
693 [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
695 [ ! -s "$B_TMP" ] && return 1
697 output
1 'Processing downloads '
698 output
2 'Sorting combined list '
699 tmpfs
set message
"$statusProcessing: sorting combined list"
700 if [ "$allowIDN" -gt 0 ]; then
701 if sort -u "$B_TMP" > "$A_TMP"; then
705 tmpfs add error
'Error: Sorting error.'
708 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
712 tmpfs add error
'Error: Sorting error.'
716 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
717 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
718 [ "$targetDNS" = 'dnsmasq.servers' ] || \
719 [ "$targetDNS" = 'unbound.adb_list' ]; then
720 # TLD optimization written by Dirk Brenken (dev@brenken.org)
721 output
2 'Optimizing combined list '
722 tmpfs
set message
"$statusProcessing: optimizing combined list"
723 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
724 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
725 if sort "$B_TMP" > "$A_TMP"; then
726 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
727 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
728 if sort -u "$A_TMP" > "$B_TMP"; then
732 tmpfs add error
'Error: Data file optimization.'
737 tmpfs add error
'Error: Data file optimization.'
741 tmpfs add error
'Error: Data file optimization.'
746 tmpfs add error
'Error: Data file optimization.'
750 tmpfs add error
'Error: Data file optimization.'
757 output
2 'Whitelisting domains '
758 tmpfs
set message
"$statusProcessing: whitelisting domains"
759 if sed -i "$w_filter" "$B_TMP"; then
763 tmpfs add error
'Error: Whitelist processing.'
766 output
2 'Formatting merged file '
767 tmpfs
set message
"$statusProcessing: formatting merged file"
768 if [ -z "$outputFilterIPv6" ]; then
769 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
773 tmpfs add error
'Error: Data file formatting.'
778 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
779 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
783 tmpfs add error
'Error: Data file formatting.'
791 output
2 'Creating DNSMASQ addnhosts file '
792 tmpfs
set message
"$statusProcessing: creating DNSMASQ addnhosts file"
795 output
2 'Creating DNSMASQ config file '
796 tmpfs
set message
"$statusProcessing: creating DNSMASQ config file"
799 output
2 'Creating DNSMASQ ipset file '
800 tmpfs
set message
"$statusProcessing: creating DNSMASQ ipset file"
803 output
2 'Creating DNSMASQ servers file '
804 tmpfs
set message
"$statusProcessing: creating DNSMASQ servers file"
807 output
2 'Creating Unbound adb_list file '
808 tmpfs
set message
"$statusProcessing: creating Unbound adb_list file"
811 if mv "$A_TMP" "$outputFile"; then
815 tmpfs add error
"Error: moving data file '${A_TMP}' to '${outputFile}'."
817 if [ "$compressedCache" -gt 0 ]; then
818 output
2 'Creating compressed cache '
819 tmpfs
set message
"$statusProcessing: creating compressed cache"
820 if cacheOps
'createGzip'; then
824 tmpfs add error
'Error: creating compressed cache.'
829 output
2 'Removing temporary files '
830 tmpfs
set message
"$statusProcessing: removing temporary files"
831 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
832 if [ $j -eq 0 ]; then
836 tmpfs add error
'Error: removing temporary files.'
846 rc_procd start_service
&& rc_procd service_triggers
851 is_enabled
'on_start' ||
return 1
852 local action status error message stats
853 if ! create_lock
; then
854 output
3 "$serviceName: another instance is starting up "; output_fail
858 status
="$(tmpfs get status)"
859 error
="$(tmpfs get error)"
860 message
="$(tmpfs get message)"
861 stats
="$(tmpfs get stats)"
862 action
="$(tmpfs get triggers)"
864 if [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
866 elif [ ! -s "$outputFile" ] && ! cacheOps
'test' && ! cacheOps
'testGzip'; then
868 elif [ ! -s "$outputFile" ] && cacheOps
'testGzip' || cacheOps
'test'; then
870 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
872 elif [ -s "$outputFile" ] && [ -n "$status" ] && [ -z "$error" ]; then
873 if [ "$1" != 'hotplug' ]; then status
; fi
882 if is_chaos_calmer ||
! is_ipset_procd
; then
883 if [ "$forceDNS" -ne 0 ]; then
884 fw3Ops
'insert' 'dns_redirect'
886 fw3Ops
'remove' 'dns_redirect'
888 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
889 fw3Ops
'insert' 'ipset'
891 fw3Ops
'remove' 'ipset'
893 procd_open_instance
'main'
894 procd_set_param
command /bin
/true
895 procd_set_param stdout
1
896 procd_set_param stderr
1
899 procd_open_instance
'main'
900 procd_set_param
command /bin
/true
901 procd_set_param stdout
1
902 procd_set_param stderr
1
904 json_add_array firewall
905 if [ "$forceDNS" -ne 0 ]; then
907 json_add_string
type redirect
908 json_add_string name simple_adblock_dns_redirect
909 json_add_string target DNAT
910 json_add_string src lan
911 json_add_string proto tcpudp
912 json_add_string src_dport
53
913 json_add_string dest_port
53
914 json_add_string reflection
0
917 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
919 json_add_string
type ipset
920 json_add_string name adb
921 json_add_string match dest_net
922 json_add_string storage
hash
923 json_add_string enabled
1
926 json_add_string
type rule
927 json_add_string name simple_adblock_ipset_rule
928 json_add_string ipset adb
929 json_add_string src lan
930 json_add_string dest
'*'
931 json_add_string proto tcpudp
932 json_add_string target REJECT
933 json_add_string enabled
1
941 if [ "$action" = 'restore' ]; then
942 output
0 "Starting $serviceName... "
943 output
3 "Starting $serviceName...\\n"
944 tmpfs
set status
"$statusStarting"
945 if cacheOps
'testGzip' && ! cacheOps
'test' && [ ! -s "$outputFile" ]; then
946 output
3 'Found compressed cache file, unpacking it '
947 tmpfs
set message
'found compressed cache file, unpacking it.'
948 if cacheOps
'unpackGzip'; then
952 output
"$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
956 if cacheOps
'test' && [ ! -s "$outputFile" ]; then
957 output
3 'Found cache file, reusing it '
958 tmpfs
set message
'found cache file, reusing it.'
959 if cacheOps
'restore'; then
964 output
"$_ERROR_: $serviceName failed to move '$outputCache' to '$outputFile'!\\n"
971 if [ -s "$outputFile" ] || cacheOps
'test' || cacheOps
'testGzip'; then
972 output
0 "Force-reloading $serviceName... "
973 output
3 "Force-reloading $serviceName...\\n"
974 tmpfs
set status
"$statusForceReloading"
976 output
0 "Starting $serviceName... "
977 output
3 "Starting $serviceName...\\n"
978 tmpfs
set status
"$statusStarting"
984 output
0 "Restarting $serviceName... "
985 output
3 "Restarting $serviceName...\\n"
986 tmpfs
set status
"$statusRestarting"
990 output
0 "Starting $serviceName... "
991 output
3 "Starting $serviceName...\\n"
992 tmpfs
set status
"$statusStarting"
996 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "$statusFail" ]; then
997 output
0 "$__OK__\\n";
998 c
="$(wc -l < "$outputFile")"
999 output
3 "$serviceName is blocking $c domains (with ${targetDNS}) "; output_okn
1001 tmpfs
set status
"$statusSuccess: $c domains blocked (with ${targetDNS})."
1002 error
="$(tmpfs get error)"
1003 if [ -n "$error" ]; then
1004 output
"$(str_replace "$error" "Error
:" "$_ERROR_:")\\n"
1007 output
0 "$__FAIL__\\n";
1008 tmpfs
set status
"$statusFail"
1009 tmpfs add error
'Error: Failed to create blocklist or restart DNS resolver.'
1014 service_started
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1015 service_stopped
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1016 restart_service
() { rc_procd start_service
'restart'; }
1017 reload_service
() { restart_service
; }
1018 restart
() { restart_service
; }
1019 reload
() { restart_service
; }
1020 dl
() { rc_procd start_service
'download'; }
1023 rm -f "$addnhostsCache" "$addnhostsGzip"
1024 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1025 rm -f "$ipsetCache" "$ipsetGzip"
1026 rm -f "$serversCache" "$serversGzip"
1027 rm -f "$unboundCache" "$unboundGzip"
1032 local status
="$(tmpfs get status)" error
="$(tmpfs get error)" message
="$(tmpfs get message)"
1033 if [ -n "$status" ] && [ -n "$message" ]; then
1034 status
="${status}: $message"
1036 [ -n "$status" ] && output
"$serviceName $status\\n"
1037 [ -n "$error" ] && output
"$error\\n"
1042 fw3Ops
'remove' 'all'
1043 if [ -s "$outputFile" ]; then
1044 output
"Stopping $serviceName... "
1046 if dnsOps
'on_stop'; then
1048 output
0 "$__OK__\\n"; output_okn
1049 tmpfs
set status
"$statusStopped"
1052 output
0 "$__FAIL__\\n"; output_fail
1053 tmpfs
set status
"$statusFail"
1054 tmpfs add error
"Error: error stopping $serviceName."
1055 output
"$_ERROR_: error stopping $serviceName!\\n"
1063 local c
="$(grep -c "$string" "$outputFile")"
1064 if [ ! -s "$outputFile" ]; then
1065 echo "No blacklist ('$outputFile') found."
1066 elif [ -z "$string" ]; then
1067 echo "Usage: /etc/init.d/${packageName} check string"
1068 elif [ "$c" -gt 0 ]; then
1069 if [ "$c" -gt 1 ]; then
1070 echo "Found $c matches for '$string' in '$outputFile':"
1072 echo "Found 1 match for '$string' in '$outputFile':"
1074 case "$targetDNS" in
1076 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1078 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1080 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1082 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1084 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1087 echo "The $string is not found in current blacklist ('$outputFile')."
1096 for i
in $blacklist_domains_urls; do
1097 [ "${i//melmac}" != "$i" ] && continue
1098 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1099 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1100 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1101 echo "# blocklist too big for most routers"
1102 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1103 echo "# blocklist may be too big for some routers"
1106 echo " list blacklist_domains_url '$i'"
1109 echo "# site was down on last check"
1110 echo "# list blacklist_domains_url '$i'"
1115 for i
in $blacklist_hosts_urls; do
1116 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1117 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1118 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1119 echo "# blocklist too big for most routers"
1120 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1121 echo "# blocklist may be too big for some routers"
1124 echo " list blacklist_hosts_url '$i'"
1127 echo "# site was down on last check"
1128 echo "# list blacklist_hosts_url '$i'"