4092d84fb6e6c40a46da5006abac945973505ef0
2 # dns based ad/abuse domain blocking script
3 # written by Dirk Brenken (dev@brenken.org)
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12 adb_pidfile
="/var/run/adblock.pid"
13 adb_scriptver
="1.4.10"
15 adb_scriptdir
="${0%/*}"
16 if [ -r "${adb_pidfile}" ]
19 logger
-s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))"
22 printf "${adb_pid}" > "${adb_pidfile}"
23 if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
25 .
"${adb_scriptdir}/adblock-helper.sh"
29 logger
-s -t "adblock[${adb_pid}] error" "adblock function library not found"
30 rm -f "${adb_pidfile}"
35 # call trap function on error signals (HUP, INT, QUIT, BUS, SEGV, TERM)
37 trap "rc=250; f_log 'error signal received/trapped'; f_exit" 1 2 3 10 11 15
43 # main loop for all block list sources
45 for src_name
in ${adb_sources}
47 # check disabled sources
49 eval "enabled=\"\${enabled_${src_name}}\""
50 if [ "${enabled}" = "0" ]
52 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
54 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
55 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
57 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
60 f_log
"=> disabled source '${src_name}' removed"
62 "${adb_uci}" -q delete
"adblock.${src_name}.adb_src_count"
63 "${adb_uci}" -q delete
"adblock.${src_name}.adb_src_timestamp"
67 f_log
"=> processing source '${src_name}'"
68 eval "url=\"\${adb_src_${src_name}}\""
69 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
70 eval "list_time=\"\${CONFIG_${src_name}_adb_src_timestamp}\""
71 adb_dnsfile
="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
73 # check 'url' and 'src_rset' values
75 if [ -z "${url}" ] ||
[ -z "${src_rset}" ]
77 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=broken config"
78 f_log
" broken source configuration, skipped"
82 # download only block list with newer/updated timestamp
84 if [ "${src_name}" = "blacklist" ]
86 url_time
="$(date -r "${url}")"
88 url_time
="$(${adb_fetch} ${fetch_parm} ${response_parm} "${url}" 2>&1 | awk '$0 ~ /Last-Modified/ {printf substr($0,18)}')"
90 if [ -z "${url_time}" ]
93 f_log
" no online timestamp"
95 if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ] || [ ! -r "${adb_dnsfile}" ] ||\
96 ([ "${backup_ok}" = "true" ] && [ ! -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ])
98 if [ "${src_name}" = "blacklist" ]
100 tmp_domains
="$(strings -n 1 "${url}")"
101 elif [ "${src_name}" = "shalla" ]
103 shalla_archive
="${adb_tmpdir}/shallalist.tar.gz"
104 shalla_file
="${adb_tmpdir}/shallalist.txt"
105 "${adb_fetch}" ${fetch_parm} -O "${shalla_archive}" "${url}"
110 for category
in ${adb_src_cat_shalla}
112 tar -xOzf "${shalla_archive}" BL/${category}/domains >> "${shalla_file}"
116 f_log
" archive extraction failed (${category})"
120 tmp_domains
="$(strings -n 1 "${shalla_file}")"
121 rm -rf "${adb_tmpdir}/BL"
122 rm -f "${shalla_archive}"
123 rm -f "${shalla_file}"
126 tmp_domains
="$(${adb_fetch} ${fetch_parm} -O- "${url}" |
strings -n 1)"
130 f_log " source doesn
't change, skipped"
134 # check download result and prepare domain output, backup/restore if needed
136 if [ $((rc)) -eq 0 ] && [ -n "${tmp_domains}" ]
138 count="$(printf "%s\n" "${tmp_domains}" | awk "${src_rset}" | tee "${adb_tmpfile}" | wc -l)"
139 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=${url_time}"
140 if [ "${backup_ok}" = "true" ]
142 gzip -cf "${adb_tmpfile}" > "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
144 f_log " source download finished (${count} entries)"
146 elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ]
148 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
150 gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
151 count="$(wc -l < "${adb_tmpfile}")"
152 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
153 f_log " empty source download, restored (${count} entries)"
155 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
157 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
160 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
161 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty download"
162 f_log " empty source download, skipped"
167 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
169 gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
170 count="$(wc -l < "${adb_tmpfile}")"
171 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
172 f_log " source download failed, restored (${count} entries)"
174 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
176 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
179 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
180 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=download failed"
181 f_log " source download failed, skipped"
186 # remove whitelist domains, sort domains and make them unique,
187 # rewrite ad/abuse domain information to separate dnsmasq files
189 if [ $((count)) -gt 0 ] && [ -n "${adb_tmpfile}" ]
191 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
193 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpfile}" | sort -u | eval "${adb_dnsformat}" > "${adb_dnsfile}"
195 sort -u "${adb_tmpfile}" | eval "${adb_dnsformat}" > "${adb_dnsfile}"
201 f_log " domain merging finished"
204 rm -f "${adb_dnsfile}"
205 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
207 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
209 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
210 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=domain merging failed"
211 f_log " domain merging failed, skipped"
215 rm -f "${adb_dnsfile}"
216 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
218 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
220 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
221 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty domain input"
222 f_log " empty domain input, skipped"
227 # overall sort, make block list entries unique
229 if [ "${rev_done}" = "true" ] && [ "${mem_ok}" = "true" ]
231 f_log "remove duplicates in separate block lists"
232 for list in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
235 if [ -s "${adb_tmpdir}/blocklist.overall" ]
237 sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/blocklist.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" | uniq -u > "${adb_tmpdir}/tmp.blocklist"
238 cat "${adb_tmpdir}/tmp.blocklist" > "${adb_dnsdir}/${adb_dnsprefix}.${list}"
240 cat "${adb_dnsdir}/${adb_dnsprefix}.${list}" >> "${adb_tmpdir}/blocklist.overall"
244 # restart & check dnsmasq with generated set of block lists
246 if [ "${rev_done}" = "true" ] || [ "${rm_done}" = "true" ] || [ -n "${mv_done}" ]
248 "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
249 /etc/init.d/dnsmasq restart
251 check="$(pgrep -f "dnsmasq")"
255 f_log "block lists with overall ${adb_count} domains loaded"
259 check="$(pgrep -f "dnsmasq")"
262 f_log "dnsmasq restart without block lists succeeded, please check your configuration"
264 f_log "dnsmasq restart without block lists failed, please check your configuration"
271 f_log "block lists with overall ${adb_count} domains are still valid, no update required"
274 # remove temporary files and exit