Created
May 10, 2024 01:23
-
-
Save pschichtel/11372894f321aac951cf044255c2d95a to your computer and use it in GitHub Desktop.
A simple bash script that can be used to fetch various DNS ad filter lists in either rpz or /etc/hosts format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
fetch_hosts_as_rpz() { | |
local action="${1?no action!}" | |
local destination="${2?no destination!}" | |
shift 2 | |
if [ $# = 0 ] | |
then | |
echo "No source URLs given!" >&2 | |
return 1 | |
fi | |
local tmp_file="$destination.tmp" | |
touch "$tmp_file" | |
cat <<'EOL' > "$tmp_file" | |
$TTL 300 | |
@ SOA localhost. root.localhost. 1714221973 43200 3600 259200 300 | |
NS localhost. | |
EOL | |
local tmp_download | |
local status_code | |
for source in "$@" | |
do | |
tmp_download="$(mktemp)" | |
if ! status_code="$(curl -s -L -o "$tmp_download" -w "%{http_code}" "$source")" || [ "$status_code" != "200" ] | |
then | |
echo "Failed to fetch source $source, skipping entire list!" >&2 | |
echo "Response status: $status_code" | |
echo "Output:" | |
cat "$tmp_download" | |
rm "$tmp_download" "$tmp_file" | |
return 0 | |
fi | |
grep -P '^\s*0\.0\.0\.0\s+' "$tmp_download" | sed -r 's/#.*$//g' | grep -Pv '\s+0\.0\.0\.0\s*$' | sed -r "s/^\s*0\.0\.0\.0\s+(\S+)/\1 300 IN CNAME $action/g" >> "$tmp_file" | |
rm "$tmp_download" | |
done | |
mv "$tmp_file" "$destination" | |
} | |
fetch_rpz() { | |
local destination="${1?no destination!}" | |
local source="${2?no source!}" | |
local tmp_file="$destination.tmp" | |
local status_code | |
status_code="$(curl -s -L -o "$tmp_file" -w "%{http_code}" "$source")" | |
if [ "$status_code" = "200" ] | |
then | |
mv "$tmp_file" "$destination" | |
else | |
echo "Failed to fetch source $source, skipping list!" >&2 | |
echo "Response status: $status_code" | |
echo "Output:" | |
cat "$tmp_file" | |
rm "$tmp_file" | |
fi | |
} | |
# https://github.com/StevenBlack/hosts | |
fetch_hosts_as_rpz '.' /etc/knot-resolver/steven-black-hosts.rpz https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling/hosts | |
# https://github.com/badmojr/1Hosts | |
fetch_rpz /etc/knot-resolver/1hosts-lite.rpz https://o0.pages.dev/Lite/rpz.txt | |
# https://github.com/sjhgvr/oisd | |
fetch_rpz /etc/knot-resolver/osid.rpz https://big.oisd.nl/rpz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment