Skip to content

Commit

Permalink
Fix: filter empty strings in regex for kresd
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Aug 21, 2024
1 parent a7c129b commit b00efea
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
46 changes: 23 additions & 23 deletions rootfs/etc/knot-resolver/kresd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ local adguard = os.getenv('ADGUARD')

policy.add(
function (_, query)
local cmdTpl = [[
domain="$(echo "%s" | sed -nr 's/(.*)\.$/\1/p')"
(echo "$domain" | grep -Eq "%s" && echo "allowed") || (echo "$domain" | grep -Eq "%s" && echo "blocked")
]]
local command = string.format(
cmdTpl,
kres.dname2str(query.sname),
regex_allowed,
regex_blocked
)
local handle = io.popen(command)
local result = handle:read("*line")
handle:close()

if result == 'blocked' then
return policy.STUB({'127.0.0.4'})
elseif result == 'allowed' then
return policy.FORWARD({dns})
end

-- filter did not match, continue with next filter
return nil
end
if (regex_allowed == '' or regex_allowed == nil) and (regex_blocked == '' or regex_blocked == nil) then
return nil
end

local command = string.format(
'/etc/knot-resolver/regex.sh "%s" "%s" "%s"',
kres.dname2str(query.sname),
regex_allowed,
regex_blocked
)
local handle = io.popen(command)
local result = handle:read("*line")
handle:close()

if result == 'blocked' then
return policy.STUB({'127.0.0.4'})
elseif result == 'allowed' then
return policy.FORWARD({dns})
end

-- filter did not match, continue with next filter
return nil
end
)

-- Forward blocked domains to dnsmap
Expand Down
11 changes: 11 additions & 0 deletions rootfs/etc/knot-resolver/regex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

domain="$(echo "$1" | sed -nr 's/(.*)\.$/\1/p')"
regex_allowed="$2"
regex_blocked="$3"

if [[ -n "$regex_allowed" ]] && [[ "$domain" =~ $regex_allowed ]]; then
echo "allowed"
elif [[ -n "$regex_blocked" ]] && [[ "$domain" =~ $regex_blocked ]]; then
echo "blocked"
fi
7 changes: 4 additions & 3 deletions rootfs/root/antizapret/build_regex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ for file in config/custom/{include,exclude}-regex-custom.txt; do

#regex_allowed
#regex_blocked
if [ "$(cat "$file" | wc -l)" -gt 0 ]; then
echo "regex_$type = '($(sed -E '/^(#.*)?[[:space:]]*$/d' "$file" | tr '\n' '|' | xargs | sed "s/|$//g"))'" >> result/knot-aliases-alt.conf
REGEX=$(sed -E '/^(#.*)?[[:space:]]*$/d' "$file" | tr '\n' '|' | xargs | sed "s/|$//g");
if [[ -n "$REGEX" ]]; then
echo "regex_$type = '($REGEX)'" >> result/knot-aliases-alt.conf
else
echo "regex_$type = '^$'" >> result/knot-aliases-alt.conf
echo "regex_$type = ''" >> result/knot-aliases-alt.conf
fi
done

0 comments on commit b00efea

Please sign in to comment.