1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039
4 # shellcheck disable=SC1091
11 export EXTRA_COMMANDS
='check dl killcache sizes show'
12 export EXTRA_HELP
=' check Checks if specified domain is found in current blacklist
13 dl Force-downloads all enabled block-list
14 sizes Displays the file-sizes of enabled block-lists
15 show Shows the service last-run status'
17 readonly packageName
='simple-adblock'
18 readonly serviceName
="$packageName $PKG_VERSION"
19 readonly addnhostsFile
="/var/run/${packageName}.addnhosts"
20 readonly addnhostsCache
="/var/run/${packageName}.addnhosts.cache"
21 readonly addnhostsGzip
="/etc/${packageName}.addnhosts.gz"
22 readonly addnhostsOutputFilter
='s|^|127.0.0.1 |;s|$||'
23 readonly addnhostsOutputFilterIPv6
='s|^|:: |;s|$||'
24 readonly dnsmasqFile
="/var/dnsmasq.d/${packageName}"
25 readonly dnsmasqCache
="/var/run/${packageName}.dnsmasq.cache"
26 readonly dnsmasqGzip
="/etc/${packageName}.dnsmasq.gz"
27 readonly dnsmasqOutputFilter
='s|^|local=/|;s|$|/|'
28 readonly ipsetFile
="/var/dnsmasq.d/${packageName}.ipset"
29 readonly ipsetCache
="/var/run/${packageName}.ipset.cache"
30 readonly ipsetGzip
="/etc/${packageName}.ipset.gz"
31 readonly ipsetOutputFilter
='s|^|ipset=/|;s|$|/adb|'
32 readonly serversFile
="/var/run/${packageName}.servers"
33 readonly serversCache
="/var/run/${packageName}.servers.cache"
34 readonly serversGzip
="/etc/${packageName}.servers.gz"
35 readonly serversOutputFilter
='s|^|server=/|;s|$|/|'
36 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
37 readonly unboundCache
="/var/run/${packageName}.unbound.cache"
38 readonly unboundGzip
="/etc/${packageName}.unbound.gz"
39 readonly unboundOutputFilter
='s|^|local-zone: "|;s|$|" static|'
40 readonly A_TMP
="/var/${packageName}.hosts.a.tmp"
41 readonly B_TMP
="/var/${packageName}.hosts.b.tmp"
42 readonly PIDFile
="/var/run/${packageName}.pid"
43 readonly jsonFile
="/var/run/${packageName}.json"
44 readonly sharedMemoryError
="/dev/shm/$packageName-error"
45 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
46 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;'
47 readonly domainsFilter
='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
48 readonly checkmark
='\xe2\x9c\x93'
49 readonly xmark
='\xe2\x9c\x97'
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'
56 readonly messageSuccess
='Success'
57 readonly messageFail
='Fail'
58 readonly messageDownloading
='Downloading'
59 readonly messageReloading
='Reloading'
60 readonly messageRestarting
='Restarting'
61 readonly messageStarting
='Starting'
62 readonly messageForceReloading
='Force-Reloading'
63 readonly messageProcessing
='Processing'
64 readonly messageStopped
='Stopped'
68 statusNoInstall
) _ret
="$serviceName is not installed or not found";;
69 statusStopped
) _ret
="Stopped";;
70 statusStarting
) _ret
="Starting";;
71 statusRestarting
) _ret
="Restarting";;
72 statusForceReloading
) _ret
="Force Reloading";;
73 statusDownloading
) _ret
="Downloading";;
74 statusError
) _ret
="Error";;
75 statusWarning
) _ret
="Warning";;
76 statusFail
) _ret
="Fail";;
77 statusSuccess
) _ret
="Success";;
84 errorOutputFileCreate
) _ret
="failed to create $outputFile file";;
85 errorFailDNSReload
) _ret
="failed to restart/reload DNS resolver";;
86 errorSharedMemory
) _ret
="failed to access shared memory";;
87 errorSorting
) _ret
="failed to sort data file";;
88 errorOptimization
) _ret
="failed to optimize data file";;
89 errorWhitelistProcessing
) _ret
="failed to process whitelist";;
90 errorDataFileFormatting
) _ret
="failed to format data file";;
91 errorMovingDataFile
) _ret
="failed to move data file '${A_TMP}' to '${outputFile}'";;
92 errorCreatingCompressedCache
) _ret
="failed to create compressed cache";;
93 errorRemovingTempFiles
) _ret
="failed to remove temporary files";;
94 errorRestoreCompressedCache
) _ret
="failed to unpack compressed cache";;
95 errorRestoreCache
) _ret
="failed to move '$outputCache' to '$outputFile'";;
96 errorOhSnap
) _ret
="failed to create blocklist or restart DNS resolver";;
97 errorStopping
) _ret
="failed to stop $serviceName";;
98 errorDNSReload
) _ret
="failed to reload/restart DNS resolver";;
99 errorDownloadingList
) _ret
="failed to download";;
100 errorParsingList
) _ret
="failed to parse";;
105 create_lock
() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
106 remove_lock
() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
107 trap remove_lock EXIT
108 output_ok
() { output
1 "$_OK_"; output
2 "$__OK__\\n"; }
109 output_okn
() { output
1 "$_OK_\\n"; output
2 "$__OK__\\n"; }
110 output_fail
() { output
1 "$_FAIL_"; output
2 "$__FAIL__\\n"; }
111 output_failn
() { output
1 "$_FAIL_\\n"; output
2 "$__FAIL__\\n"; }
112 # str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
113 # str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
114 compare_versions
() { test "$(printf '%s\n' "$@
" | sort -V | head -n 1)" != "$1"; }
115 is_chaos_calmer
() { ubus
-S call system board |
grep -q 'Chaos Calmer'; }
116 is_ipset_procd
() { compare_versions
"$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
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; }
125 # Can take a single parameter (text) to be output at any verbosity
126 # Or target verbosity level and text to be output at specifc verbosity
128 if [ $# -ne 1 ]; then
129 if [ $
((verbosity
& $1)) -gt 0 ] ||
[ "$verbosity" = "$1" ]; then shift; else return 0; fi
131 [ -t 1 ] && printf "%b" "$1"
132 msg
="${1//$serviceName /service }";
133 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
134 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
135 logger
-t "${packageName:-service} [$$]" "$(printf "%b" "${memmsg}${msg}")"
136 rm -f "$sharedMemoryOutput"
138 printf "%b
" "$msg" >> "$sharedMemoryOutput"
142 export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache
143 export targetDNS bootDelay dlTimeout curlRetry verbosity=1 led dnsInstance
144 export whitelist_domains blacklist_domains
145 export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
146 export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
147 export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
149 load_package_config() {
150 config_load "$packageName"
151 config_get_bool serviceEnabled 'config' 'enabled' 1
152 config_get_bool forceDNS 'config' 'force_dns' 1
153 config_get_bool parallelDL 'config' 'parallel_downloads' 1
154 config_get_bool debug 'config' 'debug' 0
155 config_get_bool compressedCache 'config' 'compressed_cache' 0
156 config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
157 config_get bootDelay 'config' 'boot_delay' '120'
158 config_get dlTimeout 'config' 'download_timeout' '20'
159 config_get curlRetry 'config' 'curl_retry' '3'
160 config_get verbosity 'config' 'verbosity' '2'
161 config_get led 'config' 'led'
162 config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
163 config_get dnsInstance 'config' 'dns_instance' '0'
164 config_get whitelist_domains 'config' 'whitelist_domain'
165 config_get blacklist_domains 'config' 'blacklist_domain'
166 config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
167 config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
168 config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
170 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
171 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
172 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
173 targetDNS='dnsmasq.servers'
178 outputFilter="$addnhostsOutputFilter"
179 outputFile="$addnhostsFile"
180 outputCache="$addnhostsCache"
181 outputGzip="$addnhostsGzip"
182 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
183 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
184 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
185 rm -f "$serversFile" "$serversCache" "$serversGzip"
186 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
189 outputFilter="$dnsmasqOutputFilter"
190 outputFile="$dnsmasqFile"
191 outputCache="$dnsmasqCache"
192 outputGzip="$dnsmasqGzip"
193 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
194 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
195 rm -f "$serversFile" "$serversCache" "$serversGzip"
196 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
199 outputFilter="$ipsetOutputFilter"
200 outputFile="$ipsetFile"
201 outputCache="$ipsetCache"
202 outputGzip="$ipsetGzip"
203 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
204 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
205 rm -f "$serversFile" "$serversCache" "$serversGzip"
206 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
209 outputFilter="$serversOutputFilter"
210 outputFile="$serversFile"
211 outputCache="$serversCache"
212 outputGzip="$serversGzip"
213 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
214 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
215 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
216 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
219 outputFilter="$unboundOutputFilter"
220 outputFile="$unboundFile"
221 outputCache="$unboundCache"
222 outputGzip="$unboundGzip"
223 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
224 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
225 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
226 rm -f "$serversFile" "$serversCache" "$serversGzip"
229 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
232 . /lib/functions/network.sh
233 . /usr/share/libubox/jshn.sh
234 # Prefer curl because it supports the file: scheme.
235 if [ -x /usr/bin/curl ]; then
236 dl_command="curl
--insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
238 elif wget -V 2>/dev/null | grep -q "+ssl
"; then
239 dl_command="wget
--no-check-certificate --timeout $dlTimeout -q"
242 dl_command="uclient-fetch
--no-check-certificate --timeout $dlTimeout -q"
245 led="${led:+/sys/class/leds/$led}"
251 if [ "$debug" -ne 0 ]; then
252 exec 1>>/tmp/simple-adblock.log
257 if [ "$serviceEnabled" -eq 0 ]; then
260 output "$packageName is currently disabled.
\\n
"
261 output "Run the following commands before starting service again
:\\n
"
262 output "uci
set ${packageName}.config.enabled
='1'; uci commit
$packageName;\\n
"
269 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
270 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
282 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
283 output "$_ERROR_: DNSMASQ ipset support is enabled
in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets
!\\n
"
284 targetDNS='dnsmasq.servers'
286 if ! ipset help hash:net >/dev/null 2>&1; then
287 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
"
288 targetDNS='dnsmasq.servers'
293 [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
294 [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
295 [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
296 cacheOps 'testGzip' && return 0
297 network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
298 [ -n "$wan_gw" ] && return 0
299 output "$_ERROR_: $serviceName failed to discover WAN gateway.
\\n
"; return 1;
303 local cfg="$1" param="$2"
306 if [ "$
(uci
-q get dhcp.
"$cfg".serversfile
)" = "$serversFile" ]; then
307 uci -q del dhcp."$cfg".serversfile
309 if ! uci -q get dhcp."$cfg".addnhosts | grep -q "$addnhostsFile"; then
310 uci add_list dhcp."$cfg".addnhosts="$addnhostsFile"
313 dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup)
314 uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
315 if [ "$
(uci
-q get dhcp.
"$cfg".serversfile
)" = "$serversFile" ]; then
316 uci -q del dhcp."$cfg".serversfile
320 uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
321 if [ "$
(uci
-q get dhcp.
"$cfg".serversfile
)" != "$serversFile" ]; then
322 uci set dhcp."$cfg".serversfile="$serversFile"
329 local param output_text i
332 if [ ! -s "$outputFile" ]; then
333 tmpfs set status "statusFail
"
334 tmpfs add error "errorOutputFileCreate
"
335 output "$_ERROR_: $
(getErrorText
'errorOutputFileCreate')!\\n
"
340 if [ "$dnsInstance" = "*" ]; then
341 config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
342 elif [ -n "$dnsInstance" ]; then
343 for i in $dnsInstance; do
344 dnsmasqOps "@dnsmasq
[$i]" "$targetDNS"
349 dnsmasq.addnhosts|dnsmasq.servers)
351 output_text='Reloading DNSMASQ'
353 dnsmasq.conf|dnsmasq.ipset)
354 param=dnsmasq_restart
355 output_text='Restarting DNSMASQ'
358 param=unbound_restart
359 output_text='Restarting Unbound'
363 if [ -n "$
(uci changes dhcp
)" ]; then
365 if [ "$param" = 'unbound_restart' ]; then
366 param='dnsmasq_restart; unbound_restart;'
367 output_text='Restarting Unbound/DNSMASQ'
369 param=dnsmasq_restart
370 output_text='Restarting DNSMASQ'
373 output 1 "$output_text "
374 output 2 "$output_text "
375 tmpfs set message "$output_text"
376 if eval "$param"; then
377 tmpfs set status "statusSuccess
"
382 tmpfs set status "statusFail
"
383 tmpfs add error "errorDNSReload
"
384 output "$_ERROR_: $
(getErrorText
'errorDNSReload')!\\n
"
390 dnsmasq.addnhosts | dnsmasq.servers)
393 dnsmasq.conf | dnsmasq.ipset)
394 param=dnsmasq_restart
397 param=unbound_restart
400 if [ -n "$
(uci changes dhcp
)" ]; then
402 if [ "$param" = 'unbound_restart' ]; then
403 param='dnsmasq_restart; unbound_restart;'
405 param=dnsmasq_restart
413 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
414 param=dnsmasq_restart
417 param=unbound_restart
427 local action="$1" instance="$2" value="$3"
428 local status message error stats
429 local readReload readRestart curReload curRestart ret
430 if [ -s "$jsonFile" ]; then
431 status="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['status']")"
432 message="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['message']")"
433 error="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['error']")"
434 stats="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['stats']")"
435 readReload="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['reload']")"
436 readRestart="$
(jsonfilter
-i $jsonFile -l1 -e "@['data']['restart']")"
442 printf "%b
" "$status"; return;;
444 printf "%b
" "$message"; return;;
446 printf "%b
" "$error"; return;;
448 printf "%b
" "$stats"; return;;
450 curReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
451 curRestart="$compressedCache $forceDNS $led"
452 if [ "$curReload" != "$readReload" ]; then
454 elif [ "$curRestart" != "$readRestart" ]; then
464 [ -n "$status" ] && status="$status $value" || status="$value";;
466 [ -n "$message" ] && message="$message $value" || message="$value";;
468 [ -n "$error" ] && error="$error $value" || error="$value";;
470 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
490 unset readReload; unset readRestart;;
504 readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
505 readRestart="$compressedCache $forceDNS $led"
511 json_add_object 'data'
512 json_add_string version "$PKG_VERSION"
513 json_add_string status "$status"
514 json_add_string message "$message"
515 json_add_string error "$error"
516 json_add_string stats "$stats"
517 json_add_string reload "$readReload"
518 json_add_string restart "$readRestart"
520 json_dump > "$jsonFile"
528 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
532 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
536 [ -s "$outputCache" ]
540 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
544 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
545 if gzip < "$outputFile" > "$R_TMP"; then
546 if mv "$R_TMP" "$outputGzip"; then
557 expand|unpack|expandGzip|unpackGzip)
558 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
565 local action="$1" param="$2" _restart
567 reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
568 restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
571 dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
572 ipset) uci -q del firewall.simple_adblock_ipset
573 uci -q del firewall.simple_adblock_ipset_rule;;
575 uci -q del firewall.simple_adblock_dns_redirect
576 uci -q del firewall.simple_adblock_ipset
577 uci -q del firewall.simple_adblock_ipset_rule
584 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
585 uci -q set firewall.simple_adblock_dns_redirect=redirect
586 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
587 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
588 uci -q set firewall.simple_adblock_dns_redirect.src=lan
589 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
590 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
591 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
595 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
596 uci -q set firewall.simple_adblock_ipset=ipset
597 uci -q set firewall.simple_adblock_ipset.name=adb
598 uci -q set firewall.simple_adblock_ipset.match=dest_net
599 uci -q set firewall.simple_adblock_ipset.storage=hash
600 uci -q set firewall.simple_adblock_ipset.enabled=1
603 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
604 uci -q set firewall.simple_adblock_ipset_rule=rule
605 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
606 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
607 uci -q set firewall.simple_adblock_ipset_rule.src=lan
608 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
609 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
610 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
611 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
615 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
616 uci -q set firewall.simple_adblock_dns_redirect=redirect
617 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
618 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
619 uci -q set firewall.simple_adblock_dns_redirect.src=lan
620 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
621 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
622 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
624 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
625 uci -q set firewall.simple_adblock_ipset=ipset
626 uci -q set firewall.simple_adblock_ipset.name=adb
627 uci -q set firewall.simple_adblock_ipset.match=dest_net
628 uci -q set firewall.simple_adblock_ipset.storage=hash
629 uci -q set firewall.simple_adblock_ipset.enabled=1
632 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
633 uci -q set firewall.simple_adblock_ipset_rule=rule
634 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
635 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
636 uci -q set firewall.simple_adblock_ipset_rule.src=lan
637 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
638 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
639 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
640 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
645 if [ -n "$
(uci changes firewall
)" ]; then
646 uci -q commit firewall
647 if [ -z "$_restart" ]; then
656 local label type D_TMP R_TMP
657 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
658 label="${1##*//}"; label="${label%%/*}";
659 if [ "$2" = 'hosts' ]; then
660 label="Hosts
: $label"; filter="$hostsFilter";
662 label="Domains
: $label"; filter="$domainsFilter";
664 if [ "$3" = 'blocked' ]; then
665 type='Blocked'; D_TMP="$B_TMP";
667 type='Allowed'; D_TMP="$A_TMP";
669 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
670 R_TMP="$
(mktemp
-u -q -t ${packageName}_tmp.XXXXXXXX
)"
672 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
674 output 2 "[DL
] $type $label $__FAIL__\\n
"
675 echo "errorDownloadingList
=${1}" >> "$sharedMemoryError"
677 sed -i "$filter" "$R_TMP"
678 if [ ! -s "$R_TMP" ]; then
680 output 2 "[DL
] $type $label $__FAIL__\\n
"
681 echo "errorParsingList
=${1}" >> "$sharedMemoryError"
683 cat "${R_TMP}" >> "$D_TMP"
685 output 2 "[DL
] $type $label $__OK__\\n
"
693 local hf w_filter j=0 R_TMP
695 tmpfs set message "${messageDownloading}...
"
696 tmpfs set status "statusDownloading
"
698 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
699 if [ "$
(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
700 output 3 'Low free memory, restarting resolver... '
701 if dnsOps 'quiet'; then
707 touch $A_TMP; touch $B_TMP;
708 output 1 'Downloading lists '
709 rm -f "$sharedMemoryError"
710 if [ -n "$blacklist_hosts_urls" ]; then
711 for hf in ${blacklist_hosts_urls}; do
712 if [ "$parallelDL" -gt 0 ]; then
713 process_url "$hf" 'hosts' 'blocked' &
715 process_url "$hf" 'hosts' 'blocked'
719 if [ -n "$blacklist_domains_urls" ]; then
720 for hf in ${blacklist_domains_urls}; do
721 if [ "$parallelDL" -gt 0 ]; then
722 process_url "$hf" 'domains' 'blocked' &
724 process_url "$hf" 'domains' 'blocked'
728 if [ -n "$whitelist_domains_urls" ]; then
729 for hf in ${whitelist_domains_urls}; do
730 if [ "$parallelDL" -gt 0 ]; then
731 process_url "$hf" 'domains' 'allowed' &
733 process_url "$hf" 'domains' 'allowed'
739 if [ -s "$sharedMemoryError" ]; then
740 while IFS= read -r line; do
741 tmpfs add error "$line"
742 done < "$sharedMemoryError"
743 rm -f "$sharedMemoryError"
746 [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
747 whitelist_domains="${whitelist_domains}
749 [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
751 [ ! -s "$B_TMP" ] && return 1
753 output
1 'Processing downloads '
754 output
2 'Sorting combined list '
755 tmpfs
set message
"$messageProcessing: sorting combined list"
756 if [ "$allowIDN" -gt 0 ]; then
757 if sort -u "$B_TMP" > "$A_TMP"; then
761 tmpfs add error
"errorSorting"
764 if sort -u "$B_TMP" |
grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
768 tmpfs add error
"errorSorting"
772 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
773 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
774 [ "$targetDNS" = 'dnsmasq.servers' ] || \
775 [ "$targetDNS" = 'unbound.adb_list' ]; then
776 # TLD optimization written by Dirk Brenken (dev@brenken.org)
777 output
2 'Optimizing combined list '
778 tmpfs
set message
"$messageProcessing: optimizing combined list"
779 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
780 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
781 if sort "$B_TMP" > "$A_TMP"; then
782 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
783 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
784 if sort -u "$A_TMP" > "$B_TMP"; then
788 tmpfs add error
"errorOptimization"
793 tmpfs add error
"errorOptimization"
797 tmpfs add error
"errorOptimization"
802 tmpfs add error
"errorOptimization"
806 tmpfs add error
"errorOptimization"
813 output
2 'Whitelisting domains '
814 tmpfs
set message
"$messageProcessing: whitelisting domains"
815 if sed -i "$w_filter" "$B_TMP"; then
819 tmpfs add error
"errorWhitelistProcessing"
822 output
2 'Formatting merged file '
823 tmpfs
set message
"$messageProcessing: formatting merged file"
824 if [ -z "$outputFilterIPv6" ]; then
825 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
829 tmpfs add error
"errorDataFileFormatting"
834 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
835 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
839 tmpfs add error
"errorDataFileFormatting"
847 output
2 'Creating DNSMASQ addnhosts file '
848 tmpfs
set message
"$messageProcessing: creating DNSMASQ addnhosts file"
851 output
2 'Creating DNSMASQ config file '
852 tmpfs
set message
"$messageProcessing: creating DNSMASQ config file"
855 output
2 'Creating DNSMASQ ipset file '
856 tmpfs
set message
"$messageProcessing: creating DNSMASQ ipset file"
859 output
2 'Creating DNSMASQ servers file '
860 tmpfs
set message
"$messageProcessing: creating DNSMASQ servers file"
863 output
2 'Creating Unbound adb_list file '
864 tmpfs
set message
"$messageProcessing: creating Unbound adb_list file"
867 if mv "$A_TMP" "$outputFile"; then
871 tmpfs add error
"errorMovingDataFile"
873 if [ "$compressedCache" -gt 0 ]; then
874 output
2 'Creating compressed cache '
875 tmpfs
set message
"$messageProcessing: creating compressed cache"
876 if cacheOps
'createGzip'; then
880 tmpfs add error
"errorCreatingCompressedCache"
885 output
2 'Removing temporary files '
886 tmpfs
set message
"$messageProcessing: removing temporary files"
887 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j
=1
888 if [ $j -eq 0 ]; then
892 tmpfs add error
"errorRemovingTempFiles"
902 rc_procd start_service
&& rc_procd service_triggers
907 is_enabled
'on_start' ||
return 1
908 local action status error message stats c
909 if ! create_lock
; then
910 output
3 "$serviceName: another instance is starting up "; output_fail
914 status
="$(tmpfs get status)"
915 error
="$(tmpfs get error)"
916 message
="$(tmpfs get message)"
917 stats
="$(tmpfs get stats)"
918 action
="$(tmpfs get triggers)"
920 if [ "$action" = 'download' ] ||
[ "$1" = 'download' ] ||
[ -n "$error" ]; then
922 elif [ ! -s "$outputFile" ] && ! cacheOps
'test' && ! cacheOps
'testGzip'; then
924 elif [ ! -s "$outputFile" ] && cacheOps
'testGzip' || cacheOps
'test'; then
926 elif [ "$action" = 'restart' ] ||
[ "$1" = 'restart' ]; then
928 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
929 [ "$1" != 'hotplug' ] && showstatus
938 if is_chaos_calmer ||
! is_ipset_procd
; then
939 if [ "$forceDNS" -ne 0 ]; then
940 fw3Ops
'insert' 'dns_redirect'
942 fw3Ops
'remove' 'dns_redirect'
944 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
945 fw3Ops
'insert' 'ipset'
947 fw3Ops
'remove' 'ipset'
949 procd_open_instance
'main'
950 procd_set_param
command /bin
/true
951 procd_set_param stdout
1
952 procd_set_param stderr
1
955 procd_open_instance
'main'
956 procd_set_param
command /bin
/true
957 procd_set_param stdout
1
958 procd_set_param stderr
1
960 json_add_array firewall
961 if [ "$forceDNS" -ne 0 ]; then
963 json_add_string
type redirect
964 json_add_string name simple_adblock_dns_redirect
965 json_add_string target DNAT
966 json_add_string src lan
967 json_add_string proto tcpudp
968 json_add_string src_dport
53
969 json_add_string dest_port
53
970 json_add_string reflection
0
973 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
975 json_add_string
type ipset
976 json_add_string name adb
977 json_add_string match dest_net
978 json_add_string storage
hash
979 json_add_string enabled
1
982 json_add_string
type rule
983 json_add_string name simple_adblock_ipset_rule
984 json_add_string ipset adb
985 json_add_string src lan
986 json_add_string dest
'*'
987 json_add_string proto tcpudp
988 json_add_string target REJECT
989 json_add_string enabled
1
997 if [ "$action" = 'restore' ]; then
998 output
0 "Starting $serviceName... "
999 output
3 "Starting $serviceName...\\n"
1000 tmpfs
set status
"statusStarting"
1001 if cacheOps
'testGzip' && ! cacheOps
'test' && [ ! -s "$outputFile" ]; then
1002 output
3 'Found compressed cache file, unpacking it '
1003 tmpfs
set message
'found compressed cache file, unpacking it.'
1004 if cacheOps
'unpackGzip'; then
1008 tmpfs add error
"errorRestoreCompressedCache"
1009 output
"$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
1013 if cacheOps
'test' && [ ! -s "$outputFile" ]; then
1014 output
3 'Found cache file, reusing it '
1015 tmpfs
set message
'found cache file, reusing it.'
1016 if cacheOps
'restore'; then
1021 tmpfs add error
"errorRestoreCache"
1022 output
"$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
1029 if [ -s "$outputFile" ] || cacheOps
'test' || cacheOps
'testGzip'; then
1030 output
0 "Force-reloading $serviceName... "
1031 output
3 "Force-reloading $serviceName...\\n"
1032 tmpfs
set status
"statusForceReloading"
1034 output
0 "Starting $serviceName... "
1035 output
3 "Starting $serviceName...\\n"
1036 tmpfs
set status
"statusStarting"
1042 output
0 "Restarting $serviceName... "
1043 output
3 "Restarting $serviceName...\\n"
1044 tmpfs
set status
"statusRestarting"
1048 output
0 "Starting $serviceName... "
1049 output
3 "Starting $serviceName...\\n"
1050 tmpfs
set status
"statusStarting"
1054 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
1055 output
0 "$__OK__\\n";
1057 tmpfs
set status
"statusSuccess"
1058 c
="$(wc -l < "$outputFile")"
1059 tmpfs
set stats
"$serviceName is blocking $c domains (with ${targetDNS})"
1062 output
0 "$__FAIL__\\n";
1063 tmpfs
set status
"statusFail"
1064 tmpfs add error
"errorOhSnap"
1070 service_started
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1071 service_stopped
() { is_ipset_procd
&& procd_set_config_changed firewall
; }
1072 restart_service
() { rc_procd start_service
'restart'; }
1073 reload_service
() { restart_service
; }
1074 restart
() { restart_service
; }
1075 reload
() { restart_service
; }
1076 dl
() { rc_procd start_service
'download'; }
1078 rm -f "$addnhostsCache" "$addnhostsGzip"
1079 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1080 rm -f "$ipsetCache" "$ipsetGzip"
1081 rm -f "$serversCache" "$serversGzip"
1082 rm -f "$unboundCache" "$unboundGzip"
1084 config_foreach dnsmasqOps
'dnsmasq' 'cleanup'
1085 uci
-q commit
'dhcp'
1089 show
() { showstatus
; }
1090 status_service
() { showstatus
; }
1092 local status
="$(tmpfs get status)"
1093 local message
="$(tmpfs get message)"
1094 local error
="$(tmpfs get error)"
1095 local stats
="$(tmpfs get stats)"
1097 if [ "$status" = "statusSuccess" ]; then
1098 output
"$stats "; output_okn
;
1100 [ -n "$status" ] && status
="$(getStatusText "$status")"
1101 if [ -n "$status" ] && [ -n "$message" ]; then
1102 status
="${status}: $message"
1104 [ -n "$status" ] && output
"$serviceName $status\\n"
1106 if [ -n "$error" ]; then
1111 errorDownloadingList|errorParsingList
)
1112 output
"$_ERROR_: $(getErrorText "$c") $url!\\n";;
1114 output
"$_ERROR_: $(getErrorText "$c")!\\n";;
1123 fw3Ops
'remove' 'all'
1124 if [ -s "$outputFile" ]; then
1125 output
"Stopping $serviceName... "
1127 if dnsOps
'on_stop'; then
1129 output
0 "$__OK__\\n"; output_okn
;
1130 tmpfs
set status
"statusStopped"
1133 output
0 "$__FAIL__\\n"; output_fail
;
1134 tmpfs
set status
"statusFail"
1135 tmpfs add error
"errorStopping"
1136 output
"$_ERROR_: $(getErrorText 'errorStopping')!\\n"
1141 service_triggers
() {
1142 procd_add_reload_trigger
'simple-adblock'
1148 local c
="$(grep -c "$string" "$outputFile")"
1149 if [ ! -s "$outputFile" ]; then
1150 echo "No blacklist ('$outputFile') found."
1151 elif [ -z "$string" ]; then
1152 echo "Usage: /etc/init.d/${packageName} check string"
1153 elif [ "$c" -gt 0 ]; then
1154 if [ "$c" -gt 1 ]; then
1155 echo "Found $c matches for '$string' in '$outputFile':"
1157 echo "Found 1 match for '$string' in '$outputFile':"
1159 case "$targetDNS" in
1161 grep "$string" "$outputFile" |
sed 's|^127.0.0.1 ||;s|^:: ||;';;
1163 grep "$string" "$outputFile" |
sed 's|local=/||;s|/$||;';;
1165 grep "$string" "$outputFile" |
sed 's|ipset=/||;s|/adb$||;';;
1167 grep "$string" "$outputFile" |
sed 's|server=/||;s|/$||;';;
1169 grep "$string" "$outputFile" |
sed 's|^local-zone: "||;s|" static$||;';;
1172 echo "The $string is not found in current blacklist ('$outputFile')."
1181 for i
in $blacklist_domains_urls; do
1182 [ "${i//melmac}" != "$i" ] && continue
1183 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1184 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1185 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1186 echo "# blocklist too big for most routers"
1187 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1188 echo "# blocklist may be too big for some routers"
1191 echo " list blacklist_domains_url '$i'"
1194 echo "# site was down on last check"
1195 echo "# list blacklist_domains_url '$i'"
1200 for i
in $blacklist_hosts_urls; do
1201 if $dl_command "$i" $dl_flag /tmp
/sast
2>/dev
/null
&& [ -s /tmp
/sast
]; then
1202 echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
1203 if compare_versions
"$(du -sk /tmp/sast)" "500"; then
1204 echo "# blocklist too big for most routers"
1205 elif compare_versions
"$(du -sk /tmp/sast)" "100"; then
1206 echo "# blocklist may be too big for some routers"
1209 echo " list blacklist_hosts_url '$i'"
1212 echo "# site was down on last check"
1213 echo "# list blacklist_hosts_url '$i'"