Skip to content
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
47d8791
Added hosts support, as well as support for plain domains.
Mar 16, 2025
c2b031b
ipset should `add` the ip if not already added
athyfr Mar 16, 2025
a64cb69
ipset should `add` the ip if not already added
athyfr Mar 16, 2025
d804f96
Fix escape syntax
athyfr Mar 16, 2025
3ff1dc0
Fixed escape syntax.
Mar 16, 2025
dc314b0
Fix escape syntax
athyfr Mar 16, 2025
be3bd9b
Remove redundant comment
athyfr Mar 16, 2025
8d0765e
Add comments around variable call
athyfr Mar 16, 2025
e3bd343
Remove redundant echo
athyfr Mar 16, 2025
0975a26
Remove redundant echo
athyfr Mar 16, 2025
37c26e1
Quote variable references
athyfr Mar 16, 2025
cf3de35
Fix issue: `if` to `elif`
athyfr Mar 16, 2025
f82d675
Fix variable assignment
athyfr Mar 16, 2025
822f51a
Fix variable assignment
athyfr Mar 16, 2025
8e6c1cc
Remove unnecessary variable assignments
athyfr Mar 16, 2025
c08a99b
Fixed regex.
Mar 16, 2025
287c966
Removed reference to undefined variable
athyfr Mar 17, 2025
f0d7bc8
Simplified domain regex
Mar 17, 2025
73d082a
Fixed dig call: `dig` -> `${dig}/bin/dig`
Mar 18, 2025
75064df
Added `dig` to inherit statement.
Mar 18, 2025
bac866f
Fixed weird IP error:
Mar 18, 2025
0a3158f
Fixed if statement spacing
Mar 18, 2025
050d836
Remove if statements again, did not fix weird IP error
Mar 18, 2025
80acad2
Fix regex `.` syntax
athyfr Mar 18, 2025
b833171
Update domain regex to require at least one literal `.`
athyfr Mar 18, 2025
5d55941
Fix variable calls
athyfr Mar 18, 2025
f8d0d87
Filter out non-IP addresses from `dig`
athyfr Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions update_blocklist.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ pkgs, config }:
let
inherit (pkgs) ipset wget;
inherit (pkgs) ipset wget dig;
inherit (config.services.blocklist-updater)
blocklists
ipSetName
Expand All @@ -10,7 +10,7 @@ let
in
''
# Clear ipset from previous address.
# Ignore if it fails, because we don't care
# Ignore if it fails, because we don't care

set -e
urls=(
Expand Down Expand Up @@ -40,16 +40,47 @@ in
# Create an ip set and add each ip to it one by one

# IPv4 and IPv6 regex patterns with CIDR notation support - WARNING: might not be correct for all IPs (e.g. ignore valid ones or accept wrong ones), but seems to work fine
# Also supports hosts files, as well as plain lists of domains.
ipv4_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?$"
ipv6_regex="^([0-9a-fA-F:]+::?[0-9a-fA-F]*)+(\/[0-9]{1,3})?$"
domain_regex='^[a-zA-Z0-9.-]*\.[a-zA-Z][a-zA-Z0-9.-]+$'
host_regex="^0\\.0\\.0\\.0 ''${domain_regex:1}"

blockIPv4 () {
echo -exist add "${ipSetName}" "$1"
}

blockIPv6 () {
echo -exist add "${ipV6SetName}" "$1"
}

blockDomain () {
${dig}/bin/dig "$1" A +short |
while IFS= read -r IP; do
if [[ $IP =~ $ipv4_regex ]]; then
blockIPv4 "$IP"
fi
done

${dig}/bin/dig $1 AAAA +short |
while IFS= read -r IP; do
if [[ "$IP" =~ $ipv6_regex ]]; then
blockIPv6 "$IP"
fi
done
}

# Use a temporary buffer to improve performance
{
while IFS= read -r IP; do
if [[ $IP =~ $ipv4_regex ]]; then
echo -exist add "${ipSetName}" "$IP"
elif [[ $IP =~ $ipv6_regex ]]; then
echo -exist add "${ipV6SetName}" "$IP"
blockIPv4 "$IP"
elif [[ "$IP" =~ $ipv6_regex ]]; then
blockIPv6 "$IP"
elif [[ "$IP" =~ $host_regex ]]; then
blockDomain ''${IP:8}
elif [[ "$IP" =~ $domain_regex ]]; then
blockDomain "$IP"
else
echo "Warning: Invalid line skipped -> $IP" >&2
fi
Expand Down