simple-adblock: bugfix: proper dnsmasq reload on stop, rework start/stop logic
[feed/packages.git] / net / simple-adblock / files / simple-adblock.init
1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039
4 # shellcheck disable=SC1091
5 PKG_VERSION='dev-test'
6
7 export START=94
8 export USE_PROCD=1
9 export LC_ALL=C
10
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'
16
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"
64
65 create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
66 remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
67 trap remove_lock EXIT
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; }
83
84 output() {
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
87 local msg memmsg
88 if [ $# -ne 1 ]; then
89 if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
90 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"
97 else
98 printf "%b" "$msg" >> "$sharedMemoryOutput"
99 fi
100 }
101
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
108
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'
129
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'
134 fi
135
136 case "$targetDNS" in
137 dnsmasq.addnhosts)
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"
147 ;;
148 dnsmasq.conf)
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"
157 ;;
158 dnsmasq.ipset)
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"
167 ;;
168 dnsmasq.servers)
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"
177 ;;
178 unbound.adb_list)
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"
187 ;;
188 esac
189 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
190 verbosity=1
191 fi
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"
197 dl_flag="-o"
198 else
199 dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
200 dl_flag="-O"
201 fi
202 led="${led:+/sys/class/leds/$led}"
203 }
204
205 is_enabled() {
206 load_package_config
207
208 if [ "$debug" -ne 0 ]; then
209 exec 1>>/tmp/simple-adblock.log
210 exec 2>&1
211 set -x
212 fi
213
214 if [ "$serviceEnabled" -eq 0 ]; then
215 case "$1" in
216 on_start)
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"
220 ;;
221 esac
222 return 1
223 fi
224
225 case $targetDNS in
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
228 allowIDN=0
229 else
230 allowIDN=1
231 fi
232 ;;
233 unbound.adb_list)
234 allowIDN=1;;
235 esac
236
237 case $targetDNS in
238 dnsmasq.ipset)
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'
242 fi
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'
246 fi
247 ;;
248 esac
249
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;
257 }
258
259 dnsOps() {
260 local param output_text
261 case $1 in
262 on_start)
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"
267 return 1
268 fi
269 case "$targetDNS" in
270 dnsmasq.addnhosts)
271 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
272 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
273 fi
274 if ! uci -q get dhcp.@dnsmasq["$dnsInstance"].addnhosts | grep -q "$addnhostsFile"; then
275 uci add_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
276 fi
277 param=dnsmasq_hup
278 output_text='Reloading DNSMASQ'
279 ;;
280 dnsmasq.conf)
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
284 fi
285 param=dnsmasq_restart
286 output_text='Restarting DNSMASQ'
287 ;;
288 dnsmasq.ipset)
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
292 fi
293 param=dnsmasq_restart
294 output_text='Restarting DNSMASQ'
295 ;;
296 dnsmasq.servers)
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"
300 fi
301 param=dnsmasq_hup
302 output_text='Reloading DNSMASQ'
303 ;;
304 unbound.adb_list)
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
308 fi
309 param=unbound_restart
310 output_text='Restarting Unbound'
311 ;;
312 esac
313 if [ -n "$(uci changes dhcp)" ]; then
314 uci commit dhcp
315 if [ "$param" = 'unbound_restart' ]; then
316 param='dnsmasq_restart; unbound_restart;'
317 output_text='Restarting Unbound/DNSMASQ'
318 else
319 param=dnsmasq_restart
320 output_text='Restarting DNSMASQ'
321 fi
322 fi
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"
328 led_on "$led"
329 output_okn
330 else
331 output_fail
332 tmpfs set status "$statusFail"
333 tmpfs add error "Error: $output_text error."
334 output "$_ERROR_: $serviceName $output_text error!\\n"
335 return 1
336 fi
337 ;;
338 on_stop)
339 case "$targetDNS" in
340 dnsmasq.addnhosts | dnsmasq.servers)
341 param=dnsmasq_hup
342 ;;
343 dnsmasq.conf | dnsmasq.ipset)
344 param=dnsmasq_restart
345 ;;
346 unbound.adb_list)
347 param=unbound_restart
348 ;;
349 esac
350 if [ -n "$(uci changes dhcp)" ]; then
351 uci -q commit dhcp
352 if [ "$param" = 'unbound_restart' ]; then
353 param='dnsmasq_restart; unbound_restart;'
354 else
355 param=dnsmasq_restart
356 fi
357 fi
358 eval "$param"
359 return $?
360 ;;
361 quiet)
362 case "$targetDNS" in
363 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
364 param=dnsmasq_restart
365 ;;
366 unbound.adb_list)
367 param=unbound_restart
368 ;;
369 esac
370 eval "$param"
371 return $?
372 ;;
373 esac
374 }
375
376 tmpfs() {
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']")"
387 fi
388 case "$action" in
389 get)
390 case "$instance" in
391 status)
392 echo "$status"; return;;
393 message)
394 echo "$message"; return;;
395 error)
396 echo "$error"; return;;
397 stats)
398 echo "$stats"; return;;
399 triggers)
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
403 ret='download'
404 elif [ "$curRestart" != "$readRestart" ]; then
405 ret='restart'
406 fi
407 echo "$ret"
408 return;;
409 esac
410 ;;
411 add)
412 case "$instance" in
413 status)
414 [ -n "$status" ] && status="$status $value" || status="$value";;
415 message)
416 [ -n "$message" ] && message="${message} ${value}" || message="$value";;
417 error)
418 [ -n "$error" ] && error="$error $value" || error="$value";;
419 stats)
420 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
421 esac
422 ;;
423 del)
424 case "$instance" in
425 all)
426 unset status;
427 unset message;
428 unset error;
429 unset stats;
430 ;;
431 status)
432 unset status;;
433 message)
434 unset message;;
435 error)
436 unset error;;
437 stats)
438 unset stats;;
439 triggers)
440 unset readReload; unset readRestart;;
441 esac
442 ;;
443 set)
444 case "$instance" in
445 status)
446 status="$value";;
447 message)
448 message="$value";;
449 error)
450 error="$value";;
451 stats)
452 stats="$value";;
453 triggers)
454 readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
455 readRestart="$compressedCache $forceDNS $led"
456 ;;
457 esac
458 ;;
459 esac
460 json_init
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"
469 json_close_object
470 json_dump > "$jsonFile"
471 sync
472 }
473
474 cacheOps() {
475 local R_TMP
476 case "$1" in
477 create|backup)
478 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
479 return $?
480 ;;
481 restore|use)
482 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
483 return $?
484 ;;
485 test)
486 [ -s "$outputCache" ]
487 return $?
488 ;;
489 testGzip)
490 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
491 return $?
492 ;;
493 createGzip)
494 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
495 if gzip < "$outputFile" > "$R_TMP"; then
496 if mv "$R_TMP" "$outputGzip"; then
497 rm -f "$R_TMP"
498 return 0
499 else
500 rm -f "$R_TMP"
501 return 1
502 fi
503 else
504 return 1
505 fi
506 ;;
507 expand|unpack|expandGzip|unpackGzip)
508 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
509 return $?
510 ;;
511 esac
512 }
513
514 fw3Ops() {
515 local action="$1" param="$2" _restart
516 case "$action" in
517 reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
518 restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
519 remove)
520 case "$param" in
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;;
524 *)
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
528 ;;
529 esac
530 ;;
531 insert)
532 case "$param" in
533 dns_redirect)
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
542 fi
543 ;;
544 ipset)
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
551 _restart=1
552 fi
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
562 fi
563 ;;
564 *)
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
573 fi
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
580 _restart=1
581 fi
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
591 fi
592 ;;
593 esac
594 esac
595 if [ -n "$(uci changes firewall)" ]; then
596 uci -q commit firewall
597 if [ -z "$_restart" ]; then
598 fw3Ops 'reload'
599 else
600 fw3Ops 'restart'
601 fi
602 fi
603 }
604
605 process_url() {
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";
611 else
612 label="Domains: $label"; filter="$domainsFilter";
613 fi
614 if [ "$3" = 'blocked' ]; then
615 type='Blocked'; D_TMP="$B_TMP";
616 else
617 type='Allowed'; D_TMP="$A_TMP";
618 fi
619 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
620 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
621 done
622 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
623 output 1 "$_FAIL_"
624 output 2 "[DL] $type $label $__FAIL__\\n"
625 echo "Error: downloading '${1}'." >> "$sharedMemoryError"
626 else
627 sed -i "$filter" "$R_TMP"
628 if [ ! -s "$R_TMP" ]; then
629 output 1 "$_FAIL_"
630 output 2 "[DL] $type $label $__FAIL__\\n"
631 echo "Error: parsing '${1}'." >> "$sharedMemoryError"
632 else
633 cat "${R_TMP}" >> "$D_TMP"
634 output 1 "$_OK_"
635 output 2 "[DL] $type $label $__OK__\\n"
636 fi
637 fi
638 rm -f "$R_TMP"
639 return 0
640 }
641
642 download_lists() {
643 local hf w_filter j=0 R_TMP
644
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
650 output_okn
651 else
652 output_fail
653 fi
654 fi
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' &
662 else
663 process_url "$hf" 'hosts' 'blocked'
664 fi
665 done
666 fi
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' &
671 else
672 process_url "$hf" 'domains' 'blocked'
673 fi
674 done
675 fi
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' &
680 else
681 process_url "$hf" 'domains' 'allowed'
682 fi
683 done
684 fi
685 wait
686 [ -s "$sharedMemoryError" ] && tmpfs add error "$(cat "$sharedMemoryError")"
687 rm -f "$sharedMemoryError"
688 output 1 '\n'
689
690 [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
691 whitelist_domains="${whitelist_domains}
692 $(cat $A_TMP)"
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
694
695 [ ! -s "$B_TMP" ] && return 1
696
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
702 output_ok
703 else
704 output_failn
705 tmpfs add error 'Error: Sorting error.'
706 fi
707 else
708 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
709 output_ok
710 else
711 output_failn
712 tmpfs add error 'Error: Sorting error.'
713 fi
714 fi
715
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
729 output_ok
730 else
731 output_failn
732 tmpfs add error 'Error: Data file optimization.'
733 mv "$A_TMP" "$B_TMP"
734 fi
735 else
736 output_failn
737 tmpfs add error 'Error: Data file optimization.'
738 fi
739 else
740 output_failn
741 tmpfs add error 'Error: Data file optimization.'
742 mv "$A_TMP" "$B_TMP"
743 fi
744 else
745 output_failn
746 tmpfs add error 'Error: Data file optimization.'
747 fi
748 else
749 output_failn
750 tmpfs add error 'Error: Data file optimization.'
751 mv "$A_TMP" "$B_TMP"
752 fi
753 else
754 mv "$A_TMP" "$B_TMP"
755 fi
756
757 output 2 'Whitelisting domains '
758 tmpfs set message "$statusProcessing: whitelisting domains"
759 if sed -i "$w_filter" "$B_TMP"; then
760 output_ok
761 else
762 output_failn
763 tmpfs add error 'Error: Whitelist processing.'
764 fi
765
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
770 output_ok
771 else
772 output_failn
773 tmpfs add error 'Error: Data file formatting.'
774 fi
775 else
776 case "$targetDNS" in
777 dnsmasq.addnhosts)
778 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
779 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
780 output_ok
781 else
782 output_failn
783 tmpfs add error 'Error: Data file formatting.'
784 fi
785 ;;
786 esac
787 fi
788
789 case "$targetDNS" in
790 dnsmasq.addnhosts)
791 output 2 'Creating DNSMASQ addnhosts file '
792 tmpfs set message "$statusProcessing: creating DNSMASQ addnhosts file"
793 ;;
794 dnsmasq.conf)
795 output 2 'Creating DNSMASQ config file '
796 tmpfs set message "$statusProcessing: creating DNSMASQ config file"
797 ;;
798 dnsmasq.ipset)
799 output 2 'Creating DNSMASQ ipset file '
800 tmpfs set message "$statusProcessing: creating DNSMASQ ipset file"
801 ;;
802 dnsmasq.servers)
803 output 2 'Creating DNSMASQ servers file '
804 tmpfs set message "$statusProcessing: creating DNSMASQ servers file"
805 ;;
806 unbound.adb_list)
807 output 2 'Creating Unbound adb_list file '
808 tmpfs set message "$statusProcessing: creating Unbound adb_list file"
809 ;;
810 esac
811 if mv "$A_TMP" "$outputFile"; then
812 output_ok
813 else
814 output_failn
815 tmpfs add error "Error: moving data file '${A_TMP}' to '${outputFile}'."
816 fi
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
821 output_ok
822 else
823 output_failn
824 tmpfs add error 'Error: creating compressed cache.'
825 fi
826 else
827 rm -f "$outputGzip"
828 fi
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
833 output_ok
834 else
835 output_failn
836 tmpfs add error 'Error: removing temporary files.'
837 fi
838 output 1 '\n'
839 }
840
841 boot() {
842 load_package_config
843 if create_lock; then
844 sleep "$bootDelay"
845 remove_lock
846 rc_procd start_service && rc_procd service_triggers
847 fi
848 }
849
850 start_service() {
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
855 return 0
856 fi
857
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)"
863
864 if [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
865 action='download'
866 elif [ ! -s "$outputFile" ] && ! cacheOps 'test' && ! cacheOps 'testGzip'; then
867 action='download'
868 elif [ ! -s "$outputFile" ] && cacheOps 'testGzip' || cacheOps 'test'; then
869 action='restore'
870 elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
871 action='restart'
872 elif [ -s "$outputFile" ] && [ -n "$status" ] && [ -z "$error" ]; then
873 if [ "$1" != 'hotplug' ]; then status; fi
874 exit 0
875 else
876 action='download'
877 fi
878
879 tmpfs del all
880 tmpfs set triggers
881
882 if is_chaos_calmer || ! is_ipset_procd; then
883 if [ "$forceDNS" -ne 0 ]; then
884 fw3Ops 'insert' 'dns_redirect'
885 else
886 fw3Ops 'remove' 'dns_redirect'
887 fi
888 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
889 fw3Ops 'insert' 'ipset'
890 else
891 fw3Ops 'remove' 'ipset'
892 fi
893 procd_open_instance 'main'
894 procd_set_param command /bin/true
895 procd_set_param stdout 1
896 procd_set_param stderr 1
897 procd_close_instance
898 else
899 procd_open_instance 'main'
900 procd_set_param command /bin/true
901 procd_set_param stdout 1
902 procd_set_param stderr 1
903 procd_open_data
904 json_add_array firewall
905 if [ "$forceDNS" -ne 0 ]; then
906 json_add_object ''
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
915 json_close_object
916 fi
917 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
918 json_add_object ''
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
924 json_close_object
925 json_add_object ''
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
934 json_close_object
935 fi
936 json_close_array
937 procd_close_data
938 procd_close_instance
939 fi
940
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
949 output_okn
950 else
951 output_fail
952 output "$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
953 action='download'
954 fi
955 fi
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
960 output_okn
961 dnsOps 'on_start'
962 else
963 output_fail
964 output "$_ERROR_: $serviceName failed to move '$outputCache' to '$outputFile'!\\n"
965 action='download'
966 fi
967 fi
968 fi
969 case "$action" in
970 download)
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"
975 else
976 output 0 "Starting $serviceName... "
977 output 3 "Starting $serviceName...\\n"
978 tmpfs set status "$statusStarting"
979 fi
980 download_lists
981 dnsOps 'on_start'
982 ;;
983 restart)
984 output 0 "Restarting $serviceName... "
985 output 3 "Restarting $serviceName...\\n"
986 tmpfs set status "$statusRestarting"
987 dnsOps 'on_start'
988 ;;
989 start)
990 output 0 "Starting $serviceName... "
991 output 3 "Starting $serviceName...\\n"
992 tmpfs set status "$statusStarting"
993 dnsOps 'on_start'
994 ;;
995 esac
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
1000 tmpfs del message
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"
1005 fi
1006 else
1007 output 0 "$__FAIL__\\n";
1008 tmpfs set status "$statusFail"
1009 tmpfs add error 'Error: Failed to create blocklist or restart DNS resolver.'
1010 fi
1011 remove_lock
1012 }
1013
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'; }
1021
1022 killcache() {
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"
1028 return 0
1029 }
1030
1031 status() {
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"
1035 fi
1036 [ -n "$status" ] && output "$serviceName $status\\n"
1037 [ -n "$error" ] && output "$error\\n"
1038 }
1039
1040 stop_service() {
1041 load_package_config
1042 fw3Ops 'remove' 'all'
1043 if [ -s "$outputFile" ]; then
1044 output "Stopping $serviceName... "
1045 cacheOps 'create'
1046 if dnsOps 'on_stop'; then
1047 led_off "$led"
1048 output 0 "$__OK__\\n"; output_okn
1049 tmpfs set status "$statusStopped"
1050 tmpfs del message
1051 else
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"
1056 fi
1057 fi
1058 }
1059
1060 check() {
1061 load_package_config
1062 local string="$1"
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':"
1071 else
1072 echo "Found 1 match for '$string' in '$outputFile':"
1073 fi
1074 case "$targetDNS" in
1075 dnsmasq.addnhosts)
1076 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1077 dnsmasq.conf)
1078 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1079 dnsmasq.ipset)
1080 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1081 dnsmasq.servers)
1082 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1083 unbound.adb_list)
1084 grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
1085 esac
1086 else
1087 echo "The $string is not found in current blacklist ('$outputFile')."
1088 fi
1089 }
1090
1091 sizes() {
1092 local i
1093 load_package_config
1094 echo "# $(date)"
1095
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"
1104 fi
1105 rm -rf /tmp/sast
1106 echo " list blacklist_domains_url '$i'"
1107 echo ""
1108 else
1109 echo "# site was down on last check"
1110 echo "# list blacklist_domains_url '$i'"
1111 echo ""
1112 fi
1113 done
1114
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"
1122 fi
1123 rm -rf /tmp/sast
1124 echo " list blacklist_hosts_url '$i'"
1125 echo ""
1126 else
1127 echo "# site was down on last check"
1128 echo "# list blacklist_hosts_url '$i'"
1129 echo ""
1130 fi
1131 done
1132 }