2017-01-23 14:33:32 +00:00
#!/bin/sh /etc/rc.common
2022-08-19 21:38:16 +00:00
# Copyright 2017-2022 Stan Grishin (stangri@melmac.ca)
2021-08-02 06:34:08 +00:00
# shellcheck disable=SC2039,SC1091,SC2016,SC3043,SC3057,SC3060
2019-10-06 16:33:33 +00:00
PKG_VERSION='dev-test'
2017-01-23 14:33:32 +00:00
2020-06-13 20:39:00 +00:00
# shellcheck disable=SC2034
START=94
# shellcheck disable=SC2034
USE_PROCD=1
LC_ALL=C
2019-07-04 22:38:58 +00:00
2021-08-02 06:34:08 +00:00
if type extra_command 1>/dev/null 2>&1; then
2022-08-19 21:38:16 +00:00
extra_command 'allow' 'Allows domain(s) in current block-list and config'
2021-08-02 06:34:08 +00:00
extra_command 'check' 'Checks if specified domain is found in current block-list'
extra_command 'dl' 'Force-downloads all enabled block-list'
extra_command 'killcache' 'Delete all cached files'
extra_command 'sizes' 'Displays the file-sizes of enabled block-lists'
extra_command 'version' 'Show version information'
else
# shellcheck disable=SC2034
2022-08-19 21:38:16 +00:00
EXTRA_COMMANDS='allow check dl killcache sizes status_service version'
2021-08-02 06:34:08 +00:00
# shellcheck disable=SC2034
2022-08-19 21:38:16 +00:00
EXTRA_HELP=' allow Allows domain(s) in current block-list and config
check Checks if specified domain is found in current block-list
2021-08-02 06:34:08 +00:00
dl Force-downloads all enabled block-list
sizes Displays the file-sizes of enabled block-lists'
fi
2017-01-23 14:33:32 +00:00
2019-09-24 16:11:45 +00:00
readonly packageName='simple-adblock'
2018-07-17 00:41:39 +00:00
readonly serviceName="$packageName $PKG_VERSION"
2022-08-19 21:38:16 +00:00
readonly dnsmasqAddnhostsFile="/var/run/${packageName}/dnsmasq.addnhosts"
readonly dnsmasqAddnhostsCache="/var/run/${packageName}/dnsmasq.addnhosts.cache"
readonly dnsmasqAddnhostsGzip="/etc/${packageName}.dnsmasq.addnhosts.gz"
readonly dnsmasqAddnhostsFilter='s|^|127.0.0.1 |;s|$||'
readonly dnsmasqAddnhostsFilterIPv6='s|^|:: |;s|$||'
readonly dnsmasqConfFile="/tmp/dnsmasq.d/${packageName}"
readonly dnsmasqConfCache="/var/run/${packageName}/dnsmasq.conf.cache"
readonly dnsmasqConfGzip="/etc/${packageName}.dnsmasq.conf.gz"
readonly dnsmasqConfFilter='s|^|local=/|;s|$|/|'
readonly dnsmasqIpsetFile="/tmp/dnsmasq.d/${packageName}.ipset"
readonly dnsmasqIpsetCache="/var/run/${packageName}/dnsmasq.ipset.cache"
readonly dnsmasqIpsetGzip="/etc/${packageName}.dnsmasq.ipset.gz"
readonly dnsmasqIpsetFilter='s|^|ipset=/|;s|$|/adb|'
readonly dnsmasqNftsetFile="/tmp/dnsmasq.d/${packageName}.nftset"
readonly dnsmasqNftsetCache="/var/run/${packageName}/dnsmasq.nftset.cache"
readonly dnsmasqNftsetGzip="/etc/${packageName}.dnsmasq.nftset.gz"
readonly dnsmasqNftsetFilter='s|^|nftset=/|;s|$|/adb|'
readonly dnsmasqServersFile="/var/run/${packageName}/dnsmasq.servers"
readonly dnsmasqServersCache="/var/run/${packageName}/dnsmasq.servers.cache"
readonly dnsmasqServersGzip="/etc/${packageName}.dnsmasq.servers.gz"
readonly dnsmasqServersFilter='s|^|server=/|;s|$|/|'
2019-08-17 13:09:31 +00:00
readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
2022-08-19 21:38:16 +00:00
readonly unboundCache="/var/run/${packageName}/unbound.cache"
2019-08-17 13:09:31 +00:00
readonly unboundGzip="/etc/${packageName}.unbound.gz"
2022-08-19 21:38:16 +00:00
readonly unboundFilter='s|^|local-zone: "|;s|$|" static|'
2019-08-17 13:09:31 +00:00
readonly A_TMP="/var/${packageName}.hosts.a.tmp"
readonly B_TMP="/var/${packageName}.hosts.b.tmp"
2022-08-19 21:38:16 +00:00
readonly jsonFile="/var/run/${packageName}/${packageName}.json"
2020-03-29 05:13:07 +00:00
readonly sharedMemoryError="/dev/shm/$packageName-error"
readonly sharedMemoryOutput="/dev/shm/$packageName-output"
2019-08-17 13:09:31 +00:00
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;'
readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
2018-01-26 03:35:52 +00:00
readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
readonly _ERROR_='\033[0;31mERROR\033[0m'
2021-03-08 10:25:11 +00:00
readonly _WARNING_='\033[0;33mWARNING\033[0m'
2020-10-08 02:38:54 +00:00
2022-08-19 21:38:16 +00:00
serviceEnabled=
forceDNS=
forceDNSPorts=
parallelDL=
debug=
compressedCache=
ipv6Enabled=
configUpdateEnabled=
configUpdateURL=
bootDelay=
dlTimeout=
curlRetry=
verbosity=
led=
targetDNS=
dnsInstance=
allowed_domains=
allowed_domains_urls=
blocked_domains=
blocked_domains_urls=
blocked_hosts_urls=
dl_command=
dl_flag=
isSSLSupported=
allowIDN=
outputFilter=
outputFilterIPv6=
outputFile=
outputGzip=
outputCache=
awk='awk'
uci_add_list_if_new() {
local PACKAGE="$1"
local CONFIG="$2"
local OPTION="$3"
local VALUE="$4"
local i
[ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
[ "$i" = "$VALUE" ] && return 0
done
uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
}
uci_changes() {
local PACKAGE="$1"
local CONFIG="$2"
local OPTION="$3"
/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
}
2020-10-08 02:38:54 +00:00
version() { echo "$PKG_VERSION"; }
2020-03-29 05:13:07 +00:00
getStatusText() {
local _ret
case "$1" in
statusNoInstall) _ret="$serviceName is not installed or not found";;
statusStopped) _ret="Stopped";;
statusStarting) _ret="Starting";;
statusRestarting) _ret="Restarting";;
statusForceReloading) _ret="Force Reloading";;
statusDownloading) _ret="Downloading";;
2020-06-13 20:39:00 +00:00
statusProcessing) _ret="Processing";;
2020-03-29 05:13:07 +00:00
statusError) _ret="Error";;
statusWarning) _ret="Warning";;
statusFail) _ret="Fail";;
statusSuccess) _ret="Success";;
esac
printf "%b" "$_ret"
}
getErrorText() {
local _ret
case "$1" in
errorOutputFileCreate) _ret="failed to create $outputFile file";;
errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
errorSharedMemory) _ret="failed to access shared memory";;
errorSorting) _ret="failed to sort data file";;
errorOptimization) _ret="failed to optimize data file";;
2020-06-13 20:39:00 +00:00
errorAllowListProcessing) _ret="failed to process allow-list";;
2020-03-29 05:13:07 +00:00
errorDataFileFormatting) _ret="failed to format data file";;
errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
errorCreatingCompressedCache) _ret="failed to create compressed cache";;
errorRemovingTempFiles) _ret="failed to remove temporary files";;
errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
2020-06-13 20:39:00 +00:00
errorOhSnap) _ret="failed to create block-list or restart DNS resolver";;
2020-03-29 05:13:07 +00:00
errorStopping) _ret="failed to stop $serviceName";;
errorDNSReload) _ret="failed to reload/restart DNS resolver";;
2020-09-20 00:16:36 +00:00
errorDownloadingConfigUpdate) _ret="failed to download Config Update file";;
2020-03-29 05:13:07 +00:00
errorDownloadingList) _ret="failed to download";;
2020-09-20 00:16:36 +00:00
errorParsingConfigUpdate) _ret="failed to parse Config Update file";;
2022-08-19 21:38:16 +00:00
errorParsingList) _ret="failed to parse";;
errorNoSSLSupport) _ret="no HTTPS/SSL support on device";;
errorCreatingDirectory) _ret="failed to create output/cache/gzip file directory";;
2020-03-29 05:13:07 +00:00
esac
printf "%b" "$_ret"
}
2017-01-23 14:33:32 +00:00
2019-08-17 13:09:31 +00:00
output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
2019-09-24 16:11:45 +00:00
output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
2020-06-13 20:39:00 +00:00
str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
2022-08-19 21:38:16 +00:00
is_greater() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
is_greater_or_equal() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" = "$2"; }
2019-09-24 16:11:45 +00:00
is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
2021-08-18 12:17:28 +00:00
dnsmasq_hup() { killall -q -s HUP dnsmasq; }
dnsmasq_kill() { killall -q -s KILL dnsmasq; }
2019-09-24 16:11:45 +00:00
dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
2021-03-08 10:25:11 +00:00
is_present() { command -v "$1" >/dev/null 2>&1; }
2019-09-24 16:11:45 +00:00
2018-07-17 00:41:39 +00:00
output() {
# Can take a single parameter (text) to be output at any verbosity
# Or target verbosity level and text to be output at specifc verbosity
2020-04-07 21:55:22 +00:00
local msg memmsg logmsg
2019-07-04 22:38:58 +00:00
if [ $# -ne 1 ]; then
2019-08-23 04:14:42 +00:00
if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
2018-07-17 00:41:39 +00:00
fi
2019-08-23 04:14:42 +00:00
[ -t 1 ] && printf "%b" "$1"
2020-03-29 05:13:07 +00:00
msg="${1//$serviceName /service }";
2019-08-23 04:14:42 +00:00
if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
[ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
2020-04-07 21:55:22 +00:00
logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
2019-08-23 04:14:42 +00:00
rm -f "$sharedMemoryOutput"
2018-07-17 00:41:39 +00:00
else
2019-08-23 04:14:42 +00:00
printf "%b" "$msg" >> "$sharedMemoryOutput"
2018-07-17 00:41:39 +00:00
fi
}
2019-09-24 16:11:45 +00:00
2018-01-26 03:35:52 +00:00
load_package_config() {
2022-08-19 21:38:16 +00:00
local i
2018-01-26 03:35:52 +00:00
config_load "$packageName"
2019-09-24 16:11:45 +00:00
config_get_bool serviceEnabled 'config' 'enabled' 1
config_get_bool forceDNS 'config' 'force_dns' 1
config_get_bool parallelDL 'config' 'parallel_downloads' 1
config_get_bool debug 'config' 'debug' 0
config_get_bool compressedCache 'config' 'compressed_cache' 0
config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
2020-09-20 00:16:36 +00:00
config_get_bool configUpdateEnabled 'config' 'config_update_enabled' 0
2021-03-18 01:37:30 +00:00
config_get forceDNSPorts 'config' 'force_dns_port' '53 853'
2019-09-24 16:11:45 +00:00
config_get bootDelay 'config' 'boot_delay' '120'
config_get dlTimeout 'config' 'download_timeout' '20'
config_get curlRetry 'config' 'curl_retry' '3'
config_get verbosity 'config' 'verbosity' '2'
2021-03-18 01:37:30 +00:00
config_get led 'config' 'led'
2019-09-24 16:11:45 +00:00
config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
config_get dnsInstance 'config' 'dns_instance' '0'
2020-06-13 20:39:00 +00:00
config_get allowed_domains 'config' 'allowed_domain'
config_get allowed_domains_urls 'config' 'allowed_domains_url'
config_get blocked_domains 'config' 'blocked_domain'
config_get blocked_domains_urls 'config' 'blocked_domains_url'
config_get blocked_hosts_urls 'config' 'blocked_hosts_url'
2020-10-08 02:38:54 +00:00
config_get configUpdateURL 'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
2019-09-24 16:11:45 +00:00
if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
[ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
2022-08-19 21:38:16 +00:00
[ "$targetDNS" != 'dnsmasq.ipset' ] && [ "$targetDNS" != 'dnsmasq.nftset' ] ; then
2019-09-24 16:11:45 +00:00
targetDNS='dnsmasq.servers'
2019-08-17 13:09:31 +00:00
fi
case "$targetDNS" in
dnsmasq.addnhosts)
2022-08-19 21:38:16 +00:00
outputFilter="$dnsmasqAddnhostsFilter"
outputFile="$dnsmasqAddnhostsFile"
outputCache="$dnsmasqAddnhostsCache"
outputGzip="$dnsmasqAddnhostsGzip"
[ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$dnsmasqAddnhostsFilterIPv6"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
2019-08-17 13:09:31 +00:00
rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
;;
dnsmasq.conf)
2022-08-19 21:38:16 +00:00
outputFilter="$dnsmasqConfFilter"
outputFile="$dnsmasqConfFile"
outputCache="$dnsmasqConfCache"
outputGzip="$dnsmasqConfGzip"
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
2019-09-24 16:11:45 +00:00
rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
;;
dnsmasq.ipset)
2022-08-19 21:38:16 +00:00
outputFilter="$dnsmasqIpsetFilter"
outputFile="$dnsmasqIpsetFile"
outputCache="$dnsmasqIpsetCache"
outputGzip="$dnsmasqIpsetGzip"
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
;;
dnsmasq.nftset)
outputFilter="$dnsmasqNftsetFilter"
outputFile="$dnsmasqNftsetFile"
outputCache="$dnsmasqNftsetCache"
outputGzip="$dnsmasqNftsetGzip"
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
2019-08-17 13:09:31 +00:00
rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
;;
dnsmasq.servers)
2022-08-19 21:38:16 +00:00
outputFilter="$dnsmasqServersFilter"
outputFile="$dnsmasqServersFile"
outputCache="$dnsmasqServersCache"
outputGzip="$dnsmasqServersGzip"
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
2019-08-17 13:09:31 +00:00
rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
;;
unbound.adb_list)
2022-08-19 21:38:16 +00:00
outputFilter="$unboundFilter"
2019-08-17 13:09:31 +00:00
outputFile="$unboundFile"
outputCache="$unboundCache"
outputGzip="$unboundGzip"
2022-08-19 21:38:16 +00:00
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "$dnsmasqServersGzip"
2019-08-17 13:09:31 +00:00
;;
esac
2019-07-04 22:38:58 +00:00
if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
2018-07-17 00:41:39 +00:00
verbosity=1
fi
2019-07-04 22:38:58 +00:00
. /lib/functions/network.sh
. /usr/share/libubox/jshn.sh
2021-03-08 10:25:11 +00:00
is_present 'gawk' && awk='gawk'
if ! is_present '/usr/libexec/grep-gnu' || ! is_present '/usr/libexec/sed-gnu' || \
! is_present '/usr/libexec/sort-coreutils' || ! is_present 'gawk'; then
local s="opkg update; opkg --force-overwrite install"
is_present 'gawk' || s="$s gawk"
is_present '/usr/libexec/grep-gnu' || s="$s grep"
is_present '/usr/libexec/sed-gnu' || s="$s sed"
is_present '/usr/libexec/sort-coreutils' || s="$s coreutils-sort"
output "$_WARNING_: Some recommended packages are missing, install them by running:\\n"
output "$s;\\n"
fi
2020-05-27 00:25:57 +00:00
# Prefer curl because it supports the file:// scheme.
2021-03-08 10:25:11 +00:00
if is_present 'curl'; then
2019-08-17 13:09:31 +00:00
dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
2019-07-04 22:38:58 +00:00
dl_flag="-o"
2021-04-10 18:54:41 +00:00
elif is_present '/usr/libexec/wget-ssl'; then
dl_command="/usr/libexec/wget-ssl --no-check-certificate --timeout $dlTimeout -q"
dl_flag="-O"
2021-03-08 10:25:11 +00:00
elif is_present wget && wget --version 2>/dev/null | grep -q "+https"; then
2019-07-04 22:38:58 +00:00
dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
dl_flag="-O"
2020-03-29 05:13:07 +00:00
else
dl_command="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
dl_flag="-O"
2019-07-04 22:38:58 +00:00
fi
2018-07-17 00:41:39 +00:00
led="${led:+/sys/class/leds/$led}"
2020-05-27 00:25:57 +00:00
if curl --version 2>/dev/null | grep -q "https" \
|| wget --version 2>/dev/null | grep -q "+https" \
|| grep -q "libustream-mbedtls" /usr/lib/opkg/status \
|| grep -q "libustream-openssl" /usr/lib/opkg/status \
|| grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
2020-06-13 20:39:00 +00:00
isSSLSupported=1
2020-05-27 00:25:57 +00:00
else
2020-06-13 20:39:00 +00:00
unset isSSLSupported
2020-05-27 00:25:57 +00:00
fi
2018-01-26 03:35:52 +00:00
}
is_enabled() {
2022-08-19 21:38:16 +00:00
wan_if=
wan_gw=
2018-07-17 00:41:39 +00:00
load_package_config
2017-05-07 09:24:37 +00:00
2018-07-17 00:41:39 +00:00
if [ "$debug" -ne 0 ]; then
exec 1>>/tmp/simple-adblock.log
exec 2>&1
set -x
fi
2019-07-04 22:38:58 +00:00
if [ "$serviceEnabled" -eq 0 ]; then
2019-08-17 13:09:31 +00:00
case "$1" in
on_start)
output "$packageName is currently disabled.\\n"
output "Run the following commands before starting service again:\\n"
output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
;;
esac
2018-07-17 00:41:39 +00:00
return 1
fi
2019-08-17 13:09:31 +00:00
2019-09-24 16:11:45 +00:00
case $targetDNS in
2022-08-19 21:38:16 +00:00
dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
2019-09-24 16:11:45 +00:00
if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
allowIDN=0
else
allowIDN=1
fi
;;
unbound.adb_list)
allowIDN=1;;
esac
case $targetDNS in
dnsmasq.ipset)
if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
2022-08-19 21:38:16 +00:00
output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipset!\\n"
2019-09-24 16:11:45 +00:00
targetDNS='dnsmasq.servers'
fi
if ! ipset help hash:net >/dev/null 2>&1; then
output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
targetDNS='dnsmasq.servers'
fi
;;
2022-08-19 21:38:16 +00:00
dnsmasq.nftset)
if dnsmasq -v 2>/dev/null | grep -q 'no-nftset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'nftset'; then
output "$_ERROR_: DNSMASQ nftset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support nftset!\\n"
targetDNS='dnsmasq.servers'
fi
if [ -z "$(command -v nft)" ]; then
output "$_ERROR_: DNSMASQ nftset support is enabled in $packageName, but nft is not installed!\\n"
targetDNS='dnsmasq.servers'
fi
;;
2019-09-24 16:11:45 +00:00
esac
2022-08-19 21:38:16 +00:00
for i in "$outputFile" "$outputCache" "$outputGzip"; do
if ! mkdir -p "$(dirname "$i")"; then
output "$_ERROR_: Unable to create directory for $i!\\n"
jsonOps add error "errorOutputFileCreate"
fi
done
2019-08-17 13:09:31 +00:00
cacheOps 'testGzip' && return 0
2019-07-04 22:38:58 +00:00
network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
2018-07-17 00:41:39 +00:00
[ -n "$wan_gw" ] && return 0
2019-07-04 22:38:58 +00:00
output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
2018-07-17 00:41:39 +00:00
}
2017-05-07 09:24:37 +00:00
2020-03-29 05:13:07 +00:00
dnsmasqOps() {
local cfg="$1" param="$2"
case "$param" in
dnsmasq.addnhosts)
2022-08-19 21:38:16 +00:00
if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
uci_remove 'dhcp' "$cfg" 'serversfile'
2020-03-29 05:13:07 +00:00
fi
2022-08-19 21:38:16 +00:00
uci_add_list_if_new 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
2020-03-29 05:13:07 +00:00
;;
2022-08-19 21:38:16 +00:00
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|unbound.adb_list|cleanup)
uci_remove_list 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
uci_remove 'dhcp' "$cfg" 'serversfile'
2020-03-29 05:13:07 +00:00
fi
;;
dnsmasq.servers)
2022-08-19 21:38:16 +00:00
uci_remove_list 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" != "$dnsmasqServersFile" ]; then
uci_set 'dhcp' "$cfg" 'serversfile' "$dnsmasqServersFile"
2020-03-29 05:13:07 +00:00
fi
;;
esac
}
2019-10-06 16:33:33 +00:00
dnsOps() {
2020-03-29 05:13:07 +00:00
local param output_text i
2018-07-17 00:41:39 +00:00
case $1 in
on_start)
2019-08-17 13:09:31 +00:00
if [ ! -s "$outputFile" ]; then
2021-03-18 01:37:30 +00:00
jsonOps set status "statusFail"
jsonOps add error "errorOutputFileCreate"
2020-03-29 05:13:07 +00:00
output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
2018-07-17 00:41:39 +00:00
return 1
fi
2020-03-29 05:13:07 +00:00
config_load 'dhcp'
if [ "$dnsInstance" = "*" ]; then
config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
elif [ -n "$dnsInstance" ]; then
for i in $dnsInstance; do
dnsmasqOps "@dnsmasq[$i]" "$targetDNS"
done
fi
2019-08-17 13:09:31 +00:00
case "$targetDNS" in
2020-03-29 05:13:07 +00:00
dnsmasq.addnhosts|dnsmasq.servers)
2019-10-06 16:33:33 +00:00
param=dnsmasq_hup
2019-09-24 16:11:45 +00:00
output_text='Reloading DNSMASQ'
2019-08-17 13:09:31 +00:00
;;
2022-08-19 21:38:16 +00:00
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
2019-08-17 13:09:31 +00:00
param=dnsmasq_restart
2019-09-24 16:11:45 +00:00
output_text='Restarting DNSMASQ'
2019-08-17 13:09:31 +00:00
;;
unbound.adb_list)
param=unbound_restart
2019-09-24 16:11:45 +00:00
output_text='Restarting Unbound'
2019-08-17 13:09:31 +00:00
;;
esac
2020-03-29 05:13:07 +00:00
2022-08-19 21:38:16 +00:00
if [ -n "$(uci_changes dhcp)" ]; then
uci_commit dhcp
2019-09-24 16:11:45 +00:00
if [ "$param" = 'unbound_restart' ]; then
param='dnsmasq_restart; unbound_restart;'
output_text='Restarting Unbound/DNSMASQ'
else
param=dnsmasq_restart
output_text='Restarting DNSMASQ'
fi
fi
2019-08-17 13:09:31 +00:00
output 1 "$output_text "
output 2 "$output_text "
2021-03-18 01:37:30 +00:00
jsonOps set message "$output_text"
2019-08-17 13:09:31 +00:00
if eval "$param"; then
2021-03-18 01:37:30 +00:00
jsonOps set status "statusSuccess"
2019-08-17 13:09:31 +00:00
led_on "$led"
2018-07-17 00:41:39 +00:00
output_okn
2019-08-17 13:09:31 +00:00
else
output_fail
2021-03-18 01:37:30 +00:00
jsonOps set status "statusFail"
jsonOps add error "errorDNSReload"
2020-03-29 05:13:07 +00:00
output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
2018-07-17 00:41:39 +00:00
return 1
fi
;;
2019-08-17 13:09:31 +00:00
on_stop)
case "$targetDNS" in
2022-08-19 21:38:16 +00:00
dnsmasq.addnhosts|dnsmasq.servers)
2019-10-06 16:33:33 +00:00
param=dnsmasq_hup
2019-08-17 13:09:31 +00:00
;;
2022-08-19 21:38:16 +00:00
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
2019-08-17 13:09:31 +00:00
param=dnsmasq_restart
;;
unbound.adb_list)
param=unbound_restart
;;
esac
2022-08-19 21:38:16 +00:00
if [ -n "$(uci_changes dhcp)" ]; then
uci_commit dhcp
2019-09-24 16:11:45 +00:00
if [ "$param" = 'unbound_restart' ]; then
param='dnsmasq_restart; unbound_restart;'
else
param=dnsmasq_restart
fi
fi
2019-08-17 13:09:31 +00:00
eval "$param"
return $?
;;
quiet)
case "$targetDNS" in
2022-08-19 21:38:16 +00:00
dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
2019-08-17 13:09:31 +00:00
param=dnsmasq_restart
;;
unbound.adb_list)
param=unbound_restart
;;
esac
eval "$param"
return $?
2018-07-17 00:41:39 +00:00
;;
esac
2017-01-23 14:33:32 +00:00
}
2021-03-18 01:37:30 +00:00
jsonOps() {
2021-03-08 10:25:11 +00:00
# shellcheck disable=SC2034
local action="$1" param="$2" value="$3"
2020-03-29 05:13:07 +00:00
local status message error stats
2021-02-02 22:11:06 +00:00
local reload restart curReload curRestart ret i
2019-08-17 13:09:31 +00:00
if [ -s "$jsonFile" ]; then
2021-02-02 22:11:06 +00:00
json_load_file "$jsonFile" 2>/dev/null
json_select 'data' 2>/dev/null
for i in status message error stats reload restart; do
json_get_var $i "$i" 2>/dev/null
done
2019-07-04 22:38:58 +00:00
fi
case "$action" in
get)
2021-03-08 10:25:11 +00:00
case "$param" in
2019-07-04 22:38:58 +00:00
triggers)
2021-03-18 01:37:30 +00:00
curReload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
curRestart="$compressedCache $forceDNS $led $forceDNSPorts"
2020-04-07 21:55:22 +00:00
if [ ! -s "$jsonFile" ]; then
ret='on_boot'
2021-02-02 22:11:06 +00:00
elif [ "$curReload" != "$reload" ]; then
2019-09-24 16:11:45 +00:00
ret='download'
2021-02-02 22:11:06 +00:00
elif [ "$curRestart" != "$restart" ]; then
2019-09-24 16:11:45 +00:00
ret='restart'
2019-07-04 22:38:58 +00:00
fi
2020-03-29 05:13:07 +00:00
printf "%b" "$ret"
2019-07-04 22:38:58 +00:00
return;;
2021-03-08 10:25:11 +00:00
*)
printf "%b" "$(eval echo "\$$param")"; return;;
2019-07-04 22:38:58 +00:00
esac
;;
2018-07-17 00:41:39 +00:00
add)
2021-03-18 01:37:30 +00:00
if [ -n "$(eval echo "\$$param")" ]; then
value="$(eval echo "\$$param") ${value}"
fi
eval "$param"='${value}'
;;
2019-07-04 22:38:58 +00:00
del)
2021-03-08 10:25:11 +00:00
case "$param" in
2019-09-24 16:11:45 +00:00
all)
2021-03-08 10:25:11 +00:00
unset status message error stats;;
2019-07-04 22:38:58 +00:00
triggers)
2021-03-08 10:25:11 +00:00
unset reload restart;;
*)
unset "$param";;
2019-07-04 22:38:58 +00:00
esac
2018-07-17 00:41:39 +00:00
;;
2019-07-04 22:38:58 +00:00
set)
2021-03-08 10:25:11 +00:00
case "$param" in
2019-07-04 22:38:58 +00:00
triggers)
2021-03-18 01:37:30 +00:00
reload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
restart="$compressedCache $forceDNS $led $forceDNSPorts"
2019-07-04 22:38:58 +00:00
;;
2021-03-08 10:25:11 +00:00
*)
eval "$param"='$value';;
2019-07-04 22:38:58 +00:00
esac
2018-07-17 00:41:39 +00:00
;;
esac
2020-03-29 05:13:07 +00:00
json_init
json_add_object 'data'
json_add_string version "$PKG_VERSION"
json_add_string status "$status"
json_add_string message "$message"
json_add_string error "$error"
json_add_string stats "$stats"
2021-02-02 22:11:06 +00:00
json_add_string reload "$reload"
json_add_string restart "$restart"
2020-03-29 05:13:07 +00:00
json_close_object
json_dump > "$jsonFile"
2019-07-04 22:38:58 +00:00
sync
2017-01-23 14:33:32 +00:00
}
2019-08-23 04:14:42 +00:00
cacheOps() {
2019-08-17 13:09:31 +00:00
local R_TMP
case "$1" in
create|backup)
2019-10-06 16:33:33 +00:00
[ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
2019-08-17 13:09:31 +00:00
return $?
;;
restore|use)
2019-10-06 16:33:33 +00:00
[ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
2019-08-17 13:09:31 +00:00
return $?
;;
test)
[ -s "$outputCache" ]
return $?
;;
testGzip)
[ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
return $?
;;
createGzip)
R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
if gzip < "$outputFile" > "$R_TMP"; then
if mv "$R_TMP" "$outputGzip"; then
rm -f "$R_TMP"
return 0
else
rm -f "$R_TMP"
return 1
fi
else
return 1
fi
;;
expand|unpack|expandGzip|unpackGzip)
[ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
return $?
;;
esac
}
2017-01-23 14:33:32 +00:00
process_url() {
local label type D_TMP R_TMP
2019-07-04 22:38:58 +00:00
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
label="${1##*//}"; label="${label%%/*}";
2019-09-24 16:11:45 +00:00
if [ "$2" = 'hosts' ]; then
2019-08-17 13:09:31 +00:00
label="Hosts: $label"; filter="$hostsFilter";
2018-07-17 00:41:39 +00:00
else
2019-08-17 13:09:31 +00:00
label="Domains: $label"; filter="$domainsFilter";
2018-07-17 00:41:39 +00:00
fi
2019-09-24 16:11:45 +00:00
if [ "$3" = 'blocked' ]; then
type='Blocked'; D_TMP="$B_TMP";
2018-07-17 00:41:39 +00:00
else
2019-09-24 16:11:45 +00:00
type='Allowed'; D_TMP="$A_TMP";
2018-07-17 00:41:39 +00:00
fi
2021-08-02 06:34:08 +00:00
if [ "${1:0:5}" = "https" ] && [ -z "$isSSLSupported" ]; then
2020-05-27 00:25:57 +00:00
output 1 "$_FAIL_"
output 2 "[DL] $type $label $__FAIL__\\n"
echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
return 0
fi
2019-07-04 22:38:58 +00:00
while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
2018-07-17 00:41:39 +00:00
done
2019-07-04 22:38:58 +00:00
if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
2018-07-17 00:41:39 +00:00
output 1 "$_FAIL_"
2019-08-17 13:09:31 +00:00
output 2 "[DL] $type $label $__FAIL__\\n"
2020-05-27 00:25:57 +00:00
echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
2019-08-17 13:09:31 +00:00
else
sed -i "$filter" "$R_TMP"
if [ ! -s "$R_TMP" ]; then
output 1 "$_FAIL_"
output 2 "[DL] $type $label $__FAIL__\\n"
2020-05-27 00:25:57 +00:00
echo "errorParsingList|${1}" >> "$sharedMemoryError"
2019-08-17 13:09:31 +00:00
else
cat "${R_TMP}" >> "$D_TMP"
output 1 "$_OK_"
output 2 "[DL] $type $label $__OK__\\n"
fi
2018-07-17 00:41:39 +00:00
fi
2019-08-17 13:09:31 +00:00
rm -f "$R_TMP"
2018-07-17 00:41:39 +00:00
return 0
2017-01-23 14:33:32 +00:00
}
2020-09-20 00:16:36 +00:00
process_config_update() {
local label R_TMP
[ "$configUpdateEnabled" -eq 0 ] && return 0
label="${1##*//}"; label="${label%%/*}";
while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
done
if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
output 1 "$_FAIL_"
output 2 "[DL] Config Update: $label $__FAIL__\\n"
2021-03-18 01:37:30 +00:00
jsonOps add error "errorDownloadingConfigUpdate"
2020-09-20 00:16:36 +00:00
else
2021-03-18 01:37:30 +00:00
if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock 2>/dev/null; then
2020-12-12 21:58:21 +00:00
output 1 "$_OK_"
output 2 "[DL] Config Update: $label $__OK__\\n"
else
2020-09-20 00:16:36 +00:00
output 1 "$_FAIL_"
output 2 "[DL] Config Update: $label $__FAIL__\\n"
2021-03-18 01:37:30 +00:00
jsonOps add error "errorParsingConfigUpdate"
2020-09-20 00:16:36 +00:00
fi
fi
rm -f "$R_TMP"
return 0
}
2018-07-17 00:41:39 +00:00
download_lists() {
2019-08-17 13:09:31 +00:00
local hf w_filter j=0 R_TMP
2018-07-17 00:41:39 +00:00
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusDownloading")..."
jsonOps set status "statusDownloading"
2020-03-29 05:13:07 +00:00
2019-08-17 13:09:31 +00:00
rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
2021-03-08 10:25:11 +00:00
if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
2019-09-24 16:11:45 +00:00
output 3 'Low free memory, restarting resolver... '
2019-10-06 16:33:33 +00:00
if dnsOps 'quiet'; then
2019-08-17 13:09:31 +00:00
output_okn
else
output_fail
fi
2018-07-17 00:41:39 +00:00
fi
touch $A_TMP; touch $B_TMP;
2019-09-24 16:11:45 +00:00
output 1 'Downloading lists '
2020-09-20 00:16:36 +00:00
process_config_update "$configUpdateURL"
2019-08-23 04:14:42 +00:00
rm -f "$sharedMemoryError"
2020-06-13 20:39:00 +00:00
if [ -n "$blocked_hosts_urls" ]; then
for hf in ${blocked_hosts_urls}; do
2019-07-04 22:38:58 +00:00
if [ "$parallelDL" -gt 0 ]; then
2019-09-24 16:11:45 +00:00
process_url "$hf" 'hosts' 'blocked' &
2019-07-04 22:38:58 +00:00
else
2019-09-24 16:11:45 +00:00
process_url "$hf" 'hosts' 'blocked'
2019-07-04 22:38:58 +00:00
fi
2018-07-17 00:41:39 +00:00
done
fi
2020-06-13 20:39:00 +00:00
if [ -n "$blocked_domains_urls" ]; then
for hf in ${blocked_domains_urls}; do
2019-07-04 22:38:58 +00:00
if [ "$parallelDL" -gt 0 ]; then
2019-09-24 16:11:45 +00:00
process_url "$hf" 'domains' 'blocked' &
2019-07-04 22:38:58 +00:00
else
2019-09-24 16:11:45 +00:00
process_url "$hf" 'domains' 'blocked'
2019-07-04 22:38:58 +00:00
fi
2018-07-17 00:41:39 +00:00
done
fi
2020-06-13 20:39:00 +00:00
if [ -n "$allowed_domains_urls" ]; then
for hf in ${allowed_domains_urls}; do
2019-07-04 22:38:58 +00:00
if [ "$parallelDL" -gt 0 ]; then
2019-09-24 16:11:45 +00:00
process_url "$hf" 'domains' 'allowed' &
2019-07-04 22:38:58 +00:00
else
2019-09-24 16:11:45 +00:00
process_url "$hf" 'domains' 'allowed'
2019-07-04 22:38:58 +00:00
fi
2018-07-17 00:41:39 +00:00
done
fi
wait
2019-09-24 16:11:45 +00:00
output 1 '\n'
2020-03-29 05:13:07 +00:00
if [ -s "$sharedMemoryError" ]; then
while IFS= read -r line; do
2021-03-18 01:37:30 +00:00
jsonOps add error "$line"
2020-03-29 05:13:07 +00:00
done < "$sharedMemoryError"
rm -f "$sharedMemoryError"
fi
2018-07-17 00:41:39 +00:00
2020-06-13 20:39:00 +00:00
[ -n "$blocked_domains" ] && for hf in ${blocked_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
allowed_domains="${allowed_domains}
2019-07-04 22:38:58 +00:00
$(cat $A_TMP)"
2021-03-08 10:25:11 +00:00
[ -n "$allowed_domains" ] && for hf in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/${hf}$/d;"; done
2019-08-17 13:09:31 +00:00
[ ! -s "$B_TMP" ] && return 1
2019-09-24 16:11:45 +00:00
output 1 'Processing downloads '
output 2 'Sorting combined list '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): sorting combined list"
2019-09-24 16:11:45 +00:00
if [ "$allowIDN" -gt 0 ]; then
if sort -u "$B_TMP" > "$A_TMP"; then
2019-07-04 22:38:58 +00:00
output_ok
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorSorting"
2019-08-17 13:09:31 +00:00
fi
else
2019-09-24 16:11:45 +00:00
if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
2019-08-17 13:09:31 +00:00
output_ok
else
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorSorting"
2019-07-04 22:38:58 +00:00
fi
2019-08-17 13:09:31 +00:00
fi
2019-07-04 22:38:58 +00:00
2019-09-24 16:11:45 +00:00
if [ "$targetDNS" = 'dnsmasq.conf' ] || \
2020-03-29 05:13:07 +00:00
[ "$targetDNS" = 'dnsmasq.ipset' ] || \
2022-08-19 21:38:16 +00:00
[ "$targetDNS" = 'dnsmasq.nftset' ] || \
2020-03-29 05:13:07 +00:00
[ "$targetDNS" = 'dnsmasq.servers' ] || \
2019-09-24 16:11:45 +00:00
[ "$targetDNS" = 'unbound.adb_list' ]; then
2019-08-17 13:09:31 +00:00
# TLD optimization written by Dirk Brenken (dev@brenken.org)
2019-09-24 16:11:45 +00:00
output 2 'Optimizing combined list '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): optimizing combined list"
2021-03-08 10:25:11 +00:00
# sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
2019-07-04 22:38:58 +00:00
if sort "$B_TMP" > "$A_TMP"; then
2021-03-08 10:25:11 +00:00
if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
2019-09-24 16:11:45 +00:00
if sort -u "$A_TMP" > "$B_TMP"; then
2019-07-04 22:38:58 +00:00
output_ok
2018-07-17 00:41:39 +00:00
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorOptimization"
2019-08-17 13:09:31 +00:00
mv "$A_TMP" "$B_TMP"
2018-07-17 00:41:39 +00:00
fi
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorOptimization"
2018-07-17 00:41:39 +00:00
fi
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorOptimization"
2019-08-17 13:09:31 +00:00
mv "$A_TMP" "$B_TMP"
2018-07-17 00:41:39 +00:00
fi
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorOptimization"
2018-07-17 00:41:39 +00:00
fi
2019-07-04 22:38:58 +00:00
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorOptimization"
2019-08-17 13:09:31 +00:00
mv "$A_TMP" "$B_TMP"
2019-07-04 22:38:58 +00:00
fi
2019-08-17 13:09:31 +00:00
else
mv "$A_TMP" "$B_TMP"
fi
2019-07-04 22:38:58 +00:00
2020-06-13 20:39:00 +00:00
output 2 'Allowing domains '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): allowing domains"
2019-08-17 13:09:31 +00:00
if sed -i "$w_filter" "$B_TMP"; then
output_ok
else
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorAllowListProcessing"
2019-08-17 13:09:31 +00:00
fi
2019-07-04 22:38:58 +00:00
2019-09-24 16:11:45 +00:00
output 2 'Formatting merged file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): formatting merged file"
2019-08-17 13:09:31 +00:00
if [ -z "$outputFilterIPv6" ]; then
if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
2019-07-04 22:38:58 +00:00
output_ok
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorDataFileFormatting"
2019-07-04 22:38:58 +00:00
fi
2019-08-17 13:09:31 +00:00
else
2019-09-24 16:11:45 +00:00
case "$targetDNS" in
dnsmasq.addnhosts)
if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
output_ok
else
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorDataFileFormatting"
2019-09-24 16:11:45 +00:00
fi
;;
esac
2019-08-17 13:09:31 +00:00
fi
2019-07-04 22:38:58 +00:00
2019-08-17 13:09:31 +00:00
case "$targetDNS" in
dnsmasq.addnhosts)
2019-09-24 16:11:45 +00:00
output 2 'Creating DNSMASQ addnhosts file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
2019-08-17 13:09:31 +00:00
;;
dnsmasq.conf)
2019-09-24 16:11:45 +00:00
output 2 'Creating DNSMASQ config file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
2019-08-17 13:09:31 +00:00
;;
2019-09-24 16:11:45 +00:00
dnsmasq.ipset)
output 2 'Creating DNSMASQ ipset file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
2019-09-24 16:11:45 +00:00
;;
2022-08-19 21:38:16 +00:00
dnsmasq.nftset)
output 2 'Creating DNSMASQ nftset file '
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ nftset file"
;;
2019-08-17 13:09:31 +00:00
dnsmasq.servers)
2019-09-24 16:11:45 +00:00
output 2 'Creating DNSMASQ servers file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
2019-08-17 13:09:31 +00:00
;;
unbound.adb_list)
2019-09-24 16:11:45 +00:00
output 2 'Creating Unbound adb_list file '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
2019-08-17 13:09:31 +00:00
;;
esac
if mv "$A_TMP" "$outputFile"; then
output_ok
else
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorMovingDataFile"
2019-08-17 13:09:31 +00:00
fi
if [ "$compressedCache" -gt 0 ]; then
2019-09-24 16:11:45 +00:00
output 2 'Creating compressed cache '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): creating compressed cache"
2019-08-17 13:09:31 +00:00
if cacheOps 'createGzip'; then
2019-07-04 22:38:58 +00:00
output_ok
else
2019-08-17 13:09:31 +00:00
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorCreatingCompressedCache"
2019-07-04 22:38:58 +00:00
fi
2019-08-17 13:09:31 +00:00
else
rm -f "$outputGzip"
fi
2019-09-24 16:11:45 +00:00
output 2 'Removing temporary files '
2021-03-18 01:37:30 +00:00
jsonOps set message "$(getStatusText "statusProcessing"): removing temporary files"
2019-08-17 13:09:31 +00:00
rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
if [ $j -eq 0 ]; then
output_ok
else
output_failn
2021-03-18 01:37:30 +00:00
jsonOps add error "errorRemovingTempFiles"
2019-08-17 13:09:31 +00:00
fi
2019-09-24 16:11:45 +00:00
output 1 '\n'
2019-08-17 13:09:31 +00:00
}
boot() {
load_package_config
2021-03-18 01:37:30 +00:00
sleep "$bootDelay"
rc_procd start_service 'on_boot' && rc_procd service_triggers
2018-07-17 00:41:39 +00:00
}
start_service() {
2019-08-17 13:09:31 +00:00
is_enabled 'on_start' || return 1
2020-03-29 05:13:07 +00:00
local action status error message stats c
2019-08-22 23:34:14 +00:00
2021-03-18 01:37:30 +00:00
status="$(jsonOps get status)"
error="$(jsonOps get error)"
message="$(jsonOps get message)"
stats="$(jsonOps get stats)"
action="$(jsonOps get triggers)"
2019-07-04 22:38:58 +00:00
2020-04-07 21:55:22 +00:00
if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
if cacheOps 'testGzip' || cacheOps 'test'; then
action='restore'
2020-04-22 17:45:53 +00:00
else
action='download'
2020-04-07 21:55:22 +00:00
fi
elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
2019-10-06 16:33:33 +00:00
action='download'
2020-04-07 21:55:22 +00:00
elif [ ! -s "$outputFile" ]; then
if cacheOps 'testGzip' || cacheOps 'test'; then
action='restore'
else
action='download'
fi
2019-10-06 16:33:33 +00:00
elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
action='restart'
2020-03-29 05:13:07 +00:00
elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
2021-08-02 06:34:08 +00:00
status_service
2019-10-06 16:33:33 +00:00
exit 0
else
action='download'
fi
2019-07-04 22:38:58 +00:00
2021-03-18 01:37:30 +00:00
jsonOps del all
jsonOps set triggers
2019-08-22 23:34:14 +00:00
2021-02-02 22:11:06 +00:00
procd_open_instance 'main'
procd_set_param command /bin/true
procd_set_param stdout 1
procd_set_param stderr 1
procd_open_data
json_add_array firewall
if [ "$forceDNS" -ne 0 ]; then
2021-03-18 01:37:30 +00:00
for c in $forceDNSPorts; do
if netstat -tuln | grep LISTEN | grep ":${c}" >/dev/null 2>&1; then
2021-04-10 18:54:41 +00:00
json_add_object ""
json_add_string type redirect
json_add_string target DNAT
json_add_string src lan
json_add_string proto "tcp udp"
2021-03-18 01:37:30 +00:00
json_add_string src_dport "$c"
json_add_string dest_port "$c"
2021-04-10 18:54:41 +00:00
json_add_boolean reflection 0
2021-03-18 01:37:30 +00:00
json_close_object
else
2021-04-10 18:54:41 +00:00
json_add_object ""
json_add_string type rule
json_add_string src lan
json_add_string dest "*"
json_add_string proto "tcp udp"
2021-03-18 01:37:30 +00:00
json_add_string dest_port "$c"
2021-04-10 18:54:41 +00:00
json_add_string target REJECT
2021-03-18 01:37:30 +00:00
json_close_object
fi
done
2021-02-02 22:11:06 +00:00
fi
if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
2021-04-10 18:54:41 +00:00
json_add_object ""
json_add_string type ipset
json_add_string name adb
json_add_string match dest_net
json_add_string storage hash
2021-02-02 22:11:06 +00:00
json_close_object
2021-04-10 18:54:41 +00:00
json_add_object ""
json_add_string type rule
json_add_string ipset adb
json_add_string src lan
json_add_string dest "*"
json_add_string proto "tcp udp"
json_add_string target REJECT
2021-02-02 22:11:06 +00:00
json_close_object
2019-10-06 16:33:33 +00:00
fi
2021-02-02 22:11:06 +00:00
json_close_array
procd_close_data
procd_close_instance
2019-07-04 22:38:58 +00:00
2019-10-06 16:33:33 +00:00
if [ "$action" = 'restore' ]; then
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
2021-03-18 01:37:30 +00:00
jsonOps set status "statusStarting"
2019-10-06 16:33:33 +00:00
if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
output 3 'Found compressed cache file, unpacking it '
2021-03-18 01:37:30 +00:00
jsonOps set message 'found compressed cache file, unpacking it.'
2019-10-06 16:33:33 +00:00
if cacheOps 'unpackGzip'; then
output_okn
else
output_fail
2021-03-18 01:37:30 +00:00
jsonOps add error "errorRestoreCompressedCache"
2020-03-29 05:13:07 +00:00
output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
2019-10-06 16:33:33 +00:00
action='download'
2018-07-17 00:41:39 +00:00
fi
2017-01-23 14:33:32 +00:00
fi
2019-10-06 16:33:33 +00:00
if cacheOps 'test' && [ ! -s "$outputFile" ]; then
output 3 'Found cache file, reusing it '
2021-03-18 01:37:30 +00:00
jsonOps set message 'found cache file, reusing it.'
2019-10-06 16:33:33 +00:00
if cacheOps 'restore'; then
output_okn
dnsOps 'on_start'
2020-03-29 05:13:07 +00:00
else
2019-10-06 16:33:33 +00:00
output_fail
2021-03-18 01:37:30 +00:00
jsonOps add error "errorRestoreCache"
2020-03-29 05:13:07 +00:00
output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
2019-10-06 16:33:33 +00:00
action='download'
fi
fi
fi
2021-03-18 01:37:30 +00:00
if [ "$action" = 'download' ]; then
if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
output 0 "Force-reloading $serviceName... "
output 3 "Force-reloading $serviceName...\\n"
jsonOps set status "statusForceReloading"
else
2019-10-06 16:33:33 +00:00
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
2021-03-18 01:37:30 +00:00
jsonOps set status "statusStarting"
fi
download_lists
dnsOps 'on_start'
fi
if [ "$action" = 'restart' ]; then
output 0 "Restarting $serviceName... "
output 3 "Restarting $serviceName...\\n"
jsonOps set status "statusRestarting"
dnsOps 'on_start'
fi
if [ "$action" = 'start' ]; then
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
jsonOps set status "statusStarting"
dnsOps 'on_start'
fi
if [ -s "$outputFile" ] && [ "$(jsonOps get status)" != "statusFail" ]; then
2019-10-06 16:33:33 +00:00
output 0 "$__OK__\\n";
2021-03-18 01:37:30 +00:00
jsonOps del message
jsonOps set status "statusSuccess"
jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
2021-08-02 06:34:08 +00:00
status_service
2018-07-17 00:41:39 +00:00
else
2019-10-06 16:33:33 +00:00
output 0 "$__FAIL__\\n";
2021-03-18 01:37:30 +00:00
jsonOps set status "statusFail"
jsonOps add error "errorOhSnap"
2021-08-02 06:34:08 +00:00
status_service
2017-01-23 14:33:32 +00:00
fi
2018-07-17 00:41:39 +00:00
}
2017-01-23 14:33:32 +00:00
2021-02-02 22:11:06 +00:00
service_started() { procd_set_config_changed firewall; }
service_stopped() { procd_set_config_changed firewall; }
2021-04-10 18:54:41 +00:00
2019-09-24 16:11:45 +00:00
restart_service() { rc_procd start_service 'restart'; }
2021-04-10 18:54:41 +00:00
reload_service() { rc_procd start_service 'restart'; }
2019-09-24 16:11:45 +00:00
dl() { rc_procd start_service 'download'; }
2021-04-10 18:54:41 +00:00
2019-08-17 13:09:31 +00:00
killcache() {
2022-08-19 21:38:16 +00:00
rm -f "$dnsmasqAddnhostsCache" "$dnsmasqAddnhostsGzip"
rm -f "$dnsmasqConfCache" "$dnsmasqConfGzip"
rm -f "$dnsmasqIpsetCache" "$dnsmasqIpsetGzip"
rm -f "$dnsmasqNftsetCache" "$dnsmasqNftsetGzip"
rm -f "$dnsmasqServersCache" "$dnsmasqServersGzip"
2019-08-17 13:09:31 +00:00
rm -f "$unboundCache" "$unboundGzip"
2020-03-29 05:13:07 +00:00
config_load 'dhcp'
config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
2022-08-19 21:38:16 +00:00
uci_commit 'dhcp'
2019-08-17 13:09:31 +00:00
return 0
}
2021-08-02 06:34:08 +00:00
status_service() {
2020-09-20 00:16:36 +00:00
local c url status message error stats
2021-08-02 06:34:08 +00:00
config_load "$packageName"
config_get verbosity 'config' 'verbosity' '2'
2021-03-18 01:37:30 +00:00
status="$(jsonOps get status)"
message="$(jsonOps get message)"
error="$(jsonOps get error)"
stats="$(jsonOps get stats)"
2020-03-29 05:13:07 +00:00
if [ "$status" = "statusSuccess" ]; then
output "$stats "; output_okn;
else
[ -n "$status" ] && status="$(getStatusText "$status")"
if [ -n "$status" ] && [ -n "$message" ]; then
status="${status}: $message"
fi
[ -n "$status" ] && output "$serviceName $status\\n"
fi
if [ -n "$error" ]; then
for c in $error; do
2020-05-27 00:25:57 +00:00
url="${c##*|}"
c="${c%|*}"
2020-03-29 05:13:07 +00:00
case "$c" in
errorDownloadingList|errorParsingList)
output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
*)
output "$_ERROR_: $(getErrorText "$c")!\\n";;
esac
2021-08-02 06:34:08 +00:00
n=$((n+1))
2020-03-29 05:13:07 +00:00
done
2019-07-04 22:38:58 +00:00
fi
}
2018-07-17 00:41:39 +00:00
stop_service() {
load_package_config
2019-08-17 13:09:31 +00:00
if [ -s "$outputFile" ]; then
output "Stopping $serviceName... "
2019-10-06 16:33:33 +00:00
cacheOps 'create'
if dnsOps 'on_stop'; then
2019-08-17 13:09:31 +00:00
led_off "$led"
2020-03-29 05:13:07 +00:00
output 0 "$__OK__\\n"; output_okn;
2021-03-18 01:37:30 +00:00
jsonOps set status "statusStopped"
jsonOps del message
2019-08-17 13:09:31 +00:00
else
2020-03-29 05:13:07 +00:00
output 0 "$__FAIL__\\n"; output_fail;
2021-03-18 01:37:30 +00:00
jsonOps set status "statusFail"
jsonOps add error "errorStopping"
2020-03-29 05:13:07 +00:00
output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
2019-08-17 13:09:31 +00:00
fi
2017-01-23 14:33:32 +00:00
fi
}
2020-03-29 05:13:07 +00:00
service_triggers() {
2021-02-02 22:11:06 +00:00
procd_open_trigger
2021-03-18 01:37:30 +00:00
procd_add_config_trigger 'config.change' "${packageName}" /etc/init.d/${packageName} reload
procd_add_reload_interface_trigger 'wan'
procd_add_interface_trigger 'interface.*.up' 'wan' /etc/init.d/${packageName} reload
2021-02-02 22:11:06 +00:00
procd_close_trigger
2020-03-29 05:13:07 +00:00
}
2022-08-19 21:38:16 +00:00
allow() {
load_package_config
local c string="$1"
if [ ! -s "$outputFile" ]; then
echo "No block-list ('$outputFile') found."
elif [ -z "$string" ]; then
echo "Usage: /etc/init.d/${packageName} allow domain(s)"
else
case "$targetDNS" in
dnsmasq.addnhosts|dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers)
output 1 "Allowing domain(s) and restarting DNSMASQ... "
output 2 "Allowing domain(s)... \\n"
for c in $string; do
output 2 " $c "
if sed -i "/${string}/d" "$outputFile" && \
uci_add_list "${packageName}" 'config' 'allowed_domain' "$string"; then
output_ok
else
output_fail
fi
done
output 2 "Committing changes to config... "
if [ -n "$(uci_changes "$packageName")" ] && uci_commit "$packageName"; then
2022-08-26 18:43:45 +00:00
allowed_domains="$(uci_get "$packageName" 'config' 'allowed_domain')"
2022-08-19 21:38:16 +00:00
jsonOps set triggers
jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
output_ok;
if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
output 2 "Flushing adb ipset... "
if command -v ipset && ipset flush adb; then output_ok; else output_fail; fi
fi
output 2 "Restarting DNSMASQ... "
if dnsmasq_restart; then output_okn; else output_failn; fi
else
output_fail;
fi
;;
unbound.adb_list)
output 1 "Allowing domain(s) and restarting Unbound... "
output 2 "Allowing domain(s)... \\n"
for c in $string; do
output 2 " $c "
if sed -i "/${string}/d" "$outputFile" && \
uci_add_list "$packageName" 'config' 'allowed_domain' "$string"; then
output_ok
else
output_fail
fi
done
output 2 "Committing changes to config... "
if [ -n "$(uci_changes "$packageName")" ] && uci_commit "$packageName"; then
2022-08-26 18:43:45 +00:00
allowed_domains="$(uci_get "$packageName" 'config' 'allowed_domain')"
2022-08-19 21:38:16 +00:00
jsonOps set triggers
jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
output_ok;
output 2 "Restarting Unbound... "
if unbound_restart; then output_okn; else output_failn; fi
else
output_fail;
fi
esac
fi
}
2018-01-26 03:35:52 +00:00
check() {
load_package_config
2021-10-15 21:25:08 +00:00
local c string="$1"
2019-10-06 16:33:33 +00:00
if [ ! -s "$outputFile" ]; then
2020-06-13 20:39:00 +00:00
echo "No block-list ('$outputFile') found."
2018-01-26 03:35:52 +00:00
elif [ -z "$string" ]; then
2019-09-24 16:11:45 +00:00
echo "Usage: /etc/init.d/${packageName} check string"
2022-08-19 21:38:16 +00:00
else
c="$(grep -c "$string" "$outputFile")"
if [ "$c" -gt 0 ]; then
if [ "$c" -eq 1 ]; then
echo "Found 1 match for '$string' in '$outputFile':"
else
echo "Found $c matches for '$string' in '$outputFile'."
fi
if [ "$c" -le 20 ]; then
case "$targetDNS" in
dnsmasq.addnhosts)
grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
dnsmasq.conf)
grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
dnsmasq.ipset)
grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
dnsmasq.nftset)
grep "$string" "$outputFile" | sed 's|nftset=/||;s|/adb$||;';;
dnsmasq.servers)
grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
unbound.adb_list)
grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
esac
fi
2019-09-24 16:11:45 +00:00
else
2022-08-19 21:38:16 +00:00
echo "The '$string' is not found in current block-list ('$outputFile')."
2019-09-24 16:11:45 +00:00
fi
2018-01-26 03:35:52 +00:00
fi
2017-01-23 14:33:32 +00:00
}
2019-10-04 01:41:49 +00:00
sizes() {
local i
load_package_config
echo "# $(date)"
2020-06-13 20:39:00 +00:00
for i in $blocked_domains_urls; do
2019-10-04 01:41:49 +00:00
[ "${i//melmac}" != "$i" ] && continue
if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
2021-03-08 10:25:11 +00:00
echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
2022-08-19 21:38:16 +00:00
if is_greater "$(du -sk /tmp/sast)" "500"; then
2020-06-13 20:39:00 +00:00
echo "# block-list too big for most routers"
2022-08-19 21:38:16 +00:00
elif is_greater "$(du -sk /tmp/sast)" "100"; then
2020-06-13 20:39:00 +00:00
echo "# block-list may be too big for some routers"
2019-10-04 01:41:49 +00:00
fi
rm -rf /tmp/sast
2020-06-13 20:39:00 +00:00
echo " list blocked_domains_url '$i'"
2019-10-04 01:41:49 +00:00
echo ""
else
echo "# site was down on last check"
2020-06-13 20:39:00 +00:00
echo "# list blocked_domains_url '$i'"
2019-10-04 01:41:49 +00:00
echo ""
fi
done
2020-06-13 20:39:00 +00:00
for i in $blocked_hosts_urls; do
2019-10-04 01:41:49 +00:00
if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
2021-03-08 10:25:11 +00:00
echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
2022-08-19 21:38:16 +00:00
if is_greater "$(du -sk /tmp/sast)" "500"; then
2020-06-13 20:39:00 +00:00
echo "# block-list too big for most routers"
2022-08-19 21:38:16 +00:00
elif is_greater "$(du -sk /tmp/sast)" "100"; then
2020-06-13 20:39:00 +00:00
echo "# block-list may be too big for some routers"
2019-10-04 01:41:49 +00:00
fi
rm -rf /tmp/sast
2020-06-13 20:39:00 +00:00
echo " list blocked_hosts_url '$i'"
2019-10-04 01:41:49 +00:00
echo ""
else
echo "# site was down on last check"
2020-06-13 20:39:00 +00:00
echo "# list blocked_hosts_url '$i'"
2019-10-04 01:41:49 +00:00
echo ""
fi
done
}