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