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 dnsmasqOISDFilter
='\|^server=/[[:alnum:]_.-].*/|!d'
66 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
67 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
68 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
69 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
70 readonly _ERROR_
='\033[0;31mERROR\033[0m'
71 readonly _WARNING_
='\033[0;33mWARNING\033[0m'
72 # shellcheck disable=SC2155
73 readonly ipset
="$(command -v ipset)"
74 # shellcheck disable=SC2155
75 readonly nft
="$(command -v nft)"
76 readonly canaryDomainsMozilla
='use-application-dns.net'
77 readonly canaryDomainsiCloud
='mask.icloud.com mask-h2.icloud.com'
79 debug
() { local i j
; for i
in "$@"; do eval "j=\$$i"; echo "${i}: ${j} "; done; }
81 uci_add_list_if_new() {
87 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
88 for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
89 [ "$i" = "$VALUE" ] && return 0
91 uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
97 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
100 ipset
() { "$ipset" "$@" >/dev
/null
2>&1; }
101 nft
() { "$nft" "$@" >/dev
/null
2>&1; }
106 errorConfigValidationFail
) r
="$packageName config validation failed";;
107 errorServiceDisabled
) r
="$packageName is currently disabled";;
109 r
="dnsmasq ipset support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support ipset";;
111 r
="dnsmasq ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type";;
112 errorNoDnsmasqNftset
)
113 r
="dnsmasq nft set support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support nft set";;
114 errorNoNft
) r
="dnsmasq nft sets support is enabled in $packageName, but nft is not installed";;
115 errorMkdirFail
) r
="Unable to create directory for";;
116 errorNoWanGateway
) r
="The ${serviceName} service failed to discover WAN gateway!";;
117 errorOutputDirCreate
) r
="failed to create directory for %s file";;
118 errorOutputFileCreate
) r
="failed to create $outputFile file";;
119 errorFailDNSReload
) r
="failed to restart/reload DNS resolver";;
120 errorSharedMemory
) r
="failed to access shared memory";;
121 errorSorting
) r
="failed to sort data file";;
122 errorOptimization
) r
="failed to optimize data file";;
123 errorAllowListProcessing
) r
="failed to process allow-list";;
124 errorDataFileFormatting
) r
="failed to format data file";;
125 errorMovingDataFile
) r
="failed to move data file '${A_TMP}' to '${outputFile}'";;
126 errorCreatingCompressedCache
) r
="failed to create compressed cache";;
127 errorRemovingTempFiles
) r
="failed to remove temporary files";;
128 errorRestoreCompressedCache
) r
="failed to unpack compressed cache";;
129 errorRestoreCache
) r
="failed to move '$outputCache' to '$outputFile'";;
130 errorOhSnap
) r
="failed to create block-list or restart DNS resolver";;
131 errorStopping
) r
="failed to stop $serviceName";;
132 errorDNSReload
) r
="failed to reload/restart DNS resolver";;
133 errorDownloadingConfigUpdate
) r
="failed to download Config Update file";;
134 errorDownloadingList
) r
="failed to download";;
135 errorParsingConfigUpdate
) r
="failed to parse Config Update file";;
136 errorParsingList
) r
="failed to parse";;
137 errorNoSSLSupport
) r
="no HTTPS/SSL support on device";;
138 errorCreatingDirectory
) r
="failed to create output/cache/gzip file directory";;
140 statusNoInstall
) r
="$serviceName is not installed or not found";;
141 statusStopped
) r
="Stopped";;
142 statusStarting
) r
="Starting";;
143 statusRestarting
) r
="Restarting";;
144 statusForceReloading
) r
="Force Reloading";;
145 statusDownloading
) r
="Downloading";;
146 statusProcessing
) r
="Processing";;
147 statusError
) r
="Error";;
148 statusWarning
) r
="Warning";;
149 statusFail
) r
="Fail";;
150 statusSuccess
) r
="Success";;
152 warningExternalDnsmasqConfig
)
153 r
="use of external dnsmasq config file detected, please set 'dns' option to 'dnsmasq.conf'";;
154 warningMissingRecommendedPackages
) r
="Some recommended packages are missing";;
159 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
160 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
161 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
162 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
163 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
164 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
165 is_greater
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
166 is_greater_or_equal
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" = "$2"; }
167 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
168 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
169 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
170 dnsmasq_hup
() { killall
-q -s HUP dnsmasq
; }
171 dnsmasq_kill
() { killall
-q -s KILL dnsmasq
; }
172 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
173 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
174 is_present
() { command -v "$1" >/dev
/null
2>&1; }
177 # Can take a single parameter (text) to be output at any verbosity
178 # Or target verbosity level and text to be output at specifc verbosity
179 local msg memmsg logmsg
180 verbosity
="${verbosity:-2}"
181 if [ $# -ne 1 ]; then
182 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
184 [ -t 1 ] && printf "%b" "$1"
185 msg
="${1//$serviceName /service }";
186 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
187 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
188 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
189 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
190 rm -f "$sharedMemoryOutput"
192 printf "%b" "$msg" >> "$sharedMemoryOutput"
197 local i j wan_if wan_gw
198 local validation_result
="$1" quiet
="$2"
200 if [ "$validation_result" != '0' ]; then
201 json add error
"errorConfigValidationFail"
202 output
"${_ERROR_}: $(get_text 'errorConfigValidationFail')!\\n"
203 output
"Please check if the '$packageConfigFile' contains correct values for config options.\\n"
207 if [ "$enabled" -eq 0 ]; then
208 json add error
"errorServiceDisabled"
209 output
"${_ERROR_}: $(get_text 'errorServiceDisabled')!\\n"
210 output
"Run the following commands before starting service again:\\n"
211 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
215 if [ "$debug" -ne 0 ]; then
216 exec 1>>/tmp
/simple-adblock.log
221 if [ -n "$dnsmasq_config_file_url" ]; then
225 if [ -z "$quiet" ]; then
226 json add warning
"warningExternalDnsmasqConfig"
227 output
"${_WARNING_}: $(get_text 'warningExternalDnsmasqConfig')!\\n"
234 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
235 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
245 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
246 if [ -z "$quiet" ]; then
247 json add error
"errorNoDnsmasqIpset"
248 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqIpset')!\\n"
250 dns
='dnsmasq.servers'
252 if ! ipset
help hash:net
; then
253 if [ -z "$quiet" ]; then
254 json add error
"errorNoIpset"
255 output
"${_ERROR_}: $(get_text 'errorNoIpset')!\\n"
257 dns
='dnsmasq.servers'
261 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-nftset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'nftset'; then
262 if [ -z "$quiet" ]; then
263 json add error
"errorNoDnsmasqNftset"
264 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqNftset')!\\n"
266 dns
='dnsmasq.servers'
268 if [ -z "$nft" ]; then
269 if [ -z "$quiet" ]; then
270 json add error
"errorNoNft"
271 output
"${_ERROR_}: $(get_text 'errorNoNft')!\\n"
273 dns
='dnsmasq.servers'
280 outputFilter
="$dnsmasqAddnhostsFilter"
281 outputFile
="$dnsmasqAddnhostsFile"
282 outputCache
="$dnsmasqAddnhostsCache"
283 outputGzip
="$dnsmasqAddnhostsGzip"
284 if [ "$ipv6_enabled" -ne 0 ]; then
285 outputFilterIPv6
="$dnsmasqAddnhostsFilterIPv6"
287 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
288 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
289 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
290 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
291 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
294 outputFilter
="$dnsmasqConfFilter"
295 outputFile
="$dnsmasqConfFile"
296 outputCache
="$dnsmasqConfCache"
297 outputGzip
="$dnsmasqConfGzip"
298 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
299 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
300 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
301 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
302 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
305 outputFilter
="$dnsmasqIpsetFilter"
306 outputFile
="$dnsmasqIpsetFile"
307 outputCache
="$dnsmasqIpsetCache"
308 outputGzip
="$dnsmasqIpsetGzip"
309 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
310 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
311 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
312 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
313 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
316 if [ "$ipv6_enabled" -ne 0 ]; then
317 outputFilter
="$dnsmasqNftsetFilterIPv6"
319 outputFilter
="$dnsmasqNftsetFilter"
321 outputFile
="$dnsmasqNftsetFile"
322 outputCache
="$dnsmasqNftsetCache"
323 outputGzip
="$dnsmasqNftsetGzip"
324 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
325 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
326 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
327 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
328 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
331 outputFilter
="$dnsmasqServersFilter"
332 outputFile
="$dnsmasqServersFile"
333 outputCache
="$dnsmasqServersCache"
334 outputGzip
="$dnsmasqServersGzip"
335 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
336 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
337 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
338 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
339 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
342 outputFilter
="$unboundFilter"
343 outputFile
="$unboundFile"
344 outputCache
="$unboundCache"
345 outputGzip
="$unboundGzip"
346 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
347 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
348 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
349 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
350 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
354 for i
in "$outputFile" "$outputCache" "$outputGzip"; do
355 if ! mkdir
-p "$(dirname "$i")"; then
356 json add error
"errorOutputDirCreate" "$i"
357 output
"${_ERROR_}: $(get_text 'errorMkdirFail' "$i")!\\n"
361 .
/lib
/functions
/network.sh
362 .
/usr
/share
/libubox
/jshn.sh
364 is_present
'gawk' && awk='gawk'
365 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
366 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
367 local s
="opkg update; opkg --force-overwrite install"
368 is_present
'gawk' || s
="$s gawk"
369 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
370 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
371 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
372 if [ -z "$quiet" ]; then
373 json add warning
"errorOutputFileCreate" "${i}"
374 output
"${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\\n"
378 # Prefer curl because it supports the file:// scheme.
379 if is_present
'curl'; then
380 dl_command
="curl --silent --insecure"
381 dl_command
="${dl_command}${curl_max_file_size:+ --max-filesize $curl_max_file_size}"
382 dl_command
="${dl_command}${curl_retry:+ --retry $curl_retry}"
383 dl_command
="${dl_command}${download_timeout:+ --connect-timeout $download_timeout}"
385 elif is_present
'/usr/libexec/wget-ssl'; then
386 dl_command
="/usr/libexec/wget-ssl --no-check-certificate -q"
387 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
389 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
390 dl_command
="wget --no-check-certificate -q"
391 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
394 dl_command
="uclient-fetch --no-check-certificate -q"
395 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
398 led
="${led:+/sys/class/leds/$led}"
399 if curl
--version 2>/dev
/null |
grep -q "https" \
400 || wget
--version 2>/dev
/null |
grep -q "+https" \
401 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
402 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
403 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
408 cache
'test' && return 0
409 cache
'test_gzip' && return 0
410 network_flush_cache
; network_find_wan wan_if
; network_get_gateway wan_gw
"$wan_if";
411 [ -n "$wan_gw" ] && return 0
412 json add error
"errorNoWanGateway"
413 output
"${_ERROR_}: $(get_text 'errorNoWanGateway')!\\n"; return 1;
417 local cfg
="$1" param
="$2"
420 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
421 uci_remove
'dhcp' "$cfg" 'serversfile'
423 uci_add_list_if_new
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
425 cleanup|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list
)
426 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
427 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
428 uci_remove
'dhcp' "$cfg" 'serversfile'
432 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
433 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
434 uci_set
'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
441 local param output_text i
444 if [ ! -s "$outputFile" ]; then
445 json
set status
"statusFail"
446 json add error
"errorOutputFileCreate"
447 output
"${_ERROR_}: $(get_text 'errorOutputFileCreate')!\\n"
452 if [ "$dns_instance" = "*" ]; then
453 config_foreach resolver
'dnsmasq' "$dns"
454 elif [ -n "$dns_instance" ]; then
455 for i
in $dns_instance; do
456 resolver
"@dnsmasq[$i]" "$dns" || resolver
"$i" "$dns"
461 dnsmasq.addnhosts|dnsmasq.servers
)
462 param
=dnsmasq_restart
463 output_text
='Reloading dnsmasq'
465 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
466 param
=dnsmasq_restart
467 output_text
='Restarting dnsmasq'
470 param
=unbound_restart
471 output_text
='Restarting Unbound'
475 if [ -n "$(uci_changes dhcp)" ]; then
477 if [ "$param" = 'unbound_restart' ]; then
478 param
='dnsmasq_restart; unbound_restart;'
479 output_text
='Restarting Unbound/dnsmasq'
481 param
=dnsmasq_restart
482 output_text
='Restarting dnsmasq'
485 output
1 "$output_text "
486 output
2 "$output_text "
487 json
set message
"$output_text"
488 if eval "$param"; then
489 json
set status
"statusSuccess"
494 json
set status
"statusFail"
495 json add error
"errorDNSReload"
496 output
"${_ERROR_}: $(get_text 'errorDNSReload')!\\n"
502 dnsmasq.addnhosts|dnsmasq.servers
)
503 param
=dnsmasq_restart
505 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
506 param
=dnsmasq_restart
509 param
=unbound_restart
512 if [ -n "$(uci_changes dhcp)" ]; then
514 if [ "$param" = 'unbound_restart' ]; then
515 param
='dnsmasq_restart; unbound_restart;'
517 param
=dnsmasq_restart
525 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
526 param
=dnsmasq_restart
529 param
=unbound_restart
539 # shellcheck disable=SC2034
540 local action
="$1" param
="$2" value
="$3"
542 # shellcheck disable=SC2124
543 local extras
="$@" line
544 local status message error stats
545 local reload restart curReload curRestart ret i
546 if [ -s "$jsonFile" ]; then
547 json_load_file
"$jsonFile" 2>/dev
/null
548 json_select
'data' 2>/dev
/null
549 for i
in status message error stats reload restart
; do
550 json_get_var
$i "$i" 2>/dev
/null
557 curReload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
558 $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
559 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
560 curRestart
="$compressed_cache $force_dns $led $force_dns_port"
561 if [ ! -s "$jsonFile" ]; then
563 elif [ "$curReload" != "$reload" ]; then
565 elif [ "$curRestart" != "$restart" ]; then
571 printf "%b" "$(eval echo "\$
$param")"; return;;
575 line
="$(eval echo "\$
$param")"
576 eval "$param"='${line:+$line }${value}${extras:+|$extras}'
581 unset status message error stats
;;
583 unset reload restart
;;
591 reload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
592 $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
593 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
594 restart
="$compressed_cache $force_dns $led $force_dns_port"
597 eval "$param"='${value}${extras:+|$extras}';;
602 json_add_object
'data'
603 json_add_string version
"$PKG_VERSION"
604 json_add_string status
"$status"
605 json_add_string message
"$message"
606 json_add_string error
"$error"
607 json_add_string stats
"$stats"
608 json_add_string reload
"$reload"
609 json_add_string restart
"$restart"
611 json_dump
> "$jsonFile"
619 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
623 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
627 [ -s "$outputCache" ]
631 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
635 rm -f "$outputGzip" >/dev
/null
2>/dev
/null
636 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
637 if gzip < "$outputFile" > "$R_TMP"; then
638 if mv "$R_TMP" "$outputGzip"; then
649 expand|unpack|unpack_gzip
)
650 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
657 local label
type D_TMP R_TMP
658 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
659 label
="${1##*//}"; label
="${label%%/*}";
661 dnsmasq
) label
="Dnsmasq: $label"; filter
="$dnsmasqOISDFilter";;
662 domains
) label
="Domains: $label"; filter
="$domainsFilter";;
663 hosts
) label
="Hosts: $label"; filter
="$hostsFilter";;
666 allowed
) type='Allowed'; D_TMP
="$A_TMP";;
667 blocked
) type='Blocked'; D_TMP
="$B_TMP";;
668 file) type='File'; D_TMP
="$B_TMP";;
670 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
672 output
2 "[DL] $type $label $__FAIL__\\n"
673 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
676 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
677 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
679 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
681 output
2 "[DL] $type $label $__FAIL__\\n"
682 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
684 sed -i "$filter" "$R_TMP"
685 if [ ! -s "$R_TMP" ]; then
687 output
2 "[DL] $type $label $__FAIL__\\n"
688 echo "errorParsingList|${1}" >> "$sharedMemoryError"
690 cat "${R_TMP}" >> "$D_TMP"
692 output
2 "[DL] $type $label $__OK__\\n"
699 download_dnsmasq_file
() {
700 local hf allow_filter j
=0 R_TMP
702 json
set message
"$(get_text "statusDownloading
")..."
703 json
set status
"statusDownloading"
705 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
706 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
707 output
3 'Low free memory, restarting resolver '
714 touch $A_TMP; touch $B_TMP;
715 output
1 'Downloading dnsmasq file '
716 rm -f "$sharedMemoryError"
717 process_url
"$dnsmasq_config_file_url" 'dnsmasq' 'file'
719 if [ -s "$sharedMemoryError" ]; then
720 while IFS
= read -r line
; do
721 json add error
"$line"
722 done < "$sharedMemoryError"
723 rm -f "$sharedMemoryError"
725 output
2 'Creating dnsmasq file '
726 if mv "$B_TMP" "$outputFile"; then
727 output
2 "$__OK__\\n"
729 output
2 "$__FAIL__\\n"
730 json add error
"errorMovingDataFile"
736 local hf allow_filter j
=0 R_TMP
738 json
set message
"$(get_text "statusDownloading
")..."
739 json
set status
"statusDownloading"
741 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
742 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
743 output
3 'Low free memory, restarting resolver '
750 touch $A_TMP; touch $B_TMP;
751 output
1 'Downloading lists '
752 rm -f "$sharedMemoryError"
753 for hf
in ${blocked_hosts_url}; do
754 if [ "$parallel_downloads" -gt 0 ]; then
755 process_url
"$hf" 'hosts' 'blocked' &
757 process_url
"$hf" 'hosts' 'blocked'
760 for hf
in ${blocked_domains_url}; do
761 if [ "$parallel_downloads" -gt 0 ]; then
762 process_url
"$hf" 'domains' 'blocked' &
764 process_url
"$hf" 'domains' 'blocked'
767 for hf
in ${allowed_domains_url}; do
768 if [ "$parallel_downloads" -gt 0 ]; then
769 process_url
"$hf" 'domains' 'allowed' &
771 process_url
"$hf" 'domains' 'allowed'
776 if [ -s "$sharedMemoryError" ]; then
777 while IFS
= read -r line
; do
778 json add error
"$line"
779 done < "$sharedMemoryError"
780 rm -f "$sharedMemoryError"
783 if [ "$canary_domains_icloud" -ne 0 ]; then
784 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
786 if [ "$canary_domains_mozilla" -ne 0 ]; then
787 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
790 for hf
in $blocked_domain $canaryDomains; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
791 allowed_domain
="${allowed_domain}
793 for hf
in ${allowed_domain}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; allow_filter
="$allow_filter/(^|\.)${hf}$/d;"; done
795 [ ! -s "$B_TMP" ] && return 1
797 output
1 'Processing downloads '
798 output
2 'Sorting combined list '
799 json
set status
"statusProcessing"
800 json
set message
"$(get_text "statusProcessing
"): sorting combined list"
801 if [ "$allow_non_ascii" -gt 0 ]; then
802 if sort -u "$B_TMP" > "$A_TMP"; then
806 json add error
"errorSorting"
809 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
813 json add error
"errorSorting"
817 if [ "$dns" = 'dnsmasq.conf' ] || \
818 [ "$dns" = 'dnsmasq.ipset' ] || \
819 [ "$dns" = 'dnsmasq.nftset' ] || \
820 [ "$dns" = 'dnsmasq.servers' ] || \
821 [ "$dns" = 'unbound.adb_list' ]; then
822 # TLD optimization written by Dirk Brenken (dev@brenken.org)
823 output
2 'Optimizing combined list '
824 json
set message
"$(get_text "statusProcessing
"): optimizing combined list"
825 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
826 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
827 if sort "$B_TMP" > "$A_TMP"; then
828 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
829 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
830 if sort -u "$A_TMP" > "$B_TMP"; then
834 json add error
"errorOptimization"
839 json add error
"errorOptimization"
843 json add error
"errorOptimization"
848 json add error
"errorOptimization"
852 json add error
"errorOptimization"
859 output
2 'Allowing domains '
860 json
set message
"$(get_text "statusProcessing
"): allowing domains"
861 if sed -i -E "$allow_filter" "$B_TMP"; then
865 json add error
"errorAllowListProcessing"
868 output
2 'Formatting merged file '
869 json
set message
"$(get_text "statusProcessing
"): formatting merged file"
870 if [ -z "$outputFilterIPv6" ]; then
871 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
875 json add error
"errorDataFileFormatting"
880 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
881 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
885 json add error
"errorDataFileFormatting"
893 output
2 'Creating dnsmasq addnhosts file '
894 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq addnhosts file"
897 output
2 'Creating dnsmasq config file '
898 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq config file"
901 output
2 'Creating dnsmasq ipset file '
902 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq ipset file"
905 output
2 'Creating dnsmasq nft set file '
906 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq nft set file"
909 output
2 'Creating dnsmasq servers file '
910 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq servers file"
913 output
2 'Creating Unbound adb_list file '
914 json
set message
"$(get_text "statusProcessing
"): creating Unbound adb_list file"
918 if mv "$A_TMP" "$outputFile"; then
922 json add error
"errorMovingDataFile"
924 if [ "$compressed_cache" -gt 0 ]; then
925 output
2 'Creating compressed cache '
926 json
set message
"$(get_text "statusProcessing
"): creating compressed cache"
927 if cache
'create_gzip'; then
931 json add error
"errorCreatingCompressedCache"
936 output
2 'Removing temporary files '
937 json
set message
"$(get_text "statusProcessing
"): removing temporary files"
938 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
939 if [ $j -eq 0 ]; then
943 json add error
"errorRemovingTempFiles"
949 local c hf string
="$1"
950 local validation_result
="$3"
951 load_environment
"$validation_result" 'quiet' ||
return 1
952 if [ ! -s "$outputFile" ]; then
953 output
"No block-list ('$outputFile') found.\\n"
955 elif [ -z "$string" ]; then
956 output
"Usage: /etc/init.d/${packageName} allow 'domain' ...\\n"
958 elif [ -n "$dnsmasq_config_file_url" ]; then
959 output
"Allowing individual domains is not possible when using external dnsmasq config file.\\n"
963 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
964 output
1 "Allowing domain(s) and restarting dnsmasq "
965 output
2 "Allowing domain(s) \\n"
968 hf
="$(echo "$c" | sed 's/\./\\./g')"
969 if sed -i "/(^|\.)${hf}$/d;" "$outputFile" && \
970 uci_add_list_if_new
"${packageName}" 'config' 'allowed_domain' "$c"; then
976 if [ "$compressed_cache" -gt 0 ]; then
977 output
2 'Creating compressed cache '
978 if cache
'create_gzip'; then
984 output
2 "Committing changes to config "
985 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
986 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
988 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
990 if [ "$dns" = 'dnsmasq.ipset' ]; then
991 output
2 "Flushing adb ipset "
992 if ipset
-q -! flush adb
; then output_ok
; else output_fail
; fi
994 if [ "$dns" = 'dnsmasq.nftset' ]; then
995 output
2 "Flushing adb nft sets "
996 nft flush
set inet fw4 adb6
997 if nft flush
set inet fw4 adb4
; then output_ok
; else output_fail
; fi
999 output
2 "Restarting dnsmasq "
1000 if dnsmasq_restart
; then output_okn
; else output_failn
; fi
1006 output
1 "Allowing domain(s) and restarting Unbound "
1007 output
2 "Allowing domain(s) \\n"
1008 for c
in $string; do
1010 if sed -i "/${string}/d" "$outputFile" && \
1011 uci_add_list_if_new
"$packageName" 'config' 'allowed_domain' "$string"; then
1017 if [ "$compressed_cache" -gt 0 ]; then
1018 output
2 'Creating compressed cache '
1019 if cache
'create_gzip'; then
1025 output
2 "Committing changes to config "
1026 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1027 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1029 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1031 output
2 "Restarting Unbound "
1032 if unbound_restart
; then output_okn
; else output_failn
; fi
1042 local validation_result
="$3"
1043 load_environment
"$validation_result" 'quiet' ||
return 1
1044 if [ ! -s "$outputFile" ]; then
1045 output
"No block-list ('$outputFile') found.\\n"
1047 elif [ -z "$param" ]; then
1048 output
"Usage: /etc/init.d/${packageName} check 'domain' ...\\n"
1051 for string
in ${param}; do
1052 c
="$(grep -c "$string" "$outputFile")"
1053 if [ "$c" -gt 0 ]; then
1054 if [ "$c" -eq 1 ]; then
1055 output
"Found 1 match for '$string' in '$outputFile'.\\n"
1057 output
"Found $c matches for '$string' in '$outputFile'.\\n"
1059 if [ "$c" -le 20 ]; then
1062 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1064 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1066 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1068 grep "$string" "$outputFile" |
sed 's|nftset=/||;s|/4#inet#adb#adb4||;';;
1070 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1072 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1076 output
"The '$string' is not found in current block-list ('$outputFile').\\n"
1081 adb_config_update
() {
1083 local param
="$1" validation_result
="$3"
1084 load_environment
"$validation_result" 'quiet' ||
return 1
1085 label
="${config_update_url##*//}"
1086 label
="${label%%/*}";
1087 [ "$config_update_enabled" -ne 0 ] ||
return 0
1089 if [ "$param" != 'download' ]; then
1090 cache
'test' && return 0
1091 cache
'test_gzip' && return 0
1093 output
1 'Updating config '
1094 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
1095 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
1097 if ! $dl_command "$config_update_url" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
1098 output
1 "$_FAIL_\\n"
1099 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1100 json add error
"errorDownloadingConfigUpdate"
1102 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i "$packageConfigFile" 2>/dev
/null
; then
1104 output
2 "[DL] Config Update: $label $__OK__\\n"
1106 output
1 "$_FAIL_\\n"
1107 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1108 json add error
"errorParsingConfigUpdate"
1117 local validation_result
="$3"
1118 load_environment
"$validation_result" 'quiet' ||
return 1
1121 for i
in $blocked_domains_url; do
1122 [ "${i//melmac}" != "$i" ] && continue
1123 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1124 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1125 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1126 echo "# block-list too big for most routers"
1127 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1128 echo "# block-list may be too big for some routers"
1131 echo " list blocked_domains_url '$i'"
1134 echo "# site was down on last check"
1135 echo "# list blocked_domains_url '$i'"
1139 for i
in $blocked_hosts_url; do
1140 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1141 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1142 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1143 echo "# block-list too big for most routers"
1144 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1145 echo "# block-list may be too big for some routers"
1148 echo " list blocked_hosts_url '$i'"
1151 echo "# site was down on last check"
1152 echo "# list blocked_hosts_url '$i'"
1159 local action status error message stats c
1160 local validation_result
="$3"
1162 load_environment
"$validation_result" ||
return 1
1164 status
="$(json get status)"
1165 error
="$(json get error)"
1166 message
="$(json get message)"
1167 stats
="$(json get stats)"
1168 action
="$(json get triggers)"
1170 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
1171 if cache
'test_gzip' || cache
'test'; then
1176 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
1178 elif [ ! -s "$outputFile" ]; then
1179 if cache
'test_gzip' || cache
'test'; then
1184 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
1186 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
1196 if [ "$action" = 'restore' ]; then
1197 output
0 "Starting $serviceName... "
1198 output
3 "Starting $serviceName...\\n"
1199 json
set status
"statusStarting"
1200 if cache
'test_gzip' && ! cache
'test' && [ ! -s "$outputFile" ]; then
1201 output
3 'Found compressed cache file, unpacking it '
1202 json
set message
'found compressed cache file, unpacking it.'
1203 if cache
'unpack_gzip'; then
1207 json add error
"errorRestoreCompressedCache"
1208 output
"${_ERROR_}: $(get_text 'errorRestoreCompressedCache')!\\n"
1212 if cache
'test' && [ ! -s "$outputFile" ]; then
1213 output
3 'Found cache file, reusing it '
1214 json
set message
'found cache file, reusing it.'
1215 if cache
'restore'; then
1220 json add error
"errorRestoreCache"
1221 output
"${_ERROR_}: $(get_text 'errorRestoreCache')!\\n"
1226 if [ "$action" = 'download' ]; then
1227 if [ -s "$outputFile" ] || cache
'test' || cache
'test_gzip'; then
1228 output
0 "Force-reloading $serviceName... "
1229 output
3 "Force-reloading $serviceName...\\n"
1230 json
set status
"statusForceReloading"
1232 output
0 "Starting $serviceName... "
1233 output
3 "Starting $serviceName...\\n"
1234 json
set status
"statusStarting"
1236 if [ -n "$dnsmasq_config_file_url" ]; then
1237 download_dnsmasq_file
1243 if [ "$action" = 'restart' ]; then
1244 output
0 "Restarting $serviceName... "
1245 output
3 "Restarting $serviceName...\\n"
1246 json
set status
"statusRestarting"
1249 if [ "$action" = 'start' ]; then
1250 output
0 "Starting $serviceName... "
1251 output
3 "Starting $serviceName...\\n"
1252 json
set status
"statusStarting"
1255 if [ -s "$outputFile" ] && [ "$(json get status)" != "statusFail" ]; then
1256 output
0 "$__OK__\\n";
1258 json
set status
"statusSuccess"
1259 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1262 output
0 "$__FAIL__\\n";
1263 json
set status
"statusFail"
1264 json add error
"errorOhSnap"
1268 procd_open_instance
'main'
1269 procd_set_param
command /bin
/true
1270 procd_set_param stdout
1
1271 procd_set_param stderr
1
1273 json_add_string
'status' "$(json get status)"
1274 json_add_string
'errors' "$(json get errors)"
1275 json_add_int
'entries' "$(wc -l < "$outputFile")"
1276 json_add_array firewall
1277 if [ "$force_dns" -ne 0 ]; then
1278 for c
in $force_dns_port; do
1279 if netstat
-tuln |
grep LISTEN |
grep ":${c}" >/dev
/null
2>&1; then
1281 json_add_string
type redirect
1282 json_add_string target DNAT
1283 json_add_string src lan
1284 json_add_string proto
"tcp udp"
1285 json_add_string src_dport
"$c"
1286 json_add_string dest_port
"$c"
1287 json_add_boolean reflection
0
1291 json_add_string
type rule
1292 json_add_string src lan
1293 json_add_string dest
"*"
1294 json_add_string proto
"tcp udp"
1295 json_add_string dest_port
"$c"
1296 json_add_string target REJECT
1304 json_add_string
type ipset
1305 json_add_string name adb
1306 json_add_string match dest_net
1307 json_add_string storage
hash
1310 json_add_string
type rule
1311 json_add_string ipset adb
1312 json_add_string src lan
1313 json_add_string dest
"*"
1314 json_add_string proto
"tcp udp"
1315 json_add_string target REJECT
1320 json_add_string
type ipset
1321 json_add_string name adb4
1322 json_add_string family
4
1323 json_add_string match dest_net
1326 json_add_string
type rule
1327 json_add_string ipset adb4
1328 json_add_string src lan
1329 json_add_string dest
"*"
1330 json_add_string proto
"tcp udp"
1331 json_add_string target REJECT
1333 if [ "$ipv6_enabled" -ne 0 ]; then
1335 json_add_string
type ipset
1336 json_add_string name adb6
1337 json_add_string family
6
1338 json_add_string match dest_net
1341 json_add_string
type rule
1342 json_add_string ipset adb6
1343 json_add_string src lan
1344 json_add_string dest
"*"
1345 json_add_string proto
"tcp udp"
1346 json_add_string target REJECT
1353 procd_close_instance
1357 local c url status message error stats
1358 local validation_result
="$3"
1359 load_environment
"$validation_result" 'quiet' ||
return 1
1360 status
="$(json get status)"
1361 message
="$(json get message)"
1362 error
="$(json get error)"
1363 stats
="$(json get stats)"
1364 if [ "$status" = "statusSuccess" ]; then
1365 output
"$stats "; output_okn
;
1367 [ -n "$status" ] && status
="$(get_text "$status")"
1368 if [ -n "$status" ] && [ -n "$message" ]; then
1369 status
="${status}: $message"
1371 [ -n "$status" ] && output
"$serviceName $status\\n"
1373 if [ -n "$error" ]; then
1378 errorDownloadingList|errorParsingList
)
1379 output
"${_ERROR_}: $(get_text "$c") $url!\\n";;
1381 output
"${_ERROR_}: $(get_text "$c")!\\n";;
1389 local validation_result
="$3"
1390 load_environment
"$validation_result" 'quiet' ||
return 1
1391 if [ -s "$outputFile" ]; then
1392 output
"Stopping $serviceName... "
1394 if dns
'on_stop'; then
1395 ipset
-q -! flush adb
1396 ipset
-q -! destroy adb
1397 nft delete
set inet fw4 adb4
1398 nft delete
set inet fw4 adb6
1400 output
0 "$__OK__\\n"; output_okn
;
1401 json
set status
"statusStopped"
1404 output
0 "$__FAIL__\\n"; output_fail
;
1405 json
set status
"statusFail"
1406 json add error
"errorStopping"
1407 output
"${_ERROR_}: $(get_text 'errorStopping')!\\n"
1412 service_started
() { procd_set_config_changed firewall
; }
1413 service_stopped
() { procd_set_config_changed firewall
; }
1414 restart_service
() { rc_procd start_service
'restart'; }
1415 reload_service
() { rc_procd start_service
'restart'; }
1417 load_validate_config
'config' adb_config_update
"'$*'"
1418 load_validate_config
'config' adb_start
"'$*'"
1420 stop_service
() { load_validate_config
'config' adb_stop
"'$*'"; }
1421 status_service
() { load_validate_config
'config' adb_status
"''"; }
1422 service_triggers
() {
1424 local procd_trigger_wan6
1425 config_load
"$packageName"
1426 config_get_bool procd_trigger_wan6
'config' 'procd_trigger_wan6' '0'
1427 .
/lib
/functions
/network.sh
1429 network_find_wan wan
1431 if [ "$procd_trigger_wan6" -ne 0 ]; then
1432 network_find_wan6 wan6
1433 wan6
="${wan6:-wan6}"
1435 for i
in "$wan" "$wan6"; do
1436 [ -n "$i" ] && procd_add_interface_trigger
"interface.*" "$i" "/etc/init.d/${packageName}" start
1438 procd_add_config_trigger
"config.change" "$packageName" "/etc/init.d/${packageName}" reload
1440 allow
() { load_validate_config
'config' adb_allow
"'$*'"; }
1441 check
() { load_validate_config
'config' adb_check
"'$*'"; }
1442 dl
() { rc_procd start_service
'download'; }
1444 rm -f "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
1445 rm -f "$dnsmasqConfCache" "$dnsmasqConfGzip"
1446 rm -f "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
1447 rm -f "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
1448 rm -f "$dnsmasqServersCache" "$dnsmasqServersGzip"
1449 rm -f "$unboundCache" "$unboundGzip"
1451 config_foreach resolver
'dnsmasq' 'cleanup'
1455 sizes
() { load_validate_config
'config' adb_sizes
"''"; }
1456 version
() { echo "$PKG_VERSION"; }
1458 load_validate_config
() {
1461 local isSSLSupported
1463 local outputFilterIPv6
1470 local force_dns_port
1471 local parallel_downloads
1473 local compressed_cache
1475 local allow_non_ascii
1476 local canary_domains_icloud
1477 local canary_domains_mozilla
1478 local config_update_enabled
1479 local config_update_url
1481 local download_timeout
1482 local curl_max_file_size
1488 local allowed_domain
1489 local allowed_domains_url
1490 local blocked_domain
1491 local blocked_domains_url
1492 local blocked_hosts_url
1493 local dnsmasq_config_file_url
1494 uci_load_validate
"$packageName" "$packageName" "$1" "${2}${3:+ $3}" \
1496 'force_dns:bool:1' \
1497 'force_dns_port:list(integer):53 853' \
1498 'parallel_downloads:bool:1' \
1500 'compressed_cache:bool:0' \
1501 'ipv6_enabled:bool:0' \
1502 'allow_non_ascii:bool:0' \
1503 'canary_domains_icloud:bool:0' \
1504 'canary_domains_mozilla:bool:0' \
1505 'config_update_enabled:bool:0' \
1506 'config_update_url:string:https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update' \
1507 'download_timeout:range(1,60):20' \
1508 'curl_max_file_size:uinteger' \
1509 'curl_retry:range(0,30):3' \
1510 'verbosity:range(0,2):2' \
1511 'procd_trigger_wan6:bool:0' \
1512 'led:or("", "none", file, device, string)' \
1513 'dns:or("dnsmasq.addnhosts", "dnsmasq.conf", "dnsmasq.ipset", "dnsmasq.nftset", "dnsmasq.servers", "unbound.adb_list"):dnsmasq.servers' \
1514 'dns_instance:or(list(integer, string)):0' \
1515 'allowed_domain:list(string)' \
1516 'allowed_domains_url:list(string)' \
1517 'blocked_domain:list(string)' \
1518 'blocked_domains_url:list(string)' \
1519 'blocked_hosts_url:list(string)' \
1520 'dnsmasq_config_file_url:string'