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.1"
  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})"
  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. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  66. shalla_file="${adb_tmpdir}/shallalist.txt"
  67. curl ${curl_parm} --max-time "${adb_maxtime}" "${adb_arc_shalla}" --output "${shalla_archive}" 2>/dev/null
  68. rc=${?}
  69. if [ $((rc)) -eq 0 ]
  70. then
  71. f_log "shallalist archive download finished"
  72. else
  73. f_log "shallalist archive download failed (${adb_arc_shalla})" "${rc}"
  74. f_restore
  75. fi
  76. # extract and merge only domains of selected shallalist categories
  77. #
  78. > "${shalla_file}"
  79. for category in ${adb_cat_shalla}
  80. do
  81. tar -C "${adb_tmpdir}" -xzf "${shalla_archive}" BL/${category}/domains 2>/dev/null
  82. rc=${?}
  83. if [ $((rc)) -eq 0 ]
  84. then
  85. if [ -r "${adb_tmpdir}/BL/${category}/domains" ]
  86. then
  87. cat "${adb_tmpdir}/BL/${category}/domains" 2>/dev/null >> "${shalla_file}"
  88. fi
  89. else
  90. f_log "shallalist archive extraction failed (${category})" "${rc}"
  91. f_restore
  92. fi
  93. done
  94. # finish shallalist (pre-)processing
  95. #
  96. rm -f "${shalla_archive}" >/dev/null 2>&1
  97. rm -rf "${adb_tmpdir}/BL" >/dev/null 2>&1
  98. adb_sources="${adb_sources} file:///${shalla_file}&ruleset=rset_shalla"
  99. f_log "shallalist (pre-)processing finished (${adb_cat_shalla# })"
  100. fi
  101. # loop through active adblock domain sources,
  102. # prepare output and store all extracted domains in temp file
  103. #
  104. adb_sources="${adb_sources} file://${adb_blacklist}&ruleset=rset_default"
  105. for src in ${adb_sources}
  106. do
  107. # download selected adblock sources
  108. #
  109. url="${src//\&ruleset=*/}"
  110. check_url="$(printf "${url}" | sed -n '/^https:/p')"
  111. if [ -n "${check_url}" ]
  112. then
  113. tmp_var="$(wget ${wget_parm} --timeout="${adb_maxtime}" --tries=1 --output-document=- "${url}" 2>/dev/null)"
  114. rc=${?}
  115. else
  116. tmp_var="$(curl ${curl_parm} --max-time "${adb_maxtime}" "${url}" 2>/dev/null)"
  117. rc=${?}
  118. fi
  119. # check download result and prepare domain output by regex patterns
  120. #
  121. if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
  122. then
  123. eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
  124. tmp_var="$(printf "%s\n" "${tmp_var}" | tr '[A-Z]' '[a-z]')"
  125. count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${adb_tmpfile}" | wc -l)"
  126. f_log "source download finished (${url}, ${count} entries)"
  127. if [ "${url}" = "file:///${shalla_file}" ]
  128. then
  129. rm -f "${shalla_file}" >/dev/null 2>&1
  130. fi
  131. unset tmp_var 2>/dev/null
  132. elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
  133. then
  134. f_log "empty source download finished (${url})"
  135. else
  136. f_log "source download failed (${url})" "${rc}"
  137. f_restore
  138. fi
  139. done
  140. # remove whitelist domains, sort domains and make them unique
  141. # and finally rewrite ad/abuse domain information to dnsmasq file
  142. #
  143. > "${adb_dnsfile}"
  144. grep -vxf "${adb_whitelist}" < "${adb_tmpfile}" 2>/dev/null | sort -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null >> "${adb_dnsfile}"
  145. rc=${?}
  146. if [ $((rc)) -eq 0 ]
  147. then
  148. rm -f "${adb_tmpfile}" >/dev/null 2>&1
  149. f_log "domain merging finished"
  150. else
  151. f_log "domain merging failed" "${rc}"
  152. f_restore
  153. fi
  154. # write dns file footer
  155. #
  156. f_footer
  157. # restart dnsmasq with newly generated block list
  158. #
  159. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  160. sleep 2
  161. # dnsmasq health check
  162. #
  163. f_dnscheck
  164. # remove files and exit
  165. #
  166. f_remove