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