Merge pull request #20261 from pprindeville/strongswan-update-5.9.9
[feed/packages.git] / net / simple-adblock / files / simple-adblock.init
1 #!/bin/sh /etc/rc.common
2 # Copyright 2017-2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2016,SC3043,SC3057,SC3060
4
5 # shellcheck disable=SC2034
6 START=94
7 # shellcheck disable=SC2034
8 USE_PROCD=1
9 LC_ALL=C
10
11 if type extra_command 1>/dev/null 2>&1; then
12 extra_command 'allow' 'Allows domain in current block-list and config'
13 extra_command 'check' 'Checks if specified domain is found in current block-list'
14 extra_command 'dl' 'Force-downloads all enabled block-list'
15 extra_command 'killcache' 'Delete all cached files'
16 extra_command 'sizes' 'Displays the file-sizes of enabled block-lists'
17 extra_command 'version' 'Show version information'
18 else
19 # shellcheck disable=SC2034
20 EXTRA_COMMANDS='allow check dl killcache sizes status_service version'
21 # shellcheck disable=SC2034
22 EXTRA_HELP=' allow Allows domain(s) in current block-list and config
23 check Checks if specified domain is found in current block-list
24 dl Force-downloads all enabled block-list
25 sizes Displays the file-sizes of enabled block-lists'
26 fi
27
28 readonly PKG_VERSION='dev-test'
29 readonly packageName='simple-adblock'
30 readonly serviceName="$packageName $PKG_VERSION"
31 readonly packageConfigFile="/etc/config/${packageName}"
32 readonly dnsmasqAddnhostsFile="/var/run/${packageName}/dnsmasq.addnhosts"
33 readonly dnsmasqAddnhostsCache="/var/run/${packageName}/dnsmasq.addnhosts.cache"
34 readonly dnsmasqAddnhostsGzip="/etc/${packageName}.dnsmasq.addnhosts.gz"
35 readonly dnsmasqAddnhostsFilter='s|^|127.0.0.1 |;s|$||'
36 readonly dnsmasqAddnhostsFilterIPv6='s|^|:: |;s|$||'
37 readonly dnsmasqConfFile="/tmp/dnsmasq.d/${packageName}"
38 readonly dnsmasqConfCache="/var/run/${packageName}/dnsmasq.conf.cache"
39 readonly dnsmasqConfGzip="/etc/${packageName}.dnsmasq.conf.gz"
40 readonly dnsmasqConfFilter='s|^|local=/|;s|$|/|'
41 readonly dnsmasqIpsetFile="/tmp/dnsmasq.d/${packageName}.ipset"
42 readonly dnsmasqIpsetCache="/var/run/${packageName}/dnsmasq.ipset.cache"
43 readonly dnsmasqIpsetGzip="/etc/${packageName}.dnsmasq.ipset.gz"
44 readonly dnsmasqIpsetFilter='s|^|ipset=/|;s|$|/adb|'
45 readonly dnsmasqNftsetFile="/tmp/dnsmasq.d/${packageName}.nftset"
46 readonly dnsmasqNftsetCache="/var/run/${packageName}/dnsmasq.nftset.cache"
47 readonly dnsmasqNftsetGzip="/etc/${packageName}.dnsmasq.nftset.gz"
48 readonly dnsmasqNftsetFilter='s|^|nftset=/|;s|$|/4#inet#fw4#adb4|'
49 readonly dnsmasqNftsetFilterIPv6='s|^|nftset=/|;s|$|/4#inet#fw4#adb4,6#inet#fw4#adb6|'
50 readonly dnsmasqServersFile="/var/run/${packageName}/dnsmasq.servers"
51 readonly dnsmasqServersCache="/var/run/${packageName}/dnsmasq.servers.cache"
52 readonly dnsmasqServersGzip="/etc/${packageName}.dnsmasq.servers.gz"
53 readonly dnsmasqServersFilter='s|^|server=/|;s|$|/|'
54 readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
55 readonly unboundCache="/var/run/${packageName}/unbound.cache"
56 readonly unboundGzip="/etc/${packageName}.unbound.gz"
57 readonly unboundFilter='s|^|local-zone: "|;s|$|" static|'
58 readonly A_TMP="/var/${packageName}.hosts.a.tmp"
59 readonly B_TMP="/var/${packageName}.hosts.b.tmp"
60 readonly jsonFile="/var/run/${packageName}/${packageName}.json"
61 readonly sharedMemoryError="/dev/shm/$packageName-error"
62 readonly sharedMemoryOutput="/dev/shm/$packageName-output"
63 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;'
64 readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
65 readonly adBlockPlusFilter='/^#/d;/^!/d;s/[[:space:]]*#.*$//;s/^||//;s/\^$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
66 readonly dnsmasqFileFilter='\|^server=/[[:alnum:]_.-].*/|!d'
67 readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
68 readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
69 readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
70 readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
71 readonly _ERROR_='\033[0;31mERROR\033[0m'
72 readonly _WARNING_='\033[0;33mWARNING\033[0m'
73 # shellcheck disable=SC2155
74 readonly ipset="$(command -v ipset)"
75 # shellcheck disable=SC2155
76 readonly nft="$(command -v nft)"
77 readonly canaryDomainsMozilla='use-application-dns.net'
78 readonly canaryDomainsiCloud='mask.icloud.com mask-h2.icloud.com'
79
80 dl_command=
81 dl_flag=
82 isSSLSupported=
83 outputFilter=
84 outputFilterIPv6=
85 outputFile=
86 outputGzip=
87 outputCache=
88 awk='awk'
89 load_environment_flag=
90
91 debug() { local i j; for i in "$@"; do eval "j=\$$i"; echo "${i}: ${j} "; done; }
92
93 uci_add_list_if_new() {
94 local PACKAGE="$1"
95 local CONFIG="$2"
96 local OPTION="$3"
97 local VALUE="$4"
98 local i
99 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
100 for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
101 [ "$i" = "$VALUE" ] && return 0
102 done
103 uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
104 }
105 uci_changes() {
106 local PACKAGE="$1"
107 local CONFIG="$2"
108 local OPTION="$3"
109 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
110 }
111
112 ipset() { "$ipset" "$@" >/dev/null 2>&1; }
113 nft() { "$nft" "$@" >/dev/null 2>&1; }
114
115 get_text() {
116 local r
117 case "$1" in
118 errorConfigValidationFail) r="$packageName config validation failed";;
119 errorServiceDisabled) r="$packageName is currently disabled";;
120 errorNoDnsmasqIpset)
121 r="dnsmasq ipset support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support ipset";;
122 errorNoIpset)
123 r="dnsmasq ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type";;
124 errorNoDnsmasqNftset)
125 r="dnsmasq nft set support is enabled in $packageName, but dnsmasq is either not installed or installed dnsmasq does not support nft set";;
126 errorNoNft) r="dnsmasq nft sets support is enabled in $packageName, but nft is not installed";;
127 errorNoWanGateway) r="The ${serviceName} failed to discover WAN gateway";;
128 errorOutputDirCreate) r="failed to create directory for %s file";;
129 errorOutputFileCreate) r="failed to create $outputFile file";;
130 errorFailDNSReload) r="failed to restart/reload DNS resolver";;
131 errorSharedMemory) r="failed to access shared memory";;
132 errorSorting) r="failed to sort data file";;
133 errorOptimization) r="failed to optimize data file";;
134 errorAllowListProcessing) r="failed to process allow-list";;
135 errorDataFileFormatting) r="failed to format data file";;
136 errorMovingDataFile) r="failed to move data file '${A_TMP}' to '${outputFile}'";;
137 errorCreatingCompressedCache) r="failed to create compressed cache";;
138 errorRemovingTempFiles) r="failed to remove temporary files";;
139 errorRestoreCompressedCache) r="failed to unpack compressed cache";;
140 errorRestoreCache) r="failed to move '$outputCache' to '$outputFile'";;
141 errorOhSnap) r="failed to create block-list or restart DNS resolver";;
142 errorStopping) r="failed to stop $serviceName";;
143 errorDNSReload) r="failed to reload/restart DNS resolver";;
144 errorDownloadingConfigUpdate) r="failed to download Config Update file";;
145 errorDownloadingList) r="failed to download";;
146 errorParsingConfigUpdate) r="failed to parse Config Update file";;
147 errorParsingList) r="failed to parse";;
148 errorNoSSLSupport) r="no HTTPS/SSL support on device";;
149 errorCreatingDirectory) r="failed to create output/cache/gzip file directory";;
150
151 statusNoInstall) r="$serviceName is not installed or not found";;
152 statusStopped) r="Stopped";;
153 statusStarting) r="Starting";;
154 statusRestarting) r="Restarting";;
155 statusForceReloading) r="Force Reloading";;
156 statusDownloading) r="Downloading";;
157 statusProcessing) r="Processing";;
158 statusError) r="Error";;
159 statusWarning) r="Warning";;
160 statusFail) r="Fail";;
161 statusSuccess) r="Success";;
162
163 warningExternalDnsmasqConfig)
164 r="use of external dnsmasq config file detected, please set 'dns' option to 'dnsmasq.conf'";;
165 warningMissingRecommendedPackages) r="Some recommended packages are missing";;
166 esac
167 echo "$r"
168 }
169
170 output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
171 output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
172 output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
173 output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
174 str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
175 str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
176 is_greater() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
177 is_greater_or_equal() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" = "$2"; }
178 is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
179 led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
180 led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
181 dnsmasq_hup() { killall -q -s HUP dnsmasq; }
182 dnsmasq_kill() { killall -q -s KILL dnsmasq; }
183 dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
184 unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
185 is_present() { command -v "$1" >/dev/null 2>&1; }
186
187 output() {
188 # Can take a single parameter (text) to be output at any verbosity
189 # Or target verbosity level and text to be output at specifc verbosity
190 local msg memmsg logmsg
191 verbosity="${verbosity:-2}"
192 if [ $# -ne 1 ]; then
193 if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
194 fi
195 [ -t 1 ] && printf "%b" "$1"
196 msg="${1//$serviceName /service }";
197 if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
198 [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
199 logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
200 logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
201 rm -f "$sharedMemoryOutput"
202 else
203 printf "%b" "$msg" >> "$sharedMemoryOutput"
204 fi
205 }
206
207 load_network() {
208 local param="$1"
209 local i j wan_if wan_gw wan_proto
210 local counter wan_if_timeout="$procd_boot_wan_timeout" wan_gw_timeout='5'
211 counter=0
212 while [ -z "$wan_if" ]; do
213 network_flush_cache
214 network_find_wan wan_if
215 if [ -n "$wan_if" ]; then
216 output "WAN interface found: '${wan_if}'.\\n"
217 break
218 fi
219 if [ "$counter" -gt "$wan_if_timeout" ]; then
220 output "WAN interface timeout, assuming 'wan'.\\n"
221 wan_if='wan'
222 break
223 fi
224 counter=$((counter+1))
225 output "Waiting to discover WAN Interface...\\n"
226 sleep 1
227 done
228
229 counter=0
230 wan_proto="$(uci -q get "network.${wan_if}.proto")"
231 if [ "$wan_proto" = 'pppoe' ]; then
232 wan_gw_timeout=$((wan_gw_timeout+10))
233 fi
234 while [ "$counter" -le "$wan_gw_timeout" ]; do
235 network_flush_cache
236 network_get_gateway wan_gw "$wan_if"
237 if [ -n "$wan_gw" ]; then
238 output "WAN gateway found: '${wan_gw}.'\\n"
239 return 0
240 fi
241 counter=$((counter+1))
242 output "Waiting to discover $wan_if gateway...\\n"
243 sleep 1
244 done
245 json add error "errorNoWanGateway"
246 output "${_ERROR_}: $(get_text 'errorNoWanGateway')!\\n"; return 1;
247 }
248
249 load_environment() {
250 local i j
251 local validation_result="$1" param="$2"
252
253 [ -z "$load_environment_flag" ] || return 0
254
255 if [ "$validation_result" != '0' ]; then
256 json add error "errorConfigValidationFail"
257 output "${_ERROR_}: $(get_text 'errorConfigValidationFail')!\\n"
258 output "Please check if the '$packageConfigFile' contains correct values for config options.\\n"
259 return 1
260 fi
261
262 if [ "$enabled" -eq 0 ]; then
263 json add error "errorServiceDisabled"
264 output "${_ERROR_}: $(get_text 'errorServiceDisabled')!\\n"
265 output "Run the following commands before starting service again:\\n"
266 output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
267 return 1
268 fi
269
270 if [ "$debug" -ne 0 ]; then
271 exec 1>>/tmp/simple-adblock.log
272 exec 2>&1
273 set -x
274 fi
275
276 if [ -n "$dnsmasq_config_file_url" ]; then
277 case "$dns" in
278 dnsmasq.conf) :;;
279 *)
280 if [ "$param" != 'quiet' ]; then
281 json add warning "warningExternalDnsmasqConfig"
282 output "${_WARNING_}: $(get_text 'warningExternalDnsmasqConfig')!\\n"
283 fi
284 ;;
285 esac
286 fi
287
288 case "$dns" in
289 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
290 if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
291 allow_non_ascii=0
292 fi
293 ;;
294 unbound.adb_list)
295 allow_non_ascii=1;;
296 esac
297
298 case "$dns" in
299 dnsmasq.ipset)
300 if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
301 if [ "$param" != 'quiet' ]; then
302 json add error "errorNoDnsmasqIpset"
303 output "${_ERROR_}: $(get_text 'errorNoDnsmasqIpset')!\\n"
304 fi
305 dns='dnsmasq.servers'
306 fi
307 if ! ipset help hash:net; then
308 if [ "$param" != 'quiet' ]; then
309 json add error "errorNoIpset"
310 output "${_ERROR_}: $(get_text 'errorNoIpset')!\\n"
311 fi
312 dns='dnsmasq.servers'
313 fi
314 ;;
315 dnsmasq.nftset)
316 if dnsmasq -v 2>/dev/null | grep -q 'no-nftset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'nftset'; then
317 if [ "$param" != 'quiet' ]; then
318 json add error "errorNoDnsmasqNftset"
319 output "${_ERROR_}: $(get_text 'errorNoDnsmasqNftset')!\\n"
320 fi
321 dns='dnsmasq.servers'
322 fi
323 if [ -z "$nft" ]; then
324 if [ "$param" != 'quiet' ]; then
325 json add error "errorNoNft"
326 output "${_ERROR_}: $(get_text 'errorNoNft')!\\n"
327 fi
328 dns='dnsmasq.servers'
329 fi
330 ;;
331 esac
332
333 case "$dns" in
334 dnsmasq.addnhosts)
335 outputFilter="$dnsmasqAddnhostsFilter"
336 outputFile="$dnsmasqAddnhostsFile"
337 outputCache="$dnsmasqAddnhostsCache"
338 outputGzip="$dnsmasqAddnhostsGzip"
339 if [ "$ipv6_enabled" -ne 0 ]; then
340 outputFilterIPv6="$dnsmasqAddnhostsFilterIPv6"
341 fi
342 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
343 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
344 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
345 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
346 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
347 ;;
348 dnsmasq.conf)
349 outputFilter="$dnsmasqConfFilter"
350 outputFile="$dnsmasqConfFile"
351 outputCache="$dnsmasqConfCache"
352 outputGzip="$dnsmasqConfGzip"
353 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
354 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
355 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
356 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
357 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
358 ;;
359 dnsmasq.ipset)
360 outputFilter="$dnsmasqIpsetFilter"
361 outputFile="$dnsmasqIpsetFile"
362 outputCache="$dnsmasqIpsetCache"
363 outputGzip="$dnsmasqIpsetGzip"
364 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
365 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
366 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
367 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
368 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
369 ;;
370 dnsmasq.nftset)
371 if [ "$ipv6_enabled" -ne 0 ]; then
372 outputFilter="$dnsmasqNftsetFilterIPv6"
373 else
374 outputFilter="$dnsmasqNftsetFilter"
375 fi
376 outputFile="$dnsmasqNftsetFile"
377 outputCache="$dnsmasqNftsetCache"
378 outputGzip="$dnsmasqNftsetGzip"
379 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
380 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
381 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
382 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
383 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
384 ;;
385 dnsmasq.servers)
386 outputFilter="$dnsmasqServersFilter"
387 outputFile="$dnsmasqServersFile"
388 outputCache="$dnsmasqServersCache"
389 outputGzip="$dnsmasqServersGzip"
390 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
391 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
392 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
393 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
394 rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
395 ;;
396 unbound.adb_list)
397 outputFilter="$unboundFilter"
398 outputFile="$unboundFile"
399 outputCache="$unboundCache"
400 outputGzip="$unboundGzip"
401 rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
402 rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
403 rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
404 rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
405 rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
406 ;;
407 esac
408
409 for i in "$jsonFile" "$outputFile" "$outputCache" "$outputGzip"; do
410 if ! mkdir -p "$(dirname "$i")"; then
411 if [ "$param" != 'quiet' ]; then
412 json add error "errorOutputDirCreate" "$i"
413 output "${_ERROR_}: $(get_text 'errorOutputDirCreate' "$i")!\\n"
414 fi
415 fi
416 done
417
418 is_present 'gawk' && awk='gawk'
419 if ! is_present '/usr/libexec/grep-gnu' || ! is_present '/usr/libexec/sed-gnu' || \
420 ! is_present '/usr/libexec/sort-coreutils' || ! is_present 'gawk'; then
421 local s="opkg update; opkg --force-overwrite install"
422 is_present 'gawk' || s="$s gawk"
423 is_present '/usr/libexec/grep-gnu' || s="$s grep"
424 is_present '/usr/libexec/sed-gnu' || s="$s sed"
425 is_present '/usr/libexec/sort-coreutils' || s="$s coreutils-sort"
426 if [ "$param" != 'quiet' ]; then
427 json add warning "warningMissingRecommendedPackages" "${i}"
428 output "${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\\n"
429 output "$s;\\n"
430 fi
431 fi
432 # Prefer curl because it supports the file:// scheme.
433 if is_present 'curl'; then
434 dl_command="curl --silent --insecure"
435 dl_command="${dl_command}${curl_max_file_size:+ --max-filesize $curl_max_file_size}"
436 dl_command="${dl_command}${curl_retry:+ --retry $curl_retry}"
437 dl_command="${dl_command}${download_timeout:+ --connect-timeout $download_timeout}"
438 dl_flag="-o"
439 elif is_present '/usr/libexec/wget-ssl'; then
440 dl_command="/usr/libexec/wget-ssl --no-check-certificate -q"
441 dl_command="${dl_command}${download_timeout:+ --timeout $download_timeout}"
442 dl_flag="-O"
443 elif is_present wget && wget --version 2>/dev/null | grep -q "+https"; then
444 dl_command="wget --no-check-certificate -q"
445 dl_command="${dl_command}${download_timeout:+ --timeout $download_timeout}"
446 dl_flag="-O"
447 else
448 dl_command="uclient-fetch --no-check-certificate -q"
449 dl_command="${dl_command}${download_timeout:+ --timeout $download_timeout}"
450 dl_flag="-O"
451 fi
452 led="${led:+/sys/class/leds/$led}"
453 if curl --version 2>/dev/null | grep -q "https" \
454 || wget --version 2>/dev/null | grep -q "+https" \
455 || grep -q "libustream-mbedtls" /usr/lib/opkg/status \
456 || grep -q "libustream-openssl" /usr/lib/opkg/status \
457 || grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
458 isSSLSupported=1
459 else
460 unset isSSLSupported
461 fi
462 load_environment_flag=1
463 cache 'test' && return 0
464 cache 'test_gzip' && return 0
465 if [ "$param" = 'on_boot' ]; then
466 load_network "$param"
467 return "$?"
468 else
469 return 0
470 fi
471 }
472
473 resolver() {
474 local cfg="$1" param="$2"
475 case "$param" in
476 dnsmasq.addnhosts)
477 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
478 uci_remove 'dhcp' "$cfg" 'serversfile'
479 fi
480 uci_add_list_if_new 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
481 ;;
482 cleanup|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list)
483 uci_remove_list 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
484 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
485 uci_remove 'dhcp' "$cfg" 'serversfile'
486 fi
487 ;;
488 dnsmasq.servers)
489 uci_remove_list 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
490 if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
491 uci_set 'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
492 fi
493 ;;
494 esac
495 }
496
497 dns() {
498 local param output_text i
499 case $1 in
500 on_start)
501 if [ ! -s "$outputFile" ]; then
502 json set status "statusFail"
503 json add error "errorOutputFileCreate"
504 output "${_ERROR_}: $(get_text 'errorOutputFileCreate')!\\n"
505 return 1
506 fi
507
508 config_load 'dhcp'
509 if [ "$dns_instance" = "*" ]; then
510 config_foreach resolver 'dnsmasq' "$dns"
511 elif [ -n "$dns_instance" ]; then
512 for i in $dns_instance; do
513 resolver "@dnsmasq[$i]" "$dns" || resolver "$i" "$dns"
514 done
515 fi
516
517 case "$dns" in
518 dnsmasq.addnhosts|dnsmasq.servers)
519 param=dnsmasq_restart
520 output_text='Reloading dnsmasq'
521 ;;
522 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
523 param=dnsmasq_restart
524 output_text='Restarting dnsmasq'
525 ;;
526 unbound.adb_list)
527 param=unbound_restart
528 output_text='Restarting Unbound'
529 ;;
530 esac
531
532 if [ -n "$(uci_changes dhcp)" ]; then
533 uci_commit dhcp
534 if [ "$param" = 'unbound_restart' ]; then
535 param='dnsmasq_restart; unbound_restart;'
536 output_text='Restarting Unbound/dnsmasq'
537 else
538 param=dnsmasq_restart
539 output_text='Restarting dnsmasq'
540 fi
541 fi
542 output 1 "$output_text "
543 output 2 "$output_text "
544 json set message "$output_text"
545 if eval "$param"; then
546 json set status "statusSuccess"
547 led_on "$led"
548 output_okn
549 else
550 output_fail
551 json set status "statusFail"
552 json add error "errorDNSReload"
553 output "${_ERROR_}: $(get_text 'errorDNSReload')!\\n"
554 return 1
555 fi
556 ;;
557 on_stop)
558 case "$dns" in
559 dnsmasq.addnhosts|dnsmasq.servers)
560 param=dnsmasq_restart
561 ;;
562 dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
563 param=dnsmasq_restart
564 ;;
565 unbound.adb_list)
566 param=unbound_restart
567 ;;
568 esac
569 if [ -n "$(uci_changes dhcp)" ]; then
570 uci_commit dhcp
571 if [ "$param" = 'unbound_restart' ]; then
572 param='dnsmasq_restart; unbound_restart;'
573 else
574 param=dnsmasq_restart
575 fi
576 fi
577 eval "$param"
578 return $?
579 ;;
580 quiet)
581 case "$dns" in
582 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
583 param=dnsmasq_restart
584 ;;
585 unbound.adb_list)
586 param=unbound_restart
587 ;;
588 esac
589 eval "$param"
590 return $?
591 ;;
592 esac
593 }
594
595 json() {
596 # shellcheck disable=SC2034
597 local action="$1" param="$2" value="$3"
598 shift 3
599 # shellcheck disable=SC2124
600 local extras="$@" line
601 local status message error stats
602 local reload restart curReload curRestart ret i
603 if [ -s "$jsonFile" ]; then
604 json_load_file "$jsonFile" 2>/dev/null
605 json_select 'data' 2>/dev/null
606 for i in status message error stats reload restart; do
607 json_get_var $i "$i" 2>/dev/null
608 done
609 fi
610 case "$action" in
611 get)
612 case "$param" in
613 triggers)
614 curReload="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
615 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
616 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
617 curRestart="$compressed_cache $force_dns $led $force_dns_port"
618 if [ ! -s "$jsonFile" ]; then
619 ret='on_boot'
620 elif [ "$curReload" != "$reload" ]; then
621 ret='download'
622 elif [ "$curRestart" != "$restart" ]; then
623 ret='restart'
624 fi
625 printf "%b" "$ret"
626 return;;
627 *)
628 printf "%b" "$(eval echo "\$$param")"; return;;
629 esac
630 ;;
631 add)
632 line="$(eval echo "\$$param")"
633 eval "$param"='${line:+$line }${value}${extras:+|$extras}'
634 ;;
635 del)
636 case "$param" in
637 all)
638 unset status message error stats;;
639 triggers)
640 unset reload restart;;
641 *)
642 unset "$param";;
643 esac
644 ;;
645 set)
646 case "$param" in
647 triggers)
648 reload="$parallel_downloads $debug $download_timeout $allowed_domain $blocked_domain $allowed_domains_url \
649 $blocked_adblockplus_url $blocked_domains_url $blocked_hosts_url $dns $config_update_enabled $config_update_url \
650 $dnsmasq_config_file_url $curl_max_file_size $curl_retry"
651 restart="$compressed_cache $force_dns $led $force_dns_port"
652 ;;
653 *)
654 eval "$param"='${value}${extras:+|$extras}';;
655 esac
656 ;;
657 esac
658 json_init
659 json_add_object 'data'
660 json_add_string version "$PKG_VERSION"
661 json_add_string status "$status"
662 json_add_string message "$message"
663 json_add_string error "$error"
664 json_add_string stats "$stats"
665 json_add_string reload "$reload"
666 json_add_string restart "$restart"
667 json_close_object
668 mkdir -p "$(dirname "$jsonFile")"
669 json_dump > "$jsonFile"
670 sync
671 }
672
673 cache() {
674 local R_TMP
675 case "$1" in
676 create|backup)
677 [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
678 return $?
679 ;;
680 restore|use)
681 [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
682 return $?
683 ;;
684 test)
685 [ -s "$outputCache" ]
686 return $?
687 ;;
688 test_gzip)
689 [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
690 return $?
691 ;;
692 create_gzip)
693 rm -f "$outputGzip" >/dev/null 2>/dev/null
694 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
695 if gzip < "$outputFile" > "$R_TMP"; then
696 if mv "$R_TMP" "$outputGzip"; then
697 rm -f "$R_TMP"
698 return 0
699 else
700 rm -f "$R_TMP"
701 return 1
702 fi
703 else
704 return 1
705 fi
706 ;;
707 expand|unpack|unpack_gzip)
708 [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
709 return $?
710 ;;
711 esac
712 }
713
714 process_url() {
715 local label type D_TMP R_TMP
716 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
717 label="${1##*//}"; label="${label%%/*}";
718 case "$2" in
719 adbp) label="ADBPlus: $label"; filter="$adBlockPlusFilter"
720 ;;
721 dnsmasq) label="Dnsmasq: $label"; filter="$dnsmasqFileFilter"
722 ;;
723 domains) label="Domains: $label"; filter="$domainsFilter"
724 ;;
725 hosts) label="Hosts: $label"; filter="$hostsFilter"
726 ;;
727 esac
728 case "$3" in
729 allowed) type='Allowed'; D_TMP="$A_TMP"
730 ;;
731 blocked) type='Blocked'; D_TMP="$B_TMP"
732 ;;
733 file) type='File'; D_TMP="$B_TMP"
734 ;;
735 esac
736 if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
737 output 1 "$_FAIL_"
738 output 2 "[DL] $type $label $__FAIL__\\n"
739 echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
740 return 0
741 fi
742 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
743 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
744 done
745 if ! $dl_command "$1" "$dl_flag" "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
746 output 1 "$_FAIL_"
747 output 2 "[DL] $type $label $__FAIL__\\n"
748 echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
749 else
750 sed -i "$filter" "$R_TMP"
751 if [ ! -s "$R_TMP" ]; then
752 output 1 "$_FAIL_"
753 output 2 "[DL] $type $label $__FAIL__\\n"
754 echo "errorParsingList|${1}" >> "$sharedMemoryError"
755 else
756 cat "${R_TMP}" >> "$D_TMP"
757 output 1 "$_OK_"
758 output 2 "[DL] $type $label $__OK__\\n"
759 fi
760 fi
761 rm -f "$R_TMP"
762 return 0
763 }
764
765 download_dnsmasq_file() {
766 local hf allow_filter j=0 R_TMP
767
768 json set message "$(get_text "statusDownloading")..."
769 json set status "statusDownloading"
770
771 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
772 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
773 output 3 'Low free memory, restarting resolver '
774 if dns 'quiet'; then
775 output_okn
776 else
777 output_failn
778 fi
779 fi
780 touch $A_TMP; touch $B_TMP;
781 output 1 'Downloading dnsmasq file '
782 rm -f "$sharedMemoryError"
783 process_url "$dnsmasq_config_file_url" 'dnsmasq' 'file'
784 # output 1 '\n'
785 if [ -s "$sharedMemoryError" ]; then
786 while IFS= read -r line; do
787 json add error "$line"
788 done < "$sharedMemoryError"
789 rm -f "$sharedMemoryError"
790 fi
791 output 2 'Creating dnsmasq file '
792 if mv "$B_TMP" "$outputFile"; then
793 output 2 "$__OK__\\n"
794 else
795 output 2 "$__FAIL__\\n"
796 json add error "errorMovingDataFile"
797 fi
798 output 1 '\n'
799 }
800
801 download_lists() {
802 local hf allow_filter j=0 R_TMP
803
804 json set message "$(get_text "statusDownloading")..."
805 json set status "statusDownloading"
806
807 rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
808 if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
809 output 3 'Low free memory, restarting resolver '
810 if dns 'quiet'; then
811 output_okn
812 else
813 output_failn
814 fi
815 fi
816 touch $A_TMP; touch $B_TMP;
817 output 1 'Downloading lists '
818 rm -f "$sharedMemoryError"
819 for hf in ${blocked_hosts_url}; do
820 if [ "$parallel_downloads" -gt 0 ]; then
821 process_url "$hf" 'hosts' 'blocked' &
822 else
823 process_url "$hf" 'hosts' 'blocked'
824 fi
825 done
826 for hf in ${blocked_adblockplus_url}; do
827 if [ "$parallel_downloads" -gt 0 ]; then
828 process_url "$hf" 'adbp' 'blocked' &
829 else
830 process_url "$hf" 'adbp' 'blocked'
831 fi
832 done
833 for hf in ${blocked_domains_url}; do
834 if [ "$parallel_downloads" -gt 0 ]; then
835 process_url "$hf" 'domains' 'blocked' &
836 else
837 process_url "$hf" 'domains' 'blocked'
838 fi
839 done
840 for hf in ${allowed_domains_url}; do
841 if [ "$parallel_downloads" -gt 0 ]; then
842 process_url "$hf" 'domains' 'allowed' &
843 else
844 process_url "$hf" 'domains' 'allowed'
845 fi
846 done
847 wait
848 output 1 '\n'
849 if [ -s "$sharedMemoryError" ]; then
850 while IFS= read -r line; do
851 json add error "$line"
852 done < "$sharedMemoryError"
853 rm -f "$sharedMemoryError"
854 fi
855
856 if [ "$canary_domains_icloud" -ne 0 ]; then
857 canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
858 fi
859 if [ "$canary_domains_mozilla" -ne 0 ]; then
860 canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
861 fi
862
863 for hf in $blocked_domain $canaryDomains; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
864 allowed_domain="${allowed_domain}
865 $(cat $A_TMP)"
866 for hf in ${allowed_domain}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; allow_filter="$allow_filter/(^|\.)${hf}$/d;"; done
867
868 [ ! -s "$B_TMP" ] && return 1
869
870 output 1 'Processing downloads '
871 output 2 'Sorting combined list '
872 json set status "statusProcessing"
873 json set message "$(get_text "statusProcessing"): sorting combined list"
874 if [ "$allow_non_ascii" -gt 0 ]; then
875 if sort -u "$B_TMP" > "$A_TMP"; then
876 output_ok
877 else
878 output_failn
879 json add error "errorSorting"
880 fi
881 else
882 if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
883 output_ok
884 else
885 output_failn
886 json add error "errorSorting"
887 fi
888 fi
889
890 if [ "$dns" = 'dnsmasq.conf' ] || \
891 [ "$dns" = 'dnsmasq.ipset' ] || \
892 [ "$dns" = 'dnsmasq.nftset' ] || \
893 [ "$dns" = 'dnsmasq.servers' ] || \
894 [ "$dns" = 'unbound.adb_list' ]; then
895 # TLD optimization written by Dirk Brenken (dev@brenken.org)
896 output 2 'Optimizing combined list '
897 json set message "$(get_text "statusProcessing"): optimizing combined list"
898 # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
899 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
900 if sort "$B_TMP" > "$A_TMP"; then
901 if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
902 if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
903 if sort -u "$A_TMP" > "$B_TMP"; then
904 output_ok
905 else
906 output_failn
907 json add error "errorOptimization"
908 mv "$A_TMP" "$B_TMP"
909 fi
910 else
911 output_failn
912 json add error "errorOptimization"
913 fi
914 else
915 output_failn
916 json add error "errorOptimization"
917 mv "$A_TMP" "$B_TMP"
918 fi
919 else
920 output_failn
921 json add error "errorOptimization"
922 fi
923 else
924 output_failn
925 json add error "errorOptimization"
926 mv "$A_TMP" "$B_TMP"
927 fi
928 else
929 mv "$A_TMP" "$B_TMP"
930 fi
931
932 output 2 'Allowing domains '
933 json set message "$(get_text "statusProcessing"): allowing domains"
934 if sed -i -E "$allow_filter" "$B_TMP"; then
935 output_ok
936 else
937 output_failn
938 json add error "errorAllowListProcessing"
939 fi
940
941 output 2 'Formatting merged file '
942 json set message "$(get_text "statusProcessing"): formatting merged file"
943 if [ -z "$outputFilterIPv6" ]; then
944 if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
945 output_ok
946 else
947 output_failn
948 json add error "errorDataFileFormatting"
949 fi
950 else
951 case "$dns" in
952 dnsmasq.addnhosts)
953 if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
954 sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
955 output_ok
956 else
957 output_failn
958 json add error "errorDataFileFormatting"
959 fi
960 ;;
961 esac
962 fi
963
964 case "$dns" in
965 dnsmasq.addnhosts)
966 output 2 'Creating dnsmasq addnhosts file '
967 json set message "$(get_text "statusProcessing"): creating dnsmasq addnhosts file"
968 ;;
969 dnsmasq.conf)
970 output 2 'Creating dnsmasq config file '
971 json set message "$(get_text "statusProcessing"): creating dnsmasq config file"
972 ;;
973 dnsmasq.ipset)
974 output 2 'Creating dnsmasq ipset file '
975 json set message "$(get_text "statusProcessing"): creating dnsmasq ipset file"
976 ;;
977 dnsmasq.nftset)
978 output 2 'Creating dnsmasq nft set file '
979 json set message "$(get_text "statusProcessing"): creating dnsmasq nft set file"
980 ;;
981 dnsmasq.servers)
982 output 2 'Creating dnsmasq servers file '
983 json set message "$(get_text "statusProcessing"): creating dnsmasq servers file"
984 ;;
985 unbound.adb_list)
986 output 2 'Creating Unbound adb_list file '
987 json set message "$(get_text "statusProcessing"): creating Unbound adb_list file"
988 ;;
989 esac
990
991 if mv "$A_TMP" "$outputFile"; then
992 output_ok
993 else
994 output_failn
995 json add error "errorMovingDataFile"
996 fi
997 if [ "$compressed_cache" -gt 0 ]; then
998 output 2 'Creating compressed cache '
999 json set message "$(get_text "statusProcessing"): creating compressed cache"
1000 if cache 'create_gzip'; then
1001 output_ok
1002 else
1003 output_failn
1004 json add error "errorCreatingCompressedCache"
1005 fi
1006 else
1007 rm -f "$outputGzip"
1008 fi
1009 output 2 'Removing temporary files '
1010 json set message "$(get_text "statusProcessing"): removing temporary files"
1011 rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
1012 if [ $j -eq 0 ]; then
1013 output_ok
1014 else
1015 output_failn
1016 json add error "errorRemovingTempFiles"
1017 fi
1018 output 1 '\n'
1019 }
1020
1021 adb_allow() {
1022 local c hf string="$1"
1023 local validation_result="$3"
1024 load_environment "$validation_result" 'quiet' || return 1
1025 if [ ! -s "$outputFile" ]; then
1026 output "No block-list ('$outputFile') found.\\n"
1027 return 0
1028 elif [ -z "$string" ]; then
1029 output "Usage: /etc/init.d/${packageName} allow 'domain' ...\\n"
1030 return 0
1031 elif [ -n "$dnsmasq_config_file_url" ]; then
1032 output "Allowing individual domains is not possible when using external dnsmasq config file.\\n"
1033 return 0
1034 fi
1035 case "$dns" in
1036 dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
1037 output 1 "Allowing domain(s) and restarting dnsmasq "
1038 output 2 "Allowing domain(s) \\n"
1039 for c in $string; do
1040 output 2 " $c "
1041 hf="$(echo "$c" | sed 's/\./\\./g')"
1042 if sed -i "/(^|\.)${hf}$/d;" "$outputFile" && \
1043 uci_add_list_if_new "${packageName}" 'config' 'allowed_domain' "$c"; then
1044 output_ok
1045 else
1046 output_fail
1047 fi
1048 done
1049 if [ "$compressed_cache" -gt 0 ]; then
1050 output 2 'Creating compressed cache '
1051 if cache 'create_gzip'; then
1052 output_ok
1053 else
1054 output_failn
1055 fi
1056 fi
1057 output 2 "Committing changes to config "
1058 if [ -n "$(uci_changes "$packageName")" ] && uci_commit "$packageName"; then
1059 allowed_domain="$(uci_get "$packageName" 'config' 'allowed_domain')"
1060 json set triggers
1061 json set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1062 output_ok;
1063 if [ "$dns" = 'dnsmasq.ipset' ]; then
1064 output 2 "Flushing adb ipset "
1065 if ipset -q -! flush adb; then output_ok; else output_fail; fi
1066 fi
1067 if [ "$dns" = 'dnsmasq.nftset' ]; then
1068 output 2 "Flushing adb nft sets "
1069 nft flush set inet fw4 adb6
1070 if nft flush set inet fw4 adb4; then output_ok; else output_fail; fi
1071 fi
1072 output 2 "Restarting dnsmasq "
1073 if dnsmasq_restart; then output_okn; else output_failn; fi
1074 else
1075 output_fail;
1076 fi
1077 ;;
1078 unbound.adb_list)
1079 output 1 "Allowing domain(s) and restarting Unbound "
1080 output 2 "Allowing domain(s) \\n"
1081 for c in $string; do
1082 output 2 " $c "
1083 if sed -i "/${string}/d" "$outputFile" && \
1084 uci_add_list_if_new "$packageName" 'config' 'allowed_domain' "$string"; then
1085 output_ok
1086 else
1087 output_fail
1088 fi
1089 done
1090 if [ "$compressed_cache" -gt 0 ]; then
1091 output 2 'Creating compressed cache '
1092 if cache 'create_gzip'; then
1093 output_ok
1094 else
1095 output_failn
1096 fi
1097 fi
1098 output 2 "Committing changes to config "
1099 if [ -n "$(uci_changes "$packageName")" ] && uci_commit "$packageName"; then
1100 allowed_domain="$(uci_get "$packageName" 'config' 'allowed_domain')"
1101 json set triggers
1102 json set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1103 output_ok;
1104 output 2 "Restarting Unbound "
1105 if unbound_restart; then output_okn; else output_failn; fi
1106 else
1107 output_fail;
1108 fi
1109 ;;
1110 esac
1111 }
1112
1113 adb_check() {
1114 local c param="$1"
1115 local validation_result="$3"
1116 load_environment "$validation_result" 'quiet' || return 1
1117 if [ ! -s "$outputFile" ]; then
1118 output "No block-list ('$outputFile') found.\\n"
1119 return 0
1120 elif [ -z "$param" ]; then
1121 output "Usage: /etc/init.d/${packageName} check 'domain' ...\\n"
1122 return 0
1123 fi
1124 for string in ${param}; do
1125 c="$(grep -c "$string" "$outputFile")"
1126 if [ "$c" -gt 0 ]; then
1127 if [ "$c" -eq 1 ]; then
1128 output "Found 1 match for '$string' in '$outputFile'.\\n"
1129 else
1130 output "Found $c matches for '$string' in '$outputFile'.\\n"
1131 fi
1132 if [ "$c" -le 20 ]; then
1133 case "$dns" in
1134 dnsmasq.addnhosts)
1135 grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
1136 dnsmasq.conf)
1137 grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
1138 dnsmasq.ipset)
1139 grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
1140 dnsmasq.nftset)
1141 grep "$string" "$outputFile" | sed 's|nftset=/||;s|/4#inet#adb#adb4||;';;
1142 dnsmasq.servers)
1143 grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
1144 unbound.adb_list)
1145 grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
1146 esac
1147 fi
1148 else
1149 output "The '$string' is not found in current block-list ('$outputFile').\\n"
1150 fi
1151 done
1152 }
1153
1154 adb_config_update() {
1155 local R_TMP label
1156 local param validation_result="$3"
1157 case "$1" in
1158 on_boot) param="$1";;
1159 *) param='quiet';;
1160 esac
1161 load_environment "$validation_result" "$param" || return 1
1162 label="${config_update_url##*//}"
1163 label="${label%%/*}";
1164 [ "$config_update_enabled" -ne 0 ] || return 0
1165
1166 if [ "$param" != 'download' ]; then
1167 cache 'test' && return 0
1168 cache 'test_gzip' && return 0
1169 fi
1170 output 1 'Updating config '
1171 while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
1172 R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
1173 done
1174 if ! $dl_command "$config_update_url" "$dl_flag" "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
1175 output 1 "$_FAIL_\\n"
1176 output 2 "[DL] Config Update: $label $__FAIL__\\n"
1177 json add error "errorDownloadingConfigUpdate"
1178 else
1179 if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i "$packageConfigFile" 2>/dev/null; then
1180 output 1 "$_OK_\\n"
1181 output 2 "[DL] Config Update: $label $__OK__\\n"
1182 else
1183 output 1 "$_FAIL_\\n"
1184 output 2 "[DL] Config Update: $label $__FAIL__\\n"
1185 json add error "errorParsingConfigUpdate"
1186 fi
1187 fi
1188 rm -f "$R_TMP"
1189 return 0
1190 }
1191
1192 adb_sizes() {
1193 local i
1194 local validation_result="$3"
1195 load_environment "$validation_result" 'quiet' || return 1
1196
1197 echo "# $(date)"
1198 for i in ${blocked_adblockplus_url}; do
1199 [ "${i//melmac}" != "$i" ] && continue
1200 if $dl_command "$i" "$dl_flag" /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
1201 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1202 if is_greater "$(du -sk /tmp/sast)" "500"; then
1203 echo "# block-list too big for most routers"
1204 elif is_greater "$(du -sk /tmp/sast)" "100"; then
1205 echo "# block-list may be too big for some routers"
1206 fi
1207 rm -rf /tmp/sast
1208 echo " list blocked_adblockplus_url '$i'"
1209 echo ""
1210 else
1211 echo "# site was down on last check"
1212 echo "# list blocked_adblockplus_url '$i'"
1213 echo ""
1214 fi
1215 done
1216 for i in ${blocked_domains_url}; do
1217 [ "${i//melmac}" != "$i" ] && continue
1218 if $dl_command "$i" "$dl_flag" /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
1219 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1220 if is_greater "$(du -sk /tmp/sast)" "500"; then
1221 echo "# block-list too big for most routers"
1222 elif is_greater "$(du -sk /tmp/sast)" "100"; then
1223 echo "# block-list may be too big for some routers"
1224 fi
1225 rm -rf /tmp/sast
1226 echo " list blocked_domains_url '$i'"
1227 echo ""
1228 else
1229 echo "# site was down on last check"
1230 echo "# list blocked_domains_url '$i'"
1231 echo ""
1232 fi
1233 done
1234 for i in ${blocked_hosts_url}; do
1235 if $dl_command "$i" "$dl_flag" /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
1236 echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
1237 if is_greater "$(du -sk /tmp/sast)" "500"; then
1238 echo "# block-list too big for most routers"
1239 elif is_greater "$(du -sk /tmp/sast)" "100"; then
1240 echo "# block-list may be too big for some routers"
1241 fi
1242 rm -rf /tmp/sast
1243 echo " list blocked_hosts_url '$i'"
1244 echo ""
1245 else
1246 echo "# site was down on last check"
1247 echo "# list blocked_hosts_url '$i'"
1248 echo ""
1249 fi
1250 done
1251 }
1252
1253 adb_start() {
1254 local action status error message stats c
1255 local param="$1" validation_result="$3"
1256
1257 load_environment "$validation_result" "$param" || return 1
1258
1259 status="$(json get status)"
1260 error="$(json get error)"
1261 message="$(json get message)"
1262 stats="$(json get stats)"
1263 action="$(json get triggers)"
1264
1265 if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
1266 if cache 'test_gzip' || cache 'test'; then
1267 action='restore'
1268 else
1269 action='download'
1270 fi
1271 elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
1272 action='download'
1273 elif [ ! -s "$outputFile" ]; then
1274 if cache 'test_gzip' || cache 'test'; then
1275 action='restore'
1276 else
1277 action='download'
1278 fi
1279 elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
1280 action='restart'
1281 elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
1282 status_service
1283 return 0
1284 else
1285 action='download'
1286 fi
1287
1288 json del all
1289 json set triggers
1290
1291 if [ "$action" = 'restore' ]; then
1292 output 0 "Starting $serviceName... "
1293 output 3 "Starting $serviceName...\\n"
1294 json set status "statusStarting"
1295 if cache 'test_gzip' && ! cache 'test' && [ ! -s "$outputFile" ]; then
1296 output 3 'Found compressed cache file, unpacking it '
1297 json set message 'found compressed cache file, unpacking it.'
1298 if cache 'unpack_gzip'; then
1299 output_okn
1300 else
1301 output_failn
1302 json add error "errorRestoreCompressedCache"
1303 output "${_ERROR_}: $(get_text 'errorRestoreCompressedCache')!\\n"
1304 action='download'
1305 fi
1306 fi
1307 if cache 'test' && [ ! -s "$outputFile" ]; then
1308 output 3 'Found cache file, reusing it '
1309 json set message 'found cache file, reusing it.'
1310 if cache 'restore'; then
1311 output_okn
1312 dns 'on_start'
1313 else
1314 output_failn
1315 json add error "errorRestoreCache"
1316 output "${_ERROR_}: $(get_text 'errorRestoreCache')!\\n"
1317 action='download'
1318 fi
1319 fi
1320 fi
1321 if [ "$action" = 'download' ]; then
1322 if [ -s "$outputFile" ] || cache 'test' || cache 'test_gzip'; then
1323 output 0 "Force-reloading $serviceName... "
1324 output 3 "Force-reloading $serviceName...\\n"
1325 json set status "statusForceReloading"
1326 else
1327 output 0 "Starting $serviceName... "
1328 output 3 "Starting $serviceName...\\n"
1329 json set status "statusStarting"
1330 fi
1331 if [ -n "$dnsmasq_config_file_url" ]; then
1332 download_dnsmasq_file
1333 else
1334 download_lists
1335 fi
1336 dns 'on_start'
1337 fi
1338 if [ "$action" = 'restart' ]; then
1339 output 0 "Restarting $serviceName... "
1340 output 3 "Restarting $serviceName...\\n"
1341 json set status "statusRestarting"
1342 dns 'on_start'
1343 fi
1344 if [ "$action" = 'start' ]; then
1345 output 0 "Starting $serviceName... "
1346 output 3 "Starting $serviceName...\\n"
1347 json set status "statusStarting"
1348 dns 'on_start'
1349 fi
1350 if [ -s "$outputFile" ] && [ "$(json get status)" != "statusFail" ]; then
1351 output 0 "$__OK__\\n";
1352 json del message
1353 json set status "statusSuccess"
1354 json set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${dns})"
1355 status_service
1356 else
1357 output 0 "$__FAIL__\\n";
1358 json set status "statusFail"
1359 json add error "errorOhSnap"
1360 status_service
1361 fi
1362
1363 procd_open_instance 'main'
1364 procd_set_param command /bin/true
1365 procd_set_param stdout 1
1366 procd_set_param stderr 1
1367 procd_open_data
1368 json_add_string 'status' "$(json get status)"
1369 json_add_string 'errors' "$(json get errors)"
1370 if [ -s "$outputFile" ]; then
1371 json_add_int 'entries' "$(wc -l < "$outputFile")"
1372 else
1373 json_add_int 'entries' '0'
1374 fi
1375 json_add_array firewall
1376 if [ "$force_dns" -ne 0 ]; then
1377 for c in $force_dns_port; do
1378 if netstat -tuln | grep LISTEN | grep ":${c}" >/dev/null 2>&1; then
1379 json_add_object ""
1380 json_add_string type redirect
1381 json_add_string target DNAT
1382 json_add_string src lan
1383 json_add_string proto "tcp udp"
1384 json_add_string src_dport "$c"
1385 json_add_string dest_port "$c"
1386 json_add_boolean reflection 0
1387 json_close_object
1388 else
1389 json_add_object ""
1390 json_add_string type rule
1391 json_add_string src lan
1392 json_add_string dest "*"
1393 json_add_string proto "tcp udp"
1394 json_add_string dest_port "$c"
1395 json_add_string target REJECT
1396 json_close_object
1397 fi
1398 done
1399 fi
1400 case "$dns" in
1401 dnsmasq.ipset)
1402 json_add_object ""
1403 json_add_string type ipset
1404 json_add_string name adb
1405 json_add_string match dest_net
1406 json_add_string storage hash
1407 json_close_object
1408 json_add_object ""
1409 json_add_string type rule
1410 json_add_string ipset adb
1411 json_add_string src lan
1412 json_add_string dest "*"
1413 json_add_string proto "tcp udp"
1414 json_add_string target REJECT
1415 json_close_object
1416 ;;
1417 dnsmasq.nftset)
1418 json_add_object ""
1419 json_add_string type ipset
1420 json_add_string name adb4
1421 json_add_string family 4
1422 json_add_string match dest_net
1423 json_close_object
1424 json_add_object ""
1425 json_add_string type rule
1426 json_add_string ipset adb4
1427 json_add_string src lan
1428 json_add_string dest "*"
1429 json_add_string proto "tcp udp"
1430 json_add_string target REJECT
1431 json_close_object
1432 if [ "$ipv6_enabled" -ne 0 ]; then
1433 json_add_object ""
1434 json_add_string type ipset
1435 json_add_string name adb6
1436 json_add_string family 6
1437 json_add_string match dest_net
1438 json_close_object
1439 json_add_object ""
1440 json_add_string type rule
1441 json_add_string ipset adb6
1442 json_add_string src lan
1443 json_add_string dest "*"
1444 json_add_string proto "tcp udp"
1445 json_add_string target REJECT
1446 json_close_object
1447 fi
1448 ;;
1449 esac
1450 json_close_array
1451 procd_close_data
1452 procd_close_instance
1453 }
1454
1455 adb_status() {
1456 local c url status message error stats
1457 local validation_result="$3"
1458 load_environment "$validation_result" 'quiet' || return 1
1459 status="$(json get status)"
1460 message="$(json get message)"
1461 error="$(json get error)"
1462 stats="$(json get stats)"
1463 if [ "$status" = "statusSuccess" ]; then
1464 output "$stats "; output_okn;
1465 else
1466 [ -n "$status" ] && status="$(get_text "$status")"
1467 if [ -n "$status" ] && [ -n "$message" ]; then
1468 status="${status}: $message"
1469 fi
1470 [ -n "$status" ] && output "$serviceName $status\\n"
1471 fi
1472 if [ -n "$error" ]; then
1473 for c in $error; do
1474 url="${c##*|}"
1475 c="${c%|*}"
1476 case "$c" in
1477 errorDownloadingList|errorParsingList)
1478 output "${_ERROR_}: $(get_text "$c") $url!\\n";;
1479 *)
1480 output "${_ERROR_}: $(get_text "$c")!\\n";;
1481 esac
1482 n=$((n+1))
1483 done
1484 fi
1485 }
1486
1487 adb_stop() {
1488 local validation_result="$3"
1489 load_environment "$validation_result" 'quiet' || return 1
1490 if [ -s "$outputFile" ]; then
1491 output "Stopping $serviceName... "
1492 cache 'create'
1493 if dns 'on_stop'; then
1494 ipset -q -! flush adb
1495 ipset -q -! destroy adb
1496 nft delete set inet fw4 adb4
1497 nft delete set inet fw4 adb6
1498 led_off "$led"
1499 output 0 "$__OK__\\n"; output_okn;
1500 json set status "statusStopped"
1501 json del message
1502 else
1503 output 0 "$__FAIL__\\n"; output_fail;
1504 json set status "statusFail"
1505 json add error "errorStopping"
1506 output "${_ERROR_}: $(get_text 'errorStopping')!\\n"
1507 fi
1508 fi
1509 }
1510
1511 allow() { load_validate_config 'config' adb_allow "'$*'"; }
1512 boot() {
1513 ubus -t 30 wait_for network.interface 2>/dev/null
1514 rc_procd start_service 'on_boot'
1515 }
1516 check() { load_validate_config 'config' adb_check "'$*'"; }
1517 dl() { rc_procd start_service 'download'; }
1518 killcache() {
1519 rm -f "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
1520 rm -f "$dnsmasqConfCache" "$dnsmasqConfGzip"
1521 rm -f "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
1522 rm -f "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
1523 rm -f "$dnsmasqServersCache" "$dnsmasqServersGzip"
1524 rm -f "$unboundCache" "$unboundGzip"
1525 config_load 'dhcp'
1526 config_foreach resolver 'dnsmasq' 'cleanup'
1527 uci_commit 'dhcp'
1528 return 0
1529 }
1530 reload_service() { rc_procd start_service 'restart'; }
1531 restart_service() { rc_procd start_service 'restart'; }
1532 service_started() { procd_set_config_changed firewall; }
1533 service_stopped() { procd_set_config_changed firewall; }
1534 service_triggers() {
1535 local wan wan6 i
1536 local procd_trigger_wan6
1537 config_load "$packageName"
1538 config_get_bool procd_trigger_wan6 'config' 'procd_trigger_wan6' '0'
1539 . /lib/functions/network.sh
1540 network_flush_cache
1541 network_find_wan wan
1542 wan="${wan:-wan}"
1543 if [ "$procd_trigger_wan6" -ne 0 ]; then
1544 network_find_wan6 wan6
1545 wan6="${wan6:-wan6}"
1546 fi
1547 for i in "$wan" "$wan6"; do
1548 [ -n "$i" ] && procd_add_interface_trigger "interface.*" "$i" "/etc/init.d/${packageName}" start
1549 done
1550 procd_add_config_trigger "config.change" "$packageName" "/etc/init.d/${packageName}" reload
1551 }
1552 sizes() { load_validate_config 'config' adb_sizes "''"; }
1553 start_service() {
1554 load_validate_config 'config' adb_config_update "'$*'"
1555 load_validate_config 'config' adb_start "'$*'"
1556 }
1557 status_service() { load_validate_config 'config' adb_status "''"; }
1558 stop_service() { load_validate_config 'config' adb_stop "'$*'"; }
1559 version() { echo "$PKG_VERSION"; }
1560
1561 load_validate_config() {
1562 . /lib/functions/network.sh
1563 . /usr/share/libubox/jshn.sh
1564 local enabled
1565 local force_dns
1566 local force_dns_port
1567 local parallel_downloads
1568 local debug
1569 local compressed_cache
1570 local ipv6_enabled
1571 local allow_non_ascii
1572 local canary_domains_icloud
1573 local canary_domains_mozilla
1574 local config_update_enabled
1575 local config_update_url
1576 local download_timeout
1577 local curl_max_file_size
1578 local curl_retry
1579 local verbosity
1580 local procd_trigger_wan6
1581 local procd_boot_wan_timeout
1582 local led
1583 local dns
1584 local dns_instance
1585 local allowed_domain
1586 local allowed_domains_url
1587 local blocked_adblockplus_url
1588 local blocked_domain
1589 local blocked_domains_url
1590 local blocked_hosts_url
1591 local dnsmasq_config_file_url
1592 uci_load_validate "$packageName" "$packageName" "$1" "${2}${3:+ $3}" \
1593 'enabled:bool:0' \
1594 'force_dns:bool:1' \
1595 'force_dns_port:list(integer):53 853' \
1596 'parallel_downloads:bool:1' \
1597 'debug:bool:0' \
1598 'compressed_cache:bool:0' \
1599 'ipv6_enabled:bool:0' \
1600 'allow_non_ascii:bool:0' \
1601 'canary_domains_icloud:bool:0' \
1602 'canary_domains_mozilla:bool:0' \
1603 'config_update_enabled:bool:0' \
1604 'config_update_url:string:https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update' \
1605 'download_timeout:range(1,60):20' \
1606 'curl_max_file_size:uinteger' \
1607 'curl_retry:range(0,30):3' \
1608 'verbosity:range(0,2):2' \
1609 'procd_trigger_wan6:bool:0' \
1610 'procd_boot_wan_timeout:integer:60' \
1611 'led:or("", "none", file, device, string)' \
1612 'dns:or("dnsmasq.addnhosts", "dnsmasq.conf", "dnsmasq.ipset", "dnsmasq.nftset", "dnsmasq.servers", "unbound.adb_list"):dnsmasq.servers' \
1613 'dns_instance:or(list(integer, string)):0' \
1614 'allowed_domain:list(string)' \
1615 'allowed_domains_url:list(string)' \
1616 'blocked_domain:list(string)' \
1617 'blocked_adblockplus_url:list(string)' \
1618 'blocked_domains_url:list(string)' \
1619 'blocked_hosts_url:list(string)' \
1620 'dnsmasq_config_file_url:string'
1621 }