2 #######################################################
3 # ad/abuse domain blocking script for dnsmasq/openwrt #
4 # written by Dirk Brenken (openwrt@brenken.org) #
5 #######################################################
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # set the C locale, characters are single bytes, the charset is ASCII
27 # speeds up things like sort, grep etc.
35 # get current pid, script directory and openwrt version
38 adb_scriptdir
="${0%/*}"
39 openwrt_version
="$(cat /etc/openwrt_version 2>/dev/null)"
41 # source in adblock function library
43 if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
45 .
"${adb_scriptdir}/adblock-helper.sh" 2>/dev
/null
48 /usr
/bin
/logger
-s -t "adblock[${pid}] error" "adblock function library not found, rc: ${rc}"
56 # call exit function on trap signals (HUP, INT, QUIT, BUS, SEGV, TERM)
58 trap "rc=255; f_log 'error signal received/trapped' '${rc}'; f_exit" 1 2 3 10 11 15
62 f_log
"domain adblock processing started (${adb_version}, ${openwrt_version}, $(/bin/date "+%d.
%m.
%Y
%H
:%M
:%S
"))"
76 # start shallalist (pre-)processing
78 if [ -n "${adb_arc_shalla}" ]
80 # start shallalist processing
82 shalla_archive
="${adb_tmpdir}/shallalist.tar.gz"
83 shalla_file
="${adb_tmpdir}/shallalist.txt"
85 adb_dnsfile
="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
86 list_time
="$(grep -F "# last modified: " "${adb_dnsfile}" 2>/dev/null)"
87 list_time
="${list_time/*: /}"
88 f_log
"=> (pre-)processing adblock source '${src_name}'"
90 # only process shallalist archive with updated timestamp,
91 # extract and merge only domains of selected shallalist categories
93 shalla_time
="$(wget ${wget_parm} --timeout=5 --server-response --spider "${adb_arc_shalla}" 2>&1 | grep -F "Last-Modified
: " 2>/dev/null | tr -d '\r' 2>/dev/null)"
94 shalla_time
="${shalla_time/*: /}"
95 if [ -z "${shalla_time}" ]
98 f_log
" no online timestamp received, current date will be used"
100 if [ -z "${list_time}" ] || [ "${list_time}" != "${shalla_time}" ]
102 wget
${wget_parm} --timeout="${adb_maxtime}" --output-document="${shalla_archive}" "${adb_arc_shalla}" 2>/dev/null
107 for category in ${adb_cat_shalla}
109 tar -xOzf "${shalla_archive}" BL/${category}/domains 2>/dev/null >> "${shalla_file}"
113 f_log " archive extraction failed
(${category})"
118 # remove temporary files
120 rm -f "${shalla_archive}" >/dev/null 2>&1
121 rm -rf "${adb_tmpdir}/BL
" >/dev/null 2>&1
124 adb_sources="${adb_sources} ${shalla_file}&ruleset
=rset_shalla
"
125 f_log " source archive
(pre-
)processing finished
"
130 f_log " source archive download failed
"
134 adb_srclist="! -name ${adb_dnsprefix}.
${src_name}"
135 f_log " source archive doesn
't change, no update required"
139 # add blacklist source to active adblock domain sources
141 if [ -s "${adb_blacklist}" ]
143 adb_sources="${adb_sources} ${adb_blacklist}&ruleset=rset_blacklist"
146 # loop through active adblock domain sources,
147 # download sources, prepare output and store all extracted domains in temp file
149 for src in ${adb_sources}
151 url="${src/\&ruleset=*/}"
152 src_name="${src/*\&ruleset=rset_/}"
153 adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
154 list_time="$(grep -F "# last modified: " "${adb_dnsfile}" 2>/dev/null)"
155 list_time="${list_time/*: /}"
156 f_log "=> processing adblock source '${src_name}'"
158 # prepare find statement with active adblock list sources
160 if [ -z "${adb_srclist}" ]
162 adb_srclist="! -name ${adb_dnsprefix}.${src_name}"
164 adb_srclist="${adb_srclist} -a ! -name ${adb_dnsprefix}.${src_name}"
167 # only download adblock list with newer/updated timestamp
169 if [ "${src_name}" = "blacklist" ]
171 url_time="$(date -r "${adb_blacklist}" 2>/dev/null)"
172 elif [ "${src_name}" = "shalla" ]
174 url_time="${shalla_time}"
176 url_time="$(wget ${wget_parm} --timeout=5 --server-response --spider "${url}" 2>&1 | grep -F "Last-Modified: " 2>/dev/null | tr -d '\r' 2>/dev/null)"
177 url_time="${url_time/*: /}"
179 if [ -z "${url_time}" ]
182 f_log " no online timestamp received, current date will be used"
184 if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ]
186 if [ "${src_name}" = "blacklist" ]
188 tmp_domains="$(cat "${adb_blacklist}" 2>/dev/null)"
190 elif [ "${src_name}" = "shalla" ]
192 tmp_domains="$(cat "${shalla_file}" 2>/dev/null)"
195 tmp_domains="$(wget ${wget_parm} --timeout="${adb_maxtime}" --output-document=- "${url}" 2>/dev/null)"
199 f_log " source doesn't change
, no update required
"
203 # check download result and prepare domain output by regex patterns
205 if [ $((rc)) -eq 0 ] && [ -n "${tmp_domains}" ]
207 eval "$
(printf "${src}" |
sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
208 count="$
(printf "%s\n" "${tmp_domains}" | tr '[A-Z]' '[a-z]' | eval "${ruleset}" | tee "${adb_tmpfile}" |
wc -l)"
209 f_log " source download finished
(${count} entries
)"
210 if [ "${src_name}" = "shalla
" ]
212 rm -f "${shalla_file}" >/dev/null 2>&1
215 elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ]
217 f_log " empty
source download finished
"
221 f_log " source download failed
"
225 # remove whitelist domains, sort domains and make them unique,
226 # finally rewrite ad/abuse domain information to separate dnsmasq files
228 if [ $((count)) -gt 0 ] && [ -n "${adb_tmpfile}" ]
230 if [ -s "${adb_whitelist}" ]
232 grep -Fvxf "${adb_whitelist}" "${adb_tmpfile}" 2>/dev/null | sort 2>/dev/null | uniq -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null > "${adb_dnsfile}"
235 sort "${adb_tmpfile}" 2>/dev/null | uniq -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null > "${adb_dnsfile}"
239 # prepare find statement with revised adblock list sources
241 if [ -z "${adb_revsrclist}" ]
243 adb_revsrclist="-name ${adb_dnsprefix}.
${src_name}"
245 adb_revsrclist="${adb_revsrclist} -o -name ${adb_dnsprefix}.${src_name}"
248 # write preliminary adblock list footer
252 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
254 count="$
(($
(wc -l < "${adb_dnsdir}/${adb_dnsprefix}.${src_name}") / 2))"
256 count="$
(wc -l < "${adb_dnsdir}/${adb_dnsprefix}.${src_name}")"
258 printf "%s
\n" "#------------------------------------------------------------------" >> "${adb_dnsfile}"
259 printf "%s\n" "# ${0##*/} (${adb_version}) - ${count} ad/abuse domains blocked" >> "${adb_dnsfile}"
260 printf "%s\n" "# source: ${url}" >> "${adb_dnsfile}"
261 printf "%s\n" "# last modified: ${url_time}" >> "${adb_dnsfile}"
262 f_log
" domain merging finished"
264 f_log
" domain merging failed" "${rc}"
268 f_log
" empty domain input received"
273 # remove old adblock lists and their backups
275 if [ -n "${adb_srclist}" ]
277 adb_rmfind
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -print -exec rm -f "{}" \; 2>/dev/null)"
278 if [ $
((rc
)) -eq 0 ] && [ -n "${adb_rmfind}" ]
280 f_log
"no longer used adblock lists removed" "${rc}"
281 elif [ $
((rc
)) -ne 0 ]
283 f_log
"error during removal of old adblock lists" "${rc}"
286 if [ "${backup_ok}" = "true" ]
288 find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -exec rm -f "{}" \
; 2>/dev
/null
291 f_log
"error during removal of old backups" "${rc}"
296 rm -f "${adb_dnsdir}/${adb_dnsprefix}."* >/dev
/null
2>&1
297 if [ "${backup_ok}" = "true" ]
299 rm -f "${adb_backupdir}/${adb_dnsprefix}."* >/dev
/null
2>&1
300 f_log
"all available adblock lists and backups removed"
302 f_log
"all available adblock lists removed"
306 # make separate adblock lists unique
308 if [ $
((adb_unique
)) -eq 1 ]
310 if [ -n "${adb_revsrclist}" ]
312 f_log
"remove duplicates in separate adblock lists"
314 # generate a temporary, unique overall list
316 head -qn -4 "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null | sort -u 2>/dev/null > "${adb_dnsdir}/tmp.overall"
318 # loop through all separate lists, ordered by size (ascending)
320 for list
in $
(ls -Sr "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev
/null
)
322 # check separate lists vs. overall list,
323 # rewrite only duplicate entries back to separate lists
326 sort "${adb_dnsdir}/tmp.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" 2>/dev/null | uniq -d 2>/dev/null > "${adb_dnsdir}/tmp.${list}"
328 # remove these entries from overall list,
329 # rewrite only unique entries back to overall list
331 tmp_unique
="$(sort "${adb_dnsdir}/tmp.overall" "${adb_dnsdir}/tmp.${list}" 2>/dev/null | uniq -u 2>/dev/null)"
332 printf "%s\n" "${tmp_unique}" > "${adb_dnsdir}/tmp.overall"
334 # write final adblocklist footer
336 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
338 count
="$(($(wc -l < "${adb_dnsdir}/tmp.
${list}") / 2))"
340 count
="$(wc -l < "${adb_dnsdir}/tmp.
${list}")"
342 printf "%s\n" "#------------------------------------------------------------------" >> "${adb_dnsdir}/tmp.${list}"
343 printf "%s\n" "# ${0##*/} (${adb_version}) - ${count} ad/abuse domains blocked" >> "${adb_dnsdir}/tmp.${list}"
344 tail -qn -2 "${adb_dnsdir}/$adb_dnsprefix.${list}" 2>/dev/null >> "${adb_dnsdir}/tmp.${list}"
345 mv -f "${adb_dnsdir}/tmp.${list}" "${adb_dnsdir}/${adb_dnsprefix}.${list}" >/dev
/null
2>&1
347 rm -f "${adb_dnsdir}/tmp.overall" >/dev
/null
2>&1
353 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
355 adb_count
="$(($(head -qn -4 "${adb_dnsdir}/${adb_dnsprefix}.
"* 2>/dev/null | wc -l) / 2))"
357 adb_count
="$(head -qn -4 "${adb_dnsdir}/${adb_dnsprefix}.
"* 2>/dev/null | wc -l)"
360 # restore adblock lists if overall count is null (i.e. all downloads failed)
362 if [ "${backup_ok}" = "true" ] && [ $
((adb_count
)) -eq 0 ]
367 # restart dnsmasq with newly generated or deleted adblock lists,
368 # check dnsmasq startup afterwards
370 if [ -n "${adb_revsrclist}" ] ||
[ -n "${adb_rmfind}" ]
372 /etc
/init.d
/dnsmasq restart
>/dev
/null
2>&1
374 dns_status
="$(ps 2>/dev/null | grep "[d
]nsmasq
" 2>/dev/null)"
375 if [ -n "${dns_status}" ]
377 f_log
"adblock lists with overall ${adb_count} domains loaded"
380 f_log
"dnsmasq restart failed, please check 'logread' output" "${rc}"
384 f_log
"adblock lists with overall ${adb_count} domains are still valid, no dnsmasq restart required"
387 # create adblock list backups
389 if [ "${backup_ok}" = "true" ] && [ "$(printf "${adb_dnsdir}/${adb_dnsprefix}."*)" != "${adb_dnsdir}/${adb_dnsprefix}.*" ]
391 for file in ${adb_dnsdir}/${adb_dnsprefix}.
*
393 filename
="${file##*/}"
394 if [ ! -f "${adb_backupdir}/${filename}" ] || [ "${file}" -nt "${adb_backupdir}/${filename}" ]
396 cp -pf "${file}" "${adb_backupdir}" 2>/dev
/null
400 f_log
"error during backup of adblock list (${filename})" "${rc}"
406 if [ "${backup_done}" = "true" ]
408 f_log
"new adblock list backups generated"
410 f_log
"adblock list backups are still valid, no new backups required"
414 # remove temporary files and exit