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.

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