summaryrefslogtreecommitdiff
path: root/bin/update_hosts
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2025-01-21 08:42:15 +0100
committerMarc Vertes <mvertes@free.fr>2025-01-21 08:42:15 +0100
commit0ad4e2b969c28d2b39d29e55e53ae632db710030 (patch)
tree62bb292d8da9a835becb679b5d6917c50da3d950 /bin/update_hosts
parentbac7296bb38bb46b51e4fd70c5fe234f2a58d553 (diff)
parent6d506e0b30569c547f41f6d6334a1b5133774da4 (diff)
Merge github.com:mvertes/dotfiles
Diffstat (limited to 'bin/update_hosts')
-rwxr-xr-xbin/update_hosts28
1 files changed, 28 insertions, 0 deletions
diff --git a/bin/update_hosts b/bin/update_hosts
new file mode 100755
index 0000000..74bf25b
--- /dev/null
+++ b/bin/update_hosts
@@ -0,0 +1,28 @@
+#!/bin/sh -e
+
+# Update /etc/hosts with a well curated blacklist of malware, ads, porn, etc.
+# Custom hosts are preserved.
+
+[ "$USER" = root ] || exec sudo "$0" "$@"
+
+echo 'Checking from https://github.com/StevenBlack/hosts:'
+lsd=$(curl -s 'https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1' | jq -r '.[0].commit.committer.date')
+#echo "last source update: $(date -j -f "%FT%TZ" "$lsd")"
+echo "last source update: $(date --date="$lsd")"
+echo "last local update: $(date -r /etc/hosts)"
+# [ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit
+[ $(date --date="$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit
+
+cd /etc
+cp -p hosts hosts.old
+hosts=$(awk '/^# Custom host/, /^# End of custom host/' hosts.old)
+curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | awk '
+ BEGIN { hosts = ARGV[1]; ARGV[1] = "" }
+ /^# Custom host/ { print hosts; next }
+ /^# End of custom host/ { next }
+ { print }
+' "$hosts" > hosts.new
+
+mv hosts.new hosts
+[ "$(uname -s)" != Darwin ] || { dscacheutil -flushcache; killall -HUP mDNSResponder; }
+diff -u /etc/hosts.old /etc/hosts | diffstat