1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2016,SC3043,SC3057,SC3060
5 # shellcheck disable=SC2034
7 # shellcheck disable=SC2034
11 if type extra_command
1>/dev
/null
2>&1; then
12 extra_command
'allow' 'Allows domain in current block-list and config'
13 extra_command
'check' 'Checks if specified domain is found in current block-list'
14 extra_command
'dl' 'Force-downloads all enabled block-list'
15 extra_command
'killcache' 'Delete all cached files'
16 extra_command
'sizes' 'Displays the file-sizes of enabled block-lists'
17 extra_command
'version' 'Show version information'
19 # shellcheck disable=SC2034
20 EXTRA_COMMANDS
='allow check dl killcache sizes status_service version'
21 # shellcheck disable=SC2034
22 EXTRA_HELP
=' allow Allows domain(s) in current block-list and config
23 check Checks if specified domain is found in current block-list
24 dl Force-downloads all enabled block-list
25 sizes Displays the file-sizes of enabled block-lists'
28 readonly PKG_VERSION
='dev-test'
29 readonly packageName
='simple-adblock'
30 readonly serviceName
="$packageName $PKG_VERSION"
31 readonly packageConfigFile
="/etc/config/${packageName}"
32 readonly dnsmasqAddnhostsFile
="/var/run/${packageName}/dnsmasq.addnhosts"
33 readonly dnsmasqAddnhostsCache
="/var/run/${packageName}/dnsmasq.addnhosts.cache"
34 readonly dnsmasqAddnhostsGzip
="/etc/${packageName}.dnsmasq.addnhosts.gz"
35 readonly dnsmasqAddnhostsFilter
='s|^|127.0.0.1 |;s|$||'
36 readonly dnsmasqAddnhostsFilterIPv6
='s|^|:: |;s|$||'
37 readonly dnsmasqConfFile
="/tmp/dnsmasq.d/${packageName}"
38 readonly dnsmasqConfCache
="/var/run/${packageName}/dnsmasq.conf.cache"
39 readonly dnsmasqConfGzip
="/etc/${packageName}.dnsmasq.conf.gz"
40 readonly dnsmasqConfFilter
='s|^|local=/|;s|$|/|'
41 readonly dnsmasqIpsetFile
="/tmp/dnsmasq.d/${packageName}.ipset"
42 readonly dnsmasqIpsetCache
="/var/run/${packageName}/dnsmasq.ipset.cache"
43 readonly dnsmasqIpsetGzip
="/etc/${packageName}.dnsmasq.ipset.gz"
44 readonly dnsmasqIpsetFilter
='s|^|ipset=/|;s|$|/adb|'
45 readonly dnsmasqNftsetFile
="/tmp/dnsmasq.d/${packageName}.nftset"
46 readonly dnsmasqNftsetCache
="/var/run/${packageName}/dnsmasq.nftset.cache"
47 readonly dnsmasqNftsetGzip
="/etc/${packageName}.dnsmasq.nftset.gz"
48 readonly dnsmasqNftsetFilter
='s|^|nftset=/|;s|$|/4#inet#fw4#adb4|'
49 readonly dnsmasqNftsetFilterIPv6
='s|^|nftset=/|;s|$|/4#inet#fw4#adb4,6#inet#fw4#adb6|'
50 readonly dnsmasqServersFile
="/var/run/${packageName}/dnsmasq.servers"
51 readonly dnsmasqServersCache
="/var/run/${packageName}/dnsmasq.servers.cache"
52 readonly dnsmasqServersGzip
="/etc/${packageName}.dnsmasq.servers.gz"
53 readonly dnsmasqServersFilter
='s|^|server=/|;s|$|/|'
54 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
55 readonly unboundCache
="/var/run/${packageName}/unbound.cache"
56 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
57 readonly unboundFilter
='s|^|local-zone: "|;s|$|" static|'
58 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
59 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
60 readonly jsonFile
="/var/run/${packageName}/${packageName}.json"
61 readonly sharedMemoryError
="/dev/shm/$packageName-error"
62 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
63 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;'
64 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
65 readonly adBlockPlusFilter
='/^#/d;/^!/d;s/[[:space:]]*#.*$//;s/^||//;s/\^$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
66 readonly dnsmasqFileFilter
='\|^server=/[[:alnum:]_.-].*/|!d'
67 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
68 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
69 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
70 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
71 readonly _ERROR_
='\033[0;31mERROR\033[0m'
72 readonly _WARNING_
='\033[0;33mWARNING\033[0m'
73 # shellcheck disable=SC2155
74 readonly ipset
="$(command -v ipset)"
75 # shellcheck disable=SC2155
76 readonly nft
="$(command -v nft)"
77 readonly canaryDomainsMozilla
='use-application-dns.net'
78 readonly canaryDomainsiCloud
='mask.icloud.com mask-h2.icloud.com'
89 load_environment_flag
=
91 debug
() { local i j
; for i
in "$@"; do eval "j=\$$i"; echo "${i}: ${j} "; done; }
93 uci_add_list_if_new() {
99 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
100 for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
101 [ "$i" = "$VALUE" ] && return 0
103 uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
109 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
112 ipset
() { "$ipset" "$@" >/dev
/null
2>&1; }
113 nft
() { "$nft" "$@" >/dev
/null
2>&1; }
118 errorConfigValidationFail
) r
="$packageName config validation failed";;
119 errorServiceDisabled
) r
="$packageName is currently disabled";;
121 r
="dnsmasq ipset support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support ipset";;
123 r
="dnsmasq ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type";;
124 errorNoDnsmasqNftset
)
125 r
="dnsmasq nft set support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support nft set";;
126 errorNoNft
) r
="dnsmasq nft sets support is enabled in $packageName, but nft is not installed";;
127 errorNoWanGateway
) r
="The ${serviceName} failed to discover WAN gateway";;
128 errorOutputDirCreate
) r
="failed to create directory for %s file";;
129 errorOutputFileCreate
) r
="failed to create $outputFile file";;
130 errorFailDNSReload
) r
="failed to restart/reload DNS resolver";;
131 errorSharedMemory
) r
="failed to access shared memory";;
132 errorSorting
) r
="failed to sort data file";;
133 errorOptimization
) r
="failed to optimize data file";;
134 errorAllowListProcessing
) r
="failed to process allow-list";;
135 errorDataFileFormatting
) r
="failed to format data file";;
136 errorMovingDataFile
) r
="failed to move data file '${A_TMP}' to '${outputFile}'";;
137 errorCreatingCompressedCache
) r
="failed to create compressed cache";;
138 errorRemovingTempFiles
) r
="failed to remove temporary files";;
139 errorRestoreCompressedCache
) r
="failed to unpack compressed cache";;
140 errorRestoreCache
) r
="failed to move '$outputCache' to '$outputFile'";;
141 errorOhSnap
) r
="failed to create block-list or restart DNS resolver";;
142 errorStopping
) r
="failed to stop $serviceName";;
143 errorDNSReload
) r
="failed to reload/restart DNS resolver";;
144 errorDownloadingConfigUpdate
) r
="failed to download Config Update file";;
145 errorDownloadingList
) r
="failed to download";;
146 errorParsingConfigUpdate
) r
="failed to parse Config Update file";;
147 errorParsingList
) r
="failed to parse";;
148 errorNoSSLSupport
) r
="no HTTPS/SSL support on device";;
149 errorCreatingDirectory
) r
="failed to create output/cache/gzip file directory";;
151 statusNoInstall
) r
="$serviceName is not installed or not found";;
152 statusStopped
) r
="Stopped";;
153 statusStarting
) r
="Starting";;
154 statusRestarting
) r
="Restarting";;
155 statusForceReloading
) r
="Force Reloading";;
156 statusDownloading
) r
="Downloading";;
157 statusProcessing
) r
="Processing";;
158 statusError
) r
="Error";;
159 statusWarning
) r
="Warning";;
160 statusFail
) r
="Fail";;
161 statusSuccess
) r
="Success";;
163 warningExternalDnsmasqConfig
)
164 r
="use of external dnsmasq config file detected, please set 'dns' option to 'dnsmasq.conf'";;
165 warningMissingRecommendedPackages
) r
="Some recommended packages are missing";;
170 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
171 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
172 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
173 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
174 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
175 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
176 is_greater
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
177 is_greater_or_equal
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" = "$2"; }
178 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
179 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
180 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
181 dnsmasq_hup
() { killall
-q -s HUP dnsmasq
; }
182 dnsmasq_kill
() { killall
-q -s KILL dnsmasq
; }
183 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
184 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
185 is_present
() { command -v "$1" >/dev
/null
2>&1; }
188 # Can take a single parameter (text) to be output at any verbosity
189 # Or target verbosity level and text to be output at specifc verbosity
190 local msg memmsg logmsg
191 verbosity
="${verbosity:-2}"
192 if [ $# -ne 1 ]; then
193 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
195 [ -t 1 ] && printf "%b" "$1"
196 msg
="${1//$serviceName /service }";
197 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
198 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
199 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
200 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
201 rm -f "$sharedMemoryOutput"
203 printf "%b" "$msg" >> "$sharedMemoryOutput"
209 local i j wan_if wan_gw wan_proto
210 local counter wan_if_timeout
="$procd_boot_wan_timeout" wan_gw_timeout
='5'
212 while [ -z "$wan_if" ]; do
214 network_find_wan wan_if
215 if [ -n "$wan_if" ]; then
216 output
"WAN interface found: '${wan_if}'.\\n"
219 if [ "$counter" -gt "$wan_if_timeout" ]; then
220 output
"WAN interface timeout, assuming 'wan'.\\n"
224 counter
=$
((counter
+1))
225 output
"Waiting to discover WAN Interface...\\n"
230 wan_proto
="$(uci -q get "network.
${wan_if}.proto
")"
231 if [ "$wan_proto" = 'pppoe' ]; then
232 wan_gw_timeout
=$
((wan_gw_timeout
+10))
234 while [ "$counter" -le "$wan_gw_timeout" ]; do
236 network_get_gateway wan_gw
"$wan_if"
237 if [ -n "$wan_gw" ]; then
238 output
"WAN gateway found: '${wan_gw}.'\\n"
241 counter
=$
((counter
+1))
242 output
"Waiting to discover $wan_if gateway...\\n"
245 json add error
"errorNoWanGateway"
246 output
"${_ERROR_}: $(get_text 'errorNoWanGateway')!\\n"; return 1;
251 local validation_result
="$1" param
="$2"
253 [ -z "$load_environment_flag" ] ||
return 0
255 if [ "$validation_result" != '0' ]; then
256 json add error
"errorConfigValidationFail"
257 output
"${_ERROR_}: $(get_text 'errorConfigValidationFail')!\\n"
258 output
"Please check if the '$packageConfigFile' contains correct values for config options.\\n"
262 if [ "$enabled" -eq 0 ]; then
263 json add error
"errorServiceDisabled"
264 output
"${_ERROR_}: $(get_text 'errorServiceDisabled')!\\n"
265 output
"Run the following commands before starting service again:\\n"
266 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
270 if [ "$debug" -ne 0 ]; then
271 exec 1>>/tmp
/simple-adblock.log
276 if [ -n "$dnsmasq_config_file_url" ]; then
280 if [ "$param" != 'quiet' ]; then
281 json add warning
"warningExternalDnsmasqConfig"
282 output
"${_WARNING_}: $(get_text 'warningExternalDnsmasqConfig')!\\n"
289 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
290 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
300 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
301 if [ "$param" != 'quiet' ]; then
302 json add error
"errorNoDnsmasqIpset"
303 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqIpset')!\\n"
305 dns
='dnsmasq.servers'
307 if ! ipset
help hash:net
; then
308 if [ "$param" != 'quiet' ]; then
309 json add error
"errorNoIpset"
310 output
"${_ERROR_}: $(get_text 'errorNoIpset')!\\n"
312 dns
='dnsmasq.servers'
316 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-nftset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'nftset'; then
317 if [ "$param" != 'quiet' ]; then
318 json add error
"errorNoDnsmasqNftset"
319 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqNftset')!\\n"
321 dns
='dnsmasq.servers'
323 if [ -z "$nft" ]; then
324 if [ "$param" != 'quiet' ]; then
325 json add error
"errorNoNft"
326 output
"${_ERROR_}: $(get_text 'errorNoNft')!\\n"
328 dns
='dnsmasq.servers'
335 outputFilter
="$dnsmasqAddnhostsFilter"
336 outputFile
="$dnsmasqAddnhostsFile"
337 outputCache
="$dnsmasqAddnhostsCache"
338 outputGzip
="$dnsmasqAddnhostsGzip"
339 if [ "$ipv6_enabled" -ne 0 ]; then
340 outputFilterIPv6
="$dnsmasqAddnhostsFilterIPv6"
342 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
343 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
344 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
345 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
346 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
349 outputFilter
="$dnsmasqConfFilter"
350 outputFile
="$dnsmasqConfFile"
351 outputCache
="$dnsmasqConfCache"
352 outputGzip
="$dnsmasqConfGzip"
353 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
354 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
355 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
356 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
357 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
360 outputFilter
="$dnsmasqIpsetFilter"
361 outputFile
="$dnsmasqIpsetFile"
362 outputCache
="$dnsmasqIpsetCache"
363 outputGzip
="$dnsmasqIpsetGzip"
364 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
365 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
366 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
367 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
368 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
371 if [ "$ipv6_enabled" -ne 0 ]; then
372 outputFilter
="$dnsmasqNftsetFilterIPv6"
374 outputFilter
="$dnsmasqNftsetFilter"
376 outputFile
="$dnsmasqNftsetFile"
377 outputCache
="$dnsmasqNftsetCache"
378 outputGzip
="$dnsmasqNftsetGzip"
379 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
380 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
381 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
382 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
383 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
386 outputFilter
="$dnsmasqServersFilter"
387 outputFile
="$dnsmasqServersFile"
388 outputCache
="$dnsmasqServersCache"
389 outputGzip
="$dnsmasqServersGzip"
390 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
391 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
392 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
393 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
394 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
397 outputFilter
="$unboundFilter"
398 outputFile
="$unboundFile"
399 outputCache
="$unboundCache"
400 outputGzip
="$unboundGzip"
401 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
402 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
403 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
404 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
405 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
409 for i
in "$jsonFile" "$outputFile" "$outputCache" "$outputGzip"; do
410 if ! mkdir
-p "$(dirname "$i")"; then
411 if [ "$param" != 'quiet' ]; then
412 json add error
"errorOutputDirCreate" "$i"
413 output
"${_ERROR_}: $(get_text 'errorOutputDirCreate' "$i")!\\n"
418 is_present
'gawk' && awk='gawk'
419 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
420 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
421 local s
="opkg update; opkg --force-overwrite install"
422 is_present
'gawk' || s
="$s gawk"
423 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
424 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
425 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
426 if [ "$param" != 'quiet' ]; then
427 json add warning
"warningMissingRecommendedPackages" "${i}"
428 output
"${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\\n"
432 # Prefer curl because it supports the file:// scheme.
433 if is_present
'curl'; then
434 dl_command
="curl --silent --insecure"
435 dl_command
="${dl_command}${curl_max_file_size:+ --max-filesize $curl_max_file_size}"
436 dl_command
="${dl_command}${curl_retry:+ --retry $curl_retry}"
437 dl_command
="${dl_command}${download_timeout:+ --connect-timeout $download_timeout}"
439 elif is_present
'/usr/libexec/wget-ssl'; then
440 dl_command
="/usr/libexec/wget-ssl --no-check-certificate -q"
441 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
443 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
444 dl_command
="wget --no-check-certificate -q"
445 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
448 dl_command
="uclient-fetch --no-check-certificate -q"
449 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
452 led
="${led:+/sys/class/leds/$led}"
453 if curl
--version 2>/dev
/null |
grep -q "https" \
454 || wget
--version 2>/dev
/null |
grep -q "+https" \
455 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
456 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
457 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
462 load_environment_flag
=1
463 cache
'test' && return 0
464 cache
'test_gzip' && return 0
465 if [ "$param" = 'on_boot' ]; then
466 load_network
"$param"
474 local cfg
="$1" param
="$2"
477 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
478 uci_remove
'dhcp' "$cfg" 'serversfile'
480 uci_add_list_if_new
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
482 cleanup|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list
)
483 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
484 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
485 uci_remove
'dhcp' "$cfg" 'serversfile'
489 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
490 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
491 uci_set
'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
498 local param output_text i
501 if [ ! -s "$outputFile" ]; then
502 json
set status
"statusFail"
503 json add error
"errorOutputFileCreate"
504 output
"${_ERROR_}: $(get_text 'errorOutputFileCreate')!\\n"
509 if [ "$dns_instance" = "*" ]; then
510 config_foreach resolver
'dnsmasq' "$dns"
511 elif [ -n "$dns_instance" ]; then
512 for i
in $dns_instance; do
513 resolver
"@dnsmasq[$i]" "$dns" || resolver
"$i" "$dns"
518 dnsmasq.addnhosts|dnsmasq.servers
)
519 param
=dnsmasq_restart
520 output_text
='Reloading dnsmasq'
522 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
523 param
=dnsmasq_restart
524 output_text
='Restarting dnsmasq'
527 param
=unbound_restart
528 output_text
='Restarting Unbound'
532 if [ -n "$(uci_changes dhcp)" ]; then
534 if [ "$param" = 'unbound_restart' ]; then
535 param
='dnsmasq_restart; unbound_restart;'
536 output_text
='Restarting Unbound/dnsmasq'
538 param
=dnsmasq_restart
539 output_text
='Restarting dnsmasq'
542 output
1 "$output_text "
543 output
2 "$output_text "
544 json
set message
"$output_text"
545 if eval "$param"; then
546 json
set status
"statusSuccess"
551 json
set status
"statusFail"
552 json add error
"errorDNSReload"
553 output
"${_ERROR_}: $(get_text 'errorDNSReload')!\\n"
559 dnsmasq.addnhosts|dnsmasq.servers
)
560 param
=dnsmasq_restart
562 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
563 param
=dnsmasq_restart
566 param
=unbound_restart
569 if [ -n "$(uci_changes dhcp)" ]; then
571 if [ "$param" = 'unbound_restart' ]; then
572 param
='dnsmasq_restart; unbound_restart;'
574 param
=dnsmasq_restart
582 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
583 param
=dnsmasq_restart
586 param
=unbound_restart
596 # shellcheck disable=SC2034
597 local action
="$1" param
="$2" value
="$3"
599 # shellcheck disable=SC2124
600 local extras
="$@" line
601 local status message error stats
602 local reload restart curReload curRestart ret i
603 if [ -s "$jsonFile" ]; then
604 json_load_file
"$jsonFile" 2>/dev
/null
605 json_select
'data' 2>/dev
/null
606 for i
in status message error stats reload restart
; do
607 json_get_var
$i "$i" 2>/dev
/null
614 curReload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
615 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
616 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
617 curRestart
="$compressed_cache $force_dns $led $force_dns_port"
618 if [ ! -s "$jsonFile" ]; then
620 elif [ "$curReload" != "$reload" ]; then
622 elif [ "$curRestart" != "$restart" ]; then
628 printf "%b" "$(eval echo "\$
$param")"; return;;
632 line
="$(eval echo "\$
$param")"
633 eval "$param"='${line:+$line }${value}${extras:+|$extras}'
638 unset status message error stats
;;
640 unset reload restart
;;
648 reload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
649 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
650 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
651 restart
="$compressed_cache $force_dns $led $force_dns_port"
654 eval "$param"='${value}${extras:+|$extras}';;
659 json_add_object
'data'
660 json_add_string version
"$PKG_VERSION"
661 json_add_string status
"$status"
662 json_add_string message
"$message"
663 json_add_string error
"$error"
664 json_add_string stats
"$stats"
665 json_add_string reload
"$reload"
666 json_add_string restart
"$restart"
668 mkdir
-p "$(dirname "$jsonFile")"
669 json_dump
> "$jsonFile"
677 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
681 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
685 [ -s "$outputCache" ]
689 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
693 rm -f "$outputGzip" >/dev
/null
2>/dev
/null
694 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
695 if gzip < "$outputFile" > "$R_TMP"; then
696 if mv "$R_TMP" "$outputGzip"; then
707 expand|unpack|unpack_gzip
)
708 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
715 local label
type D_TMP R_TMP
716 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
717 label
="${1##*//}"; label
="${label%%/*}";
719 adbp
) label
="ADBPlus: $label"; filter
="$adBlockPlusFilter"
721 dnsmasq
) label
="Dnsmasq: $label"; filter
="$dnsmasqFileFilter"
723 domains
) label
="Domains: $label"; filter
="$domainsFilter"
725 hosts
) label
="Hosts: $label"; filter
="$hostsFilter"
729 allowed
) type='Allowed'; D_TMP
="$A_TMP"
731 blocked
) type='Blocked'; D_TMP
="$B_TMP"
733 file) type='File'; D_TMP
="$B_TMP"
736 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
738 output
2 "[DL] $type $label $__FAIL__\\n"
739 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
742 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
743 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
745 if ! $dl_command "$1" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
747 output
2 "[DL] $type $label $__FAIL__\\n"
748 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
750 sed -i "$filter" "$R_TMP"
751 if [ ! -s "$R_TMP" ]; then
753 output
2 "[DL] $type $label $__FAIL__\\n"
754 echo "errorParsingList|${1}" >> "$sharedMemoryError"
756 cat "${R_TMP}" >> "$D_TMP"
758 output
2 "[DL] $type $label $__OK__\\n"
765 download_dnsmasq_file
() {
766 local hf allow_filter j
=0 R_TMP
768 json
set message
"$(get_text "statusDownloading
")..."
769 json
set status
"statusDownloading"
771 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
772 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
773 output
3 'Low free memory, restarting resolver '
780 touch $A_TMP; touch $B_TMP;
781 output
1 'Downloading dnsmasq file '
782 rm -f "$sharedMemoryError"
783 process_url
"$dnsmasq_config_file_url" 'dnsmasq' 'file'
785 if [ -s "$sharedMemoryError" ]; then
786 while IFS
= read -r line
; do
787 json add error
"$line"
788 done < "$sharedMemoryError"
789 rm -f "$sharedMemoryError"
791 output
2 'Creating dnsmasq file '
792 if mv "$B_TMP" "$outputFile"; then
793 output
2 "$__OK__\\n"
795 output
2 "$__FAIL__\\n"
796 json add error
"errorMovingDataFile"
802 local hf allow_filter j
=0 R_TMP
804 json
set message
"$(get_text "statusDownloading
")..."
805 json
set status
"statusDownloading"
807 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
808 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
809 output
3 'Low free memory, restarting resolver '
816 touch $A_TMP; touch $B_TMP;
817 output
1 'Downloading lists '
818 rm -f "$sharedMemoryError"
819 for hf
in ${blocked_hosts_url}; do
820 if [ "$parallel_downloads" -gt 0 ]; then
821 process_url
"$hf" 'hosts' 'blocked' &
823 process_url
"$hf" 'hosts' 'blocked'
826 for hf
in ${blocked_adblockplus_url}; do
827 if [ "$parallel_downloads" -gt 0 ]; then
828 process_url
"$hf" 'adbp' 'blocked' &
830 process_url
"$hf" 'adbp' 'blocked'
833 for hf
in ${blocked_domains_url}; do
834 if [ "$parallel_downloads" -gt 0 ]; then
835 process_url
"$hf" 'domains' 'blocked' &
837 process_url
"$hf" 'domains' 'blocked'
840 for hf
in ${allowed_domains_url}; do
841 if [ "$parallel_downloads" -gt 0 ]; then
842 process_url
"$hf" 'domains' 'allowed' &
844 process_url
"$hf" 'domains' 'allowed'
849 if [ -s "$sharedMemoryError" ]; then
850 while IFS
= read -r line
; do
851 json add error
"$line"
852 done < "$sharedMemoryError"
853 rm -f "$sharedMemoryError"
856 if [ "$canary_domains_icloud" -ne 0 ]; then
857 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
859 if [ "$canary_domains_mozilla" -ne 0 ]; then
860 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
863 for hf
in $blocked_domain $canaryDomains; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
864 allowed_domain
="${allowed_domain}
866 for hf
in ${allowed_domain}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; allow_filter
="$allow_filter/(^|\.)${hf}$/d;"; done
868 [ ! -s "$B_TMP" ] && return 1
870 output
1 'Processing downloads '
871 output
2 'Sorting combined list '
872 json
set status
"statusProcessing"
873 json
set message
"$(get_text "statusProcessing
"): sorting combined list"
874 if [ "$allow_non_ascii" -gt 0 ]; then
875 if sort -u "$B_TMP" > "$A_TMP"; then
879 json add error
"errorSorting"
882 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
886 json add error
"errorSorting"
890 if [ "$dns" = 'dnsmasq.conf' ] || \
891 [ "$dns" = 'dnsmasq.ipset' ] || \
892 [ "$dns" = 'dnsmasq.nftset' ] || \
893 [ "$dns" = 'dnsmasq.servers' ] || \
894 [ "$dns" = 'unbound.adb_list' ]; then
895 # TLD optimization written by Dirk Brenken (dev@brenken.org)
896 output
2 'Optimizing combined list '
897 json
set message
"$(get_text "statusProcessing
"): optimizing combined list"
898 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
899 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
900 if sort "$B_TMP" > "$A_TMP"; then
901 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
902 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
903 if sort -u "$A_TMP" > "$B_TMP"; then
907 json add error
"errorOptimization"
912 json add error
"errorOptimization"
916 json add error
"errorOptimization"
921 json add error
"errorOptimization"
925 json add error
"errorOptimization"
932 output
2 'Allowing domains '
933 json
set message
"$(get_text "statusProcessing
"): allowing domains"
934 if sed -i -E "$allow_filter" "$B_TMP"; then
938 json add error
"errorAllowListProcessing"
941 output
2 'Formatting merged file '
942 json
set message
"$(get_text "statusProcessing
"): formatting merged file"
943 if [ -z "$outputFilterIPv6" ]; then
944 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
948 json add error
"errorDataFileFormatting"
953 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
954 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
958 json add error
"errorDataFileFormatting"
966 output
2 'Creating dnsmasq addnhosts file '
967 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq addnhosts file"
970 output
2 'Creating dnsmasq config file '
971 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq config file"
974 output
2 'Creating dnsmasq ipset file '
975 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq ipset file"
978 output
2 'Creating dnsmasq nft set file '
979 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq nft set file"
982 output
2 'Creating dnsmasq servers file '
983 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq servers file"
986 output
2 'Creating Unbound adb_list file '
987 json
set message
"$(get_text "statusProcessing
"): creating Unbound adb_list file"
991 if mv "$A_TMP" "$outputFile"; then
995 json add error
"errorMovingDataFile"
997 if [ "$compressed_cache" -gt 0 ]; then
998 output
2 'Creating compressed cache '
999 json
set message
"$(get_text "statusProcessing
"): creating compressed cache"
1000 if cache
'create_gzip'; then
1004 json add error
"errorCreatingCompressedCache"
1009 output
2 'Removing temporary files '
1010 json
set message
"$(get_text "statusProcessing
"): removing temporary files"
1011 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
1012 if [ $j -eq 0 ]; then
1016 json add error
"errorRemovingTempFiles"
1022 local c hf string
="$1"
1023 local validation_result
="$3"
1024 load_environment
"$validation_result" 'quiet' ||
return 1
1025 if [ ! -s "$outputFile" ]; then
1026 output
"No block-list ('$outputFile') found.\\n"
1028 elif [ -z "$string" ]; then
1029 output
"Usage: /etc/init.d/${packageName} allow 'domain' ...\\n"
1031 elif [ -n "$dnsmasq_config_file_url" ]; then
1032 output
"Allowing individual domains is not possible when using external dnsmasq config file.\\n"
1036 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
1037 output
1 "Allowing domain(s) and restarting dnsmasq "
1038 output
2 "Allowing domain(s) \\n"
1039 for c
in $string; do
1041 hf
="$(echo "$c" | sed 's/\./\\./g')"
1042 if sed -i "/(^|\.)${hf}$/d;" "$outputFile" && \
1043 uci_add_list_if_new
"${packageName}" 'config' 'allowed_domain' "$c"; then
1049 if [ "$compressed_cache" -gt 0 ]; then
1050 output
2 'Creating compressed cache '
1051 if cache
'create_gzip'; then
1057 output
2 "Committing changes to config "
1058 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1059 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1061 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1063 if [ "$dns" = 'dnsmasq.ipset' ]; then
1064 output
2 "Flushing adb ipset "
1065 if ipset
-q -! flush adb
; then output_ok
; else output_fail
; fi
1067 if [ "$dns" = 'dnsmasq.nftset' ]; then
1068 output
2 "Flushing adb nft sets "
1069 nft flush
set inet fw4 adb6
1070 if nft flush
set inet fw4 adb4
; then output_ok
; else output_fail
; fi
1072 output
2 "Restarting dnsmasq "
1073 if dnsmasq_restart
; then output_okn
; else output_failn
; fi
1079 output
1 "Allowing domain(s) and restarting Unbound "
1080 output
2 "Allowing domain(s) \\n"
1081 for c
in $string; do
1083 if sed -i "/${string}/d" "$outputFile" && \
1084 uci_add_list_if_new
"$packageName" 'config' 'allowed_domain' "$string"; then
1090 if [ "$compressed_cache" -gt 0 ]; then
1091 output
2 'Creating compressed cache '
1092 if cache
'create_gzip'; then
1098 output
2 "Committing changes to config "
1099 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1100 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1102 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1104 output
2 "Restarting Unbound "
1105 if unbound_restart
; then output_okn
; else output_failn
; fi
1115 local validation_result
="$3"
1116 load_environment
"$validation_result" 'quiet' ||
return 1
1117 if [ ! -s "$outputFile" ]; then
1118 output
"No block-list ('$outputFile') found.\\n"
1120 elif [ -z "$param" ]; then
1121 output
"Usage: /etc/init.d/${packageName} check 'domain' ...\\n"
1124 for string
in ${param}; do
1125 c
="$(grep -c "$string" "$outputFile")"
1126 if [ "$c" -gt 0 ]; then
1127 if [ "$c" -eq 1 ]; then
1128 output
"Found 1 match for '$string' in '$outputFile'.\\n"
1130 output
"Found $c matches for '$string' in '$outputFile'.\\n"
1132 if [ "$c" -le 20 ]; then
1135 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1137 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1139 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1141 grep "$string" "$outputFile" |
sed 's|nftset=/||;s|/4#inet#adb#adb4||;';;
1143 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1145 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1149 output
"The '$string' is not found in current block-list ('$outputFile').\\n"
1154 adb_config_update
() {
1156 local param validation_result
="$3"
1158 on_boot
) param
="$1";;
1161 load_environment
"$validation_result" "$param" ||
return 1
1162 label
="${config_update_url##*//}"
1163 label
="${label%%/*}";
1164 [ "$config_update_enabled" -ne 0 ] ||
return 0
1166 if [ "$param" != 'download' ]; then
1167 cache
'test' && return 0
1168 cache
'test_gzip' && return 0
1170 output
1 'Updating config '
1171 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
1172 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
1174 if ! $dl_command "$config_update_url" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
1175 output
1 "$_FAIL_\\n"
1176 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1177 json add error
"errorDownloadingConfigUpdate"
1179 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i "$packageConfigFile" 2>/dev
/null
; then
1181 output
2 "[DL] Config Update: $label $__OK__\\n"
1183 output
1 "$_FAIL_\\n"
1184 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1185 json add error
"errorParsingConfigUpdate"
1194 local validation_result
="$3"
1195 load_environment
"$validation_result" 'quiet' ||
return 1
1198 for i
in ${blocked_adblockplus_url}; do
1199 [ "${i//melmac}" != "$i" ] && continue
1200 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1201 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1202 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1203 echo "# block-list too big for most routers"
1204 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1205 echo "# block-list may be too big for some routers"
1208 echo " list blocked_adblockplus_url '$i'"
1211 echo "# site was down on last check"
1212 echo "# list blocked_adblockplus_url '$i'"
1216 for i
in ${blocked_domains_url}; do
1217 [ "${i//melmac}" != "$i" ] && continue
1218 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1219 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1220 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1221 echo "# block-list too big for most routers"
1222 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1223 echo "# block-list may be too big for some routers"
1226 echo " list blocked_domains_url '$i'"
1229 echo "# site was down on last check"
1230 echo "# list blocked_domains_url '$i'"
1234 for i
in ${blocked_hosts_url}; do
1235 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1236 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1237 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1238 echo "# block-list too big for most routers"
1239 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1240 echo "# block-list may be too big for some routers"
1243 echo " list blocked_hosts_url '$i'"
1246 echo "# site was down on last check"
1247 echo "# list blocked_hosts_url '$i'"
1254 local action status error message stats c
1255 local param
="$1" validation_result
="$3"
1257 load_environment
"$validation_result" "$param" ||
return 1
1259 status
="$(json get status)"
1260 error
="$(json get error)"
1261 message
="$(json get message)"
1262 stats
="$(json get stats)"
1263 action
="$(json get triggers)"
1265 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
1266 if cache
'test_gzip' || cache
'test'; then
1271 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
1273 elif [ ! -s "$outputFile" ]; then
1274 if cache
'test_gzip' || cache
'test'; then
1279 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
1281 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
1291 if [ "$action" = 'restore' ]; then
1292 output
0 "Starting $serviceName... "
1293 output
3 "Starting $serviceName...\\n"
1294 json
set status
"statusStarting"
1295 if cache
'test_gzip' && ! cache
'test' && [ ! -s "$outputFile" ]; then
1296 output
3 'Found compressed cache file, unpacking it '
1297 json
set message
'found compressed cache file, unpacking it.'
1298 if cache
'unpack_gzip'; then
1302 json add error
"errorRestoreCompressedCache"
1303 output
"${_ERROR_}: $(get_text 'errorRestoreCompressedCache')!\\n"
1307 if cache
'test' && [ ! -s "$outputFile" ]; then
1308 output
3 'Found cache file, reusing it '
1309 json
set message
'found cache file, reusing it.'
1310 if cache
'restore'; then
1315 json add error
"errorRestoreCache"
1316 output
"${_ERROR_}: $(get_text 'errorRestoreCache')!\\n"
1321 if [ "$action" = 'download' ]; then
1322 if [ -s "$outputFile" ] || cache
'test' || cache
'test_gzip'; then
1323 output
0 "Force-reloading $serviceName... "
1324 output
3 "Force-reloading $serviceName...\\n"
1325 json
set status
"statusForceReloading"
1327 output
0 "Starting $serviceName... "
1328 output
3 "Starting $serviceName...\\n"
1329 json
set status
"statusStarting"
1331 if [ -n "$dnsmasq_config_file_url" ]; then
1332 download_dnsmasq_file
1338 if [ "$action" = 'restart' ]; then
1339 output
0 "Restarting $serviceName... "
1340 output
3 "Restarting $serviceName...\\n"
1341 json
set status
"statusRestarting"
1344 if [ "$action" = 'start' ]; then
1345 output
0 "Starting $serviceName... "
1346 output
3 "Starting $serviceName...\\n"
1347 json
set status
"statusStarting"
1350 if [ -s "$outputFile" ] && [ "$(json get status)" != "statusFail" ]; then
1351 output
0 "$__OK__\\n";
1353 json
set status
"statusSuccess"
1354 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1357 output
0 "$__FAIL__\\n";
1358 json
set status
"statusFail"
1359 json add error
"errorOhSnap"
1363 procd_open_instance
'main'
1364 procd_set_param
command /bin
/true
1365 procd_set_param stdout
1
1366 procd_set_param stderr
1
1368 json_add_string
'status' "$(json get status)"
1369 json_add_string
'errors' "$(json get errors)"
1370 if [ -s "$outputFile" ]; then
1371 json_add_int
'entries' "$(wc -l < "$outputFile")"
1373 json_add_int
'entries' '0'
1375 json_add_array firewall
1376 if [ "$force_dns" -ne 0 ]; then
1377 for c
in $force_dns_port; do
1378 if netstat
-tuln |
grep LISTEN |
grep ":${c}" >/dev
/null
2>&1; then
1380 json_add_string
type redirect
1381 json_add_string target DNAT
1382 json_add_string src lan
1383 json_add_string proto
"tcp udp"
1384 json_add_string src_dport
"$c"
1385 json_add_string dest_port
"$c"
1386 json_add_boolean reflection
0
1390 json_add_string
type rule
1391 json_add_string src lan
1392 json_add_string dest
"*"
1393 json_add_string proto
"tcp udp"
1394 json_add_string dest_port
"$c"
1395 json_add_string target REJECT
1403 json_add_string
type ipset
1404 json_add_string name adb
1405 json_add_string match dest_net
1406 json_add_string storage
hash
1409 json_add_string
type rule
1410 json_add_string ipset adb
1411 json_add_string src lan
1412 json_add_string dest
"*"
1413 json_add_string proto
"tcp udp"
1414 json_add_string target REJECT
1419 json_add_string
type ipset
1420 json_add_string name adb4
1421 json_add_string family
4
1422 json_add_string match dest_net
1425 json_add_string
type rule
1426 json_add_string ipset adb4
1427 json_add_string src lan
1428 json_add_string dest
"*"
1429 json_add_string proto
"tcp udp"
1430 json_add_string target REJECT
1432 if [ "$ipv6_enabled" -ne 0 ]; then
1434 json_add_string
type ipset
1435 json_add_string name adb6
1436 json_add_string family
6
1437 json_add_string match dest_net
1440 json_add_string
type rule
1441 json_add_string ipset adb6
1442 json_add_string src lan
1443 json_add_string dest
"*"
1444 json_add_string proto
"tcp udp"
1445 json_add_string target REJECT
1452 procd_close_instance
1456 local c url status message error stats
1457 local validation_result
="$3"
1458 load_environment
"$validation_result" 'quiet' ||
return 1
1459 status
="$(json get status)"
1460 message
="$(json get message)"
1461 error
="$(json get error)"
1462 stats
="$(json get stats)"
1463 if [ "$status" = "statusSuccess" ]; then
1464 output
"$stats "; output_okn
;
1466 [ -n "$status" ] && status
="$(get_text "$status")"
1467 if [ -n "$status" ] && [ -n "$message" ]; then
1468 status
="${status}: $message"
1470 [ -n "$status" ] && output
"$serviceName $status\\n"
1472 if [ -n "$error" ]; then
1477 errorDownloadingList|errorParsingList
)
1478 output
"${_ERROR_}: $(get_text "$c") $url!\\n";;
1480 output
"${_ERROR_}: $(get_text "$c")!\\n";;
1488 local validation_result
="$3"
1489 load_environment
"$validation_result" 'quiet' ||
return 1
1490 if [ -s "$outputFile" ]; then
1491 output
"Stopping $serviceName... "
1493 if dns
'on_stop'; then
1494 ipset
-q -! flush adb
1495 ipset
-q -! destroy adb
1496 nft delete
set inet fw4 adb4
1497 nft delete
set inet fw4 adb6
1499 output
0 "$__OK__\\n"; output_okn
;
1500 json
set status
"statusStopped"
1503 output
0 "$__FAIL__\\n"; output_fail
;
1504 json
set status
"statusFail"
1505 json add error
"errorStopping"
1506 output
"${_ERROR_}: $(get_text 'errorStopping')!\\n"
1511 allow
() { load_validate_config
'config' adb_allow
"'$*'"; }
1513 ubus
-t 30 wait_for network.interface
2>/dev
/null
1514 rc_procd start_service
'on_boot'
1516 check
() { load_validate_config
'config' adb_check
"'$*'"; }
1517 dl
() { rc_procd start_service
'download'; }
1519 rm -f "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
1520 rm -f "$dnsmasqConfCache" "$dnsmasqConfGzip"
1521 rm -f "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
1522 rm -f "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
1523 rm -f "$dnsmasqServersCache" "$dnsmasqServersGzip"
1524 rm -f "$unboundCache" "$unboundGzip"
1526 config_foreach resolver
'dnsmasq' 'cleanup'
1530 reload_service
() { rc_procd start_service
'restart'; }
1531 restart_service
() { rc_procd start_service
'restart'; }
1532 service_started
() { procd_set_config_changed firewall
; }
1533 service_stopped
() { procd_set_config_changed firewall
; }
1534 service_triggers
() {
1536 local procd_trigger_wan6
1537 config_load
"$packageName"
1538 config_get_bool procd_trigger_wan6
'config' 'procd_trigger_wan6' '0'
1539 .
/lib
/functions
/network.sh
1541 network_find_wan wan
1543 if [ "$procd_trigger_wan6" -ne 0 ]; then
1544 network_find_wan6 wan6
1545 wan6
="${wan6:-wan6}"
1547 for i
in "$wan" "$wan6"; do
1548 [ -n "$i" ] && procd_add_interface_trigger
"interface.*" "$i" "/etc/init.d/${packageName}" start
1550 procd_add_config_trigger
"config.change" "$packageName" "/etc/init.d/${packageName}" reload
1552 sizes
() { load_validate_config
'config' adb_sizes
"''"; }
1554 load_validate_config
'config' adb_config_update
"'$*'"
1555 load_validate_config
'config' adb_start
"'$*'"
1557 status_service
() { load_validate_config
'config' adb_status
"''"; }
1558 stop_service
() { load_validate_config
'config' adb_stop
"'$*'"; }
1559 version
() { echo "$PKG_VERSION"; }
1561 load_validate_config
() {
1562 .
/lib
/functions
/network.sh
1563 .
/usr
/share
/libubox
/jshn.sh
1566 local force_dns_port
1567 local parallel_downloads
1569 local compressed_cache
1571 local allow_non_ascii
1572 local canary_domains_icloud
1573 local canary_domains_mozilla
1574 local config_update_enabled
1575 local config_update_url
1576 local download_timeout
1577 local curl_max_file_size
1580 local procd_trigger_wan6
1581 local procd_boot_wan_timeout
1585 local allowed_domain
1586 local allowed_domains_url
1587 local blocked_adblockplus_url
1588 local blocked_domain
1589 local blocked_domains_url
1590 local blocked_hosts_url
1591 local dnsmasq_config_file_url
1592 uci_load_validate
"$packageName" "$packageName" "$1" "${2}${3:+ $3}" \
1594 'force_dns:bool:1' \
1595 'force_dns_port:list(integer):53 853' \
1596 'parallel_downloads:bool:1' \
1598 'compressed_cache:bool:0' \
1599 'ipv6_enabled:bool:0' \
1600 'allow_non_ascii:bool:0' \
1601 'canary_domains_icloud:bool:0' \
1602 'canary_domains_mozilla:bool:0' \
1603 'config_update_enabled:bool:0' \
1604 'config_update_url:string:https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update' \
1605 'download_timeout:range(1,60):20' \
1606 'curl_max_file_size:uinteger' \
1607 'curl_retry:range(0,30):3' \
1608 'verbosity:range(0,2):2' \
1609 'procd_trigger_wan6:bool:0' \
1610 'procd_boot_wan_timeout:integer:60' \
1611 'led:or("", "none", file, device, string)' \
1612 'dns:or("dnsmasq.addnhosts", "dnsmasq.conf", "dnsmasq.ipset", "dnsmasq.nftset", "dnsmasq.servers", "unbound.adb_list"):dnsmasq.servers' \
1613 'dns_instance:or(list(integer, string)):0' \
1614 'allowed_domain:list(string)' \
1615 'allowed_domains_url:list(string)' \
1616 'blocked_domain:list(string)' \
1617 'blocked_adblockplus_url:list(string)' \
1618 'blocked_domains_url:list(string)' \
1619 'blocked_hosts_url:list(string)' \
1620 'dnsmasq_config_file_url:string'