1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039,SC1091
6 # shellcheck disable=SC2034
8 # shellcheck disable=SC2034
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'
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'
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'
65 version
() { echo "$PKG_VERSION"; }
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";;
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";;
110 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
111 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
112 trap remove_lock EXIT
113 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
114 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
115 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
116 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
117 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
118 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
119 compare_values
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
120 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
121 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
122 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
123 dnsmasq_hup
() { killall
-q -HUP dnsmasq
; }
124 dnsmasq_kill
() { killall
-q -KILL dnsmasq
; }
125 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
126 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
129 # Can take a single parameter (text) to be output at any verbosity
130 # Or target verbosity level and text to be output at specifc verbosity
131 local msg memmsg logmsg
132 if [ $# -ne 1 ]; then
133 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
135 [ -t 1 ] && printf "%b" "$1"
136 msg
="${1//$serviceName /service }";
137 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
138 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
139 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
140 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
141 rm -f "$sharedMemoryOutput"
143 printf "%b" "$msg" >> "$sharedMemoryOutput"
153 configUpdateEnabled
=0
160 targetDNS
=dnsmasq.servers
164 allowed_domains_urls
=''
165 blocked_domains_urls
=''
166 blocked_hosts_urls
=''
177 load_package_config
() {
178 config_load
"$packageName"
179 config_get_bool serviceEnabled
'config' 'enabled' 1
180 config_get_bool forceDNS
'config' 'force_dns' 1
181 config_get_bool parallelDL
'config' 'parallel_downloads' 1
182 config_get_bool debug
'config' 'debug' 0
183 config_get_bool compressedCache
'config' 'compressed_cache' 0
184 config_get_bool ipv6Enabled
'config' 'ipv6_enabled' 0
185 config_get_bool configUpdateEnabled
'config' 'config_update_enabled' 0
186 config_get bootDelay
'config' 'boot_delay' '120'
187 config_get dlTimeout
'config' 'download_timeout' '20'
188 config_get curlRetry
'config' 'curl_retry' '3'
189 config_get verbosity
'config' 'verbosity' '2'
190 config_get led
'config' 'led'
191 config_get targetDNS
'config' 'dns' 'dnsmasq.servers'
192 config_get dnsInstance
'config' 'dns_instance' '0'
193 config_get allowed_domains
'config' 'allowed_domain'
194 config_get allowed_domains_urls
'config' 'allowed_domains_url'
195 config_get blocked_domains
'config' 'blocked_domain'
196 config_get blocked_domains_urls
'config' 'blocked_domains_url'
197 config_get blocked_hosts_urls
'config' 'blocked_hosts_url'
198 config_get configUpdateURL
'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
200 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
201 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
202 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
203 targetDNS
='dnsmasq.servers'
208 outputFilter
="$addnhostsOutputFilter"
209 outputFile
="$addnhostsFile"
210 outputCache
="$addnhostsCache"
211 outputGzip
="$addnhostsGzip"
212 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6
="$addnhostsOutputFilterIPv6"
213 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
214 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
215 rm -f "$serversFile" "$serversCache" "$serversGzip"
216 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
219 outputFilter
="$dnsmasqOutputFilter"
220 outputFile
="$dnsmasqFile"
221 outputCache
="$dnsmasqCache"
222 outputGzip
="$dnsmasqGzip"
223 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
224 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
225 rm -f "$serversFile" "$serversCache" "$serversGzip"
226 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
229 outputFilter
="$ipsetOutputFilter"
230 outputFile
="$ipsetFile"
231 outputCache
="$ipsetCache"
232 outputGzip
="$ipsetGzip"
233 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
234 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
235 rm -f "$serversFile" "$serversCache" "$serversGzip"
236 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
239 outputFilter
="$serversOutputFilter"
240 outputFile
="$serversFile"
241 outputCache
="$serversCache"
242 outputGzip
="$serversGzip"
243 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
244 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
245 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
246 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
249 outputFilter
="$unboundOutputFilter"
250 outputFile
="$unboundFile"
251 outputCache
="$unboundCache"
252 outputGzip
="$unboundGzip"
253 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
254 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
255 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
256 rm -f "$serversFile" "$serversCache" "$serversGzip"
259 if [ -z "${verbosity##*[!0-9]*}" ] ||
[ "$verbosity" -lt 0 ] ||
[ "$verbosity" -gt 2 ]; then
262 .
/lib
/functions
/network.sh
263 .
/usr
/share
/libubox
/jshn.sh
264 # Prefer curl because it supports the file:// scheme.
265 if command -v curl
>/dev
/null
2>&1; then
266 dl_command
="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
268 elif command -v wget
>/dev
/null
2>&1 && wget
--version 2>/dev
/null |
grep -q "+https"; then
269 dl_command
="wget --no-check-certificate --timeout $dlTimeout -q"
272 dl_command
="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
275 led
="${led:+/sys/class/leds/$led}"
276 if curl
--version 2>/dev
/null |
grep -q "https" \
277 || wget
--version 2>/dev
/null |
grep -q "+https" \
278 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
279 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
280 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
288 wan_if
=''; wan_gw
='';
291 if [ "$debug" -ne 0 ]; then
292 exec 1>>/tmp
/simple-adblock.log
297 if [ "$serviceEnabled" -eq 0 ]; then
300 output
"$packageName is currently disabled.\\n"
301 output
"Run the following commands before starting service again:\\n"
302 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
309 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
310 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
322 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
323 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
324 targetDNS
='dnsmasq.servers'
326 if ! ipset
help hash:net
>/dev
/null
2>&1; then
327 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
328 targetDNS
='dnsmasq.servers'
333 [ ! -d "${outputFile%/*}" ] && mkdir
-p "${outputFile%/*}"
334 [ ! -d "${outputCache%/*}" ] && mkdir
-p "${outputFile%/*}"
335 [ ! -d "${outputGzip%/*}" ] && mkdir
-p "${outputFile%/*}"
336 cacheOps
'testGzip' && return 0
337 network_flush_cache
; network_find_wan wan_if
; network_get_gateway wan_gw
"$wan_if";
338 [ -n "$wan_gw" ] && return 0
339 output
"$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
343 local cfg
="$1" param
="$2"
346 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
347 uci
-q del dhcp.
"$cfg".serversfile
349 if ! uci
-q get dhcp.
"$cfg".addnhosts |
grep -q "$addnhostsFile"; then
350 uci add_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
353 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup
)
354 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
355 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
356 uci
-q del dhcp.
"$cfg".serversfile
360 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
361 if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
362 uci
set dhcp.
"$cfg".serversfile
="$serversFile"
369 local param output_text i
372 if [ ! -s "$outputFile" ]; then
373 tmpfs
set status
"statusFail"
374 tmpfs add error
"errorOutputFileCreate"
375 output
"$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
380 if [ "$dnsInstance" = "*" ]; then
381 config_foreach dnsmasqOps
'dnsmasq' "$targetDNS"
382 elif [ -n "$dnsInstance" ]; then
383 for i
in $dnsInstance; do
384 dnsmasqOps
"@dnsmasq[$i]" "$targetDNS"
389 dnsmasq.addnhosts|dnsmasq.servers
)
391 output_text
='Reloading DNSMASQ'
393 dnsmasq.conf|dnsmasq.ipset
)
394 param
=dnsmasq_restart
395 output_text
='Restarting DNSMASQ'
398 param
=unbound_restart
399 output_text
='Restarting Unbound'
403 if [ -n "$(uci changes dhcp)" ]; then
405 if [ "$param" = 'unbound_restart' ]; then
406 param
='dnsmasq_restart; unbound_restart;'
407 output_text
='Restarting Unbound/DNSMASQ'
409 param
=dnsmasq_restart
410 output_text
='Restarting DNSMASQ'
413 output
1 "$output_text "
414 output
2 "$output_text "
415 tmpfs
set message
"$output_text"
416 if eval "$param"; then
417 tmpfs
set status
"statusSuccess"
422 tmpfs
set status
"statusFail"
423 tmpfs add error
"errorDNSReload"
424 output
"$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
430 dnsmasq.addnhosts | dnsmasq.servers
)
433 dnsmasq.conf | dnsmasq.ipset
)
434 param
=dnsmasq_restart
437 param
=unbound_restart
440 if [ -n "$(uci changes dhcp)" ]; then
442 if [ "$param" = 'unbound_restart' ]; then
443 param
='dnsmasq_restart; unbound_restart;'
445 param
=dnsmasq_restart
453 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
454 param
=dnsmasq_restart
457 param
=unbound_restart
467 local action
="$1" instance
="$2" value
="$3"
468 local status message error stats
469 local reload restart curReload curRestart ret i
470 if [ -s "$jsonFile" ]; then
471 json_load_file
"$jsonFile" 2>/dev
/null
472 json_select
'data' 2>/dev
/null
473 for i
in status message error stats reload restart
; do
474 json_get_var
$i "$i" 2>/dev
/null
481 printf "%b" "$status"; return;;
483 printf "%b" "$message"; return;;
485 printf "%b" "$error"; return;;
487 printf "%b" "$stats"; return;;
489 curReload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
490 curRestart
="$compressedCache $forceDNS $led"
491 if [ ! -s "$jsonFile" ]; then
493 elif [ "$curReload" != "$reload" ]; then
495 elif [ "$curRestart" != "$restart" ]; then
505 [ -n "$status" ] && status
="$status $value" || status
="$value";;
507 [ -n "$message" ] && message
="$message $value" || message
="$value";;
509 [ -n "$error" ] && error
="$error $value" || error
="$value";;
511 [ -n "$stats" ] && stats
="$stats $value" || stats
="$value";;
531 unset reload
; unset restart
;;
545 reload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
546 restart
="$compressedCache $forceDNS $led"
552 json_add_object
'data'
553 json_add_string version
"$PKG_VERSION"
554 json_add_string status
"$status"
555 json_add_string message
"$message"
556 json_add_string error
"$error"
557 json_add_string stats
"$stats"
558 json_add_string reload
"$reload"
559 json_add_string restart
"$restart"
561 json_dump
> "$jsonFile"
569 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
573 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
577 [ -s "$outputCache" ]
581 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
585 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
586 if gzip < "$outputFile" > "$R_TMP"; then
587 if mv "$R_TMP" "$outputGzip"; then
598 expand|unpack|expandGzip|unpackGzip
)
599 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
606 local label
type D_TMP R_TMP
607 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
608 label
="${1##*//}"; label
="${label%%/*}";
609 if [ "$2" = 'hosts' ]; then
610 label
="Hosts: $label"; filter
="$hostsFilter";
612 label
="Domains: $label"; filter
="$domainsFilter";
614 if [ "$3" = 'blocked' ]; then
615 type='Blocked'; D_TMP
="$B_TMP";
617 type='Allowed'; D_TMP
="$A_TMP";
619 if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
621 output
2 "[DL] $type $label $__FAIL__\\n"
622 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
625 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
626 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
628 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
630 output
2 "[DL] $type $label $__FAIL__\\n"
631 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
633 sed -i "$filter" "$R_TMP"
634 if [ ! -s "$R_TMP" ]; then
636 output
2 "[DL] $type $label $__FAIL__\\n"
637 echo "errorParsingList|${1}" >> "$sharedMemoryError"
639 cat "${R_TMP}" >> "$D_TMP"
641 output
2 "[DL] $type $label $__OK__\\n"
648 process_config_update
() {
650 [ "$configUpdateEnabled" -eq 0 ] && return 0
651 label
="${1##*//}"; label
="${label%%/*}";
652 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
653 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
655 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
657 output
2 "[DL] Config Update: $label $__FAIL__\\n"
658 tmpfs add error
"errorDownloadingConfigUpdate"
660 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc
/config
/simple-adblock
; then
662 output
2 "[DL] Config Update: $label $__OK__\\n"
665 output
2 "[DL] Config Update: $label $__FAIL__\\n"
666 tmpfs add error
"errorParsingConfigUpdate"
674 local hf w_filter j
=0 R_TMP
676 tmpfs
set message
"$(getStatusText "statusDownloading
")..."
677 tmpfs
set status
"statusDownloading"
679 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
680 if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
681 output
3 'Low free memory, restarting resolver... '
682 if dnsOps
'quiet'; then
688 touch $A_TMP; touch $B_TMP;
689 output
1 'Downloading lists '
690 process_config_update
"$configUpdateURL"
691 rm -f "$sharedMemoryError"
692 if [ -n "$blocked_hosts_urls" ]; then
693 for hf
in ${blocked_hosts_urls}; do
694 if [ "$parallelDL" -gt 0 ]; then
695 process_url
"$hf" 'hosts' 'blocked' &
697 process_url
"$hf" 'hosts' 'blocked'
701 if [ -n "$blocked_domains_urls" ]; then
702 for hf
in ${blocked_domains_urls}; do
703 if [ "$parallelDL" -gt 0 ]; then
704 process_url
"$hf" 'domains' 'blocked' &
706 process_url
"$hf" 'domains' 'blocked'
710 if [ -n "$allowed_domains_urls" ]; then
711 for hf
in ${allowed_domains_urls}; do
712 if [ "$parallelDL" -gt 0 ]; then
713 process_url
"$hf" 'domains' 'allowed' &
715 process_url
"$hf" 'domains' 'allowed'
721 if [ -s "$sharedMemoryError" ]; then
722 while IFS
= read -r line
; do
723 tmpfs add error
"$line"
724 done < "$sharedMemoryError"
725 rm -f "$sharedMemoryError"
728 [ -n "$blocked_domains" ] && for hf
in ${blocked_domains}; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
729 allowed_domains
="${allowed_domains}
731 [ -n "$allowed_domains" ] && for hf
in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$
/d
;"; done
733 [ ! -s "$B_TMP" ] && return 1
735 output 1 'Processing downloads '
736 output 2 'Sorting combined list '
737 tmpfs set message "$
(getStatusText
"statusProcessing"): sorting combined list
"
738 if [ "$allowIDN" -gt 0 ]; then
739 if sort -u "$B_TMP" > "$A_TMP"; then
743 tmpfs add error "errorSorting
"
746 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
750 tmpfs add error "errorSorting
"
754 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
755 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
756 [ "$targetDNS" = 'dnsmasq.servers' ] || \
757 [ "$targetDNS" = 'unbound.adb_list' ]; then
758 # TLD optimization written by Dirk Brenken (dev@brenken.org)
759 output 2 'Optimizing combined list '
760 tmpfs set message "$
(getStatusText
"statusProcessing"): optimizing combined list
"
761 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
762 if awk -F ".
" '{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' "$A_TMP" > "$B_TMP"; then
763 if sort "$B_TMP" > "$A_TMP"; then
764 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\.
"){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
765 if awk -F ".
" '{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' "$B_TMP" > "$A_TMP"; then
766 if sort -u "$A_TMP" > "$B_TMP"; then
770 tmpfs add error "errorOptimization
"
775 tmpfs add error "errorOptimization
"
779 tmpfs add error "errorOptimization
"
784 tmpfs add error "errorOptimization
"
788 tmpfs add error "errorOptimization
"
795 output 2 'Allowing domains '
796 tmpfs set message "$
(getStatusText
"statusProcessing"): allowing domains
"
797 if sed -i "$w_filter" "$B_TMP"; then
801 tmpfs add error "errorAllowListProcessing
"
804 output 2 'Formatting merged file '
805 tmpfs set message "$
(getStatusText
"statusProcessing"): formatting merged
file"
806 if [ -z "$outputFilterIPv6" ]; then
807 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
811 tmpfs add error "errorDataFileFormatting
"
816 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
817 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
821 tmpfs add error "errorDataFileFormatting
"
829 output 2 'Creating DNSMASQ addnhosts file '
830 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ addnhosts
file"
833 output 2 'Creating DNSMASQ config file '
834 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ config
file"
837 output 2 'Creating DNSMASQ ipset file '
838 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ ipset
file"
841 output 2 'Creating DNSMASQ servers file '
842 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ servers
file"
845 output 2 'Creating Unbound adb_list file '
846 tmpfs set message "$
(getStatusText
"statusProcessing"): creating Unbound adb_list
file"
849 if mv "$A_TMP" "$outputFile"; then
853 tmpfs add error "errorMovingDataFile
"
855 if [ "$compressedCache" -gt 0 ]; then
856 output 2 'Creating compressed cache '
857 tmpfs set message "$
(getStatusText
"statusProcessing"): creating compressed cache
"
858 if cacheOps 'createGzip'; then
862 tmpfs add error "errorCreatingCompressedCache
"
867 output 2 'Removing temporary files '
868 tmpfs set message "$
(getStatusText
"statusProcessing"): removing temporary files
"
869 rm -f "/tmp
/${packageName}_tmp.
*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
870 if [ $j -eq 0 ]; then
874 tmpfs add error "errorRemovingTempFiles
"
884 rc_procd start_service 'on_boot' && rc_procd service_triggers
889 is_enabled 'on_start' || return 1
890 local action status error message stats c
891 if ! create_lock; then
892 output 3 "$serviceName: another instance is starting up
"; output_fail
896 status="$
(tmpfs get status
)"
897 error="$
(tmpfs get error
)"
898 message="$
(tmpfs get message
)"
899 stats="$
(tmpfs get stats
)"
900 action="$
(tmpfs get triggers
)"
902 if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
903 if cacheOps 'testGzip' || cacheOps 'test'; then
908 elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
910 elif [ ! -s "$outputFile" ]; then
911 if cacheOps 'testGzip' || cacheOps 'test'; then
916 elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
918 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess
" ] && [ -z "$error" ]; then
919 [ "$1" != 'hotplug' ] && showstatus
928 procd_open_instance 'main'
929 procd_set_param command /bin/true
930 procd_set_param stdout 1
931 procd_set_param stderr 1
933 json_add_array firewall
934 if [ "$forceDNS" -ne 0 ]; then
936 json_add_string type redirect
937 json_add_string name simple_adblock_dns_redirect
938 json_add_string target DNAT
939 json_add_string src lan
940 json_add_string proto tcpudp
941 json_add_string src_dport 53
942 json_add_string dest_port 53
943 json_add_string reflection 0
946 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
948 json_add_string type ipset
949 json_add_string name adb
950 json_add_string match dest_net
951 json_add_string storage hash
952 json_add_string enabled 1
955 json_add_string type rule
956 json_add_string name simple_adblock_ipset_rule
957 json_add_string ipset adb
958 json_add_string src lan
959 json_add_string dest '*'
960 json_add_string proto tcpudp
961 json_add_string target REJECT
962 json_add_string enabled 1
969 if [ "$action" = 'restore' ]; then
970 output 0 "Starting
$serviceName...
"
971 output 3 "Starting
$serviceName...
\\n
"
972 tmpfs set status "statusStarting
"
973 if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
974 output 3 'Found compressed cache file, unpacking it '
975 tmpfs set message 'found compressed cache file, unpacking it.'
976 if cacheOps 'unpackGzip'; then
980 tmpfs add error "errorRestoreCompressedCache
"
981 output "$_ERROR_: $
(getErrorText
'errorRestoreCompressedCache')!\\n
"
985 if cacheOps 'test' && [ ! -s "$outputFile" ]; then
986 output 3 'Found cache file, reusing it '
987 tmpfs set message 'found cache file, reusing it.'
988 if cacheOps 'restore'; then
993 tmpfs add error "errorRestoreCache
"
994 output "$_ERROR_: $
(getErrorText
'errorRestoreCache')!\\n
"
1001 if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
1002 output 0 "Force-reloading
$serviceName...
"
1003 output 3 "Force-reloading
$serviceName...
\\n
"
1004 tmpfs set status "statusForceReloading
"
1006 output 0 "Starting
$serviceName...
"
1007 output 3 "Starting
$serviceName...
\\n
"
1008 tmpfs set status "statusStarting
"
1014 output 0 "Restarting
$serviceName...
"
1015 output 3 "Restarting
$serviceName...
\\n
"
1016 tmpfs set status "statusRestarting
"
1020 output 0 "Starting
$serviceName...
"
1021 output 3 "Starting
$serviceName...
\\n
"
1022 tmpfs set status "statusStarting
"
1026 if [ -s "$outputFile" ] && [ "$
(tmpfs get status
)" != "statusFail
" ]; then
1027 output 0 "$__OK__\\n
";
1029 tmpfs set status "statusSuccess
"
1030 c="$
(wc -l < "$outputFile")"
1031 tmpfs set stats "$serviceName is blocking
$c domains
(with
${targetDNS})"
1034 output 0 "$__FAIL__\\n
";
1035 tmpfs set status "statusFail
"
1036 tmpfs add error "errorOhSnap
"
1042 service_started() { procd_set_config_changed firewall; }
1043 service_stopped() { procd_set_config_changed firewall; }
1044 restart_service() { rc_procd start_service 'restart'; }
1045 reload_service() { restart_service; }
1046 restart() { restart_service; }
1047 reload() { restart_service; }
1048 dl() { rc_procd start_service 'download'; }
1050 rm -f "$addnhostsCache" "$addnhostsGzip"
1051 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1052 rm -f "$ipsetCache" "$ipsetGzip"
1053 rm -f "$serversCache" "$serversGzip"
1054 rm -f "$unboundCache" "$unboundGzip"
1056 config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
1057 uci -q commit 'dhcp'
1061 show() { showstatus; }
1062 status_service() { showstatus; }
1064 local c url status message error stats
1065 status="$
(tmpfs get status
)"
1066 message="$
(tmpfs get message
)"
1067 error="$
(tmpfs get error
)"
1068 stats="$
(tmpfs get stats
)"
1069 if [ "$status" = "statusSuccess
" ]; then
1070 output "$stats "; output_okn;
1072 [ -n "$status" ] && status="$
(getStatusText
"$status")"
1073 if [ -n "$status" ] && [ -n "$message" ]; then
1074 status="${status}: $message"
1076 [ -n "$status" ] && output "$serviceName $status\\n
"
1078 if [ -n "$error" ]; then
1083 errorDownloadingList|errorParsingList)
1084 output "$_ERROR_: $
(getErrorText
"$c") $url!\\n
";;
1086 output "$_ERROR_: $
(getErrorText
"$c")!\\n
";;
1095 if [ -s "$outputFile" ]; then
1096 output "Stopping
$serviceName...
"
1098 if dnsOps 'on_stop'; then
1100 output 0 "$__OK__\\n
"; output_okn;
1101 tmpfs set status "statusStopped
"
1104 output 0 "$__FAIL__\\n
"; output_fail;
1105 tmpfs set status "statusFail
"
1106 tmpfs add error "errorStopping
"
1107 output "$_ERROR_: $
(getErrorText
'errorStopping')!\\n
"
1112 service_triggers() {
1114 procd_add_config_trigger "config.change
" "${packageName}" /etc/init.d/${packageName} reload
1121 c="$
(grep -c "$string" "$outputFile")"
1122 if [ ! -s "$outputFile" ]; then
1123 echo "No block-list
('$outputFile') found.
"
1124 elif [ -z "$string" ]; then
1125 echo "Usage
: /etc
/init.d
/${packageName} check string
"
1126 elif [ "$c" -gt 0 ]; then
1127 if [ "$c" -gt 1 ]; then
1128 echo "Found
$c matches
for '$string' in '$outputFile':"
1130 echo "Found
1 match
for '$string' in '$outputFile':"
1132 case "$targetDNS" in
1134 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1136 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1138 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1140 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1142 grep "$string" "$outputFile" | sed 's|^local-zone: "||
;s|
" static$||;';;
1145 echo "The
$string is not found
in current block-list
('$outputFile').
"
1154 for i
in $blocked_domains_urls; do
1155 [ "${i//melmac}" != "$i" ] && continue
1156 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1157 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1158 if compare_values
"$(du -sk /tmp/sast)" "500"; then
1159 echo "# block-list too big for most routers"
1160 elif compare_values
"$(du -sk /tmp/sast)" "100"; then
1161 echo "# block-list may be too big for some routers"
1164 echo " list blocked_domains_url '$i'"
1167 echo "# site was down on last check"
1168 echo "# list blocked_domains_url '$i'"
1173 for i
in $blocked_hosts_urls; do
1174 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1175 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1176 if compare_values
"$(du -sk /tmp/sast)" "500"; then
1177 echo "# block-list too big for most routers"
1178 elif compare_values
"$(du -sk /tmp/sast)" "100"; then
1179 echo "# block-list may be too big for some routers"
1182 echo " list blocked_hosts_url '$i'"
1185 echo "# site was down on last check"
1186 echo "# list blocked_hosts_url '$i'"