Merge pull request #14521 from neheb/gitl
[feed/packages.git] / net / simple-adblock / files / simple-adblock.init
1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039,SC1091
4 PKG_VERSION='dev-test'
5
6 # shellcheck disable=SC2034
7 START=94
8 # shellcheck disable=SC2034
9 USE_PROCD=1
10 LC_ALL=C
11
12 if type extra_command 1>/dev/null 2>&1; then
13 extra_command 'check' 'Checks if specified domain is found in current block-list'
14 extra_command 'dl' 'Force-downloads all enabled block-list'
15 extra_command 'sizes' 'Displays the file-sizes of enabled block-listo'
16 extra_command 'show' 'Shows the service last-run status'
17 extra_command 'version' 'Show version information'
18 else
19 # shellcheck disable=SC2034
20 EXTRA_COMMANDS='check dl killcache sizes show version'
21 # shellcheck disable=SC2034
22 EXTRA_HELP=' check Checks if specified domain is found in current block-list
23 dl Force-downloads all enabled block-list
24 sizes Displays the file-sizes of enabled block-lists
25 show Shows the service last-run status'
26 fi
27
28 readonly packageName='simple-adblock'
29 readonly serviceName="$packageName $PKG_VERSION"
30 readonly addnhostsFile="/var/run/${packageName}.addnhosts"
31 readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
32 readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
33 readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
34 readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
35 readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
36 readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
37 readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
38 readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
39 readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
40 readonly ipsetCache="/var/run/${packageName}.ipset.cache"
41 readonly ipsetGzip="/etc/${packageName}.ipset.gz"
42 readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
43 readonly serversFile="/var/run/${packageName}.servers"
44 readonly serversCache="/var/run/${packageName}.servers.cache"
45 readonly serversGzip="/etc/${packageName}.servers.gz"
46 readonly serversOutputFilter='s|^|server=/|;s|$|/|'
47 readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
48 readonly unboundCache="/var/run/${packageName}.unbound.cache"
49 readonly unboundGzip="/etc/${packageName}.unbound.gz"
50 readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
51 readonly A_TMP="/var/${packageName}.hosts.a.tmp"
52 readonly B_TMP="/var/${packageName}.hosts.b.tmp"
53 readonly PIDFile="/var/run/${packageName}.pid"
54 readonly jsonFile="/var/run/${packageName}.json"
55 readonly sharedMemoryError="/dev/shm/$packageName-error"
56 readonly sharedMemoryOutput="/dev/shm/$packageName-output"
57 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;'
58 readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
59 readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
60 readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
61 readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
62 readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
63 readonly _ERROR_='\033[0;31mERROR\033[0m'
64
65 version() { echo "$PKG_VERSION"; }
66
67 getStatusText() {
68 local _ret
69 case "$1" in
70 statusNoInstall) _ret="$serviceName is not installed or not found";;
71 statusStopped) _ret="Stopped";;
72 statusStarting) _ret="Starting";;
73 statusRestarting) _ret="Restarting";;
74 statusForceReloading) _ret="Force Reloading";;
75 statusDownloading) _ret="Downloading";;
76 statusProcessing) _ret="Processing";;
77 statusError) _ret="Error";;
78 statusWarning) _ret="Warning";;
79 statusFail) _ret="Fail";;
80 statusSuccess) _ret="Success";;
81 esac
82 printf "%b" "$_ret"
83 }
84 getErrorText() {
85 local _ret
86 case "$1" in
87 errorOutputFileCreate) _ret="failed to create $outputFile file";;
88 errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
89 errorSharedMemory) _ret="failed to access shared memory";;
90 errorSorting) _ret="failed to sort data file";;
91 errorOptimization) _ret="failed to optimize data file";;
92 errorAllowListProcessing) _ret="failed to process allow-list";;
93 errorDataFileFormatting) _ret="failed to format data file";;
94 errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
95 errorCreatingCompressedCache) _ret="failed to create compressed cache";;
96 errorRemovingTempFiles) _ret="failed to remove temporary files";;
97 errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
98 errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
99 errorOhSnap) _ret="failed to create block-list or restart DNS resolver";;
100 errorStopping) _ret="failed to stop $serviceName";;
101 errorDNSReload) _ret="failed to reload/restart DNS resolver";;
102 errorDownloadingConfigUpdate) _ret="failed to download Config Update file";;
103 errorDownloadingList) _ret="failed to download";;
104 errorParsingList) _ret="failed to parse";;
105 errorParsingConfigUpdate) _ret="failed to parse Config Update file";;
106 esac
107 printf "%b" "$_ret"
108 }
109
110 create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
111 remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
112 trap remove_lock EXIT
113 output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
114 output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
115 output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
116 output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
117 str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
118 str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
119 compare_values() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
120 is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
121 led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
122 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
123 dnsmasq_hup() { killall -q -HUP dnsmasq; }
124 dnsmasq_kill() { killall -q -KILL dnsmasq; }
125 dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
126 unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
127
128 output() {
129 # Can take a single parameter (text) to be output at any verbosity
130 # Or target verbosity level and text to be output at specifc verbosity
131 local msg memmsg logmsg
132 if [ $# -ne 1 ]; then
133 if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
134 fi
135 [ -t 1 ] && printf "%b" "$1"
136 msg="${1//$serviceName /service }";
137 if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
138 [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
139 logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
140 logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
141 rm -f "$sharedMemoryOutput"
142 else
143 printf "%b" "$msg" >> "$sharedMemoryOutput"
144 fi
145 }
146
147 serviceEnabled=1
148 forceDNS=1
149 parallelDL=1
150 debug=0
151 compressedCache=0
152 ipv6Enabled=0
153 configUpdateEnabled=0
154 configUpdateURL=''
155 bootDelay=120
156 dlTimeout=20
157 curlRetry=3
158 verbosity=2
159 led=''
160 targetDNS=dnsmasq.servers
161 dnsInstance=0
162 allowed_domains=''
163 blocked_domains=''
164 allowed_domains_urls=''
165 blocked_domains_urls=''
166 blocked_hosts_urls=''
167 dl_command=''
168 dl_flag=''
169 outputFilter=''
170 outputFilterIPv6=''
171 outputFile=''
172 outputGzip=''
173 outputCache=''
174 isSSLSupported=''
175 allowIDN=0
176
177 load_package_config() {
178 config_load "$packageName"
179 config_get_bool serviceEnabled 'config' 'enabled' 1
180 config_get_bool forceDNS 'config' 'force_dns' 1
181 config_get_bool parallelDL 'config' 'parallel_downloads' 1
182 config_get_bool debug 'config' 'debug' 0
183 config_get_bool compressedCache 'config' 'compressed_cache' 0
184 config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
185 config_get_bool configUpdateEnabled 'config' 'config_update_enabled' 0
186 config_get bootDelay 'config' 'boot_delay' '120'
187 config_get dlTimeout 'config' 'download_timeout' '20'
188 config_get curlRetry 'config' 'curl_retry' '3'
189 config_get verbosity 'config' 'verbosity' '2'
190 config_get led 'config' 'led'
191 config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
192 config_get dnsInstance 'config' 'dns_instance' '0'
193 config_get allowed_domains 'config' 'allowed_domain'
194 config_get allowed_domains_urls 'config' 'allowed_domains_url'
195 config_get blocked_domains 'config' 'blocked_domain'
196 config_get blocked_domains_urls 'config' 'blocked_domains_url'
197 config_get blocked_hosts_urls 'config' 'blocked_hosts_url'
198 config_get configUpdateURL 'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
199
200 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
201 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
202 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
203 targetDNS='dnsmasq.servers'
204 fi
205
206 case "$targetDNS" in
207 dnsmasq.addnhosts)
208 outputFilter="$addnhostsOutputFilter"
209 outputFile="$addnhostsFile"
210 outputCache="$addnhostsCache"
211 outputGzip="$addnhostsGzip"
212 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
213 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
214 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
215 rm -f "$serversFile" "$serversCache" "$serversGzip"
216 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
217 ;;
218 dnsmasq.conf)
219 outputFilter="$dnsmasqOutputFilter"
220 outputFile="$dnsmasqFile"
221 outputCache="$dnsmasqCache"
222 outputGzip="$dnsmasqGzip"
223 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
224 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
225 rm -f "$serversFile" "$serversCache" "$serversGzip"
226 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
227 ;;
228 dnsmasq.ipset)
229 outputFilter="$ipsetOutputFilter"
230 outputFile="$ipsetFile"
231 outputCache="$ipsetCache"
232 outputGzip="$ipsetGzip"
233 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
234 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
235 rm -f "$serversFile" "$serversCache" "$serversGzip"
236 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
237 ;;
238 dnsmasq.servers)
239 outputFilter="$serversOutputFilter"
240 outputFile="$serversFile"
241 outputCache="$serversCache"
242 outputGzip="$serversGzip"
243 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
244 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
245 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
246 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
247 ;;
248 unbound.adb_list)
249 outputFilter="$unboundOutputFilter"
250 outputFile="$unboundFile"
251 outputCache="$unboundCache"
252 outputGzip="$unboundGzip"
253 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
254 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
255 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
256 rm -f "$serversFile" "$serversCache" "$serversGzip"
257 ;;
258 esac
259 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
260 verbosity=1
261 fi
262 . /lib/functions/network.sh
263 . /usr/share/libubox/jshn.sh
264 # Prefer curl because it supports the file:// scheme.
265 if command -v curl >/dev/null 2>&1; then
266 dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
267 dl_flag="-o"
268 elif command -v wget >/dev/null 2>&1 && wget --version 2>/dev/null | grep -q "+https"; then
269 dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
270 dl_flag="-O"
271 else
272 dl_command="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
273 dl_flag="-O"
274 fi
275 led="${led:+/sys/class/leds/$led}"
276 if curl --version 2>/dev/null | grep -q "https" \
277 || wget --version 2>/dev/null | grep -q "+https" \
278 || grep -q "libustream-mbedtls" /usr/lib/opkg/status \
279 || grep -q "libustream-openssl" /usr/lib/opkg/status \
280 || grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
281 isSSLSupported=1
282 else
283 unset isSSLSupported
284 fi
285 }
286
287 is_enabled() {
288 wan_if=''; wan_gw='';
289 load_package_config
290
291 if [ "$debug" -ne 0 ]; then
292 exec 1>>/tmp/simple-adblock.log
293 exec 2>&1
294 set -x
295 fi
296
297 if [ "$serviceEnabled" -eq 0 ]; then
298 case "$1" in
299 on_start)
300 output "$packageName is currently disabled.\\n"
301 output "Run the following commands before starting service again:\\n"
302 output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
303 ;;
304 esac
305 return 1
306 fi
307
308 case $targetDNS in
309 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
310 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
311 allowIDN=0
312 else
313 allowIDN=1
314 fi
315 ;;
316 unbound.adb_list)
317 allowIDN=1;;
318 esac
319
320 case $targetDNS in
321 dnsmasq.ipset)
322 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
323 output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
324 targetDNS='dnsmasq.servers'
325 fi
326 if ! ipset help hash:net >/dev/null 2>&1; then
327 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"
328 targetDNS='dnsmasq.servers'
329 fi
330 ;;
331 esac
332
333 [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
334 [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
335 [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
336 cacheOps 'testGzip' && return 0
337 network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
338 [ -n "$wan_gw" ] && return 0
339 output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
340 }
341
342 dnsmasqOps() {
343 local cfg="$1" param="$2"
344 case "$param" in
345 dnsmasq.addnhosts)
346 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
347 uci -q del dhcp."$cfg".serversfile
348 fi
349 if ! uci -q get dhcp."$cfg".addnhosts | grep -q "$addnhostsFile"; then
350 uci add_list dhcp."$cfg".addnhosts="$addnhostsFile"
351 fi
352 ;;
353 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup)
354 uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
355 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
356 uci -q del dhcp."$cfg".serversfile
357 fi
358 ;;
359 dnsmasq.servers)
360 uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
361 if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
362 uci set dhcp."$cfg".serversfile="$serversFile"
363 fi
364 ;;
365 esac
366 }
367
368 dnsOps() {
369 local param output_text i
370 case $1 in
371 on_start)
372 if [ ! -s "$outputFile" ]; then
373 tmpfs set status "statusFail"
374 tmpfs add error "errorOutputFileCreate"
375 output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
376 return 1
377 fi
378
379 config_load 'dhcp'
380 if [ "$dnsInstance" = "*" ]; then
381 config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
382 elif [ -n "$dnsInstance" ]; then
383 for i in $dnsInstance; do
384 dnsmasqOps "@dnsmasq[$i]" "$targetDNS"
385 done
386 fi
387
388 case "$targetDNS" in
389 dnsmasq.addnhosts|dnsmasq.servers)
390 param=dnsmasq_hup
391 output_text='Reloading DNSMASQ'
392 ;;
393 dnsmasq.conf|dnsmasq.ipset)
394 param=dnsmasq_restart
395 output_text='Restarting DNSMASQ'
396 ;;
397 unbound.adb_list)
398 param=unbound_restart
399 output_text='Restarting Unbound'
400 ;;
401 esac
402
403 if [ -n "$(uci changes dhcp)" ]; then
404 uci commit dhcp
405 if [ "$param" = 'unbound_restart' ]; then
406 param='dnsmasq_restart; unbound_restart;'
407 output_text='Restarting Unbound/DNSMASQ'
408 else
409 param=dnsmasq_restart
410 output_text='Restarting DNSMASQ'
411 fi
412 fi
413 output 1 "$output_text "
414 output 2 "$output_text "
415 tmpfs set message "$output_text"
416 if eval "$param"; then
417 tmpfs set status "statusSuccess"
418 led_on "$led"
419 output_okn
420 else
421 output_fail
422 tmpfs set status "statusFail"
423 tmpfs add error "errorDNSReload"
424 output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
425 return 1
426 fi
427 ;;
428 on_stop)
429 case "$targetDNS" in
430 dnsmasq.addnhosts | dnsmasq.servers)
431 param=dnsmasq_hup
432 ;;
433 dnsmasq.conf | dnsmasq.ipset)
434 param=dnsmasq_restart
435 ;;
436 unbound.adb_list)
437 param=unbound_restart
438 ;;
439 esac
440 if [ -n "$(uci changes dhcp)" ]; then
441 uci -q commit dhcp
442 if [ "$param" = 'unbound_restart' ]; then
443 param='dnsmasq_restart; unbound_restart;'
444 else
445 param=dnsmasq_restart
446 fi
447 fi
448 eval "$param"
449 return $?
450 ;;
451 quiet)
452 case "$targetDNS" in
453 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
454 param=dnsmasq_restart
455 ;;
456 unbound.adb_list)
457 param=unbound_restart
458 ;;
459 esac
460 eval "$param"
461 return $?
462 ;;
463 esac
464 }
465
466 tmpfs() {
467 local action="$1" instance="$2" value="$3"
468 local status message error stats
469 local reload restart curReload curRestart ret i
470 if [ -s "$jsonFile" ]; then
471 json_load_file "$jsonFile" 2>/dev/null
472 json_select 'data' 2>/dev/null
473 for i in status message error stats reload restart; do
474 json_get_var $i "$i" 2>/dev/null
475 done
476 fi
477 case "$action" in
478 get)
479 case "$instance" in
480 status)
481 printf "%b" "$status"; return;;
482 message)
483 printf "%b" "$message"; return;;
484 error)
485 printf "%b" "$error"; return;;
486 stats)
487 printf "%b" "$stats"; return;;
488 triggers)
489 curReload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
490 curRestart="$compressedCache $forceDNS $led"
491 if [ ! -s "$jsonFile" ]; then
492 ret='on_boot'
493 elif [ "$curReload" != "$reload" ]; then
494 ret='download'
495 elif [ "$curRestart" != "$restart" ]; then
496 ret='restart'
497 fi
498 printf "%b" "$ret"
499 return;;
500 esac
501 ;;
502 add)
503 case "$instance" in
504 status)
505 [ -n "$status" ] && status="$status $value" || status="$value";;
506 message)
507 [ -n "$message" ] && message="$message $value" || message="$value";;
508 error)
509 [ -n "$error" ] && error="$error $value" || error="$value";;
510 stats)
511 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
512 esac
513 ;;
514 del)
515 case "$instance" in
516 all)
517 unset status;
518 unset message;
519 unset error;
520 unset stats;
521 ;;
522 status)
523 unset status;;
524 message)
525 unset message;;
526 error)
527 unset error;;
528 stats)
529 unset stats;;
530 triggers)
531 unset reload; unset restart;;
532 esac
533 ;;
534 set)
535 case "$instance" in
536 status)
537 status="$value";;
538 message)
539 message="$value";;
540 error)
541 error="$value";;
542 stats)
543 stats="$value";;
544 triggers)
545 reload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
546 restart="$compressedCache $forceDNS $led"
547 ;;
548 esac
549 ;;
550 esac
551 json_init
552 json_add_object 'data'
553 json_add_string version "$PKG_VERSION"
554 json_add_string status "$status"
555 json_add_string message "$message"
556 json_add_string error "$error"
557 json_add_string stats "$stats"
558 json_add_string reload "$reload"
559 json_add_string restart "$restart"
560 json_close_object
561 json_dump > "$jsonFile"
562 sync
563 }
564
565 cacheOps() {
566 local R_TMP
567 case "$1" in
568 create|backup)
569 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
570 return $?
571 ;;
572 restore|use)
573 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
574 return $?
575 ;;
576 test)
577 [ -s "$outputCache" ]
578 return $?
579 ;;
580 testGzip)
581 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
582 return $?
583 ;;
584 createGzip)
585 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
586 if gzip < "$outputFile" > "$R_TMP"; then
587 if mv "$R_TMP" "$outputGzip"; then
588 rm -f "$R_TMP"
589 return 0
590 else
591 rm -f "$R_TMP"
592 return 1
593 fi
594 else
595 return 1
596 fi
597 ;;
598 expand|unpack|expandGzip|unpackGzip)
599 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
600 return $?
601 ;;
602 esac
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 if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
620 output 1 "$_FAIL_"
621 output 2 "[DL] $type $label $__FAIL__\\n"
622 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
623 return 0
624 fi
625 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
626 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
627 done
628 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
629 output 1 "$_FAIL_"
630 output 2 "[DL] $type $label $__FAIL__\\n"
631 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
632 else
633 sed -i "$filter" "$R_TMP"
634 if [ ! -s "$R_TMP" ]; then
635 output 1 "$_FAIL_"
636 output 2 "[DL] $type $label $__FAIL__\\n"
637 echo "errorParsingList|${1}" >> "$sharedMemoryError"
638 else
639 cat "${R_TMP}" >> "$D_TMP"
640 output 1 "$_OK_"
641 output 2 "[DL] $type $label $__OK__\\n"
642 fi
643 fi
644 rm -f "$R_TMP"
645 return 0
646 }
647
648 process_config_update() {
649 local label R_TMP
650 [ "$configUpdateEnabled" -eq 0 ] && return 0
651 label="${1##*//}"; label="${label%%/*}";
652 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
653 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
654 done
655 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
656 output 1 "$_FAIL_"
657 output 2 "[DL] Config Update: $label $__FAIL__\\n"
658 tmpfs add error "errorDownloadingConfigUpdate"
659 else
660 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock; then
661 output 1 "$_OK_"
662 output 2 "[DL] Config Update: $label $__OK__\\n"
663 else
664 output 1 "$_FAIL_"
665 output 2 "[DL] Config Update: $label $__FAIL__\\n"
666 tmpfs add error "errorParsingConfigUpdate"
667 fi
668 fi
669 rm -f "$R_TMP"
670 return 0
671 }
672
673 download_lists() {
674 local hf w_filter j=0 R_TMP
675
676 tmpfs set message "$(getStatusText "statusDownloading")..."
677 tmpfs set status "statusDownloading"
678
679 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
680 if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
681 output 3 'Low free memory, restarting resolver... '
682 if dnsOps 'quiet'; then
683 output_okn
684 else
685 output_fail
686 fi
687 fi
688 touch $A_TMP; touch $B_TMP;
689 output 1 'Downloading lists '
690 process_config_update "$configUpdateURL"
691 rm -f "$sharedMemoryError"
692 if [ -n "$blocked_hosts_urls" ]; then
693 for hf in ${blocked_hosts_urls}; do
694 if [ "$parallelDL" -gt 0 ]; then
695 process_url "$hf" 'hosts' 'blocked' &
696 else
697 process_url "$hf" 'hosts' 'blocked'
698 fi
699 done
700 fi
701 if [ -n "$blocked_domains_urls" ]; then
702 for hf in ${blocked_domains_urls}; do
703 if [ "$parallelDL" -gt 0 ]; then
704 process_url "$hf" 'domains' 'blocked' &
705 else
706 process_url "$hf" 'domains' 'blocked'
707 fi
708 done
709 fi
710 if [ -n "$allowed_domains_urls" ]; then
711 for hf in ${allowed_domains_urls}; do
712 if [ "$parallelDL" -gt 0 ]; then
713 process_url "$hf" 'domains' 'allowed' &
714 else
715 process_url "$hf" 'domains' 'allowed'
716 fi
717 done
718 fi
719 wait
720 output 1 '\n'
721 if [ -s "$sharedMemoryError" ]; then
722 while IFS= read -r line; do
723 tmpfs add error "$line"
724 done < "$sharedMemoryError"
725 rm -f "$sharedMemoryError"
726 fi
727
728 [ -n "$blocked_domains" ] && for hf in ${blocked_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
729 allowed_domains="${allowed_domains}
730 $(cat $A_TMP)"
731 [ -n "$allowed_domains" ] && for hf in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
732
733 [ ! -s "$B_TMP" ] && return 1
734
735 output 1 'Processing downloads '
736 output 2 'Sorting combined list '
737 tmpfs set message "$(getStatusText "statusProcessing"): sorting combined list"
738 if [ "$allowIDN" -gt 0 ]; then
739 if sort -u "$B_TMP" > "$A_TMP"; then
740 output_ok
741 else
742 output_failn
743 tmpfs add error "errorSorting"
744 fi
745 else
746 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
747 output_ok
748 else
749 output_failn
750 tmpfs add error "errorSorting"
751 fi
752 fi
753
754 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
755 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
756 [ "$targetDNS" = 'dnsmasq.servers' ] || \
757 [ "$targetDNS" = 'unbound.adb_list' ]; then
758 # TLD optimization written by Dirk Brenken (dev@brenken.org)
759 output 2 'Optimizing combined list '
760 tmpfs set message "$(getStatusText "statusProcessing"): optimizing combined list"
761 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
762 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
763 if sort "$B_TMP" > "$A_TMP"; then
764 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
765 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
766 if sort -u "$A_TMP" > "$B_TMP"; then
767 output_ok
768 else
769 output_failn
770 tmpfs add error "errorOptimization"
771 mv "$A_TMP" "$B_TMP"
772 fi
773 else
774 output_failn
775 tmpfs add error "errorOptimization"
776 fi
777 else
778 output_failn
779 tmpfs add error "errorOptimization"
780 mv "$A_TMP" "$B_TMP"
781 fi
782 else
783 output_failn
784 tmpfs add error "errorOptimization"
785 fi
786 else
787 output_failn
788 tmpfs add error "errorOptimization"
789 mv "$A_TMP" "$B_TMP"
790 fi
791 else
792 mv "$A_TMP" "$B_TMP"
793 fi
794
795 output 2 'Allowing domains '
796 tmpfs set message "$(getStatusText "statusProcessing"): allowing domains"
797 if sed -i "$w_filter" "$B_TMP"; then
798 output_ok
799 else
800 output_failn
801 tmpfs add error "errorAllowListProcessing"
802 fi
803
804 output 2 'Formatting merged file '
805 tmpfs set message "$(getStatusText "statusProcessing"): formatting merged file"
806 if [ -z "$outputFilterIPv6" ]; then
807 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
808 output_ok
809 else
810 output_failn
811 tmpfs add error "errorDataFileFormatting"
812 fi
813 else
814 case "$targetDNS" in
815 dnsmasq.addnhosts)
816 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
817 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
818 output_ok
819 else
820 output_failn
821 tmpfs add error "errorDataFileFormatting"
822 fi
823 ;;
824 esac
825 fi
826
827 case "$targetDNS" in
828 dnsmasq.addnhosts)
829 output 2 'Creating DNSMASQ addnhosts file '
830 tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
831 ;;
832 dnsmasq.conf)
833 output 2 'Creating DNSMASQ config file '
834 tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
835 ;;
836 dnsmasq.ipset)
837 output 2 'Creating DNSMASQ ipset file '
838 tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
839 ;;
840 dnsmasq.servers)
841 output 2 'Creating DNSMASQ servers file '
842 tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
843 ;;
844 unbound.adb_list)
845 output 2 'Creating Unbound adb_list file '
846 tmpfs set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
847 ;;
848 esac
849 if mv "$A_TMP" "$outputFile"; then
850 output_ok
851 else
852 output_failn
853 tmpfs add error "errorMovingDataFile"
854 fi
855 if [ "$compressedCache" -gt 0 ]; then
856 output 2 'Creating compressed cache '
857 tmpfs set message "$(getStatusText "statusProcessing"): creating compressed cache"
858 if cacheOps 'createGzip'; then
859 output_ok
860 else
861 output_failn
862 tmpfs add error "errorCreatingCompressedCache"
863 fi
864 else
865 rm -f "$outputGzip"
866 fi
867 output 2 'Removing temporary files '
868 tmpfs set message "$(getStatusText "statusProcessing"): removing temporary files"
869 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
870 if [ $j -eq 0 ]; then
871 output_ok
872 else
873 output_failn
874 tmpfs add error "errorRemovingTempFiles"
875 fi
876 output 1 '\n'
877 }
878
879 boot() {
880 load_package_config
881 if create_lock; then
882 sleep "$bootDelay"
883 remove_lock
884 rc_procd start_service 'on_boot' && rc_procd service_triggers
885 fi
886 }
887
888 start_service() {
889 is_enabled 'on_start' || return 1
890 local action status error message stats c
891 if ! create_lock; then
892 output 3 "$serviceName: another instance is starting up "; output_fail
893 return 0
894 fi
895
896 status="$(tmpfs get status)"
897 error="$(tmpfs get error)"
898 message="$(tmpfs get message)"
899 stats="$(tmpfs get stats)"
900 action="$(tmpfs get triggers)"
901
902 if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
903 if cacheOps 'testGzip' || cacheOps 'test'; then
904 action='restore'
905 else
906 action='download'
907 fi
908 elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
909 action='download'
910 elif [ ! -s "$outputFile" ]; then
911 if cacheOps 'testGzip' || cacheOps 'test'; then
912 action='restore'
913 else
914 action='download'
915 fi
916 elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
917 action='restart'
918 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
919 [ "$1" != 'hotplug' ] && showstatus
920 exit 0
921 else
922 action='download'
923 fi
924
925 tmpfs del all
926 tmpfs set triggers
927
928 procd_open_instance 'main'
929 procd_set_param command /bin/true
930 procd_set_param stdout 1
931 procd_set_param stderr 1
932 procd_open_data
933 json_add_array firewall
934 if [ "$forceDNS" -ne 0 ]; then
935 json_add_object ''
936 json_add_string type redirect
937 json_add_string name simple_adblock_dns_redirect
938 json_add_string target DNAT
939 json_add_string src lan
940 json_add_string proto tcpudp
941 json_add_string src_dport 53
942 json_add_string dest_port 53
943 json_add_string reflection 0
944 json_close_object
945 fi
946 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
947 json_add_object ''
948 json_add_string type ipset
949 json_add_string name adb
950 json_add_string match dest_net
951 json_add_string storage hash
952 json_add_string enabled 1
953 json_close_object
954 json_add_object ''
955 json_add_string type rule
956 json_add_string name simple_adblock_ipset_rule
957 json_add_string ipset adb
958 json_add_string src lan
959 json_add_string dest '*'
960 json_add_string proto tcpudp
961 json_add_string target REJECT
962 json_add_string enabled 1
963 json_close_object
964 fi
965 json_close_array
966 procd_close_data
967 procd_close_instance
968
969 if [ "$action" = 'restore' ]; then
970 output 0 "Starting $serviceName... "
971 output 3 "Starting $serviceName...\\n"
972 tmpfs set status "statusStarting"
973 if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
974 output 3 'Found compressed cache file, unpacking it '
975 tmpfs set message 'found compressed cache file, unpacking it.'
976 if cacheOps 'unpackGzip'; then
977 output_okn
978 else
979 output_fail
980 tmpfs add error "errorRestoreCompressedCache"
981 output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
982 action='download'
983 fi
984 fi
985 if cacheOps 'test' && [ ! -s "$outputFile" ]; then
986 output 3 'Found cache file, reusing it '
987 tmpfs set message 'found cache file, reusing it.'
988 if cacheOps 'restore'; then
989 output_okn
990 dnsOps 'on_start'
991 else
992 output_fail
993 tmpfs add error "errorRestoreCache"
994 output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
995 action='download'
996 fi
997 fi
998 fi
999 case "$action" in
1000 download)
1001 if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
1002 output 0 "Force-reloading $serviceName... "
1003 output 3 "Force-reloading $serviceName...\\n"
1004 tmpfs set status "statusForceReloading"
1005 else
1006 output 0 "Starting $serviceName... "
1007 output 3 "Starting $serviceName...\\n"
1008 tmpfs set status "statusStarting"
1009 fi
1010 download_lists
1011 dnsOps 'on_start'
1012 ;;
1013 restart)
1014 output 0 "Restarting $serviceName... "
1015 output 3 "Restarting $serviceName...\\n"
1016 tmpfs set status "statusRestarting"
1017 dnsOps 'on_start'
1018 ;;
1019 start)
1020 output 0 "Starting $serviceName... "
1021 output 3 "Starting $serviceName...\\n"
1022 tmpfs set status "statusStarting"
1023 dnsOps 'on_start'
1024 ;;
1025 esac
1026 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
1027 output 0 "$__OK__\\n";
1028 tmpfs del message
1029 tmpfs set status "statusSuccess"
1030 c="$(wc -l < "$outputFile")"
1031 tmpfs set stats "$serviceName is blocking $c domains (with ${targetDNS})"
1032 showstatus
1033 else
1034 output 0 "$__FAIL__\\n";
1035 tmpfs set status "statusFail"
1036 tmpfs add error "errorOhSnap"
1037 showstatus
1038 fi
1039 remove_lock
1040 }
1041
1042 service_started() { procd_set_config_changed firewall; }
1043 service_stopped() { procd_set_config_changed firewall; }
1044 restart_service() { rc_procd start_service 'restart'; }
1045 reload_service() { restart_service; }
1046 restart() { restart_service; }
1047 reload() { restart_service; }
1048 dl() { rc_procd start_service 'download'; }
1049 killcache() {
1050 rm -f "$addnhostsCache" "$addnhostsGzip"
1051 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1052 rm -f "$ipsetCache" "$ipsetGzip"
1053 rm -f "$serversCache" "$serversGzip"
1054 rm -f "$unboundCache" "$unboundGzip"
1055 config_load 'dhcp'
1056 config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
1057 uci -q commit 'dhcp'
1058 return 0
1059 }
1060
1061 show() { showstatus; }
1062 status_service() { showstatus; }
1063 showstatus() {
1064 local c url status message error stats
1065 status="$(tmpfs get status)"
1066 message="$(tmpfs get message)"
1067 error="$(tmpfs get error)"
1068 stats="$(tmpfs get stats)"
1069 if [ "$status" = "statusSuccess" ]; then
1070 output "$stats "; output_okn;
1071 else
1072 [ -n "$status" ] && status="$(getStatusText "$status")"
1073 if [ -n "$status" ] && [ -n "$message" ]; then
1074 status="${status}: $message"
1075 fi
1076 [ -n "$status" ] && output "$serviceName $status\\n"
1077 fi
1078 if [ -n "$error" ]; then
1079 for c in $error; do
1080 url="${c##*|}"
1081 c="${c%|*}"
1082 case "$c" in
1083 errorDownloadingList|errorParsingList)
1084 output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
1085 *)
1086 output "$_ERROR_: $(getErrorText "$c")!\\n";;
1087 esac
1088 let n=n+1
1089 done
1090 fi
1091 }
1092
1093 stop_service() {
1094 load_package_config
1095 if [ -s "$outputFile" ]; then
1096 output "Stopping $serviceName... "
1097 cacheOps 'create'
1098 if dnsOps 'on_stop'; then
1099 led_off "$led"
1100 output 0 "$__OK__\\n"; output_okn;
1101 tmpfs set status "statusStopped"
1102 tmpfs del message
1103 else
1104 output 0 "$__FAIL__\\n"; output_fail;
1105 tmpfs set status "statusFail"
1106 tmpfs add error "errorStopping"
1107 output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
1108 fi
1109 fi
1110 }
1111
1112 service_triggers() {
1113 procd_open_trigger
1114 procd_add_config_trigger "config.change" "${packageName}" /etc/init.d/${packageName} reload
1115 procd_close_trigger
1116 }
1117
1118 check() {
1119 load_package_config
1120 local c string="$1"
1121 c="$(grep -c "$string" "$outputFile")"
1122 if [ ! -s "$outputFile" ]; then
1123 echo "No block-list ('$outputFile') found."
1124 elif [ -z "$string" ]; then
1125 echo "Usage: /etc/init.d/${packageName} check string"
1126 elif [ "$c" -gt 0 ]; then
1127 if [ "$c" -gt 1 ]; then
1128 echo "Found $c matches for '$string' in '$outputFile':"
1129 else
1130 echo "Found 1 match for '$string' in '$outputFile':"
1131 fi
1132 case "$targetDNS" in
1133 dnsmasq.addnhosts)
1134 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1135 dnsmasq.conf)
1136 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1137 dnsmasq.ipset)
1138 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1139 dnsmasq.servers)
1140 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1141 unbound.adb_list)
1142 grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
1143 esac
1144 else
1145 echo "The $string is not found in current block-list ('$outputFile')."
1146 fi
1147 }
1148
1149 sizes() {
1150 local i
1151 load_package_config
1152 echo "# $(date)"
1153
1154 for i in $blocked_domains_urls; do
1155 [ "${i//melmac}" != "$i" ] && continue
1156 if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
1157 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1158 if compare_values "$(du -sk /tmp/sast)" "500"; then
1159 echo "# block-list too big for most routers"
1160 elif compare_values "$(du -sk /tmp/sast)" "100"; then
1161 echo "# block-list may be too big for some routers"
1162 fi
1163 rm -rf /tmp/sast
1164 echo " list blocked_domains_url '$i'"
1165 echo ""
1166 else
1167 echo "# site was down on last check"
1168 echo "# list blocked_domains_url '$i'"
1169 echo ""
1170 fi
1171 done
1172
1173 for i in $blocked_hosts_urls; do
1174 if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
1175 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1176 if compare_values "$(du -sk /tmp/sast)" "500"; then
1177 echo "# block-list too big for most routers"
1178 elif compare_values "$(du -sk /tmp/sast)" "100"; then
1179 echo "# block-list may be too big for some routers"
1180 fi
1181 rm -rf /tmp/sast
1182 echo " list blocked_hosts_url '$i'"
1183 echo ""
1184 else
1185 echo "# site was down on last check"
1186 echo "# list blocked_hosts_url '$i'"
1187 echo ""
1188 fi
1189 done
1190 }