Skip to content

Commit

Permalink
Feat: regex configs
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Aug 19, 2024
1 parent 9ae365a commit 82bc12c
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 10 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ RUN <<-"EOF" bash -ex

ln -sf /root/antizapret/doall.sh /usr/bin/doall

/root/antizapret/doall.sh

rm -frv /tmp/*
EOF

COPY rootfs /rootfs
COPY rootfs/etc/openvpn /etc/openvpn-default

RUN <<-"EOF" bash -ex
(STAGE_1=true STAGE_2=true STAGE_3=false /root/antizapret/doall.sh)
EOF

ENTRYPOINT ["/init.sh"]
2 changes: 2 additions & 0 deletions rootfs/etc/knot-resolver/knot-aliases-alt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
-- Dummy file. Filled by antizapret script.
blocked_hosts = {}
regex_blocked = '^$'
regex_allowed = '^$'
24 changes: 24 additions & 0 deletions rootfs/etc/knot-resolver/kresd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ local dns_ru = os.getenv('DNS_RU')
-- if not dns then dns = '127.0.0.11' end
-- if not dns_ru then dns = '77.88.8.8' end

policy.add(
function (_, query)
local command = string.format(
"(echo '%s' | grep -Eq '%s' && echo 'allowed') || (echo '%s' | grep -Eq '%s' && echo 'blocked')",
kres.dname2str(query.sname),
regex_allowed,
kres.dname2str(query.sname),
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
policy.add(
policy.suffix(
Expand Down
4 changes: 2 additions & 2 deletions rootfs/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ ln -sf /etc/default/antizapret /etc/profile.d/antizapret.sh


# populating directories with files
cp -rv --update=none /rootfs/etc/openvpn/* /etc/openvpn
cp -rv --update=none /etc/openvpn-default/* /etc/openvpn

for file in $(echo {exclude,include}-{ips,hosts}-custom.txt); do
for file in $(echo {exclude,include}-{ips,hosts,regex}-custom.txt); do
path=/root/antizapret/config/custom/$file
[ ! -f $path ] && touch $path
done
Expand Down
23 changes: 23 additions & 0 deletions rootfs/root/antizapret/build_regex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash -e


HERE="$(dirname "$(readlink -f "${0}")")"
cd "$HERE"


for file in config/custom/{include,exclude}-regex-custom.txt; do
if [[ "$file" =~ include ]]; then
type="blocked"
else
type="allowed"
fi

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

Empty file.
Empty file.
10 changes: 7 additions & 3 deletions rootfs/root/antizapret/doall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ if [[ $FORCE == true ]]; then
echo 'Force update detected!'
./update.sh
./parse.sh
./build_regex.sh
./process.sh
exit
fi
Expand All @@ -89,8 +90,11 @@ done
if ! diff_hashes; then create_hash > /root/.hash; STAGE_2=true; fi


[[ $STAGE_1 == true ]] && ./update.sh
[[ $STAGE_1 == true ]] && (echo "run update.sh" && ./update.sh || exit 1)

[[ $STAGE_2 == true ]] && ./parse.sh || echo 'Nothing to do.'
[[ $STAGE_2 == true ]] && (echo "run parse.sh" && ./parse.sh && echo "run build_regex.sh" && ./build_regex.sh || exit 2)

[[ $STAGE_3 == true ]] && ./process.sh 2> /dev/null
[[ $STAGE_3 == true ]] && (echo "run process.sh" && ./process.sh 2> /dev/null || exit 3)

echo "Kresd rules updated"
exit 0
2 changes: 1 addition & 1 deletion rootfs/root/dnsmap/proxy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env -S python3 -u
# -*- coding: utf-8 -*-

from __future__ import print_function
Expand Down
2 changes: 1 addition & 1 deletion rootfs/root/patches/parse.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-sort -u config/exclude-ips-{dist,custom}.txt > temp/exclude-ips.txt
-sort -u config/include-hosts-{dist,custom}.txt > temp/include-hosts.txt
-sort -u config/include-ips-{dist,custom}.txt > temp/include-ips.txt
+for file in config/custom/*-custom.txt; do
+for file in config/custom/{include,exclude}-{hosts,ips}-custom.txt; do
+ basename=$(basename $file | sed 's|-custom.txt||')
+ sort -u $file config/${basename}-dist.txt > temp/${basename}.txt
+done
Expand Down

0 comments on commit 82bc12c

Please sign in to comment.