blob: 54aa528e9d2b13038d8530af560e33d8ca73ba7d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/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" "$@"
cdate=date
case $(uname -s) in
(Darwin) cdate=gdate ;; # install gdate with 'brew install coreutils'
esac
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")" # with BSD date
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 # with BSD date # with BSD date
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
|