Skip to content

Commit 3574b0d

Browse files
committed
implement dnsmasq validity check, rename sanity check
1 parent 08e78fe commit 3574b0d

File tree

3 files changed

+77
-64
lines changed

3 files changed

+77
-64
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include $(TOPDIR)/rules.mk
55

66
PKG_NAME:=adblock-fast
77
PKG_VERSION:=1.2.1
8-
PKG_RELEASE:=2
8+
PKG_RELEASE:=3
99
PKG_MAINTAINER:=Stan Grishin <stangri@melmac.ca>
1010
PKG_LICENSE:=AGPL-3.0-or-later
1111

files/etc/init.d/adblock-fast

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fi
2424

2525
readonly packageName='adblock-fast'
2626
readonly PKG_VERSION='dev-test'
27-
readonly packageCompat='10'
27+
readonly packageCompat='11'
2828
readonly serviceName="$packageName $PKG_VERSION"
2929
readonly packageMemoryThreshold='33554432'
3030
readonly packageConfigFile="/etc/config/${packageName}"
@@ -796,12 +796,13 @@ load_package_config() {
796796
config_get_bool config_update_enabled 'config' 'config_update_enabled' '0'
797797
config_get_bool debug_init_script 'config' 'debug_init_script' '0'
798798
config_get_bool debug_performance 'config' 'debug_performance' '0'
799+
config_get_bool dnsmasq_sanity_check 'config' 'dnsmasq_sanity_check' '1'
800+
config_get_bool dnsmasq_validity_check 'config' 'dnsmasq_validity_check' '0'
799801
config_get_bool enabled 'config' 'enabled' '0'
800802
config_get_bool force_dns 'config' 'force_dns' '1'
801803
config_get_bool ipv6_enabled 'config' 'ipv6_enabled' '0'
802804
config_get_bool parallel_downloads 'config' 'parallel_downloads' '1'
803805
config_get_bool procd_trigger_wan6 'config' 'procd_trigger_wan6' '0'
804-
config_get_bool sanity_check 'config' 'sanity_check' '1'
805806
config_get_bool update_config_sizes 'config' 'update_config_sizes' '1'
806807
config_get allowed_domain 'config' 'allowed_domain'
807808
config_get blocked_domain 'config' 'blocked_domain'
@@ -831,12 +832,13 @@ load_package_config() {
831832
[ "$config_update_enabled" = '1' ] || unset config_update_enabled
832833
[ "$debug_init_script" = '1' ] || unset debug_init_script
833834
[ "$debug_performance" = '1' ] || unset debug_performance
835+
[ "$dnsmasq_sanity_check" = '1' ] || unset dnsmasq_sanity_check
836+
[ "$dnsmasq_validity_check" = '1' ] || unset dnsmasq_validity_check
834837
[ "$enabled" = '1' ] || unset enabled
835838
[ "$force_dns" = '1' ] || unset force_dns
836839
[ "$ipv6_enabled" = '1' ] || unset ipv6_enabled
837840
[ "$parallel_downloads" = '1' ] || unset parallel_downloads
838841
[ "$procd_trigger_wan6" = '1' ] || unset procd_trigger_wan6
839-
[ "$sanity_check" = '1' ] || unset sanity_check
840842
[ "$update_config_sizes" = '1' ] || unset update_config_sizes
841843

842844
dns_set_output_values "$dns"
@@ -1820,67 +1822,70 @@ download_lists() {
18201822
esac
18211823

18221824
# Validate and remove invalid domain entries (RFC 1123 compliant)
1823-
case "$dns" in
1824-
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers|dnsmasq.addnhosts)
1825-
start_time=$(date +%s)
1826-
step_title='Validating domain entries'
1827-
output 2 "[PROC] ${step_title} "
1828-
json set message "$(get_text 'statusProcessing'): ${step_title}"
1829-
invalid_file="/tmp/${packageName}.invalid.tmp"
1830-
rm -f "$invalid_file"
1831-
# Fast validation: remove entries where domain:
1832-
# - starts with dash or dot (invalid per RFC)
1833-
# - is all numeric with dots (IP-like, invalid for domain)
1834-
# - has consecutive dots
1835-
# - ends with dash or dot (invalid per RFC)
1836-
sed "$outputParseFilter" "$outputFile" | \
1837-
grep -E '^-|^\.|^[0-9.]+$|\.\.|-$|\.$' > "$invalid_file" 2>/dev/null || true
1838-
if [ -s "$invalid_file" ]; then
1839-
invalid_count=$(wc -l < "$invalid_file" 2>/dev/null || echo 0)
1840-
if [ "$invalid_count" -gt 0 ]; then
1841-
# Create pattern file for grep -vFf (fastest removal method)
1842-
# Use appropriate prefix based on dns type
1843-
case "$dns" in
1844-
dnsmasq.conf)
1845-
sed "$dnsmasqConfGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1846-
;;
1847-
dnsmasq.ipset)
1848-
sed "$dnsmasqIpsetGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1849-
;;
1850-
dnsmasq.nftset)
1851-
sed "$dnsmasqNftsetGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1852-
;;
1853-
dnsmasq.servers)
1854-
sed "$dnsmasqServersGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1855-
;;
1856-
dnsmasq.addnhosts)
1857-
# Create patterns for both IPv4 and IPv6 formats
1858-
{ sed "$dnsmasqAddnhostsGrepPatternIPv4" "$invalid_file"; sed "$dnsmasqAddnhostsGrepPatternIPv6" "$invalid_file"; } > "${invalid_file}.pat" 2>/dev/null
1859-
;;
1860-
esac
1861-
# Remove invalid entries
1862-
grep -vFf "${invalid_file}.pat" "$outputFile" > "${outputFile}.valid" 2>/dev/null && \
1863-
mv "${outputFile}.valid" "$outputFile" 2>/dev/null
1864-
# Report (limit to first 20 for performance)
1865-
logger -t "$packageName" "Removed $invalid_count invalid entries from ${dns}."
1866-
json add warning 'warningInvalidDomainsRemoved' "$invalid_count"
1867-
rm -f "${invalid_file}.pat"
1868-
fi
1825+
if [ -n "$dnsmasq_validity_check" ]; then
1826+
case "$dns" in
1827+
dnsmasq.conf|dnsmasq.ipset|dnsmasq.nftset|dnsmasq.servers|dnsmasq.addnhosts)
1828+
start_time=$(date +%s)
1829+
step_title='Validating domain entries'
1830+
output 2 "[PROC] ${step_title} "
1831+
json set message "$(get_text 'statusProcessing'): ${step_title}"
1832+
invalid_file="/tmp/${packageName}.invalid.tmp"
18691833
rm -f "$invalid_file"
1870-
fi
1871-
if [ "${invalid_count:-0}" -gt 0 ]; then
1872-
output_warn
1873-
else
1874-
output_ok
1875-
fi
1876-
end_time=$(date +%s)
1877-
elapsed=$(( end_time - start_time ))
1878-
logger_debug "[PERF-DEBUG] ${step_title} took ${elapsed}s"
1879-
;;
1880-
esac
1834+
# Fast validation: remove entries where domain:
1835+
# - starts with dash or dot (invalid per RFC)
1836+
# - is all numeric with dots (IP-like, invalid for domain)
1837+
# - has consecutive dots
1838+
# - ends with dash or dot (invalid per RFC)
1839+
sed "$outputParseFilter" "$outputFile" | \
1840+
grep -E '^-|^\.|^[0-9.]+$|\.\.|-$|\.$' > "$invalid_file" 2>/dev/null || true
1841+
if [ -s "$invalid_file" ]; then
1842+
invalid_count=$(wc -l < "$invalid_file" 2>/dev/null || echo 0)
1843+
if [ "$invalid_count" -gt 0 ]; then
1844+
# Create pattern file for grep -vFf (fastest removal method)
1845+
# Use appropriate prefix based on dns type
1846+
case "$dns" in
1847+
dnsmasq.conf)
1848+
sed "$dnsmasqConfGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1849+
;;
1850+
dnsmasq.ipset)
1851+
sed "$dnsmasqIpsetGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1852+
;;
1853+
dnsmasq.nftset)
1854+
sed "$dnsmasqNftsetGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1855+
;;
1856+
dnsmasq.servers)
1857+
sed "$dnsmasqServersGrepPattern" "$invalid_file" > "${invalid_file}.pat" 2>/dev/null
1858+
;;
1859+
dnsmasq.addnhosts)
1860+
# Create patterns for both IPv4 and IPv6 formats
1861+
{ sed "$dnsmasqAddnhostsGrepPatternIPv4" "$invalid_file"; sed "$dnsmasqAddnhostsGrepPatternIPv6" "$invalid_file"; } > "${invalid_file}.pat" 2>/dev/null
1862+
;;
1863+
esac
1864+
# Remove invalid entries
1865+
grep -vFf "${invalid_file}.pat" "$outputFile" > "${outputFile}.valid" 2>/dev/null && \
1866+
mv "${outputFile}.valid" "$outputFile" 2>/dev/null
1867+
# Report (limit to first 20 for performance)
1868+
logger -t "$packageName" "Removed $invalid_count invalid entries from ${dns}."
1869+
json add warning 'warningInvalidDomainsRemoved' "$invalid_count"
1870+
rm -f "${invalid_file}.pat"
1871+
fi
1872+
rm -f "$invalid_file"
1873+
fi
1874+
if [ "${invalid_count:-0}" -gt 0 ]; then
1875+
output_warn
1876+
else
1877+
output_ok
1878+
fi
1879+
end_time=$(date +%s)
1880+
elapsed=$(( end_time - start_time ))
1881+
logger_debug "[PERF-DEBUG] ${step_title} took ${elapsed}s"
1882+
;;
1883+
esac
1884+
fi
18811885

1882-
output 2 '[PROC] Removing temporary files '
1883-
json set message "$(get_text 'statusProcessing'): removing temporary files"
1886+
step_title='Removing temporary files'
1887+
output 2 "[PROC] ${step_title} "
1888+
json set message "$(get_text 'statusProcessing'): ${step_title}"
18841889
if rm -f "/tmp/${packageName}_tmp."* "$ALLOWED_TMP" "$A_TMP" "$B_TMP" "$SED_TMP" "$outputCache"; then
18851890
output_ok
18861891
else
@@ -2724,7 +2729,8 @@ load_validate_config() {
27242729
'smartdns_instance:list(or(integer, string)):*' \
27252730
'heartbeat_domain:or("-", string):heartbeat.melmac.ca' \
27262731
'heartbeat_sleep_timeout:range(1,60):10' \
2727-
'sanity_check:bool:1' \
2732+
'dnsmasq_sanity_check:bool:1' \
2733+
'dnsmasq_validity_check:bool:0' \
27282734
'update_config_sizes:bool:1' \
27292735
'allowed_domain:list(string)' \
27302736
'blocked_domain:list(string)' \

files/etc/uci-defaults/90-adblock-fast

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ if [ -n "$oldval" ]; then
169169
uci_remove "$packageName" 'config' 'proc_debug'
170170
fi
171171

172+
# migrate sanity_check to dnsmasq_sanity_check
173+
if [ -z "$(uci_get "$packageName" 'config' 'dnsmasq_sanity_check')" ] && [ -n "$(uci_get "$packageName" 'config' 'sanity_check')" ]; then
174+
oldval="$(uci_get "$packageName" 'config' 'sanity_check')"
175+
uci_set "$packageName" 'config' 'dnsmasq_sanity_check' "$oldval"
176+
uci_remove "$packageName" 'config' 'sanity_check'
177+
fi
178+
172179
uci_changes "$packageName" && uci_commit "$packageName"
173180

174181
exit 0

0 commit comments

Comments
 (0)