1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039,SC1091
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
'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 # shellcheck disable=SC2034
20 EXTRA_COMMANDS
='check dl killcache sizes show 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
25 show Shows the service last-run status'
28 readonly packageName
='simple-adblock'
29 readonly serviceName
="$packageName $PKG_VERSION"
30 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
31 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
32 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
33 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
34 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
35 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
36 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
37 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
38 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
39 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
40 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
41 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
42 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
43 readonly serversFile
="/var/run/${packageName}.servers"
44 readonly serversCache
="/var/run/${packageName}.servers.cache"
45 readonly serversGzip
="/etc/${packageName}.servers.gz"
46 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
47 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
48 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
49 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
50 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
51 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
52 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
53 readonly PIDFile
="/var/run/${packageName}.pid"
54 readonly jsonFile
="/var/run/${packageName}.json"
55 readonly sharedMemoryError
="/dev/shm/$packageName-error"
56 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
57 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;'
58 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
59 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
60 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
61 readonly __OK__
='\033[0;32m[\xe2\x9c\x93]\033[0m'
62 readonly __FAIL__
='\033[0;31m[\xe2\x9c\x97]\033[0m'
63 readonly _ERROR_
='\033[0;31mERROR\033[0m'
65 version
() { echo "$PKG_VERSION"; }
70 statusNoInstall
) _ret
="$serviceName is not installed or not found";;
71 statusStopped
) _ret
="Stopped";;
72 statusStarting
) _ret
="Starting";;
73 statusRestarting
) _ret
="Restarting";;
74 statusForceReloading
) _ret
="Force Reloading";;
75 statusDownloading
) _ret
="Downloading";;
76 statusProcessing
) _ret
="Processing";;
77 statusError
) _ret
="Error";;
78 statusWarning
) _ret
="Warning";;
79 statusFail
) _ret
="Fail";;
80 statusSuccess
) _ret
="Success";;
87 errorOutputFileCreate
) _ret
="failed to create $outputFile file";;
88 errorFailDNSReload
) _ret
="failed to restart/reload DNS resolver";;
89 errorSharedMemory
) _ret
="failed to access shared memory";;
90 errorSorting
) _ret
="failed to sort data file";;
91 errorOptimization
) _ret
="failed to optimize data file";;
92 errorAllowListProcessing
) _ret
="failed to process allow-list";;
93 errorDataFileFormatting
) _ret
="failed to format data file";;
94 errorMovingDataFile
) _ret
="failed to move data file '${A_TMP}' to '${outputFile}'";;
95 errorCreatingCompressedCache
) _ret
="failed to create compressed cache";;
96 errorRemovingTempFiles
) _ret
="failed to remove temporary files";;
97 errorRestoreCompressedCache
) _ret
="failed to unpack compressed cache";;
98 errorRestoreCache
) _ret
="failed to move '$outputCache' to '$outputFile'";;
99 errorOhSnap
) _ret
="failed to create block-list or restart DNS resolver";;
100 errorStopping
) _ret
="failed to stop $serviceName";;
101 errorDNSReload
) _ret
="failed to reload/restart DNS resolver";;
102 errorDownloadingConfigUpdate
) _ret
="failed to download Config Update file";;
103 errorDownloadingList
) _ret
="failed to download";;
104 errorParsingList
) _ret
="failed to parse";;
105 errorParsingConfigUpdate
) _ret
="failed to parse Config Update file";;
110 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
111 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
112 trap remove_lock EXIT
113 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
114 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
115 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
116 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
117 str_replace
() { printf "%b" "$1" |
sed -e "s/$(printf "%b
" "$2")/$(printf "%b
" "$3")/g"; }
118 str_contains
() { test "$1" != "$(str_replace "$1" "$2" '')"; }
119 compare_versions
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
120 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
121 is_ipset_procd
() { compare_versions
"$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
122 led_on
(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
123 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
124 dnsmasq_hup
() { killall
-q -HUP dnsmasq
; }
125 dnsmasq_kill
() { killall
-q -KILL dnsmasq
; }
126 dnsmasq_restart
() { /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1; }
127 unbound_restart
() { /etc
/init.d
/unbound restart
>/dev
/null
2>&1; }
130 # Can take a single parameter (text) to be output at any verbosity
131 # Or target verbosity level and text to be output at specifc verbosity
132 local msg memmsg logmsg
133 if [ $# -ne 1 ]; then
134 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
136 [ -t 1 ] && printf "%b" "$1"
137 msg
="${1//$serviceName /service }";
138 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
139 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
140 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
141 logger
-t "${packageName:-service} [$$]" "$(printf "%b
" "$logmsg")"
142 rm -f "$sharedMemoryOutput"
144 printf "%b" "$msg" >> "$sharedMemoryOutput"
154 configUpdateEnabled
=0
161 targetDNS
=dnsmasq.servers
165 allowed_domains_urls
=''
166 blocked_domains_urls
=''
167 blocked_hosts_urls
=''
178 load_package_config
() {
179 config_load
"$packageName"
180 config_get_bool serviceEnabled
'config' 'enabled' 1
181 config_get_bool forceDNS
'config' 'force_dns' 1
182 config_get_bool parallelDL
'config' 'parallel_downloads' 1
183 config_get_bool debug
'config' 'debug' 0
184 config_get_bool compressedCache
'config' 'compressed_cache' 0
185 config_get_bool ipv6Enabled
'config' 'ipv6_enabled' 0
186 config_get_bool configUpdateEnabled
'config' 'config_update_enabled' 0
187 config_get bootDelay
'config' 'boot_delay' '120'
188 config_get dlTimeout
'config' 'download_timeout' '20'
189 config_get curlRetry
'config' 'curl_retry' '3'
190 config_get verbosity
'config' 'verbosity' '2'
191 config_get led
'config' 'led'
192 config_get targetDNS
'config' 'dns' 'dnsmasq.servers'
193 config_get dnsInstance
'config' 'dns_instance' '0'
194 config_get allowed_domains
'config' 'allowed_domain'
195 config_get allowed_domains_urls
'config' 'allowed_domains_url'
196 config_get blocked_domains
'config' 'blocked_domain'
197 config_get blocked_domains_urls
'config' 'blocked_domains_url'
198 config_get blocked_hosts_urls
'config' 'blocked_hosts_url'
199 config_get configUpdateURL
'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
201 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
202 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
203 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
204 targetDNS
='dnsmasq.servers'
209 outputFilter
="$addnhostsOutputFilter"
210 outputFile
="$addnhostsFile"
211 outputCache
="$addnhostsCache"
212 outputGzip
="$addnhostsGzip"
213 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6
="$addnhostsOutputFilterIPv6"
214 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
215 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
216 rm -f "$serversFile" "$serversCache" "$serversGzip"
217 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
220 outputFilter
="$dnsmasqOutputFilter"
221 outputFile
="$dnsmasqFile"
222 outputCache
="$dnsmasqCache"
223 outputGzip
="$dnsmasqGzip"
224 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
225 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
226 rm -f "$serversFile" "$serversCache" "$serversGzip"
227 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
230 outputFilter
="$ipsetOutputFilter"
231 outputFile
="$ipsetFile"
232 outputCache
="$ipsetCache"
233 outputGzip
="$ipsetGzip"
234 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
235 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
236 rm -f "$serversFile" "$serversCache" "$serversGzip"
237 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
240 outputFilter
="$serversOutputFilter"
241 outputFile
="$serversFile"
242 outputCache
="$serversCache"
243 outputGzip
="$serversGzip"
244 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
245 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
246 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
247 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
250 outputFilter
="$unboundOutputFilter"
251 outputFile
="$unboundFile"
252 outputCache
="$unboundCache"
253 outputGzip
="$unboundGzip"
254 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
255 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
256 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
257 rm -f "$serversFile" "$serversCache" "$serversGzip"
260 if [ -z "${verbosity##*[!0-9]*}" ] ||
[ "$verbosity" -lt 0 ] ||
[ "$verbosity" -gt 2 ]; then
263 .
/lib
/functions
/network.sh
264 .
/usr
/share
/libubox
/jshn.sh
265 # Prefer curl because it supports the file:// scheme.
266 if command -v curl
>/dev
/null
2>&1; then
267 dl_command
="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
269 elif command -v wget
>/dev
/null
2>&1 && wget
--version 2>/dev
/null |
grep -q "+https"; then
270 dl_command
="wget --no-check-certificate --timeout $dlTimeout -q"
273 dl_command
="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
276 led
="${led:+/sys/class/leds/$led}"
277 if curl
--version 2>/dev
/null |
grep -q "https" \
278 || wget
--version 2>/dev
/null |
grep -q "+https" \
279 ||
grep -q "libustream-mbedtls" /usr
/lib
/opkg
/status \
280 ||
grep -q "libustream-openssl" /usr
/lib
/opkg
/status \
281 ||
grep -q "libustream-wolfssl" /usr
/lib
/opkg
/status
; then
289 wan_if
=''; wan_gw
='';
292 if [ "$debug" -ne 0 ]; then
293 exec 1>>/tmp
/simple-adblock.log
298 if [ "$serviceEnabled" -eq 0 ]; then
301 output
"$packageName is currently disabled.\\n"
302 output
"Run the following commands before starting service again:\\n"
303 output
"uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
310 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
311 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-IDN' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'IDN'; then
323 if dnsmasq
-v 2>/dev
/null |
grep -q 'no-ipset' ||
! dnsmasq
-v 2>/dev
/null |
grep -q -w 'ipset'; then
324 output
"$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
325 targetDNS
='dnsmasq.servers'
327 if ! ipset
help hash:net
>/dev
/null
2>&1; then
328 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"
329 targetDNS
='dnsmasq.servers'
334 [ ! -d "${outputFile%/*}" ] && mkdir
-p "${outputFile%/*}"
335 [ ! -d "${outputCache%/*}" ] && mkdir
-p "${outputFile%/*}"
336 [ ! -d "${outputGzip%/*}" ] && mkdir
-p "${outputFile%/*}"
337 cacheOps
'testGzip' && return 0
338 network_flush_cache
; network_find_wan wan_if
; network_get_gateway wan_gw
"$wan_if";
339 [ -n "$wan_gw" ] && return 0
340 output
"$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
344 local cfg
="$1" param
="$2"
347 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
348 uci
-q del dhcp.
"$cfg".serversfile
350 if ! uci
-q get dhcp.
"$cfg".addnhosts |
grep -q "$addnhostsFile"; then
351 uci add_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
354 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup
)
355 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
356 if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
357 uci
-q del dhcp.
"$cfg".serversfile
361 uci
-q del_list dhcp.
"$cfg".addnhosts
="$addnhostsFile"
362 if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
363 uci
set dhcp.
"$cfg".serversfile
="$serversFile"
370 local param output_text i
373 if [ ! -s "$outputFile" ]; then
374 tmpfs
set status
"statusFail"
375 tmpfs add error
"errorOutputFileCreate"
376 output
"$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
381 if [ "$dnsInstance" = "*" ]; then
382 config_foreach dnsmasqOps
'dnsmasq' "$targetDNS"
383 elif [ -n "$dnsInstance" ]; then
384 for i
in $dnsInstance; do
385 dnsmasqOps
"@dnsmasq[$i]" "$targetDNS"
390 dnsmasq.addnhosts|dnsmasq.servers
)
392 output_text
='Reloading DNSMASQ'
394 dnsmasq.conf|dnsmasq.ipset
)
395 param
=dnsmasq_restart
396 output_text
='Restarting DNSMASQ'
399 param
=unbound_restart
400 output_text
='Restarting Unbound'
404 if [ -n "$(uci changes dhcp)" ]; then
406 if [ "$param" = 'unbound_restart' ]; then
407 param
='dnsmasq_restart; unbound_restart;'
408 output_text
='Restarting Unbound/DNSMASQ'
410 param
=dnsmasq_restart
411 output_text
='Restarting DNSMASQ'
414 output
1 "$output_text "
415 output
2 "$output_text "
416 tmpfs
set message
"$output_text"
417 if eval "$param"; then
418 tmpfs
set status
"statusSuccess"
423 tmpfs
set status
"statusFail"
424 tmpfs add error
"errorDNSReload"
425 output
"$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
431 dnsmasq.addnhosts | dnsmasq.servers
)
434 dnsmasq.conf | dnsmasq.ipset
)
435 param
=dnsmasq_restart
438 param
=unbound_restart
441 if [ -n "$(uci changes dhcp)" ]; then
443 if [ "$param" = 'unbound_restart' ]; then
444 param
='dnsmasq_restart; unbound_restart;'
446 param
=dnsmasq_restart
454 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers
)
455 param
=dnsmasq_restart
458 param
=unbound_restart
468 local action
="$1" instance
="$2" value
="$3"
469 local status message error stats
470 local readReload readRestart curReload curRestart ret
471 if [ -s "$jsonFile" ]; then
472 status
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['status']")"
473 message
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['message']")"
474 error
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['error']")"
475 stats
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['stats']")"
476 readReload
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['reload']")"
477 readRestart
="$(jsonfilter -i $jsonFile -l1 -e "@
['data']['restart']")"
483 printf "%b" "$status"; return;;
485 printf "%b" "$message"; return;;
487 printf "%b" "$error"; return;;
489 printf "%b" "$stats"; return;;
491 curReload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
492 curRestart
="$compressedCache $forceDNS $led"
493 if [ ! -s "$jsonFile" ]; then
495 elif [ "$curReload" != "$readReload" ]; then
497 elif [ "$curRestart" != "$readRestart" ]; then
507 [ -n "$status" ] && status
="$status $value" || status
="$value";;
509 [ -n "$message" ] && message
="$message $value" || message
="$value";;
511 [ -n "$error" ] && error
="$error $value" || error
="$value";;
513 [ -n "$stats" ] && stats
="$stats $value" || stats
="$value";;
533 unset readReload
; unset readRestart
;;
547 readReload
="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
548 readRestart
="$compressedCache $forceDNS $led"
554 json_add_object
'data'
555 json_add_string version
"$PKG_VERSION"
556 json_add_string status
"$status"
557 json_add_string message
"$message"
558 json_add_string error
"$error"
559 json_add_string stats
"$stats"
560 json_add_string reload
"$readReload"
561 json_add_string restart
"$readRestart"
563 json_dump
> "$jsonFile"
571 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true
> "$outputFile"; } >/dev
/null
2>/dev
/null
575 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev
/null
2>/dev
/null
579 [ -s "$outputCache" ]
583 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
587 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
588 if gzip < "$outputFile" > "$R_TMP"; then
589 if mv "$R_TMP" "$outputGzip"; then
600 expand|unpack|expandGzip|unpackGzip
)
601 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
608 local action
="$1" param
="$2" _restart
610 reload
) /etc
/init.d
/firewall reload
>/dev
/null
2>&1;;
611 restart
) /etc
/init.d
/firewall restart
>/dev
/null
2>&1;;
614 dns_redirect
) uci
-q del firewall.simple_adblock_dns_redirect
;;
615 ipset
) uci
-q del firewall.simple_adblock_ipset
616 uci
-q del firewall.simple_adblock_ipset_rule
;;
618 uci
-q del firewall.simple_adblock_dns_redirect
619 uci
-q del firewall.simple_adblock_ipset
620 uci
-q del firewall.simple_adblock_ipset_rule
627 if ! uci
-q get firewall.simple_adblock_dns_redirect
>/dev
/null
; then
628 uci
-q set firewall.simple_adblock_dns_redirect
=redirect
629 uci
-q set firewall.simple_adblock_dns_redirect.name
=simple_adblock_dns_hijack
630 uci
-q set firewall.simple_adblock_dns_redirect.target
=DNAT
631 uci
-q set firewall.simple_adblock_dns_redirect.src
=lan
632 uci
-q set firewall.simple_adblock_dns_redirect.proto
=tcpudp
633 uci
-q set firewall.simple_adblock_dns_redirect.src_dport
=53
634 uci
-q set firewall.simple_adblock_dns_redirect.dest_port
=53
638 if ! uci
-q get firewall.simple_adblock_ipset
>/dev
/null
; then
639 uci
-q set firewall.simple_adblock_ipset
=ipset
640 uci
-q set firewall.simple_adblock_ipset.name
=adb
641 uci
-q set firewall.simple_adblock_ipset.match
=dest_net
642 uci
-q set firewall.simple_adblock_ipset.storage
=hash
643 uci
-q set firewall.simple_adblock_ipset.enabled
=1
646 if ! uci
-q get firewall.simple_adblock_ipset_rule
>/dev
/null
; then
647 uci
-q set firewall.simple_adblock_ipset_rule
=rule
648 uci
-q set firewall.simple_adblock_ipset_rule.name
=simple_adblock_ipset_rule
649 uci
-q set firewall.simple_adblock_ipset_rule.ipset
=adb
650 uci
-q set firewall.simple_adblock_ipset_rule.src
=lan
651 uci
-q set firewall.simple_adblock_ipset_rule.dest
='*'
652 uci
-q set firewall.simple_adblock_ipset_rule.proto
=tcpudp
653 uci
-q set firewall.simple_adblock_ipset_rule.target
=REJECT
654 uci
-q set firewall.simple_adblock_ipset_rule.enabled
=1
658 if ! uci
-q get firewall.simple_adblock_dns_redirect
>/dev
/null
; then
659 uci
-q set firewall.simple_adblock_dns_redirect
=redirect
660 uci
-q set firewall.simple_adblock_dns_redirect.name
=simple_adblock_dns_hijack
661 uci
-q set firewall.simple_adblock_dns_redirect.target
=DNAT
662 uci
-q set firewall.simple_adblock_dns_redirect.src
=lan
663 uci
-q set firewall.simple_adblock_dns_redirect.proto
=tcpudp
664 uci
-q set firewall.simple_adblock_dns_redirect.src_dport
=53
665 uci
-q set firewall.simple_adblock_dns_redirect.dest_port
=53
667 if ! uci
-q get firewall.simple_adblock_ipset
>/dev
/null
; then
668 uci
-q set firewall.simple_adblock_ipset
=ipset
669 uci
-q set firewall.simple_adblock_ipset.name
=adb
670 uci
-q set firewall.simple_adblock_ipset.match
=dest_net
671 uci
-q set firewall.simple_adblock_ipset.storage
=hash
672 uci
-q set firewall.simple_adblock_ipset.enabled
=1
675 if ! uci
-q get firewall.simple_adblock_ipset_rule
>/dev
/null
; then
676 uci
-q set firewall.simple_adblock_ipset_rule
=rule
677 uci
-q set firewall.simple_adblock_ipset_rule.name
=simple_adblock_ipset_rule
678 uci
-q set firewall.simple_adblock_ipset_rule.ipset
=adb
679 uci
-q set firewall.simple_adblock_ipset_rule.src
=lan
680 uci
-q set firewall.simple_adblock_ipset_rule.dest
='*'
681 uci
-q set firewall.simple_adblock_ipset_rule.proto
=tcpudp
682 uci
-q set firewall.simple_adblock_ipset_rule.target
=REJECT
683 uci
-q set firewall.simple_adblock_ipset_rule.enabled
=1
688 if [ -n "$(uci changes firewall)" ]; then
689 uci
-q commit firewall
690 if [ -z "$_restart" ]; then
699 local label
type D_TMP R_TMP
700 if [ -z "$1" ] ||
[ -z "$2" ] ||
[ -z "$3" ]; then return 1; fi
701 label
="${1##*//}"; label
="${label%%/*}";
702 if [ "$2" = 'hosts' ]; then
703 label
="Hosts: $label"; filter
="$hostsFilter";
705 label
="Domains: $label"; filter
="$domainsFilter";
707 if [ "$3" = 'blocked' ]; then
708 type='Blocked'; D_TMP
="$B_TMP";
710 type='Allowed'; D_TMP
="$A_TMP";
712 if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
714 output
2 "[DL] $type $label $__FAIL__\\n"
715 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
718 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
719 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
721 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
723 output
2 "[DL] $type $label $__FAIL__\\n"
724 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
726 sed -i "$filter" "$R_TMP"
727 if [ ! -s "$R_TMP" ]; then
729 output
2 "[DL] $type $label $__FAIL__\\n"
730 echo "errorParsingList|${1}" >> "$sharedMemoryError"
732 cat "${R_TMP}" >> "$D_TMP"
734 output
2 "[DL] $type $label $__OK__\\n"
741 process_config_update
() {
743 [ "$configUpdateEnabled" -eq 0 ] && return 0
744 label
="${1##*//}"; label
="${label%%/*}";
745 while [ -z "$R_TMP" ] ||
[ -e "$R_TMP" ]; do
746 R_TMP
="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
748 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev
/null ||
[ ! -s "$R_TMP" ]; then
750 output
2 "[DL] Config Update: $label $__FAIL__\\n"
751 tmpfs add error
"errorDownloadingConfigUpdate"
753 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc
/config
/simple-adblock
; then
755 output
2 "[DL] Config Update: $label $__OK__\\n"
758 output
2 "[DL] Config Update: $label $__FAIL__\\n"
759 tmpfs add error
"errorParsingConfigUpdate"
767 local hf w_filter j
=0 R_TMP
769 tmpfs
set message
"$(getStatusText "statusDownloading
")..."
770 tmpfs
set status
"statusDownloading"
772 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
773 if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")" -lt 32 ]; then
774 output
3 'Low free memory, restarting resolver... '
775 if dnsOps
'quiet'; then
781 touch $A_TMP; touch $B_TMP;
782 output
1 'Downloading lists '
783 process_config_update
"$configUpdateURL"
784 rm -f "$sharedMemoryError"
785 if [ -n "$blocked_hosts_urls" ]; then
786 for hf
in ${blocked_hosts_urls}; do
787 if [ "$parallelDL" -gt 0 ]; then
788 process_url
"$hf" 'hosts' 'blocked' &
790 process_url
"$hf" 'hosts' 'blocked'
794 if [ -n "$blocked_domains_urls" ]; then
795 for hf
in ${blocked_domains_urls}; do
796 if [ "$parallelDL" -gt 0 ]; then
797 process_url
"$hf" 'domains' 'blocked' &
799 process_url
"$hf" 'domains' 'blocked'
803 if [ -n "$allowed_domains_urls" ]; then
804 for hf
in ${allowed_domains_urls}; do
805 if [ "$parallelDL" -gt 0 ]; then
806 process_url
"$hf" 'domains' 'allowed' &
808 process_url
"$hf" 'domains' 'allowed'
814 if [ -s "$sharedMemoryError" ]; then
815 while IFS
= read -r line
; do
816 tmpfs add error
"$line"
817 done < "$sharedMemoryError"
818 rm -f "$sharedMemoryError"
821 [ -n "$blocked_domains" ] && for hf
in ${blocked_domains}; do echo "$hf" |
sed "$domainsFilter" >> $B_TMP; done
822 allowed_domains
="${allowed_domains}
824 [ -n "$allowed_domains" ] && for hf
in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$
/d
;"; done
826 [ ! -s "$B_TMP" ] && return 1
828 output 1 'Processing downloads '
829 output 2 'Sorting combined list '
830 tmpfs set message "$
(getStatusText
"statusProcessing"): sorting combined list
"
831 if [ "$allowIDN" -gt 0 ]; then
832 if sort -u "$B_TMP" > "$A_TMP"; then
836 tmpfs add error "errorSorting
"
839 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
843 tmpfs add error "errorSorting
"
847 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
848 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
849 [ "$targetDNS" = 'dnsmasq.servers' ] || \
850 [ "$targetDNS" = 'unbound.adb_list' ]; then
851 # TLD optimization written by Dirk Brenken (dev@brenken.org)
852 output 2 'Optimizing combined list '
853 tmpfs set message "$
(getStatusText
"statusProcessing"): optimizing combined list
"
854 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
855 if awk -F ".
" '{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' "$A_TMP" > "$B_TMP"; then
856 if sort "$B_TMP" > "$A_TMP"; then
857 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\.
"){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
858 if awk -F ".
" '{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' "$B_TMP" > "$A_TMP"; then
859 if sort -u "$A_TMP" > "$B_TMP"; then
863 tmpfs add error "errorOptimization
"
868 tmpfs add error "errorOptimization
"
872 tmpfs add error "errorOptimization
"
877 tmpfs add error "errorOptimization
"
881 tmpfs add error "errorOptimization
"
888 output 2 'Allowing domains '
889 tmpfs set message "$
(getStatusText
"statusProcessing"): allowing domains
"
890 if sed -i "$w_filter" "$B_TMP"; then
894 tmpfs add error "errorAllowListProcessing
"
897 output 2 'Formatting merged file '
898 tmpfs set message "$
(getStatusText
"statusProcessing"): formatting merged
file"
899 if [ -z "$outputFilterIPv6" ]; then
900 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
904 tmpfs add error "errorDataFileFormatting
"
909 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
910 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
914 tmpfs add error "errorDataFileFormatting
"
922 output 2 'Creating DNSMASQ addnhosts file '
923 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ addnhosts
file"
926 output 2 'Creating DNSMASQ config file '
927 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ config
file"
930 output 2 'Creating DNSMASQ ipset file '
931 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ ipset
file"
934 output 2 'Creating DNSMASQ servers file '
935 tmpfs set message "$
(getStatusText
"statusProcessing"): creating DNSMASQ servers
file"
938 output 2 'Creating Unbound adb_list file '
939 tmpfs set message "$
(getStatusText
"statusProcessing"): creating Unbound adb_list
file"
942 if mv "$A_TMP" "$outputFile"; then
946 tmpfs add error "errorMovingDataFile
"
948 if [ "$compressedCache" -gt 0 ]; then
949 output 2 'Creating compressed cache '
950 tmpfs set message "$
(getStatusText
"statusProcessing"): creating compressed cache
"
951 if cacheOps 'createGzip'; then
955 tmpfs add error "errorCreatingCompressedCache
"
960 output 2 'Removing temporary files '
961 tmpfs set message "$
(getStatusText
"statusProcessing"): removing temporary files
"
962 rm -f "/tmp
/${packageName}_tmp.
*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
963 if [ $j -eq 0 ]; then
967 tmpfs add error "errorRemovingTempFiles
"
977 rc_procd start_service 'on_boot' && rc_procd service_triggers
982 is_enabled 'on_start' || return 1
983 local action status error message stats c
984 if ! create_lock; then
985 output 3 "$serviceName: another instance is starting up
"; output_fail
989 status="$
(tmpfs get status
)"
990 error="$
(tmpfs get error
)"
991 message="$
(tmpfs get message
)"
992 stats="$
(tmpfs get stats
)"
993 action="$
(tmpfs get triggers
)"
995 if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
996 if cacheOps 'testGzip' || cacheOps 'test'; then
1001 elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
1003 elif [ ! -s "$outputFile" ]; then
1004 if cacheOps 'testGzip' || cacheOps 'test'; then
1009 elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
1011 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess
" ] && [ -z "$error" ]; then
1012 [ "$1" != 'hotplug' ] && showstatus
1021 if is_chaos_calmer || ! is_ipset_procd; then
1022 if [ "$forceDNS" -ne 0 ]; then
1023 fw3Ops 'insert' 'dns_redirect'
1025 fw3Ops 'remove' 'dns_redirect'
1027 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
1028 fw3Ops 'insert' 'ipset'
1030 fw3Ops 'remove' 'ipset'
1032 procd_open_instance 'main'
1033 procd_set_param command /bin/true
1034 procd_set_param stdout 1
1035 procd_set_param stderr 1
1036 procd_close_instance
1038 procd_open_instance 'main'
1039 procd_set_param command /bin/true
1040 procd_set_param stdout 1
1041 procd_set_param stderr 1
1043 json_add_array firewall
1044 if [ "$forceDNS" -ne 0 ]; then
1046 json_add_string type redirect
1047 json_add_string name simple_adblock_dns_redirect
1048 json_add_string target DNAT
1049 json_add_string src lan
1050 json_add_string proto tcpudp
1051 json_add_string src_dport 53
1052 json_add_string dest_port 53
1053 json_add_string reflection 0
1056 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
1058 json_add_string type ipset
1059 json_add_string name adb
1060 json_add_string match dest_net
1061 json_add_string storage hash
1062 json_add_string enabled 1
1065 json_add_string type rule
1066 json_add_string name simple_adblock_ipset_rule
1067 json_add_string ipset adb
1068 json_add_string src lan
1069 json_add_string dest '*'
1070 json_add_string proto tcpudp
1071 json_add_string target REJECT
1072 json_add_string enabled 1
1077 procd_close_instance
1080 if [ "$action" = 'restore' ]; then
1081 output 0 "Starting
$serviceName...
"
1082 output 3 "Starting
$serviceName...
\\n
"
1083 tmpfs set status "statusStarting
"
1084 if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
1085 output 3 'Found compressed cache file, unpacking it '
1086 tmpfs set message 'found compressed cache file, unpacking it.'
1087 if cacheOps 'unpackGzip'; then
1091 tmpfs add error "errorRestoreCompressedCache
"
1092 output "$_ERROR_: $
(getErrorText
'errorRestoreCompressedCache')!\\n
"
1096 if cacheOps 'test' && [ ! -s "$outputFile" ]; then
1097 output 3 'Found cache file, reusing it '
1098 tmpfs set message 'found cache file, reusing it.'
1099 if cacheOps 'restore'; then
1104 tmpfs add error "errorRestoreCache
"
1105 output "$_ERROR_: $
(getErrorText
'errorRestoreCache')!\\n
"
1112 if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
1113 output 0 "Force-reloading
$serviceName...
"
1114 output 3 "Force-reloading
$serviceName...
\\n
"
1115 tmpfs set status "statusForceReloading
"
1117 output 0 "Starting
$serviceName...
"
1118 output 3 "Starting
$serviceName...
\\n
"
1119 tmpfs set status "statusStarting
"
1125 output 0 "Restarting
$serviceName...
"
1126 output 3 "Restarting
$serviceName...
\\n
"
1127 tmpfs set status "statusRestarting
"
1131 output 0 "Starting
$serviceName...
"
1132 output 3 "Starting
$serviceName...
\\n
"
1133 tmpfs set status "statusStarting
"
1137 if [ -s "$outputFile" ] && [ "$
(tmpfs get status
)" != "statusFail
" ]; then
1138 output 0 "$__OK__\\n
";
1140 tmpfs set status "statusSuccess
"
1141 c="$
(wc -l < "$outputFile")"
1142 tmpfs set stats "$serviceName is blocking
$c domains
(with
${targetDNS})"
1145 output 0 "$__FAIL__\\n
";
1146 tmpfs set status "statusFail
"
1147 tmpfs add error "errorOhSnap
"
1153 service_started() { is_ipset_procd && procd_set_config_changed firewall; }
1154 service_stopped() { is_ipset_procd && procd_set_config_changed firewall; }
1155 restart_service() { rc_procd start_service 'restart'; }
1156 reload_service() { restart_service; }
1157 restart() { restart_service; }
1158 reload() { restart_service; }
1159 dl() { rc_procd start_service 'download'; }
1161 rm -f "$addnhostsCache" "$addnhostsGzip"
1162 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1163 rm -f "$ipsetCache" "$ipsetGzip"
1164 rm -f "$serversCache" "$serversGzip"
1165 rm -f "$unboundCache" "$unboundGzip"
1167 config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
1168 uci -q commit 'dhcp'
1172 show() { showstatus; }
1173 status_service() { showstatus; }
1175 local c url status message error stats
1176 status="$
(tmpfs get status
)"
1177 message="$
(tmpfs get message
)"
1178 error="$
(tmpfs get error
)"
1179 stats="$
(tmpfs get stats
)"
1180 if [ "$status" = "statusSuccess
" ]; then
1181 output "$stats "; output_okn;
1183 [ -n "$status" ] && status="$
(getStatusText
"$status")"
1184 if [ -n "$status" ] && [ -n "$message" ]; then
1185 status="${status}: $message"
1187 [ -n "$status" ] && output "$serviceName $status\\n
"
1189 if [ -n "$error" ]; then
1194 errorDownloadingList|errorParsingList)
1195 output "$_ERROR_: $
(getErrorText
"$c") $url!\\n
";;
1197 output "$_ERROR_: $
(getErrorText
"$c")!\\n
";;
1206 fw3Ops 'remove' 'all'
1207 if [ -s "$outputFile" ]; then
1208 output "Stopping
$serviceName...
"
1210 if dnsOps 'on_stop'; then
1212 output 0 "$__OK__\\n
"; output_okn;
1213 tmpfs set status "statusStopped
"
1216 output 0 "$__FAIL__\\n
"; output_fail;
1217 tmpfs set status "statusFail
"
1218 tmpfs add error "errorStopping
"
1219 output "$_ERROR_: $
(getErrorText
'errorStopping')!\\n
"
1224 service_triggers() {
1225 procd_add_config_trigger "config.change
" "$packageName" /etc/init.d/$packageName reload
1231 c="$
(grep -c "$string" "$outputFile")"
1232 if [ ! -s "$outputFile" ]; then
1233 echo "No block-list
('$outputFile') found.
"
1234 elif [ -z "$string" ]; then
1235 echo "Usage
: /etc
/init.d
/${packageName} check string
"
1236 elif [ "$c" -gt 0 ]; then
1237 if [ "$c" -gt 1 ]; then
1238 echo "Found
$c matches
for '$string' in '$outputFile':"
1240 echo "Found
1 match
for '$string' in '$outputFile':"
1242 case "$targetDNS" in
1244 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1246 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1248 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1250 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1252 grep "$string" "$outputFile" | sed 's|^local-zone: "||
;s|
" static$||;';;
1255 echo "The
$string is not found
in current block-list
('$outputFile').
"
1264 for i
in $blocked_domains_urls; do
1265 [ "${i//melmac}" != "$i" ] && continue
1266 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1267 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1268 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1269 echo "# block-list too big for most routers"
1270 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1271 echo "# block-list may be too big for some routers"
1274 echo " list blocked_domains_url '$i'"
1277 echo "# site was down on last check"
1278 echo "# list blocked_domains_url '$i'"
1283 for i
in $blocked_hosts_urls; do
1284 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1285 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1286 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1287 echo "# block-list too big for most routers"
1288 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1289 echo "# block-list may be too big for some routers"
1292 echo " list blocked_hosts_url '$i'"
1295 echo "# site was down on last check"
1296 echo "# list blocked_hosts_url '$i'"