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