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
="${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
="${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
="${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
="${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
="${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
="${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 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;'
63 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
64 readonly adBlockPlusFilter
='/^#/d;/^!/d;s/[[:space:]]*#.*$//;s/^||//;s/\^$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
65 readonly dnsmasqFileFilter
='\|^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'
88 load_environment_flag
=
90 debug
() { local i j
; for i
in "$@"; do eval "j=\$$i"; echo "${i}: ${j} "; done; }
92 uci_add_list_if_new() {
98 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
99 for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
100 [ "$i" = "$VALUE" ] && return 0
102 uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
108 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
111 ipset
() { "$ipset" "$@" >/dev
/null
2>&1; }
112 nft
() { "$nft" "$@" >/dev
/null
2>&1; }
117 errorConfigValidationFail
) r
="$packageName config validation failed";;
118 errorServiceDisabled
) r
="$packageName is currently disabled";;
120 r
="dnsmasq ipset support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support ipset";;
122 r
="dnsmasq ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type";;
123 errorNoDnsmasqNftset
)
124 r
="dnsmasq nft set support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support nft set";;
125 errorNoNft
) r
="dnsmasq nft sets support is enabled in $packageName, but nft is not installed";;
126 errorNoWanGateway
) r
="The ${serviceName} failed to discover WAN gateway";;
127 errorOutputDirCreate
) r
="failed to create directory for %s file";;
128 errorOutputFileCreate
) r
="failed to create $outputFile file";;
129 errorFailDNSReload
) r
="failed to restart/reload DNS resolver";;
130 errorSharedMemory
) r
="failed to access shared memory";;
131 errorSorting
) r
="failed to sort data file";;
132 errorOptimization
) r
="failed to optimize data file";;
133 errorAllowListProcessing
) r
="failed to process allow-list";;
134 errorDataFileFormatting
) r
="failed to format data file";;
135 errorMovingDataFile
) r
="failed to move data file '${A_TMP}' to '${outputFile}'";;
136 errorCreatingCompressedCache
) r
="failed to create compressed cache";;
137 errorRemovingTempFiles
) r
="failed to remove temporary files";;
138 errorRestoreCompressedCache
) r
="failed to unpack compressed cache";;
139 errorRestoreCache
) r
="failed to move '$outputCache' to '$outputFile'";;
140 errorOhSnap
) r
="failed to create block-list or restart DNS resolver";;
141 errorStopping
) r
="failed to stop $serviceName";;
142 errorDNSReload
) r
="failed to reload/restart DNS resolver";;
143 errorDownloadingConfigUpdate
) r
="failed to download Config Update file";;
144 errorDownloadingList
) r
="failed to download";;
145 errorParsingConfigUpdate
) r
="failed to parse Config Update file";;
146 errorParsingList
) r
="failed to parse";;
147 errorNoSSLSupport
) r
="no HTTPS/SSL support on device";;
148 errorCreatingDirectory
) r
="failed to create output/cache/gzip file directory";;
150 statusNoInstall
) r
="$serviceName is not installed or not found";;
151 statusStopped
) r
="Stopped";;
152 statusStarting
) r
="Starting";;
153 statusRestarting
) r
="Restarting";;
154 statusForceReloading
) r
="Force Reloading";;
155 statusDownloading
) r
="Downloading";;
156 statusProcessing
) r
="Processing";;
157 statusError
) r
="Error";;
158 statusWarning
) r
="Warning";;
159 statusFail
) r
="Fail";;
160 statusSuccess
) r
="Success";;
162 warningExternalDnsmasqConfig
)
163 r
="use of external dnsmasq config file detected, please set 'dns' option to 'dnsmasq.conf'";;
164 warningMissingRecommendedPackages
) r
="Some recommended packages are missing";;
165 warningInvalidCompressedCacheDir
) r
="invalid compressed cache directory '%s'";;
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; }
186 sanitize_dir
() { [ -d "$(readlink -fn "$1")" ] && readlink
-fn "$1"; }
189 # Can take a single parameter (text) to be output at any verbosity
190 # Or target verbosity level and text to be output at specifc verbosity
191 local msg memmsg logmsg
192 local sharedMemoryOutput
="/dev/shm/$packageName-output"
193 verbosity
="${verbosity:-2}"
194 if [ $# -ne 1 ]; then
195 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
197 [ -t 1 ] && printf "%b" "$1"
198 msg
="${1//$serviceName /service }";
199 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
200 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
201 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
202 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
203 rm -f "$sharedMemoryOutput"
205 printf "%b" "$msg" >> "$sharedMemoryOutput"
211 local i j wan_if wan_gw wan_proto
212 local counter wan_if_timeout
="$procd_boot_wan_timeout" wan_gw_timeout
='5'
214 while [ -z "$wan_if" ]; do
216 network_find_wan wan_if
217 if [ -n "$wan_if" ]; then
218 output
"WAN interface found: '${wan_if}'.\\n"
221 if [ "$counter" -gt "$wan_if_timeout" ]; then
222 output
"WAN interface timeout, assuming 'wan'.\\n"
226 counter
=$
((counter
+1))
227 output
"Waiting to discover WAN Interface...\\n"
232 wan_proto
="$(uci -q get "network.
${wan_if}.proto
")"
233 if [ "$wan_proto" = 'pppoe' ]; then
234 wan_gw_timeout
=$
((wan_gw_timeout
+10))
236 while [ "$counter" -le "$wan_gw_timeout" ]; do
238 network_get_gateway wan_gw
"$wan_if"
239 if [ -n "$wan_gw" ]; then
240 output
"WAN gateway found: '${wan_gw}.'\\n"
243 counter
=$
((counter
+1))
244 output
"Waiting to discover $wan_if gateway...\\n"
247 json add error
"errorNoWanGateway"
248 output
"${_ERROR_}: $(get_text 'errorNoWanGateway')!\\n"; return 1;
253 local validation_result
="$1" param
="$2"
255 [ -z "$load_environment_flag" ] ||
return 0
257 if [ "$validation_result" != '0' ]; then
258 json add error
"errorConfigValidationFail"
259 output
"${_ERROR_}: $(get_text 'errorConfigValidationFail')!\\n"
260 output
"Please check if the '$packageConfigFile' contains correct values for config options.\\n"
264 if [ "$enabled" -eq 0 ]; then
265 json add error
"errorServiceDisabled"
266 output
"${_ERROR_}: $(get_text 'errorServiceDisabled')!\\n"
267 output
"Run the following commands before starting service again:\\n"
268 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
272 if [ "$debug" -ne 0 ]; then
273 exec 1>>/tmp
/simple-adblock.log
278 if [ -n "$dnsmasq_config_file_url" ]; then
282 if [ "$param" != 'quiet' ]; then
283 json add warning
"warningExternalDnsmasqConfig"
284 output
"${_WARNING_}: $(get_text 'warningExternalDnsmasqConfig')!\\n"
291 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
292 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
302 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
303 if [ "$param" != 'quiet' ]; then
304 json add error
"errorNoDnsmasqIpset"
305 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqIpset')!\\n"
307 dns
='dnsmasq.servers'
309 if ! ipset
help hash:net
; then
310 if [ "$param" != 'quiet' ]; then
311 json add error
"errorNoIpset"
312 output
"${_ERROR_}: $(get_text 'errorNoIpset')!\\n"
314 dns
='dnsmasq.servers'
318 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-nftset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'nftset'; then
319 if [ "$param" != 'quiet' ]; then
320 json add error
"errorNoDnsmasqNftset"
321 output
"${_ERROR_}: $(get_text 'errorNoDnsmasqNftset')!\\n"
323 dns
='dnsmasq.servers'
325 if [ -z "$nft" ]; then
326 if [ "$param" != 'quiet' ]; then
327 json add error
"errorNoNft"
328 output
"${_ERROR_}: $(get_text 'errorNoNft')!\\n"
330 dns
='dnsmasq.servers'
335 if [ "$(sanitize_dir "$compressed_cache_dir")" = '/' ]; then
336 compressed_cache_dir
=''
337 elif [ -n "$(sanitize_dir "$compressed_cache_dir")" ]; then
338 compressed_cache_dir
="$(sanitize_dir "$compressed_cache_dir")"
340 json add warning
'warningInvalidCompressedCacheDir' "$compressed_cache_dir"
341 compressed_cache_dir
="/etc"
346 outputFilter
="$dnsmasqAddnhostsFilter"
347 outputFile
="$dnsmasqAddnhostsFile"
348 outputCache
="$dnsmasqAddnhostsCache"
349 outputGzip
="${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
350 if [ "$ipv6_enabled" -ne 0 ]; then
351 outputFilterIPv6
="$dnsmasqAddnhostsFilterIPv6"
353 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
354 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
355 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
356 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
357 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
360 outputFilter
="$dnsmasqConfFilter"
361 outputFile
="$dnsmasqConfFile"
362 outputCache
="$dnsmasqConfCache"
363 outputGzip
="${compressed_cache_dir}/${dnsmasqConfGzip}"
364 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
365 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
366 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
367 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
368 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
371 outputFilter
="$dnsmasqIpsetFilter"
372 outputFile
="$dnsmasqIpsetFile"
373 outputCache
="$dnsmasqIpsetCache"
374 outputGzip
="${compressed_cache_dir}/${dnsmasqIpsetGzip}"
375 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
376 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
377 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
378 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
379 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
382 if [ "$ipv6_enabled" -ne 0 ]; then
383 outputFilter
="$dnsmasqNftsetFilterIPv6"
385 outputFilter
="$dnsmasqNftsetFilter"
387 outputFile
="$dnsmasqNftsetFile"
388 outputCache
="$dnsmasqNftsetCache"
389 outputGzip
="${compressed_cache_dir}/${dnsmasqNftsetGzip}"
390 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
391 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
392 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
393 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
394 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
397 outputFilter
="$dnsmasqServersFilter"
398 outputFile
="$dnsmasqServersFile"
399 outputCache
="$dnsmasqServersCache"
400 outputGzip
="${compressed_cache_dir}/${dnsmasqServersGzip}"
401 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
402 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
403 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
404 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
405 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
408 outputFilter
="$unboundFilter"
409 outputFile
="$unboundFile"
410 outputCache
="$unboundCache"
411 outputGzip
="$unboundGzip"
412 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
413 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
414 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
415 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
416 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
420 for i
in "$jsonFile" "$outputFile" "$outputCache" "$outputGzip"; do
421 if ! mkdir
-p "$(dirname "$i")"; then
422 if [ "$param" != 'quiet' ]; then
423 json add error
"errorOutputDirCreate" "$i"
424 output
"${_ERROR_}: $(get_text 'errorOutputDirCreate' "$i")!\\n"
429 is_present
'gawk' && awk='gawk'
430 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
431 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
432 local s
="opkg update; opkg --force-overwrite install"
433 is_present
'gawk' || s
="$s gawk"
434 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
435 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
436 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
437 if [ "$param" != 'quiet' ]; then
438 json add warning
"warningMissingRecommendedPackages" "${i}"
439 output
"${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\\n"
443 # Prefer curl because it supports the file:// scheme.
444 if is_present
'curl'; then
445 dl_command
="curl --silent --insecure"
446 dl_command
="${dl_command}${curl_additional_param:+ $curl_additional_param}"
447 dl_command
="${dl_command}${curl_max_file_size:+ --max-filesize $curl_max_file_size}"
448 dl_command
="${dl_command}${curl_retry:+ --retry $curl_retry}"
449 dl_command
="${dl_command}${download_timeout:+ --connect-timeout $download_timeout}"
451 elif is_present
'/usr/libexec/wget-ssl'; then
452 dl_command
="/usr/libexec/wget-ssl --no-check-certificate -q"
453 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
455 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
456 dl_command
="wget --no-check-certificate -q"
457 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
460 dl_command
="uclient-fetch --no-check-certificate -q"
461 dl_command
="${dl_command}${download_timeout:+ --timeout $download_timeout}"
464 led
="${led:+/sys/class/leds/$led}"
465 if curl
--version 2>/dev
/null |
grep -q "https" \
466 || wget
--version 2>/dev
/null |
grep -q "+https" \
467 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
468 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
469 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
474 load_environment_flag
=1
475 cache
'test' && return 0
476 cache
'test_gzip' && return 0
477 if [ "$param" = 'on_boot' ]; then
478 load_network
"$param"
486 local cfg
="$1" param
="$2"
489 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
490 uci_remove
'dhcp' "$cfg" 'serversfile'
492 uci_add_list_if_new
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
494 cleanup|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list
)
495 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
496 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
497 uci_remove
'dhcp' "$cfg" 'serversfile'
501 uci_remove_list
'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
502 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
503 uci_set
'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
510 local param output_text i
513 if [ ! -s "$outputFile" ]; then
514 json
set status
"statusFail"
515 json add error
"errorOutputFileCreate"
516 output
"${_ERROR_}: $(get_text 'errorOutputFileCreate')!\\n"
521 if [ "$dns_instance" = "*" ]; then
522 config_foreach resolver
'dnsmasq' "$dns"
523 elif [ -n "$dns_instance" ]; then
524 for i
in $dns_instance; do
525 resolver
"@dnsmasq[$i]" "$dns" || resolver
"$i" "$dns"
530 dnsmasq.addnhosts|dnsmasq.servers
)
531 param
=dnsmasq_restart
532 output_text
='Reloading dnsmasq'
534 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
535 param
=dnsmasq_restart
536 output_text
='Restarting dnsmasq'
539 param
=unbound_restart
540 output_text
='Restarting Unbound'
544 if [ -n "$(uci_changes dhcp)" ]; then
546 if [ "$param" = 'unbound_restart' ]; then
547 param
='dnsmasq_restart; unbound_restart;'
548 output_text
='Restarting Unbound/dnsmasq'
550 param
=dnsmasq_restart
551 output_text
='Restarting dnsmasq'
554 output
1 "$output_text "
555 output
2 "$output_text "
556 json
set message
"$output_text"
557 if eval "$param"; then
558 json
set status
"statusSuccess"
563 json
set status
"statusFail"
564 json add error
"errorDNSReload"
565 output
"${_ERROR_}: $(get_text 'errorDNSReload')!\\n"
571 dnsmasq.addnhosts|dnsmasq.servers
)
572 param
=dnsmasq_restart
574 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset
)
575 param
=dnsmasq_restart
578 param
=unbound_restart
581 if [ -n "$(uci_changes dhcp)" ]; then
583 if [ "$param" = 'unbound_restart' ]; then
584 param
='dnsmasq_restart; unbound_restart;'
586 param
=dnsmasq_restart
594 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
595 param
=dnsmasq_restart
598 param
=unbound_restart
608 # shellcheck disable=SC2034
609 local action
="$1" param
="$2" value
="$3"
611 # shellcheck disable=SC2124
612 local extras
="$@" line
613 local status message error stats
614 local reload restart curReload curRestart ret i
615 if [ -s "$jsonFile" ]; then
616 json_load_file
"$jsonFile" 2>/dev
/null
617 json_select
'data' 2>/dev
/null
618 for i
in status message error stats reload restart
; do
619 json_get_var
$i "$i" 2>/dev
/null
626 curReload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
627 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
628 $dnsmasq_config_file_url $curl_additional_param $curl_max_file_size $curl_retry"
629 curRestart
="$compressed_cache $compressed_cache_dir $force_dns $led $force_dns_port"
630 if [ ! -s "$jsonFile" ]; then
632 elif [ "$curReload" != "$reload" ]; then
634 elif [ "$curRestart" != "$restart" ]; then
640 printf "%b" "$(eval echo "\$
$param")"; return;;
644 line
="$(eval echo "\$
$param")"
645 eval "$param"='${line:+$line }${value}${extras:+|$extras}'
650 unset status message error stats
;;
652 unset reload restart
;;
660 reload
="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
661 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
662 $dnsmasq_config_file_url $curl_additional_param $curl_max_file_size $curl_retry"
663 restart
="$compressed_cache $compressed_cache_dir $force_dns $led $force_dns_port"
666 eval "$param"='${value}${extras:+|$extras}';;
671 json_add_object
'data'
672 json_add_string version
"$PKG_VERSION"
673 json_add_string status
"$status"
674 json_add_string message
"$message"
675 json_add_string error
"$error"
676 json_add_string stats
"$stats"
677 json_add_string reload
"$reload"
678 json_add_string restart
"$restart"
680 mkdir
-p "$(dirname "$jsonFile")"
681 json_dump
> "$jsonFile"
689 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
693 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
697 [ -s "$outputCache" ]
701 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
705 rm -f "$outputGzip" >/dev
/null
2>/dev
/null
706 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
707 if gzip < "$outputFile" > "$R_TMP"; then
708 if mv "$R_TMP" "$outputGzip"; then
719 expand|unpack|unpack_gzip
)
720 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
727 local label
type D_TMP R_TMP
728 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
729 label
="${1##*//}"; label
="${label%%/*}";
731 adbp
) label
="ADBPlus: $label"; filter
="$adBlockPlusFilter"
733 dnsmasq
) label
="Dnsmasq: $label"; filter
="$dnsmasqFileFilter"
735 domains
) label
="Domains: $label"; filter
="$domainsFilter"
737 hosts
) label
="Hosts: $label"; filter
="$hostsFilter"
741 allowed
) type='Allowed'; D_TMP
="$A_TMP"
743 blocked
) type='Blocked'; D_TMP
="$B_TMP"
745 file) type='File'; D_TMP
="$B_TMP"
748 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
750 output
2 "[DL] $type $label $__FAIL__\\n"
751 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
754 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
755 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
757 if ! $dl_command "$1" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
759 output
2 "[DL] $type $label $__FAIL__\\n"
760 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
762 sed -i "$filter" "$R_TMP"
763 if [ ! -s "$R_TMP" ]; then
765 output
2 "[DL] $type $label $__FAIL__\\n"
766 echo "errorParsingList|${1}" >> "$sharedMemoryError"
768 cat "${R_TMP}" >> "$D_TMP"
770 output
2 "[DL] $type $label $__OK__\\n"
777 download_dnsmasq_file
() {
778 local hf allow_filter j
=0 R_TMP
780 json
set message
"$(get_text "statusDownloading
")..."
781 json
set status
"statusDownloading"
783 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
784 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
785 output
3 'Low free memory, restarting resolver '
792 touch $A_TMP; touch $B_TMP;
793 output
1 'Downloading dnsmasq file '
794 rm -f "$sharedMemoryError"
795 process_url
"$dnsmasq_config_file_url" 'dnsmasq' 'file'
797 if [ -s "$sharedMemoryError" ]; then
798 while IFS
= read -r line
; do
799 json add error
"$line"
800 done < "$sharedMemoryError"
801 rm -f "$sharedMemoryError"
803 output
2 'Creating dnsmasq file '
804 if mv "$B_TMP" "$outputFile"; then
805 output
2 "$__OK__\\n"
807 output
2 "$__FAIL__\\n"
808 json add error
"errorMovingDataFile"
814 local hf allow_filter j
=0 R_TMP
816 json
set message
"$(get_text "statusDownloading
")..."
817 json
set status
"statusDownloading"
819 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
820 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
821 output
3 'Low free memory, restarting resolver '
828 touch $A_TMP; touch $B_TMP;
829 output
1 'Downloading lists '
830 rm -f "$sharedMemoryError"
831 for hf
in ${blocked_hosts_url}; do
832 if [ "$parallel_downloads" -gt 0 ]; then
833 process_url
"$hf" 'hosts' 'blocked' &
835 process_url
"$hf" 'hosts' 'blocked'
838 for hf
in ${blocked_adblockplus_url}; do
839 if [ "$parallel_downloads" -gt 0 ]; then
840 process_url
"$hf" 'adbp' 'blocked' &
842 process_url
"$hf" 'adbp' 'blocked'
845 for hf
in ${blocked_domains_url}; do
846 if [ "$parallel_downloads" -gt 0 ]; then
847 process_url
"$hf" 'domains' 'blocked' &
849 process_url
"$hf" 'domains' 'blocked'
852 for hf
in ${allowed_domains_url}; do
853 if [ "$parallel_downloads" -gt 0 ]; then
854 process_url
"$hf" 'domains' 'allowed' &
856 process_url
"$hf" 'domains' 'allowed'
861 if [ -s "$sharedMemoryError" ]; then
862 while IFS
= read -r line
; do
863 json add error
"$line"
864 done < "$sharedMemoryError"
865 rm -f "$sharedMemoryError"
868 if [ "$canary_domains_icloud" -ne 0 ]; then
869 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
871 if [ "$canary_domains_mozilla" -ne 0 ]; then
872 canaryDomains
="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
875 for hf
in $blocked_domain $canaryDomains; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
876 allowed_domain
="${allowed_domain}
878 for hf
in ${allowed_domain}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; allow_filter
="$allow_filter/(^|\.)${hf}$/d;"; done
880 [ ! -s "$B_TMP" ] && return 1
882 output
1 'Processing downloads '
883 output
2 'Sorting combined list '
884 json
set status
"statusProcessing"
885 json
set message
"$(get_text "statusProcessing
"): sorting combined list"
886 if [ "$allow_non_ascii" -gt 0 ]; then
887 if sort -u "$B_TMP" > "$A_TMP"; then
891 json add error
"errorSorting"
894 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
898 json add error
"errorSorting"
902 if [ "$dns" = 'dnsmasq.conf' ] || \
903 [ "$dns" = 'dnsmasq.ipset' ] || \
904 [ "$dns" = 'dnsmasq.nftset' ] || \
905 [ "$dns" = 'dnsmasq.servers' ] || \
906 [ "$dns" = 'unbound.adb_list' ]; then
907 # TLD optimization written by Dirk Brenken (dev@brenken.org)
908 output
2 'Optimizing combined list '
909 json
set message
"$(get_text "statusProcessing
"): optimizing combined list"
910 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
911 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
912 if sort "$B_TMP" > "$A_TMP"; then
913 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
914 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
915 if sort -u "$A_TMP" > "$B_TMP"; then
919 json add error
"errorOptimization"
924 json add error
"errorOptimization"
928 json add error
"errorOptimization"
933 json add error
"errorOptimization"
937 json add error
"errorOptimization"
944 output
2 'Allowing domains '
945 json
set message
"$(get_text "statusProcessing
"): allowing domains"
946 if sed -i -E "$allow_filter" "$B_TMP"; then
950 json add error
"errorAllowListProcessing"
953 output
2 'Formatting merged file '
954 json
set message
"$(get_text "statusProcessing
"): formatting merged file"
955 if [ -z "$outputFilterIPv6" ]; then
956 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
960 json add error
"errorDataFileFormatting"
965 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
966 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
970 json add error
"errorDataFileFormatting"
978 output
2 'Creating dnsmasq addnhosts file '
979 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq addnhosts file"
982 output
2 'Creating dnsmasq config file '
983 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq config file"
986 output
2 'Creating dnsmasq ipset file '
987 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq ipset file"
990 output
2 'Creating dnsmasq nft set file '
991 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq nft set file"
994 output
2 'Creating dnsmasq servers file '
995 json
set message
"$(get_text "statusProcessing
"): creating dnsmasq servers file"
998 output
2 'Creating Unbound adb_list file '
999 json
set message
"$(get_text "statusProcessing
"): creating Unbound adb_list file"
1003 if mv "$A_TMP" "$outputFile"; then
1007 json add error
"errorMovingDataFile"
1009 if [ "$compressed_cache" -gt 0 ]; then
1010 output
2 'Creating compressed cache '
1011 json
set message
"$(get_text "statusProcessing
"): creating compressed cache"
1012 if cache
'create_gzip'; then
1016 json add error
"errorCreatingCompressedCache"
1021 output
2 'Removing temporary files '
1022 json
set message
"$(get_text "statusProcessing
"): removing temporary files"
1023 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
1024 if [ $j -eq 0 ]; then
1028 json add error
"errorRemovingTempFiles"
1034 local c hf string
="$1"
1035 local validation_result
="$3"
1036 load_environment
"$validation_result" 'quiet' ||
return 1
1037 if [ ! -s "$outputFile" ]; then
1038 output
"No block-list ('$outputFile') found.\\n"
1040 elif [ -z "$string" ]; then
1041 output
"Usage: /etc/init.d/${packageName} allow 'domain' ...\\n"
1043 elif [ -n "$dnsmasq_config_file_url" ]; then
1044 output
"Allowing individual domains is not possible when using external dnsmasq config file.\\n"
1048 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers
)
1049 output
1 "Allowing domain(s) and restarting dnsmasq "
1050 output
2 "Allowing domain(s) \\n"
1051 for c
in $string; do
1053 hf
="$(echo "$c" | sed 's/\./\\./g')"
1054 if sed -i "/(^|\.)${hf}$/d;" "$outputFile" && \
1055 uci_add_list_if_new
"${packageName}" 'config' 'allowed_domain' "$c"; then
1061 if [ "$compressed_cache" -gt 0 ]; then
1062 output
2 'Creating compressed cache '
1063 if cache
'create_gzip'; then
1069 output
2 "Committing changes to config "
1070 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1071 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1073 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1075 if [ "$dns" = 'dnsmasq.ipset' ]; then
1076 output
2 "Flushing adb ipset "
1077 if ipset
-q -! flush adb
; then output_ok
; else output_fail
; fi
1079 if [ "$dns" = 'dnsmasq.nftset' ]; then
1080 output
2 "Flushing adb nft sets "
1081 nft flush
set inet fw4 adb6
1082 if nft flush
set inet fw4 adb4
; then output_ok
; else output_fail
; fi
1084 output
2 "Restarting dnsmasq "
1085 if dnsmasq_restart
; then output_okn
; else output_failn
; fi
1091 output
1 "Allowing domain(s) and restarting Unbound "
1092 output
2 "Allowing domain(s) \\n"
1093 for c
in $string; do
1095 if sed -i "/${string}/d" "$outputFile" && \
1096 uci_add_list_if_new
"$packageName" 'config' 'allowed_domain' "$string"; then
1102 if [ "$compressed_cache" -gt 0 ]; then
1103 output
2 'Creating compressed cache '
1104 if cache
'create_gzip'; then
1110 output
2 "Committing changes to config "
1111 if [ -n "$(uci_changes "$packageName")" ] && uci_commit
"$packageName"; then
1112 allowed_domain
="$(uci_get "$packageName" 'config' 'allowed_domain')"
1114 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1116 output
2 "Restarting Unbound "
1117 if unbound_restart
; then output_okn
; else output_failn
; fi
1127 local validation_result
="$3"
1128 load_environment
"$validation_result" 'quiet' ||
return 1
1129 if [ ! -s "$outputFile" ]; then
1130 output
"No block-list ('$outputFile') found.\\n"
1132 elif [ -z "$param" ]; then
1133 output
"Usage: /etc/init.d/${packageName} check 'domain' ...\\n"
1136 for string
in ${param}; do
1137 c
="$(grep -c "$string" "$outputFile")"
1138 if [ "$c" -gt 0 ]; then
1139 if [ "$c" -eq 1 ]; then
1140 output
"Found 1 match for '$string' in '$outputFile'.\\n"
1142 output
"Found $c matches for '$string' in '$outputFile'.\\n"
1144 if [ "$c" -le 20 ]; then
1147 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1149 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1151 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1153 grep "$string" "$outputFile" |
sed 's|nftset=/||;s|/4#inet#adb#adb4||;';;
1155 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1157 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1161 output
"The '$string' is not found in current block-list ('$outputFile').\\n"
1166 adb_config_update
() {
1168 local param validation_result
="$3"
1170 on_boot
) param
="$1";;
1173 load_environment
"$validation_result" "$param" ||
return 1
1174 label
="${config_update_url##*//}"
1175 label
="${label%%/*}";
1176 [ "$config_update_enabled" -ne 0 ] ||
return 0
1178 if [ "$param" != 'download' ]; then
1179 cache
'test' && return 0
1180 cache
'test_gzip' && return 0
1182 output
1 'Updating config '
1183 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
1184 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
1186 if ! $dl_command "$config_update_url" "$dl_flag" "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
1187 output
1 "$_FAIL_\\n"
1188 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1189 json add error
"errorDownloadingConfigUpdate"
1191 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i "$packageConfigFile" 2>/dev
/null
; then
1193 output
2 "[DL] Config Update: $label $__OK__\\n"
1195 output
1 "$_FAIL_\\n"
1196 output
2 "[DL] Config Update: $label $__FAIL__\\n"
1197 json add error
"errorParsingConfigUpdate"
1206 local validation_result
="$3"
1207 load_environment
"$validation_result" 'quiet' ||
return 1
1210 for i
in ${blocked_adblockplus_url}; do
1211 [ "${i//melmac}" != "$i" ] && continue
1212 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1213 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1214 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1215 echo "# block-list too big for most routers"
1216 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1217 echo "# block-list may be too big for some routers"
1220 echo " list blocked_adblockplus_url '$i'"
1223 echo "# site was down on last check"
1224 echo "# list blocked_adblockplus_url '$i'"
1228 for i
in ${blocked_domains_url}; do
1229 [ "${i//melmac}" != "$i" ] && continue
1230 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1231 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1232 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1233 echo "# block-list too big for most routers"
1234 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1235 echo "# block-list may be too big for some routers"
1238 echo " list blocked_domains_url '$i'"
1241 echo "# site was down on last check"
1242 echo "# list blocked_domains_url '$i'"
1246 for i
in ${blocked_hosts_url}; do
1247 if $dl_command "$i" "$dl_flag" /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1248 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1249 if is_greater
"$(du -sk /tmp/sast)" "500"; then
1250 echo "# block-list too big for most routers"
1251 elif is_greater
"$(du -sk /tmp/sast)" "100"; then
1252 echo "# block-list may be too big for some routers"
1255 echo " list blocked_hosts_url '$i'"
1258 echo "# site was down on last check"
1259 echo "# list blocked_hosts_url '$i'"
1266 local action status error message stats c
1267 local param
="$1" validation_result
="$3"
1269 load_environment
"$validation_result" "$param" ||
return 1
1271 status
="$(json get status)"
1272 error
="$(json get error)"
1273 message
="$(json get message)"
1274 stats
="$(json get stats)"
1275 action
="$(json get triggers)"
1277 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
1278 if cache
'test_gzip' || cache
'test'; then
1283 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
1285 elif [ ! -s "$outputFile" ]; then
1286 if cache
'test_gzip' || cache
'test'; then
1291 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
1293 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
1303 if [ "$action" = 'restore' ]; then
1304 output
0 "Starting $serviceName... "
1305 output
3 "Starting $serviceName...\\n"
1306 json
set status
"statusStarting"
1307 if cache
'test_gzip' && ! cache
'test' && [ ! -s "$outputFile" ]; then
1308 output
3 'Found compressed cache file, unpacking it '
1309 json
set message
'found compressed cache file, unpacking it.'
1310 if cache
'unpack_gzip'; then
1314 json add error
"errorRestoreCompressedCache"
1315 output
"${_ERROR_}: $(get_text 'errorRestoreCompressedCache')!\\n"
1319 if cache
'test' && [ ! -s "$outputFile" ]; then
1320 output
3 'Found cache file, reusing it '
1321 json
set message
'found cache file, reusing it.'
1322 if cache
'restore'; then
1327 json add error
"errorRestoreCache"
1328 output
"${_ERROR_}: $(get_text 'errorRestoreCache')!\\n"
1333 if [ "$action" = 'download' ]; then
1334 if [ -s "$outputFile" ] || cache
'test' || cache
'test_gzip'; then
1335 output
0 "Force-reloading $serviceName... "
1336 output
3 "Force-reloading $serviceName...\\n"
1337 json
set status
"statusForceReloading"
1339 output
0 "Starting $serviceName... "
1340 output
3 "Starting $serviceName...\\n"
1341 json
set status
"statusStarting"
1343 if [ -n "$dnsmasq_config_file_url" ]; then
1344 download_dnsmasq_file
1350 if [ "$action" = 'restart' ]; then
1351 output
0 "Restarting $serviceName... "
1352 output
3 "Restarting $serviceName...\\n"
1353 json
set status
"statusRestarting"
1356 if [ "$action" = 'start' ]; then
1357 output
0 "Starting $serviceName... "
1358 output
3 "Starting $serviceName...\\n"
1359 json
set status
"statusStarting"
1362 if [ -s "$outputFile" ] && [ "$(json get status)" != "statusFail" ]; then
1363 output
0 "$__OK__\\n";
1365 json
set status
"statusSuccess"
1366 json
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1369 output
0 "$__FAIL__\\n";
1370 json
set status
"statusFail"
1371 json add error
"errorOhSnap"
1375 procd_open_instance
'main'
1376 procd_set_param
command /bin
/true
1377 procd_set_param stdout
1
1378 procd_set_param stderr
1
1380 json_add_string
'status' "$(json get status)"
1381 json_add_string
'errors' "$(json get errors)"
1382 if [ -s "$outputFile" ]; then
1383 json_add_int
'entries' "$(wc -l < "$outputFile")"
1385 json_add_int
'entries' '0'
1387 json_add_array firewall
1388 if [ "$force_dns" -ne 0 ]; then
1389 for c
in $force_dns_port; do
1390 if netstat
-tuln |
grep LISTEN |
grep ":${c}" >/dev
/null
2>&1; then
1392 json_add_string
type redirect
1393 json_add_string target DNAT
1394 json_add_string src lan
1395 json_add_string proto
"tcp udp"
1396 json_add_string src_dport
"$c"
1397 json_add_string dest_port
"$c"
1398 json_add_string family any
1399 json_add_boolean reflection
0
1403 json_add_string
type rule
1404 json_add_string src lan
1405 json_add_string dest
"*"
1406 json_add_string proto
"tcp udp"
1407 json_add_string dest_port
"$c"
1408 json_add_string target REJECT
1416 json_add_string
type ipset
1417 json_add_string name adb
1418 json_add_string match dest_net
1419 json_add_string storage
hash
1422 json_add_string
type rule
1423 json_add_string ipset adb
1424 json_add_string src lan
1425 json_add_string dest
"*"
1426 json_add_string proto
"tcp udp"
1427 json_add_string target REJECT
1432 json_add_string
type ipset
1433 json_add_string name adb4
1434 json_add_string family
4
1435 json_add_string match dest_net
1438 json_add_string
type rule
1439 json_add_string ipset adb4
1440 json_add_string src lan
1441 json_add_string dest
"*"
1442 json_add_string proto
"tcp udp"
1443 json_add_string target REJECT
1445 if [ "$ipv6_enabled" -ne 0 ]; then
1447 json_add_string
type ipset
1448 json_add_string name adb6
1449 json_add_string family
6
1450 json_add_string match dest_net
1453 json_add_string
type rule
1454 json_add_string ipset adb6
1455 json_add_string src lan
1456 json_add_string dest
"*"
1457 json_add_string proto
"tcp udp"
1458 json_add_string target REJECT
1465 procd_close_instance
1469 local c url status message error stats
1470 local validation_result
="$3"
1471 load_environment
"$validation_result" 'quiet' ||
return 1
1472 status
="$(json get status)"
1473 message
="$(json get message)"
1474 error
="$(json get error)"
1475 stats
="$(json get stats)"
1476 if [ "$status" = "statusSuccess" ]; then
1477 output
"$stats "; output_okn
;
1479 [ -n "$status" ] && status
="$(get_text "$status")"
1480 if [ -n "$status" ] && [ -n "$message" ]; then
1481 status
="${status}: $message"
1483 [ -n "$status" ] && output
"$serviceName $status\\n"
1485 if [ -n "$error" ]; then
1490 errorDownloadingList|errorParsingList
)
1491 output
"${_ERROR_}: $(get_text "$c") $url!\\n";;
1493 output
"${_ERROR_}: $(get_text "$c")!\\n";;
1501 local validation_result
="$3"
1502 load_environment
"$validation_result" 'quiet' ||
return 1
1503 if [ -s "$outputFile" ]; then
1504 output
"Stopping $serviceName... "
1506 if dns
'on_stop'; then
1507 ipset
-q -! flush adb
1508 ipset
-q -! destroy adb
1509 nft delete
set inet fw4 adb4
1510 nft delete
set inet fw4 adb6
1512 output
0 "$__OK__\\n"; output_okn
;
1513 json
set status
"statusStopped"
1516 output
0 "$__FAIL__\\n"; output_fail
;
1517 json
set status
"statusFail"
1518 json add error
"errorStopping"
1519 output
"${_ERROR_}: $(get_text 'errorStopping')!\\n"
1524 allow
() { load_validate_config
'config' adb_allow
"'$*'"; }
1526 ubus
-t 30 wait_for network.interface
2>/dev
/null
1527 rc_procd start_service
'on_boot'
1529 check
() { load_validate_config
'config' adb_check
"'$*'"; }
1530 dl
() { rc_procd start_service
'download'; }
1532 local compressed_cache_dir
1533 config_load
"$packageName"
1534 config_get compressed_cache_dir
'config' 'compressed_cache_dir' '/etc'
1535 if [ "$(sanitize_dir "$compressed_cache_dir")" = '/' ]; then
1536 compressed_cache_dir
=''
1537 elif [ -n "$(sanitize_dir "$compressed_cache_dir")" ]; then
1538 compressed_cache_dir
="$(sanitize_dir "$compressed_cache_dir")"
1540 compressed_cache_dir
="/etc"
1542 rm -f "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
1543 rm -f "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
1544 rm -f "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
1545 rm -f "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
1546 rm -f "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
1547 rm -f "$unboundCache" "$unboundGzip"
1549 config_foreach resolver
'dnsmasq' 'cleanup'
1553 reload_service
() { rc_procd start_service
'restart'; }
1554 restart_service
() { rc_procd start_service
'restart'; }
1555 service_started
() { procd_set_config_changed firewall
; }
1556 service_stopped
() { procd_set_config_changed firewall
; }
1557 service_triggers
() {
1559 local procd_trigger_wan6
1560 config_load
"$packageName"
1561 config_get_bool procd_trigger_wan6
'config' 'procd_trigger_wan6' '0'
1562 .
/lib
/functions
/network.sh
1564 network_find_wan wan
1566 if [ "$procd_trigger_wan6" -ne 0 ]; then
1567 network_find_wan6 wan6
1568 wan6
="${wan6:-wan6}"
1570 for i
in "$wan" "$wan6"; do
1571 [ -n "$i" ] && procd_add_interface_trigger
"interface.*" "$i" "/etc/init.d/${packageName}" start
1573 procd_add_config_trigger
"config.change" "$packageName" "/etc/init.d/${packageName}" reload
1575 sizes
() { load_validate_config
'config' adb_sizes
"''"; }
1577 load_validate_config
'config' adb_config_update
"'$*'"
1578 load_validate_config
'config' adb_start
"'$*'"
1580 status_service
() { load_validate_config
'config' adb_status
"''"; }
1581 stop_service
() { load_validate_config
'config' adb_stop
"'$*'"; }
1582 version
() { echo "$PKG_VERSION"; }
1584 load_validate_config
() {
1585 .
/lib
/functions
/network.sh
1586 .
/usr
/share
/libubox
/jshn.sh
1589 local force_dns_port
1590 local parallel_downloads
1592 local compressed_cache
1593 local compressed_cache_dir
1595 local allow_non_ascii
1596 local canary_domains_icloud
1597 local canary_domains_mozilla
1598 local config_update_enabled
1599 local config_update_url
1600 local download_timeout
1601 local curl_additional_param
1602 local curl_max_file_size
1605 local procd_trigger_wan6
1606 local procd_boot_wan_timeout
1610 local allowed_domain
1611 local allowed_domains_url
1612 local blocked_adblockplus_url
1613 local blocked_domain
1614 local blocked_domains_url
1615 local blocked_hosts_url
1616 local dnsmasq_config_file_url
1617 uci_load_validate
"$packageName" "$packageName" "$1" "${2}${3:+ $3}" \
1619 'force_dns:bool:1' \
1620 'force_dns_port:list(integer):53 853' \
1621 'parallel_downloads:bool:1' \
1623 'compressed_cache:bool:0' \
1624 'compressed_cache_dir:directory:/etc' \
1625 'ipv6_enabled:bool:0' \
1626 'allow_non_ascii:bool:0' \
1627 'canary_domains_icloud:bool:0' \
1628 'canary_domains_mozilla:bool:0' \
1629 'config_update_enabled:bool:0' \
1630 'config_update_url:string:https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update' \
1631 'download_timeout:range(1,60):20' \
1632 'curl_additional_param:or("", string)' \
1633 'curl_max_file_size:or("", uinteger)' \
1634 'curl_retry:range(0,30):3' \
1635 'verbosity:range(0,2):2' \
1636 'procd_trigger_wan6:bool:0' \
1637 'procd_boot_wan_timeout:integer:60' \
1638 'led:or("", "none", file, device, string)' \
1639 'dns:or("dnsmasq.addnhosts", "dnsmasq.conf", "dnsmasq.ipset", "dnsmasq.nftset", "dnsmasq.servers", "unbound.adb_list"):dnsmasq.servers' \
1640 'dns_instance:or(list(integer, string)):0' \
1641 'allowed_domain:list(string)' \
1642 'allowed_domains_url:list(string)' \
1643 'blocked_domain:list(string)' \
1644 'blocked_adblockplus_url:list(string)' \
1645 'blocked_domains_url:list(string)' \
1646 'blocked_hosts_url:list(string)' \
1647 'dnsmasq_config_file_url:string'