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 .
/lib
/functions
/network.sh
92 .
/usr
/share
/libubox
/jshn.sh
94 debug
() { local i j
; for i
in "$@"; do eval "j=\$$i"; echo "${i}: ${j} "; done; }
96 uci_add_list_if_new() {
102 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
103 for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
104 [ "$i" = "$VALUE" ] && return 0
106 uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
112 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
115 ipset
() { "$ipset" "$@" >/dev
/null
2>&1; }
116 nft
() { "$nft" "$@" >/dev
/null
2>&1; }
121 errorConfigValidationFail
) r
="$packageName config validation failed";;
122 errorServiceDisabled
) r
="$packageName is currently disabled";;
124 r
="dnsmasq ipset support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support ipset";;
126 r
="dnsmasq ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type";;
127 errorNoDnsmasqNftset
)
128 r
="dnsmasq nft set support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support nft set";;
129 errorNoNft
) r
="dnsmasq nft sets support is enabled in $packageName, but nft is not installed";;
130 errorNoWanGateway
) r
="The ${serviceName} failed to discover WAN gateway";;
131 errorOutputDirCreate
) r
="failed to create directory for %s file";;
132 errorOutputFileCreate
) r
="failed to create $outputFile file";;
133 errorFailDNSReload
) r
="failed to restart/reload DNS resolver";;
134 errorSharedMemory
) r
="failed to access shared memory";;
135 errorSorting
) r
="failed to sort data file";;
136 errorOptimization
) r
="failed to optimize data file";;
137 errorAllowListProcessing
) r
="failed to process allow-list";;
138 errorDataFileFormatting
) r
="failed to format data file";;
139 errorMovingDataFile
) r
="failed to move data file '${A_TMP}' to '${outputFile}'";;
140 errorCreatingCompressedCache
) r
="failed to create compressed cache";;
141 errorRemovingTempFiles
) r
="failed to remove temporary files";;
142 errorRestoreCompressedCache
) r
="failed to unpack compressed cache";;
143 errorRestoreCache
) r
="failed to move '$outputCache' to '$outputFile'";;
144 errorOhSnap
) r
="failed to create block-list or restart DNS resolver";;
145 errorStopping
) r
="failed to stop $serviceName";;
146 errorDNSReload
) r
="failed to reload/restart DNS resolver";;
147 errorDownloadingConfigUpdate
) r
="failed to download Config Update file";;
148 errorDownloadingList
) r
="failed to download";;
149 errorParsingConfigUpdate
) r
="failed to parse Config Update file";;
150 errorParsingList
) r
="failed to parse";;
151 errorNoSSLSupport
) r
="no HTTPS/SSL support on device";;
152 errorCreatingDirectory
) r
="failed to create output/cache/gzip file directory";;
154 statusNoInstall
) r
="$serviceName is not installed or not found";;
155 statusStopped
) r
="Stopped";;
156 statusStarting
) r
="Starting";;
157 statusRestarting
) r
="Restarting";;
158 statusForceReloading
) r
="Force Reloading";;
159 statusDownloading
) r
="Downloading";;
160 statusProcessing
) r
="Processing";;
161 statusError
) r
="Error";;
162 statusWarning
) r
="Warning";;
163 statusFail
) r
="Fail";;
164 statusSuccess
) r
="Success";;
166 warningExternalDnsmasqConfig
)
167 r
="use of external dnsmasq config file detected, please set 'dns' option to 'dnsmasq.conf'";;
168 warningMissingRecommendedPackages
) r
="Some recommended packages are missing";;
173 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
174 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
175 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
176 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
177 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
178 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
179 is_greater
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
180 is_greater_or_equal
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" = "$2"; }
181 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
182 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
183 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
184 dnsmasq_hup
() { killall
-q -s HUP dnsmasq
; }
185 dnsmasq_kill
() { killall
-q -s KILL dnsmasq
; }
186 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
187 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
188 is_present
() { command -v "$1" >/dev
/null
2>&1; }
191 # Can take a single parameter (text) to be output at any verbosity
192 # Or target verbosity level and text to be output at specifc verbosity
193 local msg memmsg logmsg
194 verbosity
="${verbosity:-2}"
195 if [ $# -ne 1 ]; then
196 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
198 [ -t 1 ] && printf "%b" "$1"
199 msg
="${1//$serviceName /service }";
200 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
201 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
202 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
203 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
204 rm -f "$sharedMemoryOutput"
206 printf "%b" "$msg" >> "$sharedMemoryOutput"
212 local i j wan_if wan_gw wan_proto
213 local counter wan_if_timeout
="$procd_boot_wan_timeout" wan_gw_timeout
='5'
215 while [ -z "$wan_if" ]; do
217 network_find_wan wan_if
218 if [ -n "$wan_if" ]; then
219 output
"WAN interface found: '${wan_if}'.\\n"
222 if [ "$counter" -gt "$wan_if_timeout" ]; then
223 output
"WAN interface timeout, assuming 'wan'.\\n"
227 counter
=$
((counter
+1))
228 output
"Waiting to discover WAN Interface...\\n"
233 wan_proto
="$(uci -q get "network.
${wan_if}.proto
")"
234 if [ "$wan_proto" = 'pppoe' ]; then
235 wan_gw_timeout
=$
((wan_gw_timeout
+10))
237 while [ "$counter" -le "$wan_gw_timeout" ]; do
239 network_get_gateway wan_gw
"$wan_if"
240 if [ -n "$wan_gw" ]; then
241 output
"WAN gateway found: '${wan_gw}.'\\n"
244 counter
=$
((counter
+1))
245 output
"Waiting to discover $wan_if gateway...\\n"
248 json add error
"errorNoWanGateway"
249 output
"${_ERROR_}: $(get_text 'errorNoWanGateway')!\\n"; return 1;
254 local validation_result
="$1" param
="$2"
256 [ -z "$load_environment_flag" ] ||
return 0
258 if [ "$validation_result" != '0' ]; then
259 json add error
"errorConfigValidationFail"
260 output
"${_ERROR_}: $(get_text 'errorConfigValidationFail')!\\n"
261 output
"Please check if the '$packageConfigFile' contains correct values for config options.\\n"
265 if [ "$enabled" -eq 0 ]; then
266 json add error
"errorServiceDisabled"
267 output
"${_ERROR_}: $(get_text 'errorServiceDisabled')!\\n"
268 output
"Run the following commands before starting service again:\\n"
269 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
273 if [ "$debug" -ne 0 ]; then
274 exec 1>>/tmp
/simple-adblock.log
279 if [ -n "$dnsmasq_config_file_url" ]; then
283 if [ "$param" != 'quiet' ]; then
284 json add warning
"warningExternalDnsmasqConfig"
285 output
"${_WARNING_}: $(get_text 'warningExternalDnsmasqConfig')!\\n"
292 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
293 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
303 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
304 if [ "$param" != 'quiet' ]; then
305 json add error
"errorNoDnsmasqIpset"
306 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqIpset')!\\n"
308 dns
='dnsmasq.servers'
310 if ! ipset
help hash:net
; then
311 if [ "$param" != 'quiet' ]; then
312 json add error
"errorNoIpset"
313 output
"${_ERROR_}: $(get_text 'errorNoIpset')!\\n"
315 dns
='dnsmasq.servers'
319 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-nftset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'nftset'; then
320 if [ "$param" != 'quiet' ]; then
321 json add error
"errorNoDnsmasqNftset"
322 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqNftset')!\\n"
324 dns
='dnsmasq.servers'
326 if [ -z "$nft" ]; then
327 if [ "$param" != 'quiet' ]; then
328 json add error
"errorNoNft"
329 output
"${_ERROR_}: $(get_text 'errorNoNft')!\\n"
331 dns
='dnsmasq.servers'
338 outputFilter
="$dnsmasqAddnhostsFilter"
339 outputFile
="$dnsmasqAddnhostsFile"
340 outputCache
="$dnsmasqAddnhostsCache"
341 outputGzip
="$dnsmasqAddnhostsGzip"
342 if [ "$ipv6_enabled" -ne 0 ]; then
343 outputFilterIPv6
="$dnsmasqAddnhostsFilterIPv6"
345 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
346 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
347 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
348 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
349 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
352 outputFilter
="$dnsmasqConfFilter"
353 outputFile
="$dnsmasqConfFile"
354 outputCache
="$dnsmasqConfCache"
355 outputGzip
="$dnsmasqConfGzip"
356 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
357 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
358 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
359 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
360 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
363 outputFilter
="$dnsmasqIpsetFilter"
364 outputFile
="$dnsmasqIpsetFile"
365 outputCache
="$dnsmasqIpsetCache"
366 outputGzip
="$dnsmasqIpsetGzip"
367 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
368 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
369 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
370 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
371 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
374 if [ "$ipv6_enabled" -ne 0 ]; then
375 outputFilter
="$dnsmasqNftsetFilterIPv6"
377 outputFilter
="$dnsmasqNftsetFilter"
379 outputFile
="$dnsmasqNftsetFile"
380 outputCache
="$dnsmasqNftsetCache"
381 outputGzip
="$dnsmasqNftsetGzip"
382 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
383 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
384 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
385 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
386 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
389 outputFilter
="$dnsmasqServersFilter"
390 outputFile
="$dnsmasqServersFile"
391 outputCache
="$dnsmasqServersCache"
392 outputGzip
="$dnsmasqServersGzip"
393 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
394 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
395 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
396 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
397 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
400 outputFilter
="$unboundFilter"
401 outputFile
="$unboundFile"
402 outputCache
="$unboundCache"
403 outputGzip
="$unboundGzip"
404 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
405 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
406 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
407 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
408 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
412 for i
in "$jsonFile" "$outputFile" "$outputCache" "$outputGzip"; do
413 if ! mkdir
-p "$(dirname "$i")"; then
414 if [ "$param" != 'quiet' ]; then
415 json add error
"errorOutputDirCreate" "$i"
416 output
"${_ERROR_}: $(get_text 'errorOutputDirCreate' "$i")!\\n"
421 is_present
'gawk' && awk='gawk'
422 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
423 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
424 local s
="opkg update; opkg --force-overwrite install"
425 is_present
'gawk' || s
="$s gawk"
426 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
427 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
428 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
429 if [ "$param" != 'quiet' ]; then
430 json add warning
"warningMissingRecommendedPackages" "${i}"
431 output
"${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\\n"
435 # Prefer curl because it supports the file:// scheme.
436 if is_present
'curl'; then
437 dl_command
="curl --silent --insecure"
438 dl_command
="${dl_command}${curl_max_file_size:+ --max-filesize $curl_max_file_size}"
439 dl_command
="${dl_command}${curl_retry:+ --retry $curl_retry}"
440 dl_command
="${dl_command}${download_timeout:+ --connect-timeout $download_timeout}"
442 elif is_present
'/usr/libexec/wget-ssl'; then
443 dl_command
="/usr/libexec/wget-ssl --no-check-certificate -q"
444 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
446 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
447 dl_command
="wget --no-check-certificate -q"
448 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
451 dl_command
="uclient-fetch --no-check-certificate -q"
452 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
455 led
="${led:+/sys/class/leds/$led}"
456 if curl
--version 2>/dev
/null |
grep -q "https" \
457 || wget
--version 2>/dev
/null |
grep -q "+https" \
458 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
459 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
460 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
465 load_environment_flag
=1
466 cache
'test' && return 0
467 cache
'test_gzip' && return 0
468 if [ "$param" = 'on_boot' ]; then
469 load_network
"$param"
477 local cfg
="$1" param
="$2"
480 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
481 uci_remove
'dhcp' "$cfg" 'serversfile'
483 uci_add_list_if_new
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
485 cleanup|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list
)
486 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
487 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
488 uci_remove
'dhcp' "$cfg" 'serversfile'
492 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
493 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
494 uci_set
'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
501 local param output_text i
504 if [ ! -s "$outputFile" ]; then
505 json
set status
"statusFail"
506 json add error
"errorOutputFileCreate"
507 output
"${_ERROR_}: $(get_text 'errorOutputFileCreate')!\\n"
512 if [ "$dns_instance" = "*" ]; then
513 config_foreach resolver
'dnsmasq' "$dns"
514 elif [ -n "$dns_instance" ]; then
515 for i
in $dns_instance; do
516 resolver
"@dnsmasq[$i]" "$dns" || resolver
"$i" "$dns"
521 dnsmasq.addnhosts|dnsmasq.servers
)
522 param
=dnsmasq_restart
523 output_text
='Reloading dnsmasq'
525 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
526 param
=dnsmasq_restart
527 output_text
='Restarting dnsmasq'
530 param
=unbound_restart
531 output_text
='Restarting Unbound'
535 if [ -n "$(uci_changes dhcp)" ]; then
537 if [ "$param" = 'unbound_restart' ]; then
538 param
='dnsmasq_restart; unbound_restart;'
539 output_text
='Restarting Unbound/dnsmasq'
541 param
=dnsmasq_restart
542 output_text
='Restarting dnsmasq'
545 output
1 "$output_text "
546 output
2 "$output_text "
547 json
set message
"$output_text"
548 if eval "$param"; then
549 json
set status
"statusSuccess"
554 json
set status
"statusFail"
555 json add error
"errorDNSReload"
556 output
"${_ERROR_}: $(get_text 'errorDNSReload')!\\n"
562 dnsmasq.addnhosts|dnsmasq.servers
)
563 param
=dnsmasq_restart
565 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
566 param
=dnsmasq_restart
569 param
=unbound_restart
572 if [ -n "$(uci_changes dhcp)" ]; then
574 if [ "$param" = 'unbound_restart' ]; then
575 param
='dnsmasq_restart; unbound_restart;'
577 param
=dnsmasq_restart
585 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
586 param
=dnsmasq_restart
589 param
=unbound_restart
599 # shellcheck disable=SC2034
600 local action
="$1" param
="$2" value
="$3"
602 # shellcheck disable=SC2124
603 local extras
="$@" line
604 local status message error stats
605 local reload restart curReload curRestart ret i
606 if [ -s "$jsonFile" ]; then
607 json_load_file
"$jsonFile" 2>/dev
/null
608 json_select
'data' 2>/dev
/null
609 for i
in status message error stats reload restart
; do
610 json_get_var
$i "$i" 2>/dev
/null
617 curReload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
618 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
619 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
620 curRestart
="$compressed_cache $force_dns $led $force_dns_port"
621 if [ ! -s "$jsonFile" ]; then
623 elif [ "$curReload" != "$reload" ]; then
625 elif [ "$curRestart" != "$restart" ]; then
631 printf "%b" "$(eval echo "\$
$param")"; return;;
635 line
="$(eval echo "\$
$param")"
636 eval "$param"='${line:+$line }${value}${extras:+|$extras}'
641 unset status message error stats
;;
643 unset reload restart
;;
651 reload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
652 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
653 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
654 restart
="$compressed_cache $force_dns $led $force_dns_port"
657 eval "$param"='${value}${extras:+|$extras}';;
662 json_add_object
'data'
663 json_add_string version
"$PKG_VERSION"
664 json_add_string status
"$status"
665 json_add_string message
"$message"
666 json_add_string error
"$error"
667 json_add_string stats
"$stats"
668 json_add_string reload
"$reload"
669 json_add_string restart
"$restart"
671 mkdir
-p "$(dirname "$jsonFile")"
672 json_dump
> "$jsonFile"
680 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
684 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
688 [ -s "$outputCache" ]
692 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
696 rm -f "$outputGzip" >/dev
/null
2>/dev
/null
697 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
698 if gzip < "$outputFile" > "$R_TMP"; then
699 if mv "$R_TMP" "$outputGzip"; then
710 expand|unpack|unpack_gzip
)
711 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
718 local label
type D_TMP R_TMP
719 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
720 label
="${1##*//}"; label
="${label%%/*}";
722 adbp
) label
="ADBPlus: $label"; filter
="$adBlockPlusFilter"
724 dnsmasq
) label
="Dnsmasq: $label"; filter
="$dnsmasqFileFilter"
726 domains
) label
="Domains: $label"; filter
="$domainsFilter"
728 hosts
) label
="Hosts: $label"; filter
="$hostsFilter"
732 allowed
) type='Allowed'; D_TMP
="$A_TMP"
734 blocked
) type='Blocked'; D_TMP
="$B_TMP"
736 file) type='File'; D_TMP
="$B_TMP"
739 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
741 output
2 "[DL] $type $label $__FAIL__\\n"
742 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
745 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
746 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
748 if ! $dl_command "$1" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
750 output
2 "[DL] $type $label $__FAIL__\\n"
751 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
753 sed -i "$filter" "$R_TMP"
754 if [ ! -s "$R_TMP" ]; then
756 output
2 "[DL] $type $label $__FAIL__\\n"
757 echo "errorParsingList|${1}" >> "$sharedMemoryError"
759 cat "${R_TMP}" >> "$D_TMP"
761 output
2 "[DL] $type $label $__OK__\\n"
768 download_dnsmasq_file
() {
769 local hf allow_filter j
=0 R_TMP
771 json
set message
"$(get_text "statusDownloading
")..."
772 json
set status
"statusDownloading"
774 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
775 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
776 output
3 'Low free memory, restarting resolver '
783 touch $A_TMP; touch $B_TMP;
784 output
1 'Downloading dnsmasq file '
785 rm -f "$sharedMemoryError"
786 process_url
"$dnsmasq_config_file_url" 'dnsmasq' 'file'
788 if [ -s "$sharedMemoryError" ]; then
789 while IFS
= read -r line
; do
790 json add error
"$line"
791 done < "$sharedMemoryError"
792 rm -f "$sharedMemoryError"
794 output
2 'Creating dnsmasq file '
795 if mv "$B_TMP" "$outputFile"; then
796 output
2 "$__OK__\\n"
798 output
2 "$__FAIL__\\n"
799 json add error
"errorMovingDataFile"
805 local hf allow_filter j
=0 R_TMP
807 json
set message
"$(get_text "statusDownloading
")..."
808 json
set status
"statusDownloading"
810 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
811 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
812 output
3 'Low free memory, restarting resolver '
819 touch $A_TMP; touch $B_TMP;
820 output
1 'Downloading lists '
821 rm -f "$sharedMemoryError"
822 for hf
in ${blocked_hosts_url}; do
823 if [ "$parallel_downloads" -gt 0 ]; then
824 process_url
"$hf" 'hosts' 'blocked' &
826 process_url
"$hf" 'hosts' 'blocked'
829 for hf
in ${blocked_adblockplus_url}; do
830 if [ "$parallel_downloads" -gt 0 ]; then
831 process_url
"$hf" 'adbp' 'blocked' &
833 process_url
"$hf" 'adbp' 'blocked'
836 for hf
in ${blocked_domains_url}; do
837 if [ "$parallel_downloads" -gt 0 ]; then
838 process_url
"$hf" 'domains' 'blocked' &
840 process_url
"$hf" 'domains' 'blocked'
843 for hf
in ${allowed_domains_url}; do
844 if [ "$parallel_downloads" -gt 0 ]; then
845 process_url
"$hf" 'domains' 'allowed' &
847 process_url
"$hf" 'domains' 'allowed'
852 if [ -s "$sharedMemoryError" ]; then
853 while IFS
= read -r line
; do
854 json add error
"$line"
855 done < "$sharedMemoryError"
856 rm -f "$sharedMemoryError"
859 if [ "$canary_domains_icloud" -ne 0 ]; then
860 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
862 if [ "$canary_domains_mozilla" -ne 0 ]; then
863 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
866 for hf
in $blocked_domain $canaryDomains; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
867 allowed_domain
="${allowed_domain}
869 for hf
in ${allowed_domain}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; allow_filter
="$allow_filter/(^|\.)${hf}$/d;"; done
871 [ ! -s "$B_TMP" ] && return 1
873 output
1 'Processing downloads '
874 output
2 'Sorting combined list '
875 json
set status
"statusProcessing"
876 json
set message
"$(get_text "statusProcessing
"): sorting combined list"
877 if [ "$allow_non_ascii" -gt 0 ]; then
878 if sort -u "$B_TMP" > "$A_TMP"; then
882 json add error
"errorSorting"
885 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
889 json add error
"errorSorting"
893 if [ "$dns" = 'dnsmasq.conf' ] || \
894 [ "$dns" = 'dnsmasq.ipset' ] || \
895 [ "$dns" = 'dnsmasq.nftset' ] || \
896 [ "$dns" = 'dnsmasq.servers' ] || \
897 [ "$dns" = 'unbound.adb_list' ]; then
898 # TLD optimization written by Dirk Brenken (dev@brenken.org)
899 output
2 'Optimizing combined list '
900 json
set message
"$(get_text "statusProcessing
"): optimizing combined list"
901 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
902 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
903 if sort "$B_TMP" > "$A_TMP"; then
904 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
905 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
906 if sort -u "$A_TMP" > "$B_TMP"; then
910 json add error
"errorOptimization"
915 json add error
"errorOptimization"
919 json add error
"errorOptimization"
924 json add error
"errorOptimization"
928 json add error
"errorOptimization"
935 output
2 'Allowing domains '
936 json
set message
"$(get_text "statusProcessing
"): allowing domains"
937 if sed -i -E "$allow_filter" "$B_TMP"; then
941 json add error
"errorAllowListProcessing"
944 output
2 'Formatting merged file '
945 json
set message
"$(get_text "statusProcessing
"): formatting merged file"
946 if [ -z "$outputFilterIPv6" ]; then
947 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
951 json add error
"errorDataFileFormatting"
956 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
957 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
961 json add error
"errorDataFileFormatting"
969 output
2 'Creating dnsmasq addnhosts file '
970 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq addnhosts file"
973 output
2 'Creating dnsmasq config file '
974 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq config file"
977 output
2 'Creating dnsmasq ipset file '
978 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq ipset file"
981 output
2 'Creating dnsmasq nft set file '
982 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq nft set file"
985 output
2 'Creating dnsmasq servers file '
986 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq servers file"
989 output
2 'Creating Unbound adb_list file '
990 json
set message
"$(get_text "statusProcessing
"): creating Unbound adb_list file"
994 if mv "$A_TMP" "$outputFile"; then
998 json add error
"errorMovingDataFile"
1000 if [ "$compressed_cache" -gt 0 ]; then
1001 output
2 'Creating compressed cache '
1002 json
set message
"$(get_text "statusProcessing
"): creating compressed cache"
1003 if cache
'create_gzip'; then
1007 json add error
"errorCreatingCompressedCache"
1012 output
2 'Removing temporary files '
1013 json
set message
"$(get_text "statusProcessing
"): removing temporary files"
1014 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
1015 if [ $j -eq 0 ]; then
1019 json add error
"errorRemovingTempFiles"
1025 local c hf string
="$1"
1026 local validation_result
="$3"
1027 load_environment
"$validation_result" 'quiet' ||
return 1
1028 if [ ! -s "$outputFile" ]; then
1029 output
"No block-list ('$outputFile') found.\\n"
1031 elif [ -z "$string" ]; then
1032 output
"Usage: /etc/init.d/${packageName} allow 'domain' ...\\n"
1034 elif [ -n "$dnsmasq_config_file_url" ]; then
1035 output
"Allowing individual domains is not possible when using external dnsmasq config file.\\n"
1039 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
1040 output
1 "Allowing domain(s) and restarting dnsmasq "
1041 output
2 "Allowing domain(s) \\n"
1042 for c
in $string; do
1044 hf
="$(echo "$c" | sed 's/\./\\./g')"
1045 if sed -i "/(^|\.)${hf}$/d;" "$outputFile" && \
1046 uci_add_list_if_new
"${packageName}" 'config' 'allowed_domain' "$c"; then
1052 if [ "$compressed_cache" -gt 0 ]; then
1053 output
2 'Creating compressed cache '
1054 if cache
'create_gzip'; then
1060 output
2 "Committing changes to config "
1061 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1062 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1064 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1066 if [ "$dns" = 'dnsmasq.ipset' ]; then
1067 output
2 "Flushing adb ipset "
1068 if ipset
-q -! flush adb
; then output_ok
; else output_fail
; fi
1070 if [ "$dns" = 'dnsmasq.nftset' ]; then
1071 output
2 "Flushing adb nft sets "
1072 nft flush
set inet fw4 adb6
1073 if nft flush
set inet fw4 adb4
; then output_ok
; else output_fail
; fi
1075 output
2 "Restarting dnsmasq "
1076 if dnsmasq_restart
; then output_okn
; else output_failn
; fi
1082 output
1 "Allowing domain(s) and restarting Unbound "
1083 output
2 "Allowing domain(s) \\n"
1084 for c
in $string; do
1086 if sed -i "/${string}/d" "$outputFile" && \
1087 uci_add_list_if_new
"$packageName" 'config' 'allowed_domain' "$string"; then
1093 if [ "$compressed_cache" -gt 0 ]; then
1094 output
2 'Creating compressed cache '
1095 if cache
'create_gzip'; then
1101 output
2 "Committing changes to config "
1102 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1103 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1105 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1107 output
2 "Restarting Unbound "
1108 if unbound_restart
; then output_okn
; else output_failn
; fi
1118 local validation_result
="$3"
1119 load_environment
"$validation_result" 'quiet' ||
return 1
1120 if [ ! -s "$outputFile" ]; then
1121 output
"No block-list ('$outputFile') found.\\n"
1123 elif [ -z "$param" ]; then
1124 output
"Usage: /etc/init.d/${packageName} check 'domain' ...\\n"
1127 for string
in ${param}; do
1128 c
="$(grep -c "$string" "$outputFile")"
1129 if [ "$c" -gt 0 ]; then
1130 if [ "$c" -eq 1 ]; then
1131 output
"Found 1 match for '$string' in '$outputFile'.\\n"
1133 output
"Found $c matches for '$string' in '$outputFile'.\\n"
1135 if [ "$c" -le 20 ]; then
1138 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1140 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1142 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1144 grep "$string" "$outputFile" |
sed 's|nftset=/||;s|/4#inet#adb#adb4||;';;
1146 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1148 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1152 output
"The '$string' is not found in current block-list ('$outputFile').\\n"
1157 adb_config_update
() {
1159 local param validation_result
="$3"
1161 on_boot
) param
="$1";;
1164 load_environment
"$validation_result" "$param" ||
return 1
1165 label
="${config_update_url##*//}"
1166 label
="${label%%/*}";
1167 [ "$config_update_enabled" -ne 0 ] ||
return 0
1169 if [ "$param" != 'download' ]; then
1170 cache
'test' && return 0
1171 cache
'test_gzip' && return 0
1173 output
1 'Updating config '
1174 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
1175 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
1177 if ! $dl_command "$config_update_url" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
1178 output
1 "$_FAIL_\\n"
1179 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1180 json add error
"errorDownloadingConfigUpdate"
1182 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i "$packageConfigFile" 2>/dev
/null
; then
1184 output
2 "[DL] Config Update: $label $__OK__\\n"
1186 output
1 "$_FAIL_\\n"
1187 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1188 json add error
"errorParsingConfigUpdate"
1197 local validation_result
="$3"
1198 load_environment
"$validation_result" 'quiet' ||
return 1
1201 for i
in ${blocked_adblockplus_url}; do
1202 [ "${i//melmac}" != "$i" ] && continue
1203 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1204 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1205 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1206 echo "# block-list too big for most routers"
1207 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1208 echo "# block-list may be too big for some routers"
1211 echo " list blocked_adblockplus_url '$i'"
1214 echo "# site was down on last check"
1215 echo "# list blocked_adblockplus_url '$i'"
1219 for i
in ${blocked_domains_url}; do
1220 [ "${i//melmac}" != "$i" ] && continue
1221 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1222 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1223 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1224 echo "# block-list too big for most routers"
1225 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1226 echo "# block-list may be too big for some routers"
1229 echo " list blocked_domains_url '$i'"
1232 echo "# site was down on last check"
1233 echo "# list blocked_domains_url '$i'"
1237 for i
in ${blocked_hosts_url}; do
1238 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1239 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1240 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1241 echo "# block-list too big for most routers"
1242 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1243 echo "# block-list may be too big for some routers"
1246 echo " list blocked_hosts_url '$i'"
1249 echo "# site was down on last check"
1250 echo "# list blocked_hosts_url '$i'"
1257 local action status error message stats c
1258 local param
="$1" validation_result
="$3"
1260 load_environment
"$validation_result" "$param" ||
return 1
1262 status
="$(json get status)"
1263 error
="$(json get error)"
1264 message
="$(json get message)"
1265 stats
="$(json get stats)"
1266 action
="$(json get triggers)"
1268 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
1269 if cache
'test_gzip' || cache
'test'; then
1274 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
1276 elif [ ! -s "$outputFile" ]; then
1277 if cache
'test_gzip' || cache
'test'; then
1282 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
1284 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
1294 if [ "$action" = 'restore' ]; then
1295 output
0 "Starting $serviceName... "
1296 output
3 "Starting $serviceName...\\n"
1297 json
set status
"statusStarting"
1298 if cache
'test_gzip' && ! cache
'test' && [ ! -s "$outputFile" ]; then
1299 output
3 'Found compressed cache file, unpacking it '
1300 json
set message
'found compressed cache file, unpacking it.'
1301 if cache
'unpack_gzip'; then
1305 json add error
"errorRestoreCompressedCache"
1306 output
"${_ERROR_}: $(get_text 'errorRestoreCompressedCache')!\\n"
1310 if cache
'test' && [ ! -s "$outputFile" ]; then
1311 output
3 'Found cache file, reusing it '
1312 json
set message
'found cache file, reusing it.'
1313 if cache
'restore'; then
1318 json add error
"errorRestoreCache"
1319 output
"${_ERROR_}: $(get_text 'errorRestoreCache')!\\n"
1324 if [ "$action" = 'download' ]; then
1325 if [ -s "$outputFile" ] || cache
'test' || cache
'test_gzip'; then
1326 output
0 "Force-reloading $serviceName... "
1327 output
3 "Force-reloading $serviceName...\\n"
1328 json
set status
"statusForceReloading"
1330 output
0 "Starting $serviceName... "
1331 output
3 "Starting $serviceName...\\n"
1332 json
set status
"statusStarting"
1334 if [ -n "$dnsmasq_config_file_url" ]; then
1335 download_dnsmasq_file
1341 if [ "$action" = 'restart' ]; then
1342 output
0 "Restarting $serviceName... "
1343 output
3 "Restarting $serviceName...\\n"
1344 json
set status
"statusRestarting"
1347 if [ "$action" = 'start' ]; then
1348 output
0 "Starting $serviceName... "
1349 output
3 "Starting $serviceName...\\n"
1350 json
set status
"statusStarting"
1353 if [ -s "$outputFile" ] && [ "$(json get status)" != "statusFail" ]; then
1354 output
0 "$__OK__\\n";
1356 json
set status
"statusSuccess"
1357 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1360 output
0 "$__FAIL__\\n";
1361 json
set status
"statusFail"
1362 json add error
"errorOhSnap"
1366 procd_open_instance
'main'
1367 procd_set_param
command /bin
/true
1368 procd_set_param stdout
1
1369 procd_set_param stderr
1
1371 json_add_string
'status' "$(json get status)"
1372 json_add_string
'errors' "$(json get errors)"
1373 if [ -s "$outputFile" ]; then
1374 json_add_int
'entries' "$(wc -l < "$outputFile")"
1376 json_add_int
'entries' '0'
1378 json_add_array firewall
1379 if [ "$force_dns" -ne 0 ]; then
1380 for c
in $force_dns_port; do
1381 if netstat
-tuln |
grep LISTEN |
grep ":${c}" >/dev
/null
2>&1; then
1383 json_add_string
type redirect
1384 json_add_string target DNAT
1385 json_add_string src lan
1386 json_add_string proto
"tcp udp"
1387 json_add_string src_dport
"$c"
1388 json_add_string dest_port
"$c"
1389 json_add_boolean reflection
0
1393 json_add_string
type rule
1394 json_add_string src lan
1395 json_add_string dest
"*"
1396 json_add_string proto
"tcp udp"
1397 json_add_string dest_port
"$c"
1398 json_add_string target REJECT
1406 json_add_string
type ipset
1407 json_add_string name adb
1408 json_add_string match dest_net
1409 json_add_string storage
hash
1412 json_add_string
type rule
1413 json_add_string ipset adb
1414 json_add_string src lan
1415 json_add_string dest
"*"
1416 json_add_string proto
"tcp udp"
1417 json_add_string target REJECT
1422 json_add_string
type ipset
1423 json_add_string name adb4
1424 json_add_string family
4
1425 json_add_string match dest_net
1428 json_add_string
type rule
1429 json_add_string ipset adb4
1430 json_add_string src lan
1431 json_add_string dest
"*"
1432 json_add_string proto
"tcp udp"
1433 json_add_string target REJECT
1435 if [ "$ipv6_enabled" -ne 0 ]; then
1437 json_add_string
type ipset
1438 json_add_string name adb6
1439 json_add_string family
6
1440 json_add_string match dest_net
1443 json_add_string
type rule
1444 json_add_string ipset adb6
1445 json_add_string src lan
1446 json_add_string dest
"*"
1447 json_add_string proto
"tcp udp"
1448 json_add_string target REJECT
1455 procd_close_instance
1459 local c url status message error stats
1460 local validation_result
="$3"
1461 load_environment
"$validation_result" 'quiet' ||
return 1
1462 status
="$(json get status)"
1463 message
="$(json get message)"
1464 error
="$(json get error)"
1465 stats
="$(json get stats)"
1466 if [ "$status" = "statusSuccess" ]; then
1467 output
"$stats "; output_okn
;
1469 [ -n "$status" ] && status
="$(get_text "$status")"
1470 if [ -n "$status" ] && [ -n "$message" ]; then
1471 status
="${status}: $message"
1473 [ -n "$status" ] && output
"$serviceName $status\\n"
1475 if [ -n "$error" ]; then
1480 errorDownloadingList|errorParsingList
)
1481 output
"${_ERROR_}: $(get_text "$c") $url!\\n";;
1483 output
"${_ERROR_}: $(get_text "$c")!\\n";;
1491 local validation_result
="$3"
1492 load_environment
"$validation_result" 'quiet' ||
return 1
1493 if [ -s "$outputFile" ]; then
1494 output
"Stopping $serviceName... "
1496 if dns
'on_stop'; then
1497 ipset
-q -! flush adb
1498 ipset
-q -! destroy adb
1499 nft delete
set inet fw4 adb4
1500 nft delete
set inet fw4 adb6
1502 output
0 "$__OK__\\n"; output_okn
;
1503 json
set status
"statusStopped"
1506 output
0 "$__FAIL__\\n"; output_fail
;
1507 json
set status
"statusFail"
1508 json add error
"errorStopping"
1509 output
"${_ERROR_}: $(get_text 'errorStopping')!\\n"
1514 allow
() { load_validate_config
'config' adb_allow
"'$*'"; }
1516 ubus
-t 30 wait_for network.interface
2>/dev
/null
1517 rc_procd start_service
'on_boot'
1519 check
() { load_validate_config
'config' adb_check
"'$*'"; }
1520 dl
() { rc_procd start_service
'download'; }
1522 rm -f "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
1523 rm -f "$dnsmasqConfCache" "$dnsmasqConfGzip"
1524 rm -f "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
1525 rm -f "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
1526 rm -f "$dnsmasqServersCache" "$dnsmasqServersGzip"
1527 rm -f "$unboundCache" "$unboundGzip"
1529 config_foreach resolver
'dnsmasq' 'cleanup'
1533 reload_service
() { rc_procd start_service
'restart'; }
1534 restart_service
() { rc_procd start_service
'restart'; }
1535 service_started
() { procd_set_config_changed firewall
; }
1536 service_stopped
() { procd_set_config_changed firewall
; }
1537 service_triggers
() {
1539 local procd_trigger_wan6
1540 config_load
"$packageName"
1541 config_get_bool procd_trigger_wan6
'config' 'procd_trigger_wan6' '0'
1542 .
/lib
/functions
/network.sh
1544 network_find_wan wan
1546 if [ "$procd_trigger_wan6" -ne 0 ]; then
1547 network_find_wan6 wan6
1548 wan6
="${wan6:-wan6}"
1550 for i
in "$wan" "$wan6"; do
1551 [ -n "$i" ] && procd_add_interface_trigger
"interface.*" "$i" "/etc/init.d/${packageName}" start
1553 procd_add_config_trigger
"config.change" "$packageName" "/etc/init.d/${packageName}" reload
1555 sizes
() { load_validate_config
'config' adb_sizes
"''"; }
1557 load_validate_config
'config' adb_config_update
"'$*'"
1558 load_validate_config
'config' adb_start
"'$*'"
1560 status_service
() { load_validate_config
'config' adb_status
"''"; }
1561 stop_service
() { load_validate_config
'config' adb_stop
"'$*'"; }
1562 version
() { echo "$PKG_VERSION"; }
1564 load_validate_config
() {
1567 local force_dns_port
1568 local parallel_downloads
1570 local compressed_cache
1572 local allow_non_ascii
1573 local canary_domains_icloud
1574 local canary_domains_mozilla
1575 local config_update_enabled
1576 local config_update_url
1577 local download_timeout
1578 local curl_max_file_size
1581 local procd_trigger_wan6
1582 local procd_boot_wan_timeout
1586 local allowed_domain
1587 local allowed_domains_url
1588 local blocked_adblockplus_url
1589 local blocked_domain
1590 local blocked_domains_url
1591 local blocked_hosts_url
1592 local dnsmasq_config_file_url
1593 uci_load_validate
"$packageName" "$packageName" "$1" "${2}${3:+ $3}" \
1595 'force_dns:bool:1' \
1596 'force_dns_port:list(integer):53 853' \
1597 'parallel_downloads:bool:1' \
1599 'compressed_cache:bool:0' \
1600 'ipv6_enabled:bool:0' \
1601 'allow_non_ascii:bool:0' \
1602 'canary_domains_icloud:bool:0' \
1603 'canary_domains_mozilla:bool:0' \
1604 'config_update_enabled:bool:0' \
1605 'config_update_url:string:https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update' \
1606 'download_timeout:range(1,60):20' \
1607 'curl_max_file_size:uinteger' \
1608 'curl_retry:range(0,30):3' \
1609 'verbosity:range(0,2):2' \
1610 'procd_trigger_wan6:bool:0' \
1611 'procd_boot_wan_timeout:integer:60' \
1612 'led:or("", "none", file, device, string)' \
1613 'dns:or("dnsmasq.addnhosts", "dnsmasq.conf", "dnsmasq.ipset", "dnsmasq.nftset", "dnsmasq.servers", "unbound.adb_list"):dnsmasq.servers' \
1614 'dns_instance:or(list(integer, string)):0' \
1615 'allowed_domain:list(string)' \
1616 'allowed_domains_url:list(string)' \
1617 'blocked_domain:list(string)' \
1618 'blocked_adblockplus_url:list(string)' \
1619 'blocked_domains_url:list(string)' \
1620 'blocked_hosts_url:list(string)' \
1621 'dnsmasq_config_file_url:string'