You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.2 KiB

  1. #!/bin/sh
  2. #######################################################
  3. # ad/abuse domain blocking script for dnsmasq/openwrt #
  4. # written by Dirk Brenken (dirk@brenken.org) #
  5. #######################################################
  6. # LICENSE
  7. # ========
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ###############
  21. # environment #
  22. ###############
  23. # set script version
  24. #
  25. adb_version="0.22.2"
  26. # get current pid, script directory and openwrt version
  27. #
  28. pid=${$}
  29. adb_scriptdir="${0%/*}"
  30. openwrt_version="$(cat /etc/openwrt_version 2>/dev/null)"
  31. # source in adblock function library
  32. #
  33. if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
  34. then
  35. . "${adb_scriptdir}/adblock-helper.sh"
  36. else
  37. rc=500
  38. /usr/bin/logger -s -t "adblock[${pid}] error" "adblock function library not found, rc: ${rc}"
  39. exit ${rc}
  40. fi
  41. ################
  42. # main program #
  43. ################
  44. # call restore function on trap signals (HUP, INT, QUIT, BUS, SEGV, TERM)
  45. #
  46. trap "f_log 'trap error' '600'; f_restore" 1 2 3 10 11 15
  47. # start logging
  48. #
  49. f_log "domain adblock processing started (${adb_version}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  50. # load environment
  51. #
  52. f_envload
  53. # parse environment
  54. #
  55. f_envparse
  56. # check environment
  57. #
  58. f_envcheck
  59. # start shallalist (pre-)processing
  60. #
  61. if [ -n "${adb_arc_shalla}" ]
  62. then
  63. # download shallalist archive
  64. #
  65. f_log "shallalist (pre-)processing started ..."
  66. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  67. shalla_file="${adb_tmpdir}/shallalist.txt"
  68. curl ${curl_parm} --max-time "${adb_maxtime}" "${adb_arc_shalla}" --output "${shalla_archive}" 2>/dev/null
  69. rc=${?}
  70. if [ $((rc)) -ne 0 ]
  71. then
  72. f_log "shallalist archive download failed (${adb_arc_shalla})" "${rc}"
  73. f_restore
  74. fi
  75. # extract and merge only domains of selected shallalist categories
  76. #
  77. > "${shalla_file}"
  78. for category in ${adb_cat_shalla}
  79. do
  80. tar -xOzf "${shalla_archive}" BL/${category}/domains 2>/dev/null >> "${shalla_file}"
  81. rc=${?}
  82. if [ $((rc)) -ne 0 ]
  83. then
  84. f_log "shallalist archive extraction failed (${category})" "${rc}"
  85. f_restore
  86. fi
  87. done
  88. # finish shallalist (pre-)processing
  89. #
  90. rm -f "${shalla_archive}" >/dev/null 2>&1
  91. rm -rf "${adb_tmpdir}/BL" >/dev/null 2>&1
  92. adb_sources="${adb_sources} file:///${shalla_file}&ruleset=rset_shalla"
  93. f_log "shallalist (pre-)processing finished (${adb_cat_shalla# })"
  94. fi
  95. # loop through active adblock domain sources,
  96. # prepare output and store all extracted domains in temp file
  97. #
  98. adb_sources="${adb_sources} file://${adb_blacklist}&ruleset=rset_default"
  99. for src in ${adb_sources}
  100. do
  101. # download selected adblock sources
  102. #
  103. url="${src//\&ruleset=*/}"
  104. check_url="$(printf "${url}" | sed -n '/^https:/p')"
  105. if [ -n "${check_url}" ]
  106. then
  107. tmp_var="$(wget ${wget_parm} --timeout="${adb_maxtime}" --tries=1 --output-document=- "${url}" 2>/dev/null)"
  108. rc=${?}
  109. else
  110. tmp_var="$(curl ${curl_parm} --max-time "${adb_maxtime}" "${url}" 2>/dev/null)"
  111. rc=${?}
  112. fi
  113. # check download result and prepare domain output by regex patterns
  114. #
  115. if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
  116. then
  117. eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
  118. tmp_var="$(printf "%s\n" "${tmp_var}" | tr '[A-Z]' '[a-z]')"
  119. count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${adb_tmpfile}" | wc -l)"
  120. f_log "source download finished (${url}, ${count} entries)"
  121. if [ "${url}" = "file:///${shalla_file}" ]
  122. then
  123. rm -f "${shalla_file}" >/dev/null 2>&1
  124. fi
  125. unset tmp_var 2>/dev/null
  126. elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
  127. then
  128. f_log "empty source download finished (${url})"
  129. else
  130. f_log "source download failed (${url})" "${rc}"
  131. f_restore
  132. fi
  133. done
  134. # remove whitelist domains, sort domains and make them unique
  135. # and finally rewrite ad/abuse domain information to dnsmasq file
  136. #
  137. if [ -s "${adb_whitelist}" ]
  138. then
  139. grep -Fvxf "${adb_whitelist}" "${adb_tmpfile}" 2>/dev/null | sort -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null > "${adb_dnsfile}"
  140. rc=${?}
  141. else
  142. sort -u "${adb_tmpfile}" 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null > "${adb_dnsfile}"
  143. rc=${?}
  144. fi
  145. if [ $((rc)) -eq 0 ]
  146. then
  147. rm -f "${adb_tmpfile}" >/dev/null 2>&1
  148. f_log "domain merging finished"
  149. else
  150. f_log "domain merging failed" "${rc}"
  151. f_restore
  152. fi
  153. # write dns file footer
  154. #
  155. f_footer
  156. # restart dnsmasq with newly generated block list
  157. #
  158. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  159. sleep 2
  160. # dnsmasq health check
  161. #
  162. f_dnscheck
  163. # remove files and exit
  164. #
  165. f_remove