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