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"
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"; }
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='';
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'
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
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
esac
}
-tmpfs() {
+jsonOps() {
# shellcheck disable=SC2034
local action="$1" param="$2" value="$3"
local status message error stats
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
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)
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';;
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"
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
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
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
[ "$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
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
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
output_ok
else
output_failn
- tmpfs add error "errorDataFileFormatting"
+ jsonOps add error "errorDataFileFormatting"
fi
;;
esac
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
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
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
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; }
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
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
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
}