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.

186 lines
5.1 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.0"
  26. # get current pid and script directory
  27. #
  28. pid=${$}
  29. adb_scriptdir="${0%/*}"
  30. # source in adblock function library
  31. #
  32. if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
  33. then
  34. . "${adb_scriptdir}/adblock-helper.sh"
  35. else
  36. rc=500
  37. /usr/bin/logger -s -t "adblock[${pid}] error" "adblock function library not found, rc: ${rc}"
  38. exit ${rc}
  39. fi
  40. ################
  41. # main program #
  42. ################
  43. # call restore function on trap signals (HUP, INT, QUIT, BUS, SEGV, TERM)
  44. #
  45. trap "f_log 'trap error' '600'; f_restore" 1 2 3 10 11 15
  46. # start logging
  47. #
  48. f_log "domain adblock processing started (${adb_version})"
  49. # load environment
  50. #
  51. f_envload
  52. # parse environment
  53. #
  54. f_envparse
  55. # check environment
  56. #
  57. f_envcheck
  58. # start shallalist (pre-)processing
  59. #
  60. if [ -n "${adb_arc_shalla}" ]
  61. then
  62. # download shallalist archive
  63. #
  64. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  65. shalla_file="${adb_tmpdir}/shallalist.txt"
  66. curl "${curl_parm}" --max-time "${adb_maxtime}" "${adb_arc_shalla}" -o "${shalla_archive}" 2>/dev/null
  67. rc=${?}
  68. if [ $((rc)) -eq 0 ]
  69. then
  70. f_log "shallalist archive download finished"
  71. else
  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 -C "${adb_tmpdir}" -xzf "${shalla_archive}" BL/${category}/domains 2>/dev/null
  81. rc=${?}
  82. if [ $((rc)) -eq 0 ]
  83. then
  84. if [ -r "${adb_tmpdir}/BL/${category}/domains" ]
  85. then
  86. cat "${adb_tmpdir}/BL/${category}/domains" >> "${shalla_file}" 2>/dev/null
  87. fi
  88. else
  89. f_log "shallalist archive extraction failed (${category})" "${rc}"
  90. f_restore
  91. fi
  92. done
  93. # finish shallalist (pre-)processing
  94. #
  95. rm -f "${shalla_archive}" >/dev/null 2>&1
  96. rm -rf "${adb_tmpdir}/BL" >/dev/null 2>&1
  97. adb_sources="${adb_sources} file:///${shalla_file}&ruleset=rset_shalla"
  98. f_log "shallalist (pre-)processing finished (${adb_cat_shalla# })"
  99. fi
  100. # loop through active adblock domain sources,
  101. # prepare output and store all extracted domains in temp file
  102. #
  103. adb_sources="${adb_sources} file://${adb_blacklist}&ruleset=rset_default"
  104. for src in ${adb_sources}
  105. do
  106. # download selected adblock sources
  107. #
  108. url="${src//\&ruleset=*/}"
  109. check_url="$(printf "${url}" | sed -n '/^https:/p')"
  110. if [ -n "${check_url}" ]
  111. then
  112. tmp_var="$(wget "${wget_parm}" --timeout="${adb_maxtime}" --tries=1 --output-document=- "${url}" 2>/dev/null)"
  113. rc=${?}
  114. else
  115. tmp_var="$(curl "${curl_parm}" --max-time "${adb_maxtime}" "${url}" 2>/dev/null)"
  116. rc=${?}
  117. fi
  118. # check download result and prepare domain output by regex patterns
  119. #
  120. if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
  121. then
  122. eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
  123. tmp_var="$(printf "%s\n" "${tmp_var}" | tr '[A-Z]' '[a-z]')"
  124. count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${adb_tmpfile}" | wc -l)"
  125. f_log "source download finished (${url}, ${count} entries)"
  126. if [ "${url}" = "file:///${shalla_file}" ]
  127. then
  128. rm -f "${shalla_file}" >/dev/null 2>&1
  129. fi
  130. unset tmp_var
  131. elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
  132. then
  133. f_log "empty source download finished (${url})"
  134. else
  135. f_log "source download failed (${url})" "${rc}"
  136. f_restore
  137. fi
  138. done
  139. # remove whitelist domains, sort domains and make them unique
  140. # and finally rewrite ad/abuse domain information to dnsmasq file
  141. #
  142. > "${adb_dnsfile}"
  143. grep -vxf "${adb_whitelist}" < "${adb_tmpfile}" 2>/dev/null | sort -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null >> "${adb_dnsfile}" 2>/dev/null
  144. rc=${?}
  145. if [ $((rc)) -eq 0 ]
  146. then
  147. unset adb_tmpfile
  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