forked from vedetta-com/vedetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsblock.sh
executable file
·66 lines (53 loc) · 1.75 KB
/
dnsblock.sh
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
# Update the DNS based adblock (var/unbound/etc/dnsblock.conf)
# https://www.filters.com
# https://github.com/StevenBlack/hosts
# https://deadc0de.re/articles/unbound-blocking-ads.html
# https://pgl.yoyo.org/adservers/serverlist.php?hostformat=unbound&showintro=0&mimetype=plaintext
#set -eu
set -o errexit
set -o nounset
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
app=$(basename $0)
AWK=/usr/bin/awk
FTP=/usr/bin/ftp
CAT=/bin/cat
GREP=/usr/bin/grep
EGREP=/usr/bin/egrep
SORT=/usr/bin/sort
PFCTL=/sbin/pfctl
RM=/bin/rm
CP=/bin/cp
CHMOD=/bin/chmod
TR=/usr/bin/tr
RCCTL=/usr/sbin/rcctl
hostsurl="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
hoststmp="$(mktemp -t ${hostsurl##*/}.XXXXXXXXXX)" || exit 1
dnsblocktmp="$(mktemp)" || exit 1
dnsblock=dnsblock.conf
unboundchroot=/var/unbound
error_exit () {
echo "${app}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
# Bail out if non-privileged UID
[ 0 = "$(id -u)" ] || \
error_exit "$LINENO: ERROR: You are using a non-privileged account."
# Download
"${FTP}" -o "${hoststmp}" "${hostsurl}" || \
error_exit "$LINENO: ERROR: download failed."
# Convert hosts to unbound.conf
"${CAT}" "${hoststmp}" | "${GREP}" '^0\.0\.0\.0' | \
"${AWK}" '{print "local-zone: \""$2"\" redirect\nlocal-data: \""$2" A 0.0.0.0\""}' > \
"${dnsblocktmp}"
# Install
"${RM}" "${unboundchroot}"/etc/"${dnsblock}"
"${CP}" "${dnsblocktmp}" "${unboundchroot}"/etc/"${dnsblock}" || \
error_exit "$LINENO: ERROR: ${dnsblock} copy failed."
"${CHMOD}" 600 "${unboundchroot}"/etc/"${dnsblock}" || exit
# Populate unbound dns block
"${RCCTL}" stop unbound
"${RCCTL}" start unbound || \
error_exit "$LINENO: ERROR: unbound failed."
# Remove temp files
"${RM}" -rf "${hoststmp}" "${dnsblocktmp}"