simple-adblock: dnsmasq.ipset option support, better handling of IDNs, updated README
[feed/packages.git] / net / simple-adblock / files / simple-adblock.init
1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
3 # shellcheck disable=SC2039
4 # shellcheck disable=SC1091
5 PKG_VERSION=
6
7 export START=94
8 export USE_PROCD=1
9 export LC_ALL=C
10
11 export EXTRA_COMMANDS='check dl killcache status'
12 export EXTRA_HELP=' check Checks if specified domain is found in current blacklist
13 dl Force-redownloads all the list
14 status Shows the service last-run status'
15
16 readonly packageName='simple-adblock'
17 readonly serviceName="$packageName $PKG_VERSION"
18 readonly addnhostsFile="/var/run/${packageName}.addnhosts"
19 readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
20 readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
21 readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
22 readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
23 readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
24 readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
25 readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
26 readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
27 readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
28 readonly ipsetCache="/var/run/${packageName}.ipset.cache"
29 readonly ipsetGzip="/etc/${packageName}.ipset.gz"
30 readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
31 readonly serversFile="/var/run/${packageName}.servers"
32 readonly serversCache="/var/run/${packageName}.servers.cache"
33 readonly serversGzip="/etc/${packageName}.servers.gz"
34 readonly serversOutputFilter='s|^|server=/|;s|$|/|'
35 readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
36 readonly unboundCache="/var/run/${packageName}.unbound.cache"
37 readonly unboundGzip="/etc/${packageName}.unbound.gz"
38 readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
39 readonly A_TMP="/var/${packageName}.hosts.a.tmp"
40 readonly B_TMP="/var/${packageName}.hosts.b.tmp"
41 readonly PIDFile="/var/run/${packageName}.pid"
42 readonly jsonFile="/var/run/${packageName}.json"
43 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;'
44 readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
45 readonly checkmark='\xe2\x9c\x93'
46 readonly xmark='\xe2\x9c\x97'
47 readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
48 readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
49 readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
50 readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
51 readonly _ERROR_='\033[0;31mERROR\033[0m'
52 readonly statusSuccess='Success'
53 readonly statusFail='Fail'
54 readonly statusDownloading='Downloading'
55 readonly statusReloading='Reloading'
56 readonly statusRestarting='Restarting'
57 readonly statusStarting='Starting'
58 readonly statusForceReloading='Force-Reloading'
59 readonly statusProcessing='Processing'
60 readonly statusStopped='Stopped'
61 readonly sharedMemoryError="/dev/shm/$packageName-error"
62 readonly sharedMemoryOutput="/dev/shm/$packageName-output"
63
64 create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
65 remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
66 trap remove_lock EXIT
67 output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
68 output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
69 output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
70 output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
71 str_replace() { echo "$1" | sed -e "s/$2/$3/g"; }
72 str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
73 compare_versions() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
74 is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
75 is_ipset_procd() { compare_versions "$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
76 led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
77 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
78 dnsmasq_kill() { killall -q -HUP dnsmasq; }
79 dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
80 unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
81
82 output() {
83 # Can take a single parameter (text) to be output at any verbosity
84 # Or target verbosity level and text to be output at specifc verbosity
85 local msg memmsg
86 if [ $# -ne 1 ]; then
87 if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
88 fi
89 [ -t 1 ] && printf "%b" "$1"
90 msg="$(printf "%s" "$(str_replace "$1" "$serviceName " "service ")" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g')";
91 if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
92 [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
93 logger -t "${packageName:-service} [$$]" "$(printf "%b" "${memmsg}${msg}")"
94 rm -f "$sharedMemoryOutput"
95 else
96 printf "%b" "$msg" >> "$sharedMemoryOutput"
97 fi
98 }
99
100 export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache
101 export targetDNS bootDelay dlTimeout curlRetry verbosity led dnsInstance
102 export whitelist_domains blacklist_domains
103 export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
104 export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
105 export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
106
107 load_package_config() {
108 config_load "$packageName"
109 config_get_bool serviceEnabled 'config' 'enabled' 1
110 config_get_bool forceDNS 'config' 'force_dns' 1
111 config_get_bool parallelDL 'config' 'parallel_downloads' 1
112 config_get_bool debug 'config' 'debug' 0
113 config_get_bool compressedCache 'config' 'compressed_cache' 0
114 config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
115 config_get bootDelay 'config' 'boot_delay' '120'
116 config_get dlTimeout 'config' 'download_timeout' '20'
117 config_get curlRetry 'config' 'curl_retry' '3'
118 config_get verbosity 'config' 'verbosity' '2'
119 config_get led 'config' 'led'
120 config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
121 config_get dnsInstance 'config' 'dns_instance' '0'
122 config_get whitelist_domains 'config' 'whitelist_domain'
123 config_get blacklist_domains 'config' 'blacklist_domain'
124 config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
125 config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
126 config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
127
128 if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
129 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
130 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
131 targetDNS='dnsmasq.servers'
132 fi
133
134 case "$targetDNS" in
135 dnsmasq.addnhosts)
136 outputFilter="$addnhostsOutputFilter"
137 outputFile="$addnhostsFile"
138 outputCache="$addnhostsCache"
139 outputGzip="$addnhostsGzip"
140 [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
141 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
142 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
143 rm -f "$serversFile" "$serversCache" "$serversGzip"
144 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
145 ;;
146 dnsmasq.conf)
147 outputFilter="$dnsmasqOutputFilter"
148 outputFile="$dnsmasqFile"
149 outputCache="$dnsmasqCache"
150 outputGzip="$dnsmasqGzip"
151 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
152 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
153 rm -f "$serversFile" "$serversCache" "$serversGzip"
154 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
155 ;;
156 dnsmasq.ipset)
157 outputFilter="$ipsetOutputFilter"
158 outputFile="$ipsetFile"
159 outputCache="$ipsetCache"
160 outputGzip="$ipsetGzip"
161 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
162 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
163 rm -f "$serversFile" "$serversCache" "$serversGzip"
164 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
165 ;;
166 dnsmasq.servers)
167 outputFilter="$serversOutputFilter"
168 outputFile="$serversFile"
169 outputCache="$serversCache"
170 outputGzip="$serversGzip"
171 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
172 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
173 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
174 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
175 ;;
176 unbound.adb_list)
177 outputFilter="$unboundOutputFilter"
178 outputFile="$unboundFile"
179 outputCache="$unboundCache"
180 outputGzip="$unboundGzip"
181 rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
182 rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
183 rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
184 rm -f "$serversFile" "$serversCache" "$serversGzip"
185 ;;
186 esac
187 if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
188 verbosity=1
189 fi
190 . /lib/functions/network.sh
191 . /usr/share/libubox/jshn.sh
192 # Prefer curl because it supports the file: scheme.
193 if [ -x /usr/bin/curl ] ; then
194 dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
195 dl_flag="-o"
196 else
197 dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
198 dl_flag="-O"
199 fi
200 led="${led:+/sys/class/leds/$led}"
201 }
202
203 is_enabled() {
204 load_package_config
205
206 if [ "$debug" -ne 0 ]; then
207 exec 1>>/tmp/simple-adblock.log
208 exec 2>&1
209 set -x
210 fi
211
212 if [ "$serviceEnabled" -eq 0 ]; then
213 case "$1" in
214 on_start)
215 output "$packageName is currently disabled.\\n"
216 output "Run the following commands before starting service again:\\n"
217 output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
218 ;;
219 esac
220 return 1
221 fi
222
223 case $targetDNS in
224 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
225 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
226 allowIDN=0
227 else
228 allowIDN=1
229 fi
230 ;;
231 unbound.adb_list)
232 allowIDN=1;;
233 esac
234
235 case $targetDNS in
236 dnsmasq.ipset)
237 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
238 output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
239 targetDNS='dnsmasq.servers'
240 fi
241 if ! ipset help hash:net >/dev/null 2>&1; then
242 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"
243 targetDNS='dnsmasq.servers'
244 fi
245 ;;
246 esac
247
248 [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
249 [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
250 [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
251 cacheOps 'testGzip' && return 0
252 network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
253 [ -n "$wan_gw" ] && return 0
254 output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
255 }
256
257 reload_resolver() {
258 local param output_text
259 case $1 in
260 on_start)
261 if [ ! -s "$outputFile" ]; then
262 tmpfs set status "$statusFail"
263 tmpfs add error "Error: Failed to create $outputFile file."
264 output "$_ERROR_: $serviceName failed to create its data file!\\n"
265 return 1
266 fi
267 case "$targetDNS" in
268 dnsmasq.addnhosts)
269 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
270 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
271 fi
272 if ! uci -q get dhcp.@dnsmasq["$dnsInstance"].addnhosts | grep -q "$addnhostsFile"; then
273 uci add_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
274 fi
275 param=dnsmasq_kill
276 output_text='Reloading DNSMASQ'
277 ;;
278 dnsmasq.conf)
279 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
280 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
281 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
282 fi
283 param=dnsmasq_restart
284 output_text='Restarting DNSMASQ'
285 ;;
286 dnsmasq.ipset)
287 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
288 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
289 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
290 fi
291 param=dnsmasq_restart
292 output_text='Restarting DNSMASQ'
293 ;;
294 dnsmasq.servers)
295 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
296 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" != "$serversFile" ]; then
297 uci set dhcp.@dnsmasq["$dnsInstance"].serversfile="$serversFile"
298 fi
299 param=dnsmasq_kill
300 output_text='Reloading DNSMASQ'
301 ;;
302 unbound.adb_list)
303 uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
304 if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
305 uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
306 fi
307 param=unbound_restart
308 output_text='Restarting Unbound'
309 ;;
310 esac
311 if [ -n "$(uci changes dhcp)" ]; then
312 uci commit dhcp
313 if [ "$param" = 'unbound_restart' ]; then
314 param='dnsmasq_restart; unbound_restart;'
315 output_text='Restarting Unbound/DNSMASQ'
316 else
317 param=dnsmasq_restart
318 output_text='Restarting DNSMASQ'
319 fi
320 fi
321 output 1 "$output_text "
322 output 2 "$output_text "
323 tmpfs set message "$output_text"
324 if eval "$param"; then
325 tmpfs set status "$statusSuccess"
326 led_on "$led"
327 output_okn
328 else
329 output_fail
330 tmpfs set status "$statusFail"
331 tmpfs add error "Error: $output_text error."
332 output "$_ERROR_: $serviceName $output_text error!\\n"
333 return 1
334 fi
335 ;;
336 on_stop)
337 cacheOps 'create'
338 case "$targetDNS" in
339 dnsmasq.addnhosts | dnsmasq.servers)
340 param=dnsmasq_kill
341 ;;
342 dnsmasq.conf | dnsmasq.ipset)
343 param=dnsmasq_restart
344 ;;
345 unbound.adb_list)
346 param=unbound_restart
347 ;;
348 esac
349 if [ -n "$(uci changes dhcp)" ]; then
350 uci -q commit dhcp
351 if [ "$param" = 'unbound_restart' ]; then
352 param='dnsmasq_restart; unbound_restart;'
353 else
354 param=dnsmasq_restart
355 fi
356 fi
357 eval "$param"
358 return $?
359 ;;
360 quiet)
361 case "$targetDNS" in
362 dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
363 param=dnsmasq_restart
364 ;;
365 unbound.adb_list)
366 param=unbound_restart
367 ;;
368 esac
369 eval "$param"
370 return $?
371 ;;
372 esac
373 }
374
375 tmpfs() {
376 local action="$1" instance="$2" value="$3"
377 local status message error stats
378 local readReload readRestart curReload curRestart ret
379 if [ -s "$jsonFile" ]; then
380 status="$(jsonfilter -i $jsonFile -l1 -e "@['data']['status']")"
381 message="$(jsonfilter -i $jsonFile -l1 -e "@['data']['message']")"
382 error="$(jsonfilter -i $jsonFile -l1 -e "@['data']['error']")"
383 stats="$(jsonfilter -i $jsonFile -l1 -e "@['data']['stats']")"
384 readReload="$(jsonfilter -i $jsonFile -l1 -e "@['data']['reload']")"
385 readRestart="$(jsonfilter -i $jsonFile -l1 -e "@['data']['restart']")"
386 fi
387 case "$action" in
388 get)
389 case "$instance" in
390 status)
391 echo "$status"; return;;
392 message)
393 echo "$message"; return;;
394 error)
395 echo "$error"; return;;
396 stats)
397 echo "$stats"; return;;
398 triggers)
399 curReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
400 curRestart="$compressedCache $forceDNS $led"
401 if [ "$curReload" != "$readReload" ]; then
402 ret='download'
403 elif [ "$curRestart" != "$readRestart" ]; then
404 ret='restart'
405 fi
406 echo "$ret"
407 return;;
408 esac
409 ;;
410 add)
411 case "$instance" in
412 status)
413 [ -n "$status" ] && status="$status $value" || status="$value";;
414 message)
415 [ -n "$message" ] && message="${message} ${value}" || message="$value";;
416 error)
417 [ -n "$error" ] && error="$error $value" || error="$value";;
418 stats)
419 [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
420 esac
421 ;;
422 del)
423 case "$instance" in
424 all)
425 unset status;
426 unset message;
427 unset error;
428 unset stats;
429 ;;
430 status)
431 unset status;;
432 message)
433 unset message;;
434 error)
435 unset error;;
436 stats)
437 unset stats;;
438 triggers)
439 unset readReload; unset readRestart;;
440 esac
441 ;;
442 set)
443 case "$instance" in
444 status)
445 status="$value";;
446 message)
447 message="$value";;
448 error)
449 error="$value";;
450 stats)
451 stats="$value";;
452 triggers)
453 readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
454 readRestart="$compressedCache $forceDNS $led"
455 ;;
456 esac
457 ;;
458 esac
459 json_init
460 json_add_object 'data'
461 json_add_string version "$PKG_VERSION"
462 json_add_string status "$status"
463 json_add_string message "$message"
464 json_add_string error "$error"
465 json_add_string stats "$stats"
466 json_add_string reload "$readReload"
467 json_add_string restart "$readRestart"
468 json_close_object
469 json_dump > "$jsonFile"
470 sync
471 }
472
473 cacheOps() {
474 local R_TMP
475 case "$1" in
476 create|backup)
477 [ -f "$outputFile" ] && mv "$outputFile" "$outputCache" >/dev/null 2>/dev/null
478 return $?
479 ;;
480 restore|use)
481 [ -f "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
482 return $?
483 ;;
484 test)
485 [ -s "$outputCache" ]
486 return $?
487 ;;
488 testGzip)
489 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
490 return $?
491 ;;
492 createGzip)
493 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
494 if gzip < "$outputFile" > "$R_TMP"; then
495 if mv "$R_TMP" "$outputGzip"; then
496 rm -f "$R_TMP"
497 return 0
498 else
499 rm -f "$R_TMP"
500 return 1
501 fi
502 else
503 return 1
504 fi
505 ;;
506 expand|unpack|expandGzip|unpackGzip)
507 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
508 return $?
509 ;;
510 esac
511 }
512
513 fw3Ops() {
514 local action="$1" param="$2" _restart
515 case "$action" in
516 reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
517 restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
518 remove)
519 case "$param" in
520 dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
521 ipset) uci -q del firewall.simple_adblock_ipset
522 uci -q del firewall.simple_adblock_ipset_rule;;
523 *)
524 uci -q del firewall.simple_adblock_dns_redirect
525 uci -q del firewall.simple_adblock_ipset
526 uci -q del firewall.simple_adblock_ipset_rule
527 ;;
528 esac
529 ;;
530 insert)
531 case "$param" in
532 dns_redirect)
533 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
534 uci -q set firewall.simple_adblock_dns_redirect=redirect
535 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
536 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
537 uci -q set firewall.simple_adblock_dns_redirect.src=lan
538 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
539 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
540 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
541 fi
542 ;;
543 ipset)
544 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
545 uci -q set firewall.simple_adblock_ipset=ipset
546 uci -q set firewall.simple_adblock_ipset.name=adb
547 uci -q set firewall.simple_adblock_ipset.match=dest_net
548 uci -q set firewall.simple_adblock_ipset.storage=hash
549 uci -q set firewall.simple_adblock_ipset.enabled=1
550 _restart=1
551 fi
552 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
553 uci -q set firewall.simple_adblock_ipset_rule=rule
554 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
555 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
556 uci -q set firewall.simple_adblock_ipset_rule.src=lan
557 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
558 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
559 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
560 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
561 fi
562 ;;
563 *)
564 if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
565 uci -q set firewall.simple_adblock_dns_redirect=redirect
566 uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
567 uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
568 uci -q set firewall.simple_adblock_dns_redirect.src=lan
569 uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
570 uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
571 uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
572 fi
573 if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
574 uci -q set firewall.simple_adblock_ipset=ipset
575 uci -q set firewall.simple_adblock_ipset.name=adb
576 uci -q set firewall.simple_adblock_ipset.match=dest_net
577 uci -q set firewall.simple_adblock_ipset.storage=hash
578 uci -q set firewall.simple_adblock_ipset.enabled=1
579 _restart=1
580 fi
581 if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
582 uci -q set firewall.simple_adblock_ipset_rule=rule
583 uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
584 uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
585 uci -q set firewall.simple_adblock_ipset_rule.src=lan
586 uci -q set firewall.simple_adblock_ipset_rule.dest='*'
587 uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
588 uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
589 uci -q set firewall.simple_adblock_ipset_rule.enabled=1
590 fi
591 ;;
592 esac
593 esac
594 if [ -n "$(uci changes firewall)" ]; then
595 uci -q commit firewall
596 if [ -z "$_restart" ]; then
597 fw3Ops 'reload'
598 else
599 fw3Ops 'restart'
600 fi
601 fi
602 }
603
604 process_url() {
605 local label type D_TMP R_TMP
606 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
607 label="${1##*//}"; label="${label%%/*}";
608 if [ "$2" = 'hosts' ]; then
609 label="Hosts: $label"; filter="$hostsFilter";
610 else
611 label="Domains: $label"; filter="$domainsFilter";
612 fi
613 if [ "$3" = 'blocked' ]; then
614 type='Blocked'; D_TMP="$B_TMP";
615 else
616 type='Allowed'; D_TMP="$A_TMP";
617 fi
618 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
619 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
620 done
621 if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
622 output 1 "$_FAIL_"
623 output 2 "[DL] $type $label $__FAIL__\\n"
624 echo "Error: downloading '${1}'." >> "$sharedMemoryError"
625 else
626 sed -i "$filter" "$R_TMP"
627 if [ ! -s "$R_TMP" ]; then
628 output 1 "$_FAIL_"
629 output 2 "[DL] $type $label $__FAIL__\\n"
630 echo "Error: parsing '${1}'." >> "$sharedMemoryError"
631 else
632 cat "${R_TMP}" >> "$D_TMP"
633 output 1 "$_OK_"
634 output 2 "[DL] $type $label $__OK__\\n"
635 fi
636 fi
637 rm -f "$R_TMP"
638 return 0
639 }
640
641 download_lists() {
642 local hf w_filter j=0 R_TMP
643
644 tmpfs set message "${statusDownloading}..."
645 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
646 if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
647 output 3 'Low free memory, restarting resolver... '
648 if reload_resolver 'quiet'; then
649 output_okn
650 else
651 output_fail
652 fi
653 fi
654 touch $A_TMP; touch $B_TMP;
655 output 1 'Downloading lists '
656 rm -f "$sharedMemoryError"
657 if [ -n "$blacklist_hosts_urls" ]; then
658 for hf in ${blacklist_hosts_urls}; do
659 if [ "$parallelDL" -gt 0 ]; then
660 process_url "$hf" 'hosts' 'blocked' &
661 else
662 process_url "$hf" 'hosts' 'blocked'
663 fi
664 done
665 fi
666 if [ -n "$blacklist_domains_urls" ]; then
667 for hf in ${blacklist_domains_urls}; do
668 if [ "$parallelDL" -gt 0 ]; then
669 process_url "$hf" 'domains' 'blocked' &
670 else
671 process_url "$hf" 'domains' 'blocked'
672 fi
673 done
674 fi
675 if [ -n "$whitelist_domains_urls" ]; then
676 for hf in ${whitelist_domains_urls}; do
677 if [ "$parallelDL" -gt 0 ]; then
678 process_url "$hf" 'domains' 'allowed' &
679 else
680 process_url "$hf" 'domains' 'allowed'
681 fi
682 done
683 fi
684 wait
685 [ -s "$sharedMemoryError" ] && tmpfs add error "$(cat "$sharedMemoryError")"
686 rm -f "$sharedMemoryError"
687 output 1 '\n'
688
689 [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
690 whitelist_domains="${whitelist_domains}
691 $(cat $A_TMP)"
692 [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
693
694 [ ! -s "$B_TMP" ] && return 1
695
696 output 1 'Processing downloads '
697 output 2 'Sorting combined list '
698 tmpfs set message "$statusProcessing: sorting combined list"
699 if [ "$allowIDN" -gt 0 ]; then
700 if sort -u "$B_TMP" > "$A_TMP"; then
701 output_ok
702 else
703 output_failn
704 tmpfs add error 'Error: Sorting error.'
705 fi
706 else
707 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
708 output_ok
709 else
710 output_failn
711 tmpfs add error 'Error: Sorting error.'
712 fi
713 fi
714
715 if [ "$targetDNS" = 'dnsmasq.conf' ] || \
716 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
717 [ "$targetDNS" = 'dnsmasq.servers' ] || \
718 [ "$targetDNS" = 'unbound.adb_list' ]; then
719 # TLD optimization written by Dirk Brenken (dev@brenken.org)
720 output 2 'Optimizing combined list '
721 tmpfs set message "$statusProcessing: optimizing combined list"
722 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
723 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
724 if sort "$B_TMP" > "$A_TMP"; then
725 if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
726 if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
727 if sort -u "$A_TMP" > "$B_TMP"; then
728 output_ok
729 else
730 output_failn
731 tmpfs add error 'Error: Data file optimization.'
732 mv "$A_TMP" "$B_TMP"
733 fi
734 else
735 output_failn
736 tmpfs add error 'Error: Data file optimization.'
737 fi
738 else
739 output_failn
740 tmpfs add error 'Error: Data file optimization.'
741 mv "$A_TMP" "$B_TMP"
742 fi
743 else
744 output_failn
745 tmpfs add error 'Error: Data file optimization.'
746 fi
747 else
748 output_failn
749 tmpfs add error 'Error: Data file optimization.'
750 mv "$A_TMP" "$B_TMP"
751 fi
752 else
753 mv "$A_TMP" "$B_TMP"
754 fi
755
756 output 2 'Whitelisting domains '
757 tmpfs set message "$statusProcessing: whitelisting domains"
758 if sed -i "$w_filter" "$B_TMP"; then
759 output_ok
760 else
761 output_failn
762 tmpfs add error 'Error: Whitelist processing.'
763 fi
764
765 output 2 'Formatting merged file '
766 tmpfs set message "$statusProcessing: formatting merged file"
767 if [ -z "$outputFilterIPv6" ]; then
768 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
769 output_ok
770 else
771 output_failn
772 tmpfs add error 'Error: Data file formatting.'
773 fi
774 else
775 case "$targetDNS" in
776 dnsmasq.addnhosts)
777 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
778 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
779 output_ok
780 else
781 output_failn
782 tmpfs add error 'Error: Data file formatting.'
783 fi
784 ;;
785 esac
786 fi
787
788 case "$targetDNS" in
789 dnsmasq.addnhosts)
790 output 2 'Creating DNSMASQ addnhosts file '
791 tmpfs set message "$statusProcessing: creating DNSMASQ addnhosts file"
792 ;;
793 dnsmasq.conf)
794 output 2 'Creating DNSMASQ config file '
795 tmpfs set message "$statusProcessing: creating DNSMASQ config file"
796 ;;
797 dnsmasq.ipset)
798 output 2 'Creating DNSMASQ ipset file '
799 tmpfs set message "$statusProcessing: creating DNSMASQ ipset file"
800 ;;
801 dnsmasq.servers)
802 output 2 'Creating DNSMASQ servers file '
803 tmpfs set message "$statusProcessing: creating DNSMASQ servers file"
804 ;;
805 unbound.adb_list)
806 output 2 'Creating Unbound adb_list file '
807 tmpfs set message "$statusProcessing: creating Unbound adb_list file"
808 ;;
809 esac
810 if mv "$A_TMP" "$outputFile"; then
811 output_ok
812 else
813 output_failn
814 tmpfs add error "Error: moving data file '${A_TMP}' to '${outputFile}'."
815 fi
816 if [ "$compressedCache" -gt 0 ]; then
817 output 2 'Creating compressed cache '
818 tmpfs set message "$statusProcessing: creating compressed cache"
819 if cacheOps 'createGzip'; then
820 output_ok
821 else
822 output_failn
823 tmpfs add error 'Error: creating compressed cache.'
824 fi
825 else
826 rm -f "$outputGzip"
827 fi
828 output 2 'Removing temporary files '
829 tmpfs set message "$statusProcessing: removing temporary files"
830 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
831 if [ $j -eq 0 ]; then
832 output_ok
833 else
834 output_failn
835 tmpfs add error 'Error: removing temporary files.'
836 fi
837 output 1 '\n'
838 }
839
840 boot() {
841 load_package_config
842 if create_lock; then
843 sleep "$bootDelay"
844 remove_lock
845 rc_procd start_service && rc_procd service_triggers
846 fi
847 }
848
849 start_service() {
850 is_enabled 'on_start' || return 1
851 local action status error message stats
852 if create_lock; then
853 if is_chaos_calmer || ! is_ipset_procd; then
854 if [ "$forceDNS" -ne 0 ]; then
855 fw3Ops 'insert' 'dns_redirect'
856 else
857 fw3Ops 'remove' 'dns_redirect'
858 fi
859 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
860 fw3Ops 'insert' 'ipset'
861 else
862 fw3Ops 'remove' 'ipset'
863 fi
864 procd_open_instance 'main'
865 procd_set_param command /bin/true
866 procd_set_param stdout 1
867 procd_set_param stderr 1
868 procd_close_instance
869 else
870 procd_open_instance 'main'
871 procd_set_param command /bin/true
872 procd_set_param stdout 1
873 procd_set_param stderr 1
874 procd_open_data
875 json_add_array firewall
876 if [ "$forceDNS" -ne 0 ]; then
877 json_add_object ''
878 json_add_string type redirect
879 json_add_string name simple_adblock_dns_redirect
880 json_add_string target DNAT
881 json_add_string src lan
882 json_add_string proto tcpudp
883 json_add_string src_dport 53
884 json_add_string dest_port 53
885 json_add_string reflection 0
886 json_close_object
887 fi
888 if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
889 json_add_object ''
890 json_add_string type ipset
891 json_add_string name adb
892 json_add_string match dest_net
893 json_add_string storage hash
894 json_add_string enabled 1
895 json_close_object
896 json_add_object ''
897 json_add_string type rule
898 json_add_string name simple_adblock_ipset_rule
899 json_add_string ipset adb
900 json_add_string src lan
901 json_add_string dest '*'
902 json_add_string proto tcpudp
903 json_add_string target REJECT
904 json_add_string enabled 1
905 json_close_object
906 fi
907 json_close_array
908 procd_close_data
909 procd_close_instance
910 fi
911
912 status="$(tmpfs get status)"
913 error="$(tmpfs get error)"
914 message="$(tmpfs get message)"
915 stats="$(tmpfs get stats)"
916 action="$(tmpfs get triggers)"
917
918 case "$1" in
919 download) action='download';;
920 restart|*)
921 if [ -s "$outputFile" ] && [ -n "$status" ]; then
922 status
923 exit 0
924 elif [ ! -s "$outputFile" ] && ! cacheOps 'test' && ! cacheOps 'testGzip'; then
925 action='download'
926 elif cacheOps 'test' || cacheOps 'testGzip'; then
927 action='start'
928 fi
929 if [ -n "$error" ]; then
930 action='download'
931 fi
932 action="${action:-$1}"
933 ;;
934 esac
935
936 tmpfs del all
937 tmpfs set triggers
938
939 case $action in
940 download)
941 if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
942 output 0 "Force-reloading $serviceName... "
943 output 3 "Force-reloading $serviceName...\\n"
944 tmpfs set status "$statusForceReloading"
945 else
946 output 0 "Starting $serviceName... "
947 output 3 "Starting $serviceName...\\n"
948 tmpfs set status "$statusStarting"
949 fi
950 download_lists
951 reload_resolver 'on_start'
952 ;;
953 restart|start)
954 if [ "$action" = 'restart' ]; then
955 output 0 "Restarting $serviceName... "
956 output 3 "Restarting $serviceName...\\n"
957 tmpfs set status "$statusRestarting"
958 else
959 output 0 "Starting $serviceName... "
960 output 3 "Starting $serviceName...\\n"
961 tmpfs set status "$statusStarting"
962 fi
963 if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
964 output 3 'Found compressed cache file, unpacking it '
965 tmpfs set message 'found compressed cache file, unpacking it.'
966 if cacheOps 'unpackGzip'; then
967 output_okn
968 else
969 output_fail
970 output "$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
971 tmpfs add error 'Error: Failed to unpack compressed cache.'
972 return 1
973 fi
974 fi
975 if cacheOps 'test' && [ ! -s "$outputFile" ]; then
976 output 3 'Found cache file, reusing it '
977 tmpfs set message 'found cache file, reusing it.'
978 if cacheOps 'restore'; then
979 output_okn
980 else
981 output_fail
982 tmpfs add error "Error: moving '$outputCache' to '$outputFile'."
983 fi
984 fi
985 reload_resolver 'on_start'
986 ;;
987 esac
988
989 if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "$statusFail" ]; then
990 output 0 "$__OK__\\n";
991 c="$(wc -l < "$outputFile")"
992 output 3 "$serviceName is blocking $c domains (with ${targetDNS}) "; output_okn
993 tmpfs del message
994 tmpfs set status "$statusSuccess: $c domains blocked (with ${targetDNS})."
995 error="$(tmpfs get error)"
996 if [ -n "$error" ]; then
997 output "$(str_replace "$error" "Error:" "$_ERROR_:")\\n"
998 fi
999 else
1000 output 0 "$__FAIL__\\n";
1001 tmpfs set status "$statusFail"
1002 tmpfs add error 'Error: Failed to create blocklist.'
1003 fi
1004 remove_lock
1005 else
1006 output 3 "$serviceName: another instance is starting up "; output_fail
1007 return 0
1008 fi
1009 }
1010
1011 service_started() { is_ipset_procd && procd_set_config_changed firewall; }
1012 service_stopped() { is_ipset_procd && procd_set_config_changed firewall; }
1013 restart_service() { rc_procd start_service 'restart'; }
1014 reload_service() { restart_service; }
1015 restart() { restart_service; }
1016 reload() { restart_service; }
1017 dl() { rc_procd start_service 'download'; }
1018
1019 killcache() {
1020 rm -f "$addnhostsCache" "$addnhostsGzip"
1021 rm -f "$dnsmasqCache" "$dnsmasqGzip"
1022 rm -f "$ipsetCache" "$ipsetGzip"
1023 rm -f "$serversCache" "$serversGzip"
1024 rm -f "$unboundCache" "$unboundGzip"
1025 return 0
1026 }
1027
1028 status() {
1029 local status="$(tmpfs get status)" error="$(tmpfs get error)" message="$(tmpfs get message)"
1030 if [ -n "$status" ] && [ -n "$message" ]; then
1031 status="${status}: $message"
1032 fi
1033 [ -n "$status" ] && output "$serviceName $status\\n"
1034 [ -n "$error" ] && output "$error\\n"
1035 }
1036
1037 stop_service() {
1038 load_package_config
1039 fw3Ops 'remove' 'all'
1040 if [ -s "$outputFile" ]; then
1041 output "Stopping $serviceName... "
1042 tmpfs del triggers
1043 if reload_resolver 'on_stop'; then
1044 led_off "$led"
1045 output 0 "$__OK__\\n"; output_okn
1046 tmpfs set status "$statusStopped"
1047 tmpfs del message
1048 else
1049 output 0 "$__FAIL__\\n"; output_fail
1050 tmpfs set status "$statusFail"
1051 tmpfs add error "Error: error stopping $serviceName."
1052 output "$_ERROR_: error stopping $serviceName!\\n"
1053 fi
1054 fi
1055 }
1056
1057 check() {
1058 load_package_config
1059 local string="$1"
1060 local c="$(grep -c "$string" "$outputFile")"
1061 if [ ! -f "$outputFile" ]; then
1062 echo "No blacklist ('$outputFile') found."
1063 elif [ -z "$string" ]; then
1064 echo "Usage: /etc/init.d/${packageName} check string"
1065 elif [ "$c" -gt 0 ]; then
1066 if [ "$c" -gt 1 ]; then
1067 echo "Found $c matches for '$string' in '$outputFile':"
1068 else
1069 echo "Found 1 match for '$string' in '$outputFile':"
1070 fi
1071 case "$targetDNS" in
1072 dnsmasq.addnhosts)
1073 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1074 dnsmasq.conf)
1075 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1076 dnsmasq.ipset)
1077 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1078 dnsmasq.servers)
1079 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1080 unbound.adb_list)
1081 grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
1082 esac
1083 else
1084 echo "The $string is not found in current blacklist ('$outputFile')."
1085 fi
1086 }