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.

206 lines
6.0 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.20.3"
  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. /usr/bin/logger -t "adblock[${pid}]" "error: adblock function library not found"
  37. exit 200
  38. fi
  39. ################
  40. # main program #
  41. ################
  42. # call restore function on trap signals (HUP, INT, QUIT, BUS, SEGV, TERM)
  43. #
  44. trap "restore_msg='trap error'; f_restore" 1 2 3 10 11 15
  45. # start logging
  46. #
  47. /usr/bin/logger -t "adblock[${pid}]" "info: domain adblock processing started (${adb_version})"
  48. # load environment
  49. #
  50. f_envload
  51. # parse environment
  52. #
  53. f_envparse
  54. # check environment
  55. #
  56. f_envcheck
  57. # check ntp time sync
  58. #
  59. f_ntpcheck
  60. # check wan update interface(s)
  61. #
  62. f_wancheck
  63. # check/start shallalist (pre-)processing
  64. #
  65. if [ -n "${adb_arc_shalla}" ]
  66. then
  67. # download shallalist archive
  68. #
  69. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  70. shalla_file="${adb_tmpdir}/shallalist.txt"
  71. curl --insecure --max-time "${max_time}" "${adb_arc_shalla}" -o "${shalla_archive}" 2>/dev/null
  72. rc=$?
  73. if [ $((rc)) -eq 0 ]
  74. then
  75. /usr/bin/logger -t "adblock[${pid}]" "info: shallalist archive download finished"
  76. else
  77. /usr/bin/logger -t "adblock[${pid}]" "error: shallalist archive download failed (${adb_arc_shalla})"
  78. printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive download failed (${adb_arc_shalla})" >> "${adb_logfile}"
  79. restore_msg="archive download failed"
  80. f_restore
  81. fi
  82. # extract shallalist archive
  83. #
  84. tar -xzf "${shalla_archive}" -C "${adb_tmpdir}" 2>/dev/null
  85. rc=$?
  86. if [ $((rc)) -eq 0 ]
  87. then
  88. /usr/bin/logger -t "adblock[${pid}]" "info: shallalist archive extraction finished"
  89. else
  90. /usr/bin/logger -t "adblock[${pid}]" "error: shallalist archive extraction failed"
  91. printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive extraction failed" >> "${adb_logfile}"
  92. restore_msg="archive extraction failed"
  93. f_restore
  94. fi
  95. # merge selected shallalist categories
  96. #
  97. > "${shalla_file}"
  98. for category in ${adb_cat_shalla}
  99. do
  100. if [ -f "${adb_tmpdir}/BL/${category}/domains" ]
  101. then
  102. cat "${adb_tmpdir}/BL/${category}/domains" >> "${shalla_file}" 2>/dev/null
  103. rc=$?
  104. else
  105. rc=220
  106. fi
  107. if [ $((rc)) -ne 0 ]
  108. then
  109. break
  110. fi
  111. done
  112. # finish shallalist (pre-)processing
  113. #
  114. if [ $((rc)) -eq 0 ]
  115. then
  116. adb_sources="${adb_sources} file:///${shalla_file}&ruleset=rset_shalla"
  117. /usr/bin/logger -t "adblock[${pid}]" "info: shallalist (pre-)processing finished (${adb_cat_shalla})"
  118. else
  119. /usr/bin/logger -t "adblock[${pid}]" "error: shallalist (pre-)processing failed (${rc}, ${adb_cat_shalla})"
  120. printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist (pre-)processing failed (${rc}, ${adb_cat_shalla})" >> "${adb_logfile}"
  121. restore_msg="shallalist merge failed"
  122. f_restore
  123. fi
  124. fi
  125. # loop through active adblock domain sources,
  126. # prepare output and store all extracted domains in temp file
  127. #
  128. adb_sources="${adb_sources} file://${adb_blacklist}&ruleset=rset_default"
  129. for src in ${adb_sources}
  130. do
  131. # download selected adblock sources
  132. #
  133. url="${src//\&ruleset=*/}"
  134. check_url="$(printf "${url}" | sed -n '/^https:/p')"
  135. if [ -n "${check_url}" ]
  136. then
  137. tmp_var="$(wget --timeout="${max_time}" --tries=1 --output-document=- "${url}" 2>/dev/null)"
  138. rc=$?
  139. else
  140. tmp_var="$(curl --insecure --max-time "${max_time}" "${url}" 2>/dev/null)"
  141. rc=$?
  142. fi
  143. # check download result and prepare domain output by regex patterns
  144. #
  145. if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
  146. then
  147. eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
  148. tmp_var="$(printf "%s\n" "${tmp_var}" | tr '[A-Z]' '[a-z]')"
  149. adb_count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${adb_tmpfile}" | wc -l)"
  150. /usr/bin/logger -t "adblock[${pid}]" "info: source download finished (${url}, ${adb_count} entries)"
  151. elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
  152. then
  153. /usr/bin/logger -t "adblock[${pid}]" "info: empty source download finished (${url})"
  154. else
  155. /usr/bin/logger -t "adblock[${pid}]" "error: source download failed (${url})"
  156. printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: source download failed (${url})" >> "${adb_logfile}"
  157. restore_msg="download failed"
  158. f_restore
  159. fi
  160. done
  161. # create empty destination file
  162. #
  163. > "${adb_dnsfile}"
  164. # rewrite ad/abuse domain information to dns file,
  165. # remove duplicates and whitelist entries
  166. #
  167. grep -vxf "${adb_whitelist}" < "${adb_tmpfile}" | eval "${adb_dnsformat}" | sort -u 2>/dev/null >> "${adb_dnsfile}"
  168. # write dns file footer
  169. #
  170. f_footer
  171. # restart dnsmasq with newly generated block list
  172. #
  173. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  174. sleep 2
  175. # dnsmasq health check
  176. #
  177. f_dnscheck
  178. # remove files and exit
  179. #
  180. f_remove
  181. exit 0