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