1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039,SC1091,SC2016
6 # shellcheck disable=SC2034
8 # shellcheck disable=SC2034
12 extra_command
'check' 'Checks if specified domain is found in current block-list'
13 extra_command
'dl' 'Force-downloads all enabled block-list'
14 extra_command
'killcache' 'Delete all cached files'
15 extra_command
'sizes' 'Displays the file-sizes of enabled block-listo'
16 extra_command
'show' 'Shows the service last-run status'
17 extra_command
'version' 'Show version information'
19 readonly packageName
='simple-adblock'
20 readonly serviceName
="$packageName $PKG_VERSION"
21 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
22 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
23 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
24 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
25 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
26 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
27 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
28 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
29 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
30 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
31 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
32 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
33 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
34 readonly serversFile
="/var/run/${packageName}.servers"
35 readonly serversCache
="/var/run/${packageName}.servers.cache"
36 readonly serversGzip
="/etc/${packageName}.servers.gz"
37 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
38 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
39 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
40 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
41 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
42 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
43 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
44 readonly PIDFile
="/var/run/${packageName}.pid"
45 readonly jsonFile
="/var/run/${packageName}.json"
46 readonly sharedMemoryError
="/dev/shm/$packageName-error"
47 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
48 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;'
49 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
50 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
51 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
52 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
53 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
54 readonly _ERROR_
='\033[0;31mERROR\033[0m'
55 readonly _WARNING_
='\033[0;33mWARNING\033[0m'
57 version
() { echo "$PKG_VERSION"; }
62 statusNoInstall
) _ret
="$serviceName is not installed or not found";;
63 statusStopped
) _ret
="Stopped";;
64 statusStarting
) _ret
="Starting";;
65 statusRestarting
) _ret
="Restarting";;
66 statusForceReloading
) _ret
="Force Reloading";;
67 statusDownloading
) _ret
="Downloading";;
68 statusProcessing
) _ret
="Processing";;
69 statusError
) _ret
="Error";;
70 statusWarning
) _ret
="Warning";;
71 statusFail
) _ret
="Fail";;
72 statusSuccess
) _ret
="Success";;
79 errorOutputFileCreate
) _ret
="failed to create $outputFile file";;
80 errorFailDNSReload
) _ret
="failed to restart/reload DNS resolver";;
81 errorSharedMemory
) _ret
="failed to access shared memory";;
82 errorSorting
) _ret
="failed to sort data file";;
83 errorOptimization
) _ret
="failed to optimize data file";;
84 errorAllowListProcessing
) _ret
="failed to process allow-list";;
85 errorDataFileFormatting
) _ret
="failed to format data file";;
86 errorMovingDataFile
) _ret
="failed to move data file '${A_TMP}' to '${outputFile}'";;
87 errorCreatingCompressedCache
) _ret
="failed to create compressed cache";;
88 errorRemovingTempFiles
) _ret
="failed to remove temporary files";;
89 errorRestoreCompressedCache
) _ret
="failed to unpack compressed cache";;
90 errorRestoreCache
) _ret
="failed to move '$outputCache' to '$outputFile'";;
91 errorOhSnap
) _ret
="failed to create block-list or restart DNS resolver";;
92 errorStopping
) _ret
="failed to stop $serviceName";;
93 errorDNSReload
) _ret
="failed to reload/restart DNS resolver";;
94 errorDownloadingConfigUpdate
) _ret
="failed to download Config Update file";;
95 errorDownloadingList
) _ret
="failed to download";;
96 errorParsingList
) _ret
="failed to parse";;
97 errorParsingConfigUpdate
) _ret
="failed to parse Config Update file";;
102 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
103 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
104 trap remove_lock EXIT
105 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
106 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
107 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
108 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
109 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
110 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
111 compare_values
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
112 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
113 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
114 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
115 dnsmasq_hup
() { killall
-q -HUP dnsmasq
; }
116 dnsmasq_kill
() { killall
-q -KILL dnsmasq
; }
117 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
118 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
119 is_force_dns_active
() { iptables-save |
grep -q -w -- '--dport 53'; }
120 is_present
() { command -v "$1" >/dev
/null
2>&1; }
123 # Can take a single parameter (text) to be output at any verbosity
124 # Or target verbosity level and text to be output at specifc verbosity
125 local msg memmsg logmsg
126 if [ $# -ne 1 ]; then
127 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
129 [ -t 1 ] && printf "%b" "$1"
130 msg
="${1//$serviceName /service }";
131 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
132 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
133 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
134 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
135 rm -f "$sharedMemoryOutput"
137 printf "%b" "$msg" >> "$sharedMemoryOutput"
141 serviceEnabled
=''; forceDNS
=''; parallelDL
=''; debug
=''; compressedCache
='';
142 ipv6Enabled
=''; configUpdateEnabled
=''; configUpdateURL
=''; bootDelay
='';
143 dlTimeout
=''; curlRetry
=''; verbosity
=''; led
=''; targetDNS
=''; dnsInstance
='';
144 allowed_domains
=''; allowed_domains_urls
='';
145 blocked_domains
=''; blocked_domains_urls
=''; blocked_hosts_urls
='';
146 dl_command
=''; dl_flag
=''; isSSLSupported
=''; allowIDN
='';
147 outputFilter
=''; outputFilterIPv6
=''; outputFile
=''; outputGzip
=''; outputCache
='';
150 load_package_config
() {
151 config_load
"$packageName"
152 config_get_bool serviceEnabled
'config' 'enabled' 1
153 config_get_bool forceDNS
'config' 'force_dns' 1
154 config_get_bool parallelDL
'config' 'parallel_downloads' 1
155 config_get_bool debug
'config' 'debug' 0
156 config_get_bool compressedCache
'config' 'compressed_cache' 0
157 config_get_bool ipv6Enabled
'config' 'ipv6_enabled' 0
158 config_get_bool configUpdateEnabled
'config' 'config_update_enabled' 0
159 config_get bootDelay
'config' 'boot_delay' '120'
160 config_get dlTimeout
'config' 'download_timeout' '20'
161 config_get curlRetry
'config' 'curl_retry' '3'
162 config_get verbosity
'config' 'verbosity' '2'
163 config_get led
'config' 'led'
164 config_get targetDNS
'config' 'dns' 'dnsmasq.servers'
165 config_get dnsInstance
'config' 'dns_instance' '0'
166 config_get allowed_domains
'config' 'allowed_domain'
167 config_get allowed_domains_urls
'config' 'allowed_domains_url'
168 config_get blocked_domains
'config' 'blocked_domain'
169 config_get blocked_domains_urls
'config' 'blocked_domains_url'
170 config_get blocked_hosts_urls
'config' 'blocked_hosts_url'
171 config_get configUpdateURL
'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
173 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
174 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
175 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
176 targetDNS
='dnsmasq.servers'
181 outputFilter
="$addnhostsOutputFilter"
182 outputFile
="$addnhostsFile"
183 outputCache
="$addnhostsCache"
184 outputGzip
="$addnhostsGzip"
185 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6
="$addnhostsOutputFilterIPv6"
186 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
187 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
188 rm -f "$serversFile" "$serversCache" "$serversGzip"
189 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
192 outputFilter
="$dnsmasqOutputFilter"
193 outputFile
="$dnsmasqFile"
194 outputCache
="$dnsmasqCache"
195 outputGzip
="$dnsmasqGzip"
196 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
197 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
198 rm -f "$serversFile" "$serversCache" "$serversGzip"
199 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
202 outputFilter
="$ipsetOutputFilter"
203 outputFile
="$ipsetFile"
204 outputCache
="$ipsetCache"
205 outputGzip
="$ipsetGzip"
206 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
207 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
208 rm -f "$serversFile" "$serversCache" "$serversGzip"
209 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
212 outputFilter
="$serversOutputFilter"
213 outputFile
="$serversFile"
214 outputCache
="$serversCache"
215 outputGzip
="$serversGzip"
216 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
217 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
218 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
219 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
222 outputFilter
="$unboundOutputFilter"
223 outputFile
="$unboundFile"
224 outputCache
="$unboundCache"
225 outputGzip
="$unboundGzip"
226 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
227 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
228 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
229 rm -f "$serversFile" "$serversCache" "$serversGzip"
232 if [ -z "${verbosity##*[!0-9]*}" ] ||
[ "$verbosity" -lt 0 ] ||
[ "$verbosity" -gt 2 ]; then
235 .
/lib
/functions
/network.sh
236 .
/usr
/share
/libubox
/jshn.sh
237 is_present
'gawk' && awk='gawk'
238 if ! is_present
'/usr/libexec/grep-gnu' ||
! is_present
'/usr/libexec/sed-gnu' || \
239 ! is_present
'/usr/libexec/sort-coreutils' ||
! is_present
'gawk'; then
240 local s
="opkg update; opkg --force-overwrite install"
241 is_present
'gawk' || s
="$s gawk"
242 is_present
'/usr/libexec/grep-gnu' || s
="$s grep"
243 is_present
'/usr/libexec/sed-gnu' || s
="$s sed"
244 is_present
'/usr/libexec/sort-coreutils' || s
="$s coreutils-sort"
245 output
"$_WARNING_: Some recommended packages are missing, install them by running:\\n"
248 # Prefer curl because it supports the file:// scheme.
249 if is_present
'curl'; then
250 dl_command
="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
252 elif is_present wget
&& wget
--version 2>/dev
/null |
grep -q "+https"; then
253 dl_command
="wget --no-check-certificate --timeout $dlTimeout -q"
256 dl_command
="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
259 led
="${led:+/sys/class/leds/$led}"
260 if curl
--version 2>/dev
/null |
grep -q "https" \
261 || wget
--version 2>/dev
/null |
grep -q "+https" \
262 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
263 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
264 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
272 wan_if
=''; wan_gw
='';
275 if [ "$debug" -ne 0 ]; then
276 exec 1>>/tmp
/simple-adblock.log
281 if [ "$serviceEnabled" -eq 0 ]; then
284 output
"$packageName is currently disabled.\\n"
285 output
"Run the following commands before starting service again:\\n"
286 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
293 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
294 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
306 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
307 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
308 targetDNS
='dnsmasq.servers'
310 if ! ipset
help hash:net
>/dev
/null
2>&1; then
311 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"
312 targetDNS
='dnsmasq.servers'
317 [ ! -d "${outputFile%/*}" ] && mkdir
-p "${outputFile%/*}"
318 [ ! -d "${outputCache%/*}" ] && mkdir
-p "${outputFile%/*}"
319 [ ! -d "${outputGzip%/*}" ] && mkdir
-p "${outputFile%/*}"
320 cacheOps
'testGzip' && return 0
321 network_flush_cache
; network_find_wan wan_if
; network_get_gateway wan_gw
"$wan_if";
322 [ -n "$wan_gw" ] && return 0
323 output
"$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
327 local cfg
="$1" param
="$2"
330 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
331 uci
-q del dhcp.
"$cfg".serversfile
333 if ! uci
-q get dhcp.
"$cfg".addnhosts |
grep -q "$addnhostsFile"; then
334 uci add_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
337 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup
)
338 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
339 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
340 uci
-q del dhcp.
"$cfg".serversfile
344 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
345 if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
346 uci
set dhcp.
"$cfg".serversfile
="$serversFile"
353 local param output_text i
356 if [ ! -s "$outputFile" ]; then
357 tmpfs
set status
"statusFail"
358 tmpfs add error
"errorOutputFileCreate"
359 output
"$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
364 if [ "$dnsInstance" = "*" ]; then
365 config_foreach dnsmasqOps
'dnsmasq' "$targetDNS"
366 elif [ -n "$dnsInstance" ]; then
367 for i
in $dnsInstance; do
368 dnsmasqOps
"@dnsmasq[$i]" "$targetDNS"
373 dnsmasq.addnhosts|dnsmasq.servers
)
375 output_text
='Reloading DNSMASQ'
377 dnsmasq.conf|dnsmasq.ipset
)
378 param
=dnsmasq_restart
379 output_text
='Restarting DNSMASQ'
382 param
=unbound_restart
383 output_text
='Restarting Unbound'
387 if [ -n "$(uci changes dhcp)" ]; then
389 if [ "$param" = 'unbound_restart' ]; then
390 param
='dnsmasq_restart; unbound_restart;'
391 output_text
='Restarting Unbound/DNSMASQ'
393 param
=dnsmasq_restart
394 output_text
='Restarting DNSMASQ'
397 output
1 "$output_text "
398 output
2 "$output_text "
399 tmpfs
set message
"$output_text"
400 if eval "$param"; then
401 tmpfs
set status
"statusSuccess"
406 tmpfs
set status
"statusFail"
407 tmpfs add error
"errorDNSReload"
408 output
"$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
414 dnsmasq.addnhosts | dnsmasq.servers
)
417 dnsmasq.conf | dnsmasq.ipset
)
418 param
=dnsmasq_restart
421 param
=unbound_restart
424 if [ -n "$(uci changes dhcp)" ]; then
426 if [ "$param" = 'unbound_restart' ]; then
427 param
='dnsmasq_restart; unbound_restart;'
429 param
=dnsmasq_restart
437 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
438 param
=dnsmasq_restart
441 param
=unbound_restart
451 # shellcheck disable=SC2034
452 local action
="$1" param
="$2" value
="$3"
453 local status message error stats
454 local reload restart curReload curRestart ret i
455 if [ -s "$jsonFile" ]; then
456 json_load_file
"$jsonFile" 2>/dev
/null
457 json_select
'data' 2>/dev
/null
458 for i
in status message error stats reload restart
; do
459 json_get_var
$i "$i" 2>/dev
/null
466 curReload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
467 curRestart
="$compressedCache $forceDNS $led"
468 if [ ! -s "$jsonFile" ]; then
470 elif [ "$curReload" != "$reload" ]; then
472 elif [ "$curRestart" != "$restart" ]; then
478 printf "%b" "$(eval echo "\$
$param")"; return;;
482 eval "$param"='$(eval echo "\$$param")${value} ';;
486 unset status message error stats
;;
488 unset reload restart
;;
496 reload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
497 restart
="$compressedCache $forceDNS $led"
500 eval "$param"='$value';;
505 json_add_object
'data'
506 json_add_string version
"$PKG_VERSION"
507 json_add_string status
"$status"
508 json_add_string message
"$message"
509 json_add_string error
"$error"
510 json_add_string stats
"$stats"
511 json_add_string reload
"$reload"
512 json_add_string restart
"$restart"
514 json_dump
> "$jsonFile"
522 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
526 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
530 [ -s "$outputCache" ]
534 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
538 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
539 if gzip < "$outputFile" > "$R_TMP"; then
540 if mv "$R_TMP" "$outputGzip"; then
551 expand|unpack|expandGzip|unpackGzip
)
552 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
559 local label
type D_TMP R_TMP
560 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
561 label
="${1##*//}"; label
="${label%%/*}";
562 if [ "$2" = 'hosts' ]; then
563 label
="Hosts: $label"; filter
="$hostsFilter";
565 label
="Domains: $label"; filter
="$domainsFilter";
567 if [ "$3" = 'blocked' ]; then
568 type='Blocked'; D_TMP
="$B_TMP";
570 type='Allowed'; D_TMP
="$A_TMP";
572 if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
574 output
2 "[DL] $type $label $__FAIL__\\n"
575 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
578 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
579 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
581 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
583 output
2 "[DL] $type $label $__FAIL__\\n"
584 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
586 sed -i "$filter" "$R_TMP"
587 if [ ! -s "$R_TMP" ]; then
589 output
2 "[DL] $type $label $__FAIL__\\n"
590 echo "errorParsingList|${1}" >> "$sharedMemoryError"
592 cat "${R_TMP}" >> "$D_TMP"
594 output
2 "[DL] $type $label $__OK__\\n"
601 process_config_update
() {
603 [ "$configUpdateEnabled" -eq 0 ] && return 0
604 label
="${1##*//}"; label
="${label%%/*}";
605 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
606 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
608 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
610 output
2 "[DL] Config Update: $label $__FAIL__\\n"
611 tmpfs add error
"errorDownloadingConfigUpdate"
613 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc
/config
/simple-adblock
; then
615 output
2 "[DL] Config Update: $label $__OK__\\n"
618 output
2 "[DL] Config Update: $label $__FAIL__\\n"
619 tmpfs add error
"errorParsingConfigUpdate"
627 local hf w_filter j
=0 R_TMP
629 tmpfs
set message
"$(getStatusText "statusDownloading
")..."
630 tmpfs
set status
"statusDownloading"
632 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
633 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
634 output
3 'Low free memory, restarting resolver... '
635 if dnsOps
'quiet'; then
641 touch $A_TMP; touch $B_TMP;
642 output
1 'Downloading lists '
643 process_config_update
"$configUpdateURL"
644 rm -f "$sharedMemoryError"
645 if [ -n "$blocked_hosts_urls" ]; then
646 for hf
in ${blocked_hosts_urls}; do
647 if [ "$parallelDL" -gt 0 ]; then
648 process_url
"$hf" 'hosts' 'blocked' &
650 process_url
"$hf" 'hosts' 'blocked'
654 if [ -n "$blocked_domains_urls" ]; then
655 for hf
in ${blocked_domains_urls}; do
656 if [ "$parallelDL" -gt 0 ]; then
657 process_url
"$hf" 'domains' 'blocked' &
659 process_url
"$hf" 'domains' 'blocked'
663 if [ -n "$allowed_domains_urls" ]; then
664 for hf
in ${allowed_domains_urls}; do
665 if [ "$parallelDL" -gt 0 ]; then
666 process_url
"$hf" 'domains' 'allowed' &
668 process_url
"$hf" 'domains' 'allowed'
674 if [ -s "$sharedMemoryError" ]; then
675 while IFS
= read -r line
; do
676 tmpfs add error
"$line"
677 done < "$sharedMemoryError"
678 rm -f "$sharedMemoryError"
681 [ -n "$blocked_domains" ] && for hf
in ${blocked_domains}; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
682 allowed_domains
="${allowed_domains}
684 [ -n "$allowed_domains" ] && for hf
in ${allowed_domains}; do hf
="$(echo "$hf" | sed 's/\./\\./g')"; w_filter
="$w_filter/${hf}$/d;"; done
686 [ ! -s "$B_TMP" ] && return 1
688 output
1 'Processing downloads '
689 output
2 'Sorting combined list '
690 tmpfs
set message
"$(getStatusText "statusProcessing
"): sorting combined list"
691 if [ "$allowIDN" -gt 0 ]; then
692 if sort -u "$B_TMP" > "$A_TMP"; then
696 tmpfs add error
"errorSorting"
699 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
703 tmpfs add error
"errorSorting"
707 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
708 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
709 [ "$targetDNS" = 'dnsmasq.servers' ] || \
710 [ "$targetDNS" = 'unbound.adb_list' ]; then
711 # TLD optimization written by Dirk Brenken (dev@brenken.org)
712 output
2 'Optimizing combined list '
713 tmpfs
set message
"$(getStatusText "statusProcessing
"): optimizing combined list"
714 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
715 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
716 if sort "$B_TMP" > "$A_TMP"; then
717 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
718 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
719 if sort -u "$A_TMP" > "$B_TMP"; then
723 tmpfs add error
"errorOptimization"
728 tmpfs add error
"errorOptimization"
732 tmpfs add error
"errorOptimization"
737 tmpfs add error
"errorOptimization"
741 tmpfs add error
"errorOptimization"
748 output
2 'Allowing domains '
749 tmpfs
set message
"$(getStatusText "statusProcessing
"): allowing domains"
750 if sed -i "$w_filter" "$B_TMP"; then
754 tmpfs add error
"errorAllowListProcessing"
757 output
2 'Formatting merged file '
758 tmpfs
set message
"$(getStatusText "statusProcessing
"): formatting merged file"
759 if [ -z "$outputFilterIPv6" ]; then
760 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
764 tmpfs add error
"errorDataFileFormatting"
769 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
770 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
774 tmpfs add error
"errorDataFileFormatting"
782 output
2 'Creating DNSMASQ addnhosts file '
783 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ addnhosts file"
786 output
2 'Creating DNSMASQ config file '
787 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ config file"
790 output
2 'Creating DNSMASQ ipset file '
791 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ ipset file"
794 output
2 'Creating DNSMASQ servers file '
795 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating DNSMASQ servers file"
798 output
2 'Creating Unbound adb_list file '
799 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating Unbound adb_list file"
802 if mv "$A_TMP" "$outputFile"; then
806 tmpfs add error
"errorMovingDataFile"
808 if [ "$compressedCache" -gt 0 ]; then
809 output
2 'Creating compressed cache '
810 tmpfs
set message
"$(getStatusText "statusProcessing
"): creating compressed cache"
811 if cacheOps
'createGzip'; then
815 tmpfs add error
"errorCreatingCompressedCache"
820 output
2 'Removing temporary files '
821 tmpfs
set message
"$(getStatusText "statusProcessing
"): removing temporary files"
822 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
823 if [ $j -eq 0 ]; then
827 tmpfs add error
"errorRemovingTempFiles"
837 rc_procd start_service
'on_boot' && rc_procd service_triggers
842 is_enabled
'on_start' ||
return 1
843 local action status error message stats c
844 if ! create_lock
; then
845 output
3 "$serviceName: another instance is starting up "; output_fail
849 status
="$(tmpfs get status)"
850 error
="$(tmpfs get error)"
851 message
="$(tmpfs get message)"
852 stats
="$(tmpfs get stats)"
853 action
="$(tmpfs get triggers)"
855 if [ "$action" = 'on_boot' ] ||
[ "$1" = 'on_boot' ]; then
856 if cacheOps
'testGzip' || cacheOps
'test'; then
861 elif [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
863 elif [ ! -s "$outputFile" ]; then
864 if cacheOps
'testGzip' || cacheOps
'test'; then
869 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
871 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
872 [ "$1" != 'hotplug' ] && showstatus
881 procd_open_instance
'main'
882 procd_set_param
command /bin
/true
883 procd_set_param stdout
1
884 procd_set_param stderr
1
886 json_add_array firewall
887 if [ "$forceDNS" -ne 0 ]; then
889 json_add_string
type redirect
890 json_add_string name simple_adblock_dns_redirect
891 json_add_string target DNAT
892 json_add_string src lan
893 json_add_string proto tcpudp
894 json_add_string src_dport
53
895 json_add_string dest_port
53
896 json_add_string reflection
0
899 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
901 json_add_string
type ipset
902 json_add_string name adb
903 json_add_string match dest_net
904 json_add_string storage
hash
905 json_add_string enabled
1
908 json_add_string
type rule
909 json_add_string name simple_adblock_ipset_rule
910 json_add_string ipset adb
911 json_add_string src lan
912 json_add_string dest
'*'
913 json_add_string proto tcpudp
914 json_add_string target REJECT
915 json_add_string enabled
1
922 if [ "$action" = 'restore' ]; then
923 output
0 "Starting $serviceName... "
924 output
3 "Starting $serviceName...\\n"
925 tmpfs
set status
"statusStarting"
926 if cacheOps
'testGzip' && ! cacheOps
'test' && [ ! -s "$outputFile" ]; then
927 output
3 'Found compressed cache file, unpacking it '
928 tmpfs
set message
'found compressed cache file, unpacking it.'
929 if cacheOps
'unpackGzip'; then
933 tmpfs add error
"errorRestoreCompressedCache"
934 output
"$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
938 if cacheOps
'test' && [ ! -s "$outputFile" ]; then
939 output
3 'Found cache file, reusing it '
940 tmpfs
set message
'found cache file, reusing it.'
941 if cacheOps
'restore'; then
946 tmpfs add error
"errorRestoreCache"
947 output
"$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
954 if [ -s "$outputFile" ] || cacheOps
'test' || cacheOps
'testGzip'; then
955 output
0 "Force-reloading $serviceName... "
956 output
3 "Force-reloading $serviceName...\\n"
957 tmpfs
set status
"statusForceReloading"
959 output
0 "Starting $serviceName... "
960 output
3 "Starting $serviceName...\\n"
961 tmpfs
set status
"statusStarting"
967 output
0 "Restarting $serviceName... "
968 output
3 "Restarting $serviceName...\\n"
969 tmpfs
set status
"statusRestarting"
973 output
0 "Starting $serviceName... "
974 output
3 "Starting $serviceName...\\n"
975 tmpfs
set status
"statusStarting"
979 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
980 output
0 "$__OK__\\n";
982 tmpfs
set status
"statusSuccess"
983 c
="$(wc -l < "$outputFile")"
984 tmpfs
set stats
"$serviceName is blocking $c domains (with ${targetDNS})"
987 output
0 "$__FAIL__\\n";
988 tmpfs
set status
"statusFail"
989 tmpfs add error
"errorOhSnap"
995 service_started
() { procd_set_config_changed firewall
; }
996 service_stopped
() { procd_set_config_changed firewall
; }
997 restart_service
() { rc_procd start_service
'restart'; }
998 reload_service
() { restart_service
; }
999 restart
() { restart_service
; }
1000 reload
() { restart_service
; }
1001 dl
() { rc_procd start_service
'download'; }
1003 rm -f "$addnhostsCache" "$addnhostsGzip"
1004 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1005 rm -f "$ipsetCache" "$ipsetGzip"
1006 rm -f "$serversCache" "$serversGzip"
1007 rm -f "$unboundCache" "$unboundGzip"
1009 config_foreach dnsmasqOps
'dnsmasq' 'cleanup'
1010 uci
-q commit
'dhcp'
1014 show
() { showstatus
; }
1015 status_service
() { showstatus
; }
1017 local c url status message error stats
1018 status
="$(tmpfs get status)"
1019 message
="$(tmpfs get message)"
1020 error
="$(tmpfs get error)"
1021 stats
="$(tmpfs get stats)"
1022 if [ "$status" = "statusSuccess" ]; then
1023 output
"$stats "; output_okn
;
1025 [ -n "$status" ] && status
="$(getStatusText "$status")"
1026 if [ -n "$status" ] && [ -n "$message" ]; then
1027 status
="${status}: $message"
1029 [ -n "$status" ] && output
"$serviceName $status\\n"
1031 if [ -n "$error" ]; then
1036 errorDownloadingList|errorParsingList
)
1037 output
"$_ERROR_: $(getErrorText "$c") $url!\\n";;
1039 output
"$_ERROR_: $(getErrorText "$c")!\\n";;
1048 if [ -s "$outputFile" ]; then
1049 output
"Stopping $serviceName... "
1051 if dnsOps
'on_stop'; then
1053 output
0 "$__OK__\\n"; output_okn
;
1054 tmpfs
set status
"statusStopped"
1057 output
0 "$__FAIL__\\n"; output_fail
;
1058 tmpfs
set status
"statusFail"
1059 tmpfs add error
"errorStopping"
1060 output
"$_ERROR_: $(getErrorText 'errorStopping')!\\n"
1065 service_triggers
() {
1067 procd_add_config_trigger
"config.change" "${packageName}" /etc
/init.d
/${packageName} reload
1074 c
="$(grep -c "$string" "$outputFile")"
1075 if [ ! -s "$outputFile" ]; then
1076 echo "No block-list ('$outputFile') found."
1077 elif [ -z "$string" ]; then
1078 echo "Usage: /etc/init.d/${packageName} check string"
1079 elif [ "$c" -gt 0 ]; then
1080 if [ "$c" -gt 1 ]; then
1081 echo "Found $c matches for '$string' in '$outputFile':"
1083 echo "Found 1 match for '$string' in '$outputFile':"
1085 case "$targetDNS" in
1087 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1089 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1091 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1093 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1095 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1098 echo "The $string is not found in current block-list ('$outputFile')."
1107 for i
in $blocked_domains_urls; do
1108 [ "${i//melmac}" != "$i" ] && continue
1109 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1110 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1111 if compare_values
"$(du -sk /tmp/sast)" "500"; then
1112 echo "# block-list too big for most routers"
1113 elif compare_values
"$(du -sk /tmp/sast)" "100"; then
1114 echo "# block-list may be too big for some routers"
1117 echo " list blocked_domains_url '$i'"
1120 echo "# site was down on last check"
1121 echo "# list blocked_domains_url '$i'"
1126 for i
in $blocked_hosts_urls; do
1127 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1128 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1129 if compare_values
"$(du -sk /tmp/sast)" "500"; then
1130 echo "# block-list too big for most routers"
1131 elif compare_values
"$(du -sk /tmp/sast)" "100"; then
1132 echo "# block-list may be too big for some routers"
1135 echo " list blocked_hosts_url '$i'"
1138 echo "# site was down on last check"
1139 echo "# list blocked_hosts_url '$i'"