-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdns-recreator.sh
295 lines (266 loc) · 9.19 KB
/
pdns-recreator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
#
###########
# Pdns-Recreator Ver. 0.1 for Linux
# ---------------------------------
# Quick n Dirty Script for Converting some Pi-Hole and/or Custom Blacklists to
# PowerDNS Recursor LUA Files and also creating HOSTS files
# Useful for Ads-Blocking
#
# Copyright (C) 2020 - by suuhm - suuhmer@coldwareveryday.com
#
# GNU General Public License v2.0
# -------------------------------
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
###########
#
# Oneliner
# grep -vE "^#|^\W" blacklist_raw_.txt | sed 's/^/"/g;s/\r/\"\,/g' | tr -d '\n' | awk '{print "return: " $1 " }"}'
#
###########
_PROG=${0##*/}
_COMMAND_MODE='none'
_BASE_D=/opt/pdns-recreator/
_CONF_R=${_BASE_D%pd*} #/opt/
_BL_FILE=${_BASE_D}bl_full_raw.lst
_LUA_FILE=${_BASE_D}blocklist.lua
_PDNS_DIR=/etc/powerdns/
_URL_FILE=""
_URI_PH=https://www.sunshine.it/blacklist.txt
_URI_HOSTS=https://raw.githubusercontent.com/suuhm/pdns-recreator/master/yt-adblock.lst
_OPT_DL=0
_usage()
{
echo "Usage: $_PROG main-mode bl-options [options]>
main-mode: recursor, hostsfile, pihole
bl-options:
-b, --builtin-bl Using the builtin Blacklist file.
-f, --bl-file=URL Download custom extern Blacklist-File. Needs URI-format!
options:
-C, --convert Just Converts Blacklist to powerdns *.lua file.
-i, --install Setup the Blacklists (includes convert function).
-u, --update Updates / Installs the Blacklistfile to PDNS or HOSTS
-r, --reset Rsets the PDNS or HOSTS Files which are created
-s, --syslog Write messages into the system log.
-v, --version Prints script-version.
-h, --help Print this help message.
"
}
_version()
{
echo -e "\nPdns-Recreator Ver. 0.1 for Linux"
echo -e "Copyright 2020 - by suuhm - it@coldwareveryday.com"
echo -e "Have fun with your Ad-free browsing and feel free to report Bugs!\n"
}
_pdns_convert ()
{
echo 'return{' > $_LUA_FILE
grep -vE "^#|^\W" $_BL_FILE | sed 's/^/"/g;s/\r\|$/\"\,/g' | head -c -1 >> $_LUA_FILE
#tail -n1 $_LUA_FILE | tr -d '\n' >> $_LUA_FILE
printf "\n}" >> $_LUA_FILE
##
cat << EOF > ${_BASE_D}adblock.lua
adservers=newDS()
adservers:add(dofile("${_PDNS_DIR}${_LUA_FILE##*/}"))
function preresolve(dq)
if(not adservers:check(dq.qname) or (dq.qtype ~= pdns.A and dq.qtype ~= pdns.AAAA)) then
return false
end
-- Adding some fake.subdomain SOA Records
dq:addRecord(pdns.SOA,
"fake."..dq.qname:toString().." fake."..dq.qname:toString().." 1 7200 900 1209600 86400",
2)
return true
end
EOF
##
echo -e "[*] DONE!"
echo -e "\n************************************************************************************************\n"
echo -e "Now, please set: \n1.\t lua-dns-script=${_PDNS_DIR}adblock.lua in your recursor.conf
\n2.\t reload with: rec_control reload-lua-script && rec_control reload-zones"
echo -e "\n************************************************************************************************\n"
echo -e "[*] Cleaning up temp Blocklist Files...\n"
cp -ra ${_BASE_D}*.lua $_PDNS_DIR
#rm $_BL_FILE //No necessary for douple downloading..
}
_set_update()
{
if [[ $_COMMAND_MODE == "recursor" ]]; then
[ $_OPT_DL == 2 ] && _get_bl 1 || _get_bl 2
_pdns_convert
echo -e "[*] Auto-Reload Zones..."
rec_control reload-lua-script && rec_control reload-zones
elif [[ $_COMMAND_MODE == "hosts" ]]; then
[ $_OPT_DL == 2 ] && _get_bl 1 || _get_bl 3
TMP=`grep -vE "0.0.0|YT-A" /etc/hosts`
echo -e "$TMP\n" > /etc/hosts
echo -e "\n#### HOSTS CREATED AGAINST YT-ADS <$(wc -l $_BL_FILE) ENTRIES> ####" >> /etc/hosts
grep -vE "^#|^\W\W" $_BL_FILE | awk -F '\r' '{ print "0.0.0.0\t" $1 }' | head -c -1 >> /etc/hosts
echo "[*] Hosts successfully updated!"
elif [[ $_COMMAND_MODE == "pihole" ]]; then
echo NULL
else
exit 222;
fi
}
_set_install()
{
if [[ $_OPT_DL == 2 ]]; then
echo "[*] Using URL: $_OPT_DL $_URL_FILE for download"
_get_bl 22
fi
#BL CHECK
[ $_OPT_DL == 0 ] && echo "Not setting Blacklist. Using default $_BL_FILE ..."
if [[ $_COMMAND_MODE == "recursor" ]]; then
_pdns_convert
elif [[ $_COMMAND_MODE == "hosts" ]]; then
[ $_OPT_DL == 2 ] && _get_bl 1 || _get_bl 3
TMP=`grep -vE "0.0.0|YT-A" /etc/hosts`
echo -e "$TMP\n" > /etc/hosts
cp -a /etc/hosts /etc/hosts.prbak
echo -e "\n#### HOSTS CREATED AGAINST YT-ADS <$(wc -l $_BL_FILE) ENTRIES> ####" >> /etc/hosts
echo "Creating HOSTS LIST..."
#{ print "127.0.0.1\t" $1 }
grep -vE "^#|^\W\W" $_BL_FILE | awk -F '\r' '{ print "0.0.0.0\t" $1 }' | head -c -1 >> /etc/hosts
echo "[*] Hosts successfully created!"
elif [[ $_COMMAND_MODE == "pihole" ]]; then
echo NULL
else
exit 223;
fi
}
_reset_m()
{
if [[ $_COMMAND_MODE == "recursor" ]]; then
echo -e "Not implemented yet. \nJust comment out in recursor.conf File\n"
elif [[ $_COMMAND_MODE == "hosts" ]]; then
TMP=`grep -vE "0.0.0|YT-A" /etc/hosts`
echo -e "$TMP\n" > /etc/hosts
echo "[*] Hosts-File successfully resetted"
elif [[ $_COMMAND_MODE == "pihole" ]]; then
echo NULL
else
exit 224;
fi
}
_get_bl()
{
# 2 - pihole
# 3 - hosts<-zero
if [[ -z $1 ]]; then
_t=0
else
_t=$1
fi
if [[ $_t == 1 ]]; then
echo "[*] Downloading File..."
curl -sL `echo $_URL_FILE | cut -d "=" -f 2` > $_BL_FILE
elif [[ $_t == 2 ]]; then
echo "[*] Downloading File..."
curl -sL $_URI_PH > $_BL_FILE
elif [[ $_t == 3 ]]; then
echo "[*] Downloading File..."
curl -sL $_URI_HOSTS > $_BL_FILE
sed -i 's/^0.*0//g' $_BL_FILE
else
echo "[*] Downloading File..."
curl -sL `echo $_URL_FILE | cut -d "=" -f 2` > $_BL_FILE
fi
}
if [[ "$#" == '0' ]]; then
echo "Argument list empty"
exit 110
fi
tmppt=${1%%-*}
_COMMAND_MODE="$1"
if [[ "$tmppt" != '' ]]; then
shift 1
else
if [[ $_COMMAND_MODE != "-h" && $_COMMAND_MODE != "-v" && $_COMMAND_MODE != "--help" && $_COMMAND_MODE != "--version" ]]; then
_usage ; exit 121
fi
fi
if [[ ! -e $_BL_FILE ]]; then
echo "[*] WARNING NO BLACKLIST FILE FOUND!!"
echo "[*] TRYING TO DOWNLOAD ONE..."
_get_bl 2
fi
GETOPT=`getopt -T`
if [[ $? != 4 && $? != 1 ]]; then
echo "Error 111: GETOPT missing"
exit 111
fi
_getopt=$(getopt -o vhCbfi::usr --long version,help,convert,builtin-bl,bl-file::,update,install,reset,syslog -n $_PROG -- "$@")
if [[ $? != 0 ]] ; then
echo "bad command line options" >&2 ; exit 13 ;
fi
eval set -- ${_getopt}
while true; do
case "$1" in
-v|--version)
_version; exit 0
;;
-h|--help)
_usage; exit 0
;;
-C|--convert)
if [[ "$_COMMAND_MODE" == "recursor" ]]; then
_pdns_convert; break
else
echo -e "Only in Recursor mode!\n"
exit 117;
fi
;;
-b|--builtin-bl)
if [[ -z "$4" ]]; then
_OPT_DL=1
shift;
else
_usage
exit 116;
fi
continue
;;
-f|--bl-file)
if [[ -z "$2" ]]; then
echo -e "Missing URL!\n" ; _usage
exit 116;
else
_OPT_DL=2 ; _URL_FILE=$2
shift 2;
fi
continue
;;
-u|--update)
_set_update; shift; continue
;;
-i|--install)
#[ $_OPT_DL -gt 0 || "$2" == '' ] &&
_set_install; shift; continue
;;
-r|--reset)
_reset_m; break
;;
-s|--syslog)
_syslog #TODO
break
;;
--)
shift; break
;;
*)
echo "BAD OPTION $1"
_usage
exit 123
;;
esac
done
exit 0;