simple-adblock: jsonOps-related bugfixes

Signed-off-by: Stan Grishin <stangri@melmac.net>
This commit is contained in:
Stan Grishin 2021-03-18 01:37:30 +00:00 committed by Paul Spooren
parent b164a3aa32
commit e695cc26b0
3 changed files with 150 additions and 142 deletions

View File

@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=simple-adblock
PKG_VERSION:=1.8.5
PKG_VERSION:=1.8.7
PKG_RELEASE:=1
PKG_MAINTAINER:=Stan Grishin <stangri@melmac.net>
PKG_LICENSE:=GPL-3.0-or-later
@ -38,15 +38,11 @@ define Build/Compile
endef
define Package/simple-adblock/install
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_DIR) $(1)/etc/init.d $(1)/etc/config $(1)/tmp
$(INSTALL_BIN) ./files/simple-adblock.init $(1)/etc/init.d/simple-adblock
$(SED) "s|^\(PKG_VERSION\).*|\1='$(PKG_VERSION)-$(PKG_RELEASE)'|" $(1)/etc/init.d/simple-adblock
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_CONF) ./files/simple-adblock.conf $(1)/etc/config/simple-adblock
$(INSTALL_DIR) $(1)/tmp
$(INSTALL_CONF) ./files/simple-adblock.conf.update $(1)/tmp/simple-adblock.conf.update
$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
$(INSTALL_BIN) ./files/simple-adblock.hotplug $(1)/etc/hotplug.d/iface/80-simple-adblock
endef
define Package/simple-adblock/postinst

View File

@ -6,6 +6,16 @@ config simple-adblock 'config'
option dns_instance '0'
option verbosity '2'
option force_dns '1'
list force_dns_port '53'
list force_dns_port '853'
# ports listed below are used by some
# of the dnscrypt-proxy v1 resolvers
# list force_dns_port '553'
# list force_dns_port '1443'
# list force_dns_port '4343'
# list force_dns_port '4434'
# list force_dns_port '5443'
# list force_dns_port '8443'
option led 'none'
option boot_delay '120'
option download_timeout '10'

View File

@ -41,7 +41,6 @@ readonly unboundGzip="/etc/${packageName}.unbound.gz"
readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
readonly A_TMP="/var/${packageName}.hosts.a.tmp"
readonly B_TMP="/var/${packageName}.hosts.b.tmp"
readonly PIDFile="/var/run/${packageName}.pid"
readonly jsonFile="/var/run/${packageName}.json"
readonly sharedMemoryError="/dev/shm/$packageName-error"
readonly sharedMemoryOutput="/dev/shm/$packageName-output"
@ -99,9 +98,6 @@ getErrorText() {
printf "%b" "$_ret"
}
create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
trap remove_lock EXIT
output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
@ -138,9 +134,10 @@ output() {
fi
}
serviceEnabled=''; forceDNS=''; parallelDL=''; debug=''; compressedCache='';
ipv6Enabled=''; configUpdateEnabled=''; configUpdateURL=''; bootDelay='';
dlTimeout=''; curlRetry=''; verbosity=''; led=''; targetDNS=''; dnsInstance='';
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='';
@ -156,11 +153,12 @@ load_package_config() {
config_get_bool compressedCache 'config' 'compressed_cache' 0
config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
config_get_bool configUpdateEnabled 'config' 'config_update_enabled' 0
config_get forceDNSPorts 'config' 'force_dns_port' '53 853'
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'
config_get led 'config' 'led'
config_get led 'config' 'led'
config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
config_get dnsInstance 'config' 'dns_instance' '0'
config_get allowed_domains 'config' 'allowed_domain'
@ -354,8 +352,8 @@ dnsOps() {
case $1 in
on_start)
if [ ! -s "$outputFile" ]; then
tmpfs set status "statusFail"
tmpfs add error "errorOutputFileCreate"
jsonOps set status "statusFail"
jsonOps add error "errorOutputFileCreate"
output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
return 1
fi
@ -396,15 +394,15 @@ dnsOps() {
fi
output 1 "$output_text "
output 2 "$output_text "
tmpfs set message "$output_text"
jsonOps set message "$output_text"
if eval "$param"; then
tmpfs set status "statusSuccess"
jsonOps set status "statusSuccess"
led_on "$led"
output_okn
else
output_fail
tmpfs set status "statusFail"
tmpfs add error "errorDNSReload"
jsonOps set status "statusFail"
jsonOps add error "errorDNSReload"
output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
return 1
fi
@ -447,7 +445,7 @@ dnsOps() {
esac
}
tmpfs() {
jsonOps() {
# shellcheck disable=SC2034
local action="$1" param="$2" value="$3"
local status message error stats
@ -463,8 +461,8 @@ tmpfs() {
get)
case "$param" in
triggers)
curReload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
curRestart="$compressedCache $forceDNS $led"
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"
if [ ! -s "$jsonFile" ]; then
ret='on_boot'
elif [ "$curReload" != "$reload" ]; then
@ -479,7 +477,11 @@ tmpfs() {
esac
;;
add)
eval "$param"='$(eval echo "\$$param")${value} ';;
if [ -n "$(eval echo "\$$param")" ]; then
value="$(eval echo "\$$param") ${value}"
fi
eval "$param"='${value}'
;;
del)
case "$param" in
all)
@ -493,8 +495,8 @@ tmpfs() {
set)
case "$param" in
triggers)
reload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS"
restart="$compressedCache $forceDNS $led"
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"
;;
*)
eval "$param"='$value';;
@ -608,15 +610,15 @@ process_config_update() {
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"
tmpfs add error "errorDownloadingConfigUpdate"
jsonOps add error "errorDownloadingConfigUpdate"
else
if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock; then
if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock 2>/dev/null; then
output 1 "$_OK_"
output 2 "[DL] Config Update: $label $__OK__\\n"
else
output 1 "$_FAIL_"
output 2 "[DL] Config Update: $label $__FAIL__\\n"
tmpfs add error "errorParsingConfigUpdate"
jsonOps add error "errorParsingConfigUpdate"
fi
fi
rm -f "$R_TMP"
@ -626,8 +628,8 @@ process_config_update() {
download_lists() {
local hf w_filter j=0 R_TMP
tmpfs set message "$(getStatusText "statusDownloading")..."
tmpfs set status "statusDownloading"
jsonOps set message "$(getStatusText "statusDownloading")..."
jsonOps set status "statusDownloading"
rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
@ -673,7 +675,7 @@ download_lists() {
output 1 '\n'
if [ -s "$sharedMemoryError" ]; then
while IFS= read -r line; do
tmpfs add error "$line"
jsonOps add error "$line"
done < "$sharedMemoryError"
rm -f "$sharedMemoryError"
fi
@ -687,20 +689,20 @@ $(cat $A_TMP)"
output 1 'Processing downloads '
output 2 'Sorting combined list '
tmpfs set message "$(getStatusText "statusProcessing"): sorting combined list"
jsonOps set message "$(getStatusText "statusProcessing"): sorting combined list"
if [ "$allowIDN" -gt 0 ]; then
if sort -u "$B_TMP" > "$A_TMP"; then
output_ok
else
output_failn
tmpfs add error "errorSorting"
jsonOps add error "errorSorting"
fi
else
if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
output_ok
else
output_failn
tmpfs add error "errorSorting"
jsonOps add error "errorSorting"
fi
fi
@ -710,7 +712,7 @@ $(cat $A_TMP)"
[ "$targetDNS" = 'unbound.adb_list' ]; then
# TLD optimization written by Dirk Brenken (dev@brenken.org)
output 2 'Optimizing combined list '
tmpfs set message "$(getStatusText "statusProcessing"): optimizing combined list"
jsonOps set message "$(getStatusText "statusProcessing"): optimizing combined list"
# 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
if sort "$B_TMP" > "$A_TMP"; then
@ -720,25 +722,25 @@ $(cat $A_TMP)"
output_ok
else
output_failn
tmpfs add error "errorOptimization"
jsonOps add error "errorOptimization"
mv "$A_TMP" "$B_TMP"
fi
else
output_failn
tmpfs add error "errorOptimization"
jsonOps add error "errorOptimization"
fi
else
output_failn
tmpfs add error "errorOptimization"
jsonOps add error "errorOptimization"
mv "$A_TMP" "$B_TMP"
fi
else
output_failn
tmpfs add error "errorOptimization"
jsonOps add error "errorOptimization"
fi
else
output_failn
tmpfs add error "errorOptimization"
jsonOps add error "errorOptimization"
mv "$A_TMP" "$B_TMP"
fi
else
@ -746,22 +748,22 @@ $(cat $A_TMP)"
fi
output 2 'Allowing domains '
tmpfs set message "$(getStatusText "statusProcessing"): allowing domains"
jsonOps set message "$(getStatusText "statusProcessing"): allowing domains"
if sed -i "$w_filter" "$B_TMP"; then
output_ok
else
output_failn
tmpfs add error "errorAllowListProcessing"
jsonOps add error "errorAllowListProcessing"
fi
output 2 'Formatting merged file '
tmpfs set message "$(getStatusText "statusProcessing"): formatting merged file"
jsonOps set message "$(getStatusText "statusProcessing"): formatting merged file"
if [ -z "$outputFilterIPv6" ]; then
if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
output_ok
else
output_failn
tmpfs add error "errorDataFileFormatting"
jsonOps add error "errorDataFileFormatting"
fi
else
case "$targetDNS" in
@ -771,7 +773,7 @@ $(cat $A_TMP)"
output_ok
else
output_failn
tmpfs add error "errorDataFileFormatting"
jsonOps add error "errorDataFileFormatting"
fi
;;
esac
@ -780,77 +782,70 @@ $(cat $A_TMP)"
case "$targetDNS" in
dnsmasq.addnhosts)
output 2 'Creating DNSMASQ addnhosts file '
tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
;;
dnsmasq.conf)
output 2 'Creating DNSMASQ config file '
tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
;;
dnsmasq.ipset)
output 2 'Creating DNSMASQ ipset file '
tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
;;
dnsmasq.servers)
output 2 'Creating DNSMASQ servers file '
tmpfs set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
;;
unbound.adb_list)
output 2 'Creating Unbound adb_list file '
tmpfs set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
jsonOps set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
;;
esac
if mv "$A_TMP" "$outputFile"; then
output_ok
else
output_failn
tmpfs add error "errorMovingDataFile"
jsonOps add error "errorMovingDataFile"
fi
if [ "$compressedCache" -gt 0 ]; then
output 2 'Creating compressed cache '
tmpfs set message "$(getStatusText "statusProcessing"): creating compressed cache"
jsonOps set message "$(getStatusText "statusProcessing"): creating compressed cache"
if cacheOps 'createGzip'; then
output_ok
else
output_failn
tmpfs add error "errorCreatingCompressedCache"
jsonOps add error "errorCreatingCompressedCache"
fi
else
rm -f "$outputGzip"
fi
output 2 'Removing temporary files '
tmpfs set message "$(getStatusText "statusProcessing"): removing temporary files"
jsonOps set message "$(getStatusText "statusProcessing"): removing temporary files"
rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
if [ $j -eq 0 ]; then
output_ok
else
output_failn
tmpfs add error "errorRemovingTempFiles"
jsonOps add error "errorRemovingTempFiles"
fi
output 1 '\n'
}
boot() {
load_package_config
if create_lock; then
sleep "$bootDelay"
remove_lock
rc_procd start_service 'on_boot' && rc_procd service_triggers
fi
sleep "$bootDelay"
rc_procd start_service 'on_boot' && rc_procd service_triggers
}
start_service() {
is_enabled 'on_start' || return 1
local action status error message stats c
if ! create_lock; then
output 3 "$serviceName: another instance is starting up "; output_fail
return 0
fi
status="$(tmpfs get status)"
error="$(tmpfs get error)"
message="$(tmpfs get message)"
stats="$(tmpfs get stats)"
action="$(tmpfs get triggers)"
status="$(jsonOps get status)"
error="$(jsonOps get error)"
message="$(jsonOps get message)"
stats="$(jsonOps get stats)"
action="$(jsonOps get triggers)"
if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
if cacheOps 'testGzip' || cacheOps 'test'; then
@ -869,14 +864,14 @@ start_service() {
elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
action='restart'
elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
[ "$1" != 'hotplug' ] && showstatus
showstatus
exit 0
else
action='download'
fi
tmpfs del all
tmpfs set triggers
jsonOps del all
jsonOps set triggers
procd_open_instance 'main'
procd_set_param command /bin/true
@ -885,34 +880,43 @@ start_service() {
procd_open_data
json_add_array firewall
if [ "$forceDNS" -ne 0 ]; then
json_add_object ''
json_add_string type redirect
json_add_string name simple_adblock_dns_redirect
json_add_string target DNAT
json_add_string src lan
json_add_string proto tcpudp
json_add_string src_dport 53
json_add_string dest_port 53
json_add_string reflection 0
json_close_object
for c in $forceDNSPorts; do
if netstat -tuln | grep LISTEN | grep ":${c}" >/dev/null 2>&1; then
json_add_object ''
json_add_string type 'redirect'
json_add_string target 'DNAT'
json_add_string src 'lan'
json_add_string proto 'tcp udp'
json_add_string src_dport "$c"
json_add_string dest_port "$c"
json_add_string reflection '0'
json_close_object
else
json_add_object ''
json_add_string type 'rule'
json_add_string src 'lan'
json_add_string dest '*'
json_add_string proto 'tcp udp'
json_add_string dest_port "$c"
json_add_string target 'REJECT'
json_close_object
fi
done
fi
if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
json_add_object ''
json_add_string type ipset
json_add_string name adb
json_add_string match dest_net
json_add_string storage hash
json_add_string enabled 1
json_add_string type 'ipset'
json_add_string name 'adb'
json_add_string match 'dest_net'
json_add_string storage 'hash'
json_close_object
json_add_object ''
json_add_string type rule
json_add_string name simple_adblock_ipset_rule
json_add_string ipset adb
json_add_string src lan
json_add_string type 'rule'
json_add_string ipset 'adb'
json_add_string src 'lan'
json_add_string dest '*'
json_add_string proto tcpudp
json_add_string target REJECT
json_add_string enabled 1
json_add_string proto 'tcp udp'
json_add_string target 'REJECT'
json_close_object
fi
json_close_array
@ -922,74 +926,70 @@ start_service() {
if [ "$action" = 'restore' ]; then
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
tmpfs set status "statusStarting"
jsonOps set status "statusStarting"
if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
output 3 'Found compressed cache file, unpacking it '
tmpfs set message 'found compressed cache file, unpacking it.'
jsonOps set message 'found compressed cache file, unpacking it.'
if cacheOps 'unpackGzip'; then
output_okn
else
output_fail
tmpfs add error "errorRestoreCompressedCache"
jsonOps add error "errorRestoreCompressedCache"
output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
action='download'
fi
fi
if cacheOps 'test' && [ ! -s "$outputFile" ]; then
output 3 'Found cache file, reusing it '
tmpfs set message 'found cache file, reusing it.'
jsonOps set message 'found cache file, reusing it.'
if cacheOps 'restore'; then
output_okn
dnsOps 'on_start'
else
output_fail
tmpfs add error "errorRestoreCache"
jsonOps add error "errorRestoreCache"
output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
action='download'
fi
fi
fi
case "$action" in
download)
if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
output 0 "Force-reloading $serviceName... "
output 3 "Force-reloading $serviceName...\\n"
tmpfs set status "statusForceReloading"
else
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
tmpfs set status "statusStarting"
fi
download_lists
dnsOps 'on_start'
;;
restart)
output 0 "Restarting $serviceName... "
output 3 "Restarting $serviceName...\\n"
tmpfs set status "statusRestarting"
dnsOps 'on_start'
;;
start)
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
output 0 "Starting $serviceName... "
output 3 "Starting $serviceName...\\n"
tmpfs set status "statusStarting"
dnsOps 'on_start'
;;
esac
if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
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
output 0 "$__OK__\\n";
tmpfs del message
tmpfs set status "statusSuccess"
c="$(wc -l < "$outputFile")"
tmpfs set stats "$serviceName is blocking $c domains (with ${targetDNS})"
jsonOps del message
jsonOps set status "statusSuccess"
jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
showstatus
else
output 0 "$__FAIL__\\n";
tmpfs set status "statusFail"
tmpfs add error "errorOhSnap"
jsonOps set status "statusFail"
jsonOps add error "errorOhSnap"
showstatus
fi
remove_lock
}
service_started() { procd_set_config_changed firewall; }
@ -1015,10 +1015,10 @@ show() { showstatus; }
status_service() { showstatus; }
showstatus() {
local c url status message error stats
status="$(tmpfs get status)"
message="$(tmpfs get message)"
error="$(tmpfs get error)"
stats="$(tmpfs get stats)"
status="$(jsonOps get status)"
message="$(jsonOps get message)"
error="$(jsonOps get error)"
stats="$(jsonOps get stats)"
if [ "$status" = "statusSuccess" ]; then
output "$stats "; output_okn;
else
@ -1051,12 +1051,12 @@ stop_service() {
if dnsOps 'on_stop'; then
led_off "$led"
output 0 "$__OK__\\n"; output_okn;
tmpfs set status "statusStopped"
tmpfs del message
jsonOps set status "statusStopped"
jsonOps del message
else
output 0 "$__FAIL__\\n"; output_fail;
tmpfs set status "statusFail"
tmpfs add error "errorStopping"
jsonOps set status "statusFail"
jsonOps add error "errorStopping"
output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
fi
fi
@ -1064,7 +1064,9 @@ stop_service() {
service_triggers() {
procd_open_trigger
procd_add_config_trigger "config.change" "${packageName}" /etc/init.d/${packageName} reload
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
procd_close_trigger
}