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.

161 lines
3.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. #
  3. START=99
  4. EXTRA_COMMANDS="toggle stats cfgup query"
  5. EXTRA_HELP=" toggle Toggle adblock 'on' or 'off'
  6. stats Update adblock statistics
  7. cfgup Update adblock configuration file
  8. query <DOMAIN> Query active blocklists for specific domain"
  9. adb_debug=0
  10. adb_pid="${$}"
  11. adb_script="/usr/bin/adblock-update.sh"
  12. adb_helper="/usr/bin/adblock-helper.sh"
  13. adb_pidfile="/var/run/adblock.pid"
  14. bg_parm="&"
  15. if [ $((adb_debug)) -eq 0 ]
  16. then
  17. exec 2>/dev/null
  18. fi
  19. if [ -r "${adb_pidfile}" ]
  20. then
  21. logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
  22. exit 255
  23. fi
  24. . "${adb_helper}"
  25. f_envload
  26. if [ "${adb_restricted}" = "1" ]
  27. then
  28. adb_uci="$(which true)"
  29. fi
  30. boot()
  31. {
  32. return 0
  33. }
  34. start()
  35. {
  36. if [ -t 1 ]
  37. then
  38. unset bg_parm
  39. fi
  40. eval "${adb_script}" ${bg_parm}
  41. return 0
  42. }
  43. restart()
  44. {
  45. stop
  46. start
  47. }
  48. reload()
  49. {
  50. reload="true"
  51. stop
  52. start
  53. }
  54. stop()
  55. {
  56. f_rmdns
  57. f_rmuhttpd
  58. config_foreach f_rmconfig source
  59. if [ -z "${reload}" ]
  60. then
  61. f_rmfirewall
  62. fi
  63. if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "$(${adb_uci} -q changes adblock)" ]
  64. then
  65. "${adb_uci}" -q commit adblock
  66. f_log "all adblock related services stopped"
  67. fi
  68. return 0
  69. }
  70. toggle()
  71. {
  72. if [ -d "${adb_dnshidedir}" ]
  73. then
  74. list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  75. list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  76. if [ -n "${list_dns}" ]
  77. then
  78. source="${adb_dnsdir}/${adb_dnsprefix}"
  79. target="${adb_dnshidedir}"
  80. pos="off"
  81. elif [ -n "${list_dnshide}" ]
  82. then
  83. source="${adb_dnshidedir}/${adb_dnsprefix}"
  84. target="${adb_dnsdir}"
  85. pos="on"
  86. fi
  87. if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
  88. then
  89. mv -f "${source}"* "${target}"
  90. /etc/init.d/dnsmasq restart
  91. "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
  92. "${adb_uci}" -q commit "adblock"
  93. f_log "adblock toggle switched '${pos}'"
  94. fi
  95. fi
  96. return 0
  97. }
  98. stats()
  99. {
  100. f_statistics
  101. "${adb_uci}" -q commit "adblock"
  102. return 0
  103. }
  104. cfgup()
  105. {
  106. stop
  107. cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
  108. rc=$?
  109. if [ $((rc)) -eq 0 ]
  110. then
  111. f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'"
  112. else
  113. f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'"
  114. fi
  115. return 0
  116. }
  117. query()
  118. {
  119. domain="${1}"
  120. tld="${domain#*.}"
  121. list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  122. if [ -z "${list_dns}" ]
  123. then
  124. f_log "no active blocklists found, please start adblock first"
  125. elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
  126. then
  127. f_log "invalid domain query input, please submit a specific (sub-)domain, i.e. 'www.abc.xyz'"
  128. else
  129. while [ "${domain}" != "${tld}" ]
  130. do
  131. search="${domain//./\.}"
  132. result="$(grep -Hm 1 "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk -F ':|/' '{print " "$4"\t: "$6}')"
  133. count="$(grep -hc "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk '{sum += $1} END {printf sum}')"
  134. printf "%s\n" "=> distinct results for domain '${domain}' (overall ${count})"
  135. if [ -z "${result}" ]
  136. then
  137. printf "%s\n" " no matches in active blocklists"
  138. else
  139. printf "%s\n" "${result}"
  140. fi
  141. domain="${tld}"
  142. tld="${domain#*.}"
  143. done
  144. fi
  145. return 0
  146. }