Skip to content

Commit

Permalink
adblock-fast: bugfix: dnsmasq instances confdir hack
Browse files Browse the repository at this point in the history
* temporary hack until openwrt/openwrt#16806 is merged

Signed-off-by: Stan Grishin <[email protected]>
  • Loading branch information
stangri committed Dec 6, 2024
1 parent 8267680 commit c320e46
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion net/adblock-fast/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=adblock-fast
PKG_VERSION:=1.1.2
PKG_RELEASE:=10
PKG_RELEASE:=18
PKG_MAINTAINER:=Stan Grishin <[email protected]>
PKG_LICENSE:=AGPL-3.0-or-later

Expand Down
83 changes: 53 additions & 30 deletions net/adblock-fast/files/etc/init.d/adblock-fast
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fi

readonly packageName='adblock-fast'
readonly PKG_VERSION='dev-test'
readonly packageCompat='3'
readonly packageCompat='4'
readonly serviceName="$packageName $PKG_VERSION"
readonly packageConfigFile="/etc/config/${packageName}"
readonly dnsmasqAddnhostsFile="/var/run/${packageName}/dnsmasq.addnhosts"
Expand Down Expand Up @@ -465,9 +465,9 @@ get_url_filesize() {

# shellcheck disable=SC3060
output() {
local v="${verbosity:-1}"
[ -z "$verbosity" ] && verbosity="$(uci_get "$packageName" 'config' 'verbosity' '1')"
[ "$#" -ne '1' ] && {
case "$1" in [0-9]) [ $((v & $1)) -gt 0 ] && shift || return 0;; esac }
case "$1" in [0-9]) [ $((verbosity & $1)) -gt 0 ] && shift || return 0;; esac }
local msg="$*" queue="/dev/shm/$packageName-output"
[ -t 1 ] && printf "%b" "$msg"
[ "$msg" != "${msg//\\n}" ] && {
Expand Down Expand Up @@ -765,9 +765,9 @@ load_environment() {
dns_set_output_values "$dns"

[ "$dns" = 'dnsmasq.addnhosts' ] || rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
[ "$dns" = 'dnsmasq.conf' ] || rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
[ "$dns" = 'dnsmasq.ipset' ] || rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
[ "$dns" = 'dnsmasq.nftset' ] || rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
[ "$dns" = 'dnsmasq.conf' ] || rm -f "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
[ "$dns" = 'dnsmasq.ipset' ] || rm -f "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
[ "$dns" = 'dnsmasq.nftset' ] || rm -f "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
[ "$dns" = 'dnsmasq.servers' ] || rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
[ "$dns" = 'smartdns.domainset' ] || rm -f "$smartdnsDomainSetFile" "$smartdnsDomainSetCache" "${compressed_cache_dir}/${smartdnsDomainSetGzip}" "$smartdnsDomainSetConfig"
[ "$dns" = 'smartdns.ipset' ] || rm -f "$smartdnsIpsetFile" "$smartdnsIpsetCache" "${compressed_cache_dir}/${smartdnsIpsetGzip}" "$smartdnsIpsetConfig"
Expand All @@ -788,12 +788,11 @@ load_environment() {
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
is_present 'gawk' || s="${s:+$s }gawk"
is_present '/usr/libexec/grep-gnu' || s="${s:+$s }grep"
is_present '/usr/libexec/sed-gnu' || s="${s:+$s }sed"
is_present '/usr/libexec/sort-coreutils' || s="${s:+$s }coreutils-sort"
is_present 'gawk' || { json add warning 'warningMissingRecommendedPackages' 'gawk'; s="${s:+$s }gawk"; }
is_present '/usr/libexec/grep-gnu' || { json add warning 'warningMissingRecommendedPackages' 'grep'; s="${s:+$s }grep"; }
is_present '/usr/libexec/sed-gnu' || { json add warning 'warningMissingRecommendedPackages' 'sed'; s="${s:+$s }sed"; }
is_present '/usr/libexec/sort-coreutils' || { json add warning 'warningMissingRecommendedPackages' 'coreutils-sort'; s="${s:+$s }coreutils-sort"; }
if [ "$param" != 'quiet' ]; then
json add warning 'warningMissingRecommendedPackages' "$s"
output "${_WARNING_}: $(get_text 'warningMissingRecommendedPackages'), install them by running:\n"
output "opkg update; opkg --force-overwrite install $s;\n"
fi
Expand Down Expand Up @@ -855,6 +854,13 @@ resolver() {
uci_add_list_if_new 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
;;
cleanup|unbound.adb_list)
# shellcheck disable=SC2016
if grep -q 'config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq${cfg:+.$cfg}.d"' '/etc/init.d/dnsmasq'; then
config_get confdir "$cfg" 'confdir' "/tmp/dnsmasq${cfg:+.$cfg}.d"
else
config_get confdir "$cfg" 'confdir' '/tmp/dnsmasq.d'
fi
rm -f "${confdir}/${dnsmasqConfFile}" "${confdir}/${dnsmasqIpsetFile}" "${confdir}/${dnsmasqNftsetFile}"
uci_remove_list 'dhcp' "$cfg" 'addnhosts' "$dnsmasqAddnhostsFile"
if [ "$(uci_get 'dhcp' "$cfg" 'serversfile')" = "$dnsmasqServersFile" ]; then
uci_remove 'dhcp' "$cfg" 'serversfile'
Expand All @@ -874,16 +880,20 @@ resolver() {
;;
esac
}
# shellcheck disable=SC2317
# shellcheck disable=SC2016,SC2317
_dnsmasq_instance_init() {
local cfg="$1" param="$2" confdir confdirFile
[ -s "/etc/config/dhcp" ] || return 0
case "$param" in
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
config_get confdir "$cfg" 'confdir' '/tmp/dnsmasq.d'
if grep -q 'config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq${cfg:+.$cfg}.d"' '/etc/init.d/dnsmasq'; then
config_get confdir "$cfg" 'confdir' "/tmp/dnsmasq${cfg:+.$cfg}.d"
else
config_get confdir "$cfg" 'confdir' '/tmp/dnsmasq.d'
fi
confdirFile="${confdir}/${outputFile}"
if ! str_contains "$dnsmasqFileList" "$confdirFile"; then
dnsmasqFileList="${dnsmasqFileList:+$dnsmasqFileList }${confdirFile}"
if ! str_contains "$outputDnsmasqFileList" "$confdirFile"; then
outputDnsmasqFileList="${outputDnsmasqFileList:+$outputDnsmasqFileList }${confdirFile}"
fi
;;
esac
Expand Down Expand Up @@ -920,9 +930,9 @@ resolver() {
case $1 in
cleanup)
rm -f "$dnsmasqAddnhostsFile" "$dnsmasqAddnhostsCache" "${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
rm -f "$dnsmasqConfFile" "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
rm -f "$dnsmasqIpsetFile" "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
rm -f "$dnsmasqNftsetFile" "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
rm -f "$dnsmasqConfCache" "${compressed_cache_dir}/${dnsmasqConfGzip}"
rm -f "$dnsmasqIpsetCache" "${compressed_cache_dir}/${dnsmasqIpsetGzip}"
rm -f "$dnsmasqNftsetCache" "${compressed_cache_dir}/${dnsmasqNftsetGzip}"
rm -f "$dnsmasqServersFile" "$dnsmasqServersCache" "${compressed_cache_dir}/${dnsmasqServersGzip}"
rm -f "$smartdnsDomainSetFile" "$smartdnsDomainSetCache" "${compressed_cache_dir}/${smartdnsDomainSetGzip}" "$smartdnsDomainSetConfig"
rm -f "$smartdnsIpsetFile" "$smartdnsIpsetCache" "${compressed_cache_dir}/${smartdnsIpsetGzip}" "$smartdnsIpsetConfig"
Expand All @@ -942,7 +952,7 @@ resolver() {
on_load)
case "$dns" in
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
[ -z "$dnsmasqFileList" ] || return 0
[ -z "$outputDnsmasqFileList" ] || return 0
config_load 'dhcp'
if [ "$dnsmasq_instance" = "*" ]; then
config_foreach _dnsmasq_instance_init 'dnsmasq' "$dns"
Expand All @@ -951,7 +961,7 @@ resolver() {
_dnsmasq_instance_init "@dnsmasq[$i]" "$dns" || _dnsmasq_instance_init "$i" "$dns"
done
fi
outputFile="$(str_first_word "$dnsmasqFileList")"
outputFile="$(str_first_word "$outputDnsmasqFileList")"
;;
esac
;;
Expand Down Expand Up @@ -981,9 +991,9 @@ resolver() {

case "$dns" in
dnsmasq.*)
if [ -n "$dnsmasqFileList" ]; then
if [ -n "$outputDnsmasqFileList" ]; then
local i
for i in $dnsmasqFileList; do
for i in $outputDnsmasqFileList; do
chmod 660 "$i"
chown root:dnsmasq "$i" >/dev/null 2>/dev/null
done
Expand Down Expand Up @@ -1087,9 +1097,9 @@ cache() {
local R_TMP
case "$1" in
create|backup)
if [ -n "$dnsmasqFileList" ]; then
if [ -n "$outputDnsmasqFileList" ]; then
local i __firstFile
for i in $dnsmasqFileList; do
for i in $outputDnsmasqFileList; do
if [ -z "$__firstFile" ]; then
__firstFile="$i"
if ! mv "$i" "$outputCache"; then
Expand All @@ -1107,9 +1117,9 @@ cache() {
fi
;;
restore|use)
if [ -n "$dnsmasqFileList" ]; then
if [ -n "$outputDnsmasqFileList" ]; then
local i __firstFile
for i in $dnsmasqFileList; do
for i in $outputDnsmasqFileList; do
if [ -z "$__firstFile" ]; then
__firstFile="$i"
if ! mv "$outputCache" "$i"; then
Expand Down Expand Up @@ -1170,6 +1180,16 @@ process_file_url_wrapper() {
}

process_file_url() {
_sanitize_source() {
local type="$1" file="$2"
case "$type" in
hosts)
sed -i '/# Title: StevenBlack/,/# Start StevenBlack/d' "$file"
# sed -i -E '/^(.*)[\t ](local|localhost|localhost.localdomain)$/d;/^255.255.255.255[\t ]broadcasthost$/d;/^0.0.0.0[\t ]0.0.0.0$/d' "$file"
# sed -i -E '/^(.*)[\t ](ip6-localhost|ip6-loopback|ip6-localnet|ip6-mcastprefix|ip6-allnodes|ip6-allrouters|ip6-allhosts)/d' "$file"
;;
esac
}
local cfg="$1" new_size
local label type D_TMP R_TMP filter
if [ -z "$cfg" ] || [ -n "${2}${3}" ]; then
Expand Down Expand Up @@ -1219,7 +1239,10 @@ process_file_url() {
dnsmasq2) filter="$dnsmasq2FileFilter";;
dnsmasq3) filter="$dnsmasq3FileFilter";;
domains) filter="$domainsFilter";;
hosts) filter="$hostsFilter";;
hosts)
filter="$hostsFilter"
_sanitize_source 'hosts' "$R_TMP"
;;
*)
output 1 "$_FAIL_"
output 2 "[DL] $type $label $__FAIL__\n"
Expand Down Expand Up @@ -1271,7 +1294,7 @@ download_dnsmasq_file() {
fi
output 2 'Moving dnsmasq file '
local i __firstFile
for i in $dnsmasqFileList; do
for i in $outputDnsmasqFileList; do
if [ -z "$__firstFile" ]; then
__firstFile="$i"
if mv "$B_TMP" "$i"; then
Expand Down Expand Up @@ -1545,7 +1568,7 @@ $(sed '/^[[:space:]]*$/d' "$A_TMP")"
case "$dns" in
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset)
local i __firstFile
for i in $dnsmasqFileList; do
for i in $outputDnsmasqFileList; do
if [ -z "$__firstFile" ]; then
__firstFile="$i"
if mv "$B_TMP" "$i"; then
Expand Down Expand Up @@ -1628,7 +1651,7 @@ adb_allow() {
output 2 " $c "
hf="$(echo "$c" | sed 's/\./\\./g')"
local f
for f in ${dnsmasqFileList:-$outputFile}; do
for f in ${outputDnsmasqFileList:-$outputFile}; do
if sed -i "\:\(/\|\.\)${hf}/:d" "$f"; then
output_ok
else
Expand Down

0 comments on commit c320e46

Please sign in to comment.