1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039,SC1091,SC2016,SC3043,SC3057,SC3060
6 # shellcheck disable=SC2034
8 # shellcheck disable=SC2034
12 if type extra_command
1>/dev
/null
2>&1; then
13 extra_command
'check' 'Checks if specified domain is found in current block-list'
14 extra_command
'dl' 'Force-downloads all enabled block-list'
15 extra_command
'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
='check dl killcache sizes status_service version'
21 # shellcheck disable=SC2034
22 EXTRA_HELP
=' check Checks if specified domain is found in current block-list
23 dl Force-downloads all enabled block-list
24 sizes Displays the file-sizes of enabled block-lists'
27 readonly packageName
='simple-adblock'
28 readonly serviceName
="$packageName $PKG_VERSION"
29 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
30 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
31 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
32 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
33 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
34 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
35 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
36 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
37 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
38 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
39 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
40 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
41 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
42 readonly serversFile
="/var/run/${packageName}.servers"
43 readonly serversCache
="/var/run/${packageName}.servers.cache"
44 readonly serversGzip
="/etc/${packageName}.servers.gz"
45 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
46 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
47 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
48 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
49 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
50 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
51 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
52 readonly jsonFile
="/var/run/${packageName}.json"
53 readonly sharedMemoryError
="/dev/shm/$packageName-error"
54 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
55 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;'
56 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
57 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
58 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
59 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
60 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
61 readonly _ERROR_
='\033[0;31mERROR\033[0m'
62 readonly _WARNING_
='\033[0;33mWARNING\033[0m'
64 version
() { echo "$PKG_VERSION"; }
69 statusNoInstall
) _ret
="$serviceName is not installed or not found";;
70 statusStopped
) _ret
="Stopped";;
71 statusStarting
) _ret
="Starting";;
72 statusRestarting
) _ret
="Restarting";;
73 statusForceReloading
) _ret
="Force Reloading";;
74 statusDownloading
) _ret
="Downloading";;
75 statusProcessing
) _ret
="Processing";;
76 statusError
) _ret
="Error";;
77 statusWarning
) _ret
="Warning";;
78 statusFail
) _ret
="Fail";;
79 statusSuccess
) _ret
="Success";;
86 errorOutputFileCreate
) _ret
="failed to create $outputFile file";;
87 errorFailDNSReload
) _ret
="failed to restart/reload DNS resolver";;
88 errorSharedMemory
) _ret
="failed to access shared memory";;
89 errorSorting
) _ret
="failed to sort data file";;
90 errorOptimization
) _ret
="failed to optimize data file";;
91 errorAllowListProcessing
) _ret
="failed to process allow-list";;
92 errorDataFileFormatting
) _ret
="failed to format data file";;
93 errorMovingDataFile
) _ret
="failed to move data file '${A_TMP}' to '${outputFile}'";;
94 errorCreatingCompressedCache
) _ret
="failed to create compressed cache";;
95 errorRemovingTempFiles
) _ret
="failed to remove temporary files";;
96 errorRestoreCompressedCache
) _ret
="failed to unpack compressed cache";;
97 errorRestoreCache
) _ret
="failed to move '$outputCache' to '$outputFile'";;
98 errorOhSnap
) _ret
="failed to create block-list or restart DNS resolver";;
99 errorStopping
) _ret
="failed to stop $serviceName";;
100 errorDNSReload
) _ret
="failed to reload/restart DNS resolver";;
101 errorDownloadingConfigUpdate
) _ret
="failed to download Config Update file";;
102 errorDownloadingList
) _ret
="failed to download";;
103 errorParsingList
) _ret
="failed to parse";;
104 errorParsingConfigUpdate
) _ret
="failed to parse Config Update file";;
109 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
110 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
111 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
112 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
113 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
114 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
115 compare_values
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
116 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
117 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
118 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
119 dnsmasq_hup
() { killall
-q -HUP dnsmasq
; }
120 dnsmasq_kill
() { killall
-q -KILL dnsmasq
; }
121 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
122 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
123 is_force_dns_active
() { iptables-save |
grep -q -w -- '--dport 53'; }
124 is_present
() { command -v "$1" >/dev
/null
2>&1; }
127 # Can take a single parameter (text) to be output at any verbosity
128 # Or target verbosity level and text to be output at specifc verbosity
129 local msg memmsg logmsg
130 if [ $# -ne 1 ]; then
131 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
133 [ -t 1 ] && printf "%b" "$1"
134 msg
="${1//$serviceName /service }";
135 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
136 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
137 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
138 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
139 rm -f "$sharedMemoryOutput"
141 printf "%b" "$msg" >> "$sharedMemoryOutput"
145 serviceEnabled
=''; forceDNS
=''; forceDNSPorts
=''; parallelDL
=''; debug
='';
146 compressedCache
=''; ipv6Enabled
=''; configUpdateEnabled
=''; configUpdateURL
='';
147 bootDelay
=''; dlTimeout
=''; curlRetry
=''; verbosity
=''; led
='';
148 targetDNS
=''; dnsInstance
='';
149 allowed_domains
=''; allowed_domains_urls
='';
150 blocked_domains
=''; blocked_domains_urls
=''; blocked_hosts_urls
='';
151 dl_command
=''; dl_flag
=''; isSSLSupported
=''; allowIDN
='';
152 outputFilter
=''; outputFilterIPv6
=''; outputFile
=''; outputGzip
=''; outputCache
='';
155 load_package_config
() {
156 config_load
"$packageName"
157 config_get_bool serviceEnabled
'config' 'enabled' 1
158 config_get_bool forceDNS
'config' 'force_dns' 1
159 config_get_bool parallelDL
'config' 'parallel_downloads' 1
160 config_get_bool debug
'config' 'debug' 0
161 config_get_bool compressedCache
'config' 'compressed_cache' 0
162 config_get_bool ipv6Enabled
'config' 'ipv6_enabled' 0
163 config_get_bool configUpdateEnabled
'config' 'config_update_enabled' 0
164 config_get forceDNSPorts
'config' 'force_dns_port' '53 853'
165 config_get bootDelay
'config' 'boot_delay' '120'
166 config_get dlTimeout
'config' 'download_timeout' '20'
167 config_get curlRetry
'config' 'curl_retry' '3'
168 config_get verbosity
'config' 'verbosity' '2'
169 config_get led
'config' 'led'
170 config_get targetDNS
'config' 'dns' 'dnsmasq.servers'
171 config_get dnsInstance
'config' 'dns_instance' '0'
172 config_get allowed_domains
'config' 'allowed_domain'
173 config_get allowed_domains_urls
'config' 'allowed_domains_url'
174 config_get blocked_domains
'config' 'blocked_domain'
175 config_get blocked_domains_urls
'config' 'blocked_domains_url'
176 config_get blocked_hosts_urls
'config' 'blocked_hosts_url'
177 config_get configUpdateURL
'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
179 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
180 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
181 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
182 targetDNS
='dnsmasq.servers'
187 outputFilter
="$addnhostsOutputFilter"
188 outputFile
="$addnhostsFile"
189 outputCache
="$addnhostsCache"
190 outputGzip
="$addnhostsGzip"
191 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6
="$addnhostsOutputFilterIPv6"
192 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
193 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
194 rm -f "$serversFile" "$serversCache" "$serversGzip"
195 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
198 outputFilter
="$dnsmasqOutputFilter"
199 outputFile
="$dnsmasqFile"
200 outputCache
="$dnsmasqCache"
201 outputGzip
="$dnsmasqGzip"
202 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
203 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
204 rm -f "$serversFile" "$serversCache" "$serversGzip"
205 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
208 outputFilter
="$ipsetOutputFilter"
209 outputFile
="$ipsetFile"
210 outputCache
="$ipsetCache"
211 outputGzip
="$ipsetGzip"
212 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
213 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
214 rm -f "$serversFile" "$serversCache" "$serversGzip"
215 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
218 outputFilter
="$serversOutputFilter"
219 outputFile
="$serversFile"
220 outputCache
="$serversCache"
221 outputGzip
="$serversGzip"
222 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
223 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
224 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
225 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
228 outputFilter
="$unboundOutputFilter"
229 outputFile
="$unboundFile"
230 outputCache
="$unboundCache"
231 outputGzip
="$unboundGzip"
232 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
233 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
234 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
235 rm -f "$serversFile" "$serversCache" "$serversGzip"
238 if [ -z "${verbosity##*[!0-9]*}" ] ||
[ "$verbosity" -lt 0 ] ||
[ "$verbosity" -gt 2 ]; then
241 .
/lib
/functions
/network.sh
242 .
/usr
/share
/libubox
/jshn.sh
243 is_present
'gawk' && awk='gawk'
244 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
245 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
246 local s
="opkg update; opkg --force-overwrite install"
247 is_present
'gawk' || s
="$s gawk"
248 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
249 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
250 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
251 output
"$_WARNING_: Some recommended packages are missing, install them by running:\\n"
254 # Prefer curl because it supports the file:// scheme.
255 if is_present
'curl'; then
256 dl_command
="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
258 elif is_present
'/usr/libexec/wget-ssl'; then
259 dl_command
="/usr/libexec/wget-ssl --no-check-certificate --timeout $dlTimeout -q"
261 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
262 dl_command
="wget --no-check-certificate --timeout $dlTimeout -q"
265 dl_command
="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
268 led
="${led:+/sys/class/leds/$led}"
269 if curl
--version 2>/dev
/null |
grep -q "https" \
270 || wget
--version 2>/dev
/null |
grep -q "+https" \
271 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
272 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
273 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
281 wan_if
=''; wan_gw
='';
284 if [ "$debug" -ne 0 ]; then
285 exec 1>>/tmp
/simple-adblock.log
290 if [ "$serviceEnabled" -eq 0 ]; then
293 output
"$packageName is currently disabled.\\n"
294 output
"Run the following commands before starting service again:\\n"
295 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
302 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
303 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
315 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
316 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
317 targetDNS
='dnsmasq.servers'
319 if ! ipset
help hash:net
>/dev
/null
2>&1; then
320 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
321 targetDNS
='dnsmasq.servers'
326 [ ! -d "${outputFile%/*}" ] && mkdir
-p "${outputFile%/*}"
327 [ ! -d "${outputCache%/*}" ] && mkdir
-p "${outputFile%/*}"
328 [ ! -d "${outputGzip%/*}" ] && mkdir
-p "${outputFile%/*}"
329 cacheOps
'testGzip' && return 0
330 network_flush_cache
; network_find_wan wan_if
; network_get_gateway wan_gw
"$wan_if";
331 [ -n "$wan_gw" ] && return 0
332 output
"$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
336 local cfg
="$1" param
="$2"
339 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
340 uci
-q del dhcp.
"$cfg".serversfile
342 if ! uci
-q get dhcp.
"$cfg".addnhosts |
grep -q "$addnhostsFile"; then
343 uci add_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
346 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup
)
347 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
348 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
349 uci
-q del dhcp.
"$cfg".serversfile
353 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
354 if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
355 uci
set dhcp.
"$cfg".serversfile
="$serversFile"
362 local param output_text i
365 if [ ! -s "$outputFile" ]; then
366 jsonOps
set status
"statusFail"
367 jsonOps add error
"errorOutputFileCreate"
368 output
"$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
373 if [ "$dnsInstance" = "*" ]; then
374 config_foreach dnsmasqOps
'dnsmasq' "$targetDNS"
375 elif [ -n "$dnsInstance" ]; then
376 for i
in $dnsInstance; do
377 dnsmasqOps
"@dnsmasq[$i]" "$targetDNS"
382 dnsmasq.addnhosts|dnsmasq.servers
)
384 output_text
='Reloading DNSMASQ'
386 dnsmasq.conf|dnsmasq.ipset
)
387 param
=dnsmasq_restart
388 output_text
='Restarting DNSMASQ'
391 param
=unbound_restart
392 output_text
='Restarting Unbound'
396 if [ -n "$(uci changes dhcp)" ]; then
398 if [ "$param" = 'unbound_restart' ]; then
399 param
='dnsmasq_restart; unbound_restart;'
400 output_text
='Restarting Unbound/DNSMASQ'
402 param
=dnsmasq_restart
403 output_text
='Restarting DNSMASQ'
406 output
1 "$output_text "
407 output
2 "$output_text "
408 jsonOps
set message
"$output_text"
409 if eval "$param"; then
410 jsonOps
set status
"statusSuccess"
415 jsonOps
set status
"statusFail"
416 jsonOps add error
"errorDNSReload"
417 output
"$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
423 dnsmasq.addnhosts | dnsmasq.servers
)
426 dnsmasq.conf | dnsmasq.ipset
)
427 param
=dnsmasq_restart
430 param
=unbound_restart
433 if [ -n "$(uci changes dhcp)" ]; then
435 if [ "$param" = 'unbound_restart' ]; then
436 param
='dnsmasq_restart; unbound_restart;'
438 param
=dnsmasq_restart
446 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
447 param
=dnsmasq_restart
450 param
=unbound_restart
460 # shellcheck disable=SC2034
461 local action
="$1" param
="$2" value
="$3"
462 local status message error stats
463 local reload restart curReload curRestart ret i
464 if [ -s "$jsonFile" ]; then
465 json_load_file
"$jsonFile" 2>/dev
/null
466 json_select
'data' 2>/dev
/null
467 for i
in status message error stats reload restart
; do
468 json_get_var
$i "$i" 2>/dev
/null
475 curReload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
476 curRestart
="$compressedCache $forceDNS $led $forceDNSPorts"
477 if [ ! -s "$jsonFile" ]; then
479 elif [ "$curReload" != "$reload" ]; then
481 elif [ "$curRestart" != "$restart" ]; then
487 printf "%b" "$(eval echo "\$
$param")"; return;;
491 if [ -n "$(eval echo "\$
$param")" ]; then
492 value
="$(eval echo "\$
$param") ${value}"
494 eval "$param"='${value}'
499 unset status message error stats
;;
501 unset reload restart
;;
509 reload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
510 restart
="$compressedCache $forceDNS $led $forceDNSPorts"
513 eval "$param"='$value';;
518 json_add_object
'data'
519 json_add_string version
"$PKG_VERSION"
520 json_add_string status
"$status"
521 json_add_string message
"$message"
522 json_add_string error
"$error"
523 json_add_string stats
"$stats"
524 json_add_string reload
"$reload"
525 json_add_string restart
"$restart"
527 json_dump
> "$jsonFile"
535 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
539 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
543 [ -s "$outputCache" ]
547 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
551 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
552 if gzip < "$outputFile" > "$R_TMP"; then
553 if mv "$R_TMP" "$outputGzip"; then
564 expand|unpack|expandGzip|unpackGzip
)
565 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
572 local label
type D_TMP R_TMP
573 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
574 label
="${1##*//}"; label
="${label%%/*}";
575 if [ "$2" = 'hosts' ]; then
576 label
="Hosts: $label"; filter
="$hostsFilter";
578 label
="Domains: $label"; filter
="$domainsFilter";
580 if [ "$3" = 'blocked' ]; then
581 type='Blocked'; D_TMP
="$B_TMP";
583 type='Allowed'; D_TMP
="$A_TMP";
585 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
587 output
2 "[DL] $type $label $__FAIL__\\n"
588 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
591 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
592 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
594 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
596 output
2 "[DL] $type $label $__FAIL__\\n"
597 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
599 sed -i "$filter" "$R_TMP"
600 if [ ! -s "$R_TMP" ]; then
602 output
2 "[DL] $type $label $__FAIL__\\n"
603 echo "errorParsingList|${1}" >> "$sharedMemoryError"
605 cat "${R_TMP}" >> "$D_TMP"
607 output
2 "[DL] $type $label $__OK__\\n"
614 process_config_update
() {
616 [ "$configUpdateEnabled" -eq 0 ] && return 0
617 label
="${1##*//}"; label
="${label%%/*}";
618 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
619 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
621 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
623 output
2 "[DL] Config Update: $label $__FAIL__\\n"
624 jsonOps add error
"errorDownloadingConfigUpdate"
626 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc
/config
/simple-adblock
2>/dev
/null
; then
628 output
2 "[DL] Config Update: $label $__OK__\\n"
631 output
2 "[DL] Config Update: $label $__FAIL__\\n"
632 jsonOps add error
"errorParsingConfigUpdate"
640 local hf w_filter j
=0 R_TMP
642 jsonOps
set message
"$(getStatusText "statusDownloading
")..."
643 jsonOps
set status
"statusDownloading"
645 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
646 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
647 output
3 'Low free memory, restarting resolver... '
648 if dnsOps
'quiet'; then
654 touch $A_TMP; touch $B_TMP;
655 output
1 'Downloading lists '
656 process_config_update
"$configUpdateURL"
657 rm -f "$sharedMemoryError"
658 if [ -n "$blocked_hosts_urls" ]; then
659 for hf
in ${blocked_hosts_urls}; do
660 if [ "$parallelDL" -gt 0 ]; then
661 process_url
"$hf" 'hosts' 'blocked' &
663 process_url
"$hf" 'hosts' 'blocked'
667 if [ -n "$blocked_domains_urls" ]; then
668 for hf
in ${blocked_domains_urls}; do
669 if [ "$parallelDL" -gt 0 ]; then
670 process_url
"$hf" 'domains' 'blocked' &
672 process_url
"$hf" 'domains' 'blocked'
676 if [ -n "$allowed_domains_urls" ]; then
677 for hf
in ${allowed_domains_urls}; do
678 if [ "$parallelDL" -gt 0 ]; then
679 process_url
"$hf" 'domains' 'allowed' &
681 process_url
"$hf" 'domains' 'allowed'
687 if [ -s "$sharedMemoryError" ]; then
688 while IFS
= read -r line
; do
689 jsonOps add error
"$line"
690 done < "$sharedMemoryError"
691 rm -f "$sharedMemoryError"
694 [ -n "$blocked_domains" ] && for hf
in ${blocked_domains}; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
695 allowed_domains
="${allowed_domains}
697 [ -n "$allowed_domains" ] && for hf
in ${allowed_domains}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; w_filter
="$w_filter/${hf}$/d;"; done
699 [ ! -s "$B_TMP" ] && return 1
701 output
1 'Processing downloads '
702 output
2 'Sorting combined list '
703 jsonOps
set message
"$(getStatusText "statusProcessing
"): sorting combined list"
704 if [ "$allowIDN" -gt 0 ]; then
705 if sort -u "$B_TMP" > "$A_TMP"; then
709 jsonOps add error
"errorSorting"
712 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
716 jsonOps add error
"errorSorting"
720 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
721 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
722 [ "$targetDNS" = 'dnsmasq.servers' ] || \
723 [ "$targetDNS" = 'unbound.adb_list' ]; then
724 # TLD optimization written by Dirk Brenken (dev@brenken.org)
725 output
2 'Optimizing combined list '
726 jsonOps
set message
"$(getStatusText "statusProcessing
"): optimizing combined list"
727 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
728 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
729 if sort "$B_TMP" > "$A_TMP"; then
730 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
731 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
732 if sort -u "$A_TMP" > "$B_TMP"; then
736 jsonOps add error
"errorOptimization"
741 jsonOps add error
"errorOptimization"
745 jsonOps add error
"errorOptimization"
750 jsonOps add error
"errorOptimization"
754 jsonOps add error
"errorOptimization"
761 output
2 'Allowing domains '
762 jsonOps
set message
"$(getStatusText "statusProcessing
"): allowing domains"
763 if sed -i "$w_filter" "$B_TMP"; then
767 jsonOps add error
"errorAllowListProcessing"
770 output
2 'Formatting merged file '
771 jsonOps
set message
"$(getStatusText "statusProcessing
"): formatting merged file"
772 if [ -z "$outputFilterIPv6" ]; then
773 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
777 jsonOps add error
"errorDataFileFormatting"
782 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
783 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
787 jsonOps add error
"errorDataFileFormatting"
795 output
2 'Creating DNSMASQ addnhosts file '
796 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ addnhosts file"
799 output
2 'Creating DNSMASQ config file '
800 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ config file"
803 output
2 'Creating DNSMASQ ipset file '
804 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ ipset file"
807 output
2 'Creating DNSMASQ servers file '
808 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ servers file"
811 output
2 'Creating Unbound adb_list file '
812 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating Unbound adb_list file"
815 if mv "$A_TMP" "$outputFile"; then
819 jsonOps add error
"errorMovingDataFile"
821 if [ "$compressedCache" -gt 0 ]; then
822 output
2 'Creating compressed cache '
823 jsonOps
set message
"$(getStatusText "statusProcessing
"): creating compressed cache"
824 if cacheOps
'createGzip'; then
828 jsonOps add error
"errorCreatingCompressedCache"
833 output
2 'Removing temporary files '
834 jsonOps
set message
"$(getStatusText "statusProcessing
"): removing temporary files"
835 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
836 if [ $j -eq 0 ]; then
840 jsonOps add error
"errorRemovingTempFiles"
848 rc_procd start_service
'on_boot' && rc_procd service_triggers
852 is_enabled
'on_start' ||
return 1
853 local action status error message stats c
855 status
="$(jsonOps get status)"
856 error
="$(jsonOps get error)"
857 message
="$(jsonOps get message)"
858 stats
="$(jsonOps get stats)"
859 action
="$(jsonOps get triggers)"
861 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
862 if cacheOps
'testGzip' || cacheOps
'test'; then
867 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
869 elif [ ! -s "$outputFile" ]; then
870 if cacheOps
'testGzip' || cacheOps
'test'; then
875 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
877 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
887 procd_open_instance
'main'
888 procd_set_param
command /bin
/true
889 procd_set_param stdout
1
890 procd_set_param stderr
1
892 json_add_array firewall
893 if [ "$forceDNS" -ne 0 ]; then
894 for c
in $forceDNSPorts; do
895 if netstat
-tuln |
grep LISTEN |
grep ":${c}" >/dev
/null
2>&1; then
897 json_add_string
type redirect
898 json_add_string target DNAT
899 json_add_string src lan
900 json_add_string proto
"tcp udp"
901 json_add_string src_dport
"$c"
902 json_add_string dest_port
"$c"
903 json_add_boolean reflection
0
907 json_add_string
type rule
908 json_add_string src lan
909 json_add_string dest
"*"
910 json_add_string proto
"tcp udp"
911 json_add_string dest_port
"$c"
912 json_add_string target REJECT
917 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
919 json_add_string
type ipset
920 json_add_string name adb
921 json_add_string match dest_net
922 json_add_string storage
hash
925 json_add_string
type rule
926 json_add_string ipset adb
927 json_add_string src lan
928 json_add_string dest
"*"
929 json_add_string proto
"tcp udp"
930 json_add_string target REJECT
937 if [ "$action" = 'restore' ]; then
938 output
0 "Starting $serviceName... "
939 output
3 "Starting $serviceName...\\n"
940 jsonOps
set status
"statusStarting"
941 if cacheOps
'testGzip' && ! cacheOps
'test' && [ ! -s "$outputFile" ]; then
942 output
3 'Found compressed cache file, unpacking it '
943 jsonOps
set message
'found compressed cache file, unpacking it.'
944 if cacheOps
'unpackGzip'; then
948 jsonOps add error
"errorRestoreCompressedCache"
949 output
"$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
953 if cacheOps
'test' && [ ! -s "$outputFile" ]; then
954 output
3 'Found cache file, reusing it '
955 jsonOps
set message
'found cache file, reusing it.'
956 if cacheOps
'restore'; then
961 jsonOps add error
"errorRestoreCache"
962 output
"$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
967 if [ "$action" = 'download' ]; then
968 if [ -s "$outputFile" ] || cacheOps
'test' || cacheOps
'testGzip'; then
969 output
0 "Force-reloading $serviceName... "
970 output
3 "Force-reloading $serviceName...\\n"
971 jsonOps
set status
"statusForceReloading"
973 output
0 "Starting $serviceName... "
974 output
3 "Starting $serviceName...\\n"
975 jsonOps
set status
"statusStarting"
980 if [ "$action" = 'restart' ]; then
981 output
0 "Restarting $serviceName... "
982 output
3 "Restarting $serviceName...\\n"
983 jsonOps
set status
"statusRestarting"
986 if [ "$action" = 'start' ]; then
987 output
0 "Starting $serviceName... "
988 output
3 "Starting $serviceName...\\n"
989 jsonOps
set status
"statusStarting"
992 if [ -s "$outputFile" ] && [ "$(jsonOps get status)" != "statusFail" ]; then
993 output
0 "$__OK__\\n";
995 jsonOps
set status
"statusSuccess"
996 jsonOps
set stats
"$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
999 output
0 "$__FAIL__\\n";
1000 jsonOps
set status
"statusFail"
1001 jsonOps add error
"errorOhSnap"
1006 service_started
() { procd_set_config_changed firewall
; }
1007 service_stopped
() { procd_set_config_changed firewall
; }
1009 restart_service
() { rc_procd start_service
'restart'; }
1010 reload_service
() { rc_procd start_service
'restart'; }
1011 dl
() { rc_procd start_service
'download'; }
1014 rm -f "$addnhostsCache" "$addnhostsGzip"
1015 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1016 rm -f "$ipsetCache" "$ipsetGzip"
1017 rm -f "$serversCache" "$serversGzip"
1018 rm -f "$unboundCache" "$unboundGzip"
1020 config_foreach dnsmasqOps
'dnsmasq' 'cleanup'
1021 uci
-q commit
'dhcp'
1026 local c url status message error stats
1027 config_load
"$packageName"
1028 config_get verbosity
'config' 'verbosity' '2'
1029 status
="$(jsonOps get status)"
1030 message
="$(jsonOps get message)"
1031 error
="$(jsonOps get error)"
1032 stats
="$(jsonOps get stats)"
1033 if [ "$status" = "statusSuccess" ]; then
1034 output
"$stats "; output_okn
;
1036 [ -n "$status" ] && status
="$(getStatusText "$status")"
1037 if [ -n "$status" ] && [ -n "$message" ]; then
1038 status
="${status}: $message"
1040 [ -n "$status" ] && output
"$serviceName $status\\n"
1042 if [ -n "$error" ]; then
1047 errorDownloadingList|errorParsingList
)
1048 output
"$_ERROR_: $(getErrorText "$c") $url!\\n";;
1050 output
"$_ERROR_: $(getErrorText "$c")!\\n";;
1059 if [ -s "$outputFile" ]; then
1060 output
"Stopping $serviceName... "
1062 if dnsOps
'on_stop'; then
1064 output
0 "$__OK__\\n"; output_okn
;
1065 jsonOps
set status
"statusStopped"
1068 output
0 "$__FAIL__\\n"; output_fail
;
1069 jsonOps
set status
"statusFail"
1070 jsonOps add error
"errorStopping"
1071 output
"$_ERROR_: $(getErrorText 'errorStopping')!\\n"
1076 service_triggers
() {
1078 procd_add_config_trigger
'config.change' "${packageName}" /etc
/init.d
/${packageName} reload
1079 procd_add_reload_interface_trigger
'wan'
1080 procd_add_interface_trigger
'interface.*.up' 'wan' /etc
/init.d
/${packageName} reload
1087 c
="$(grep -c "$string" "$outputFile")"
1088 if [ ! -s "$outputFile" ]; then
1089 echo "No block-list ('$outputFile') found."
1090 elif [ -z "$string" ]; then
1091 echo "Usage: /etc/init.d/${packageName} check string"
1092 elif [ "$c" -gt 0 ]; then
1093 if [ "$c" -gt 1 ]; then
1094 echo "Found $c matches for '$string' in '$outputFile':"
1096 echo "Found 1 match for '$string' in '$outputFile':"
1098 case "$targetDNS" in
1100 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1102 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1104 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1106 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1108 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1111 echo "The $string is not found in current block-list ('$outputFile')."
1120 for i
in $blocked_domains_urls; do
1121 [ "${i//melmac}" != "$i" ] && continue
1122 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1123 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1124 if compare_values
"$(du -sk /tmp/sast)" "500"; then
1125 echo "# block-list too big for most routers"
1126 elif compare_values
"$(du -sk /tmp/sast)" "100"; then
1127 echo "# block-list may be too big for some routers"
1130 echo " list blocked_domains_url '$i'"
1133 echo "# site was down on last check"
1134 echo "# list blocked_domains_url '$i'"
1139 for i
in $blocked_hosts_urls; 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 compare_values
"$(du -sk /tmp/sast)" "500"; then
1143 echo "# block-list too big for most routers"
1144 elif compare_values
"$(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'"