adblock.init 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (c) 2015-2022 Dirk Brenken (dev@brenken.org)
  3. # This is free software, licensed under the GNU General Public License v3.
  4. # disable (s)hellcheck in release
  5. # shellcheck disable=all
  6. START=30
  7. USE_PROCD=1
  8. extra_command "suspend" "Suspend adblock processing"
  9. extra_command "resume" "Resume adblock processing"
  10. extra_command "query" "<domain> Query active blocklists and backups for a specific domain"
  11. extra_command "report" "[[<cli>|<mail>|<gen>|<json>] [<top_count>] [<res_count>] [<search>]] Print DNS statistics with an optional search parameter"
  12. extra_command "list" "[<add>|<add_utc>|<add_eng>|<add_stb>|<remove>|<remove_utc>|<remove_eng>|<remove_stb>] <source(s)> List/Edit available sources"
  13. extra_command "timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
  14. adb_init="/etc/init.d/adblock"
  15. adb_script="/usr/bin/adblock.sh"
  16. adb_pidfile="/var/run/adblock.pid"
  17. if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] ||
  18. [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] ||
  19. [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] ||
  20. { [ "${action}" = "list" ] && [ -n "${1}" ]; }; }; then
  21. return 0
  22. fi
  23. boot() {
  24. [ -s "${adb_pidfile}" ] && : >"${adb_pidfile}"
  25. rc_procd start_service
  26. }
  27. start_service() {
  28. if "${adb_init}" enabled; then
  29. if [ "${action}" = "boot" ]; then
  30. [ -n "$(uci_get adblock global adb_trigger)" ] && return 0
  31. fi
  32. procd_open_instance "adblock"
  33. procd_set_param command "${adb_script}" "${@}"
  34. procd_set_param pidfile "${adb_pidfile}"
  35. procd_set_param nice "$(uci_get adblock global adb_nice "0")"
  36. procd_set_param stdout 1
  37. procd_set_param stderr 1
  38. procd_close_instance
  39. fi
  40. }
  41. reload_service() {
  42. rc_procd start_service reload
  43. }
  44. stop_service() {
  45. rc_procd "${adb_script}" stop
  46. }
  47. restart() {
  48. rc_procd start_service restart
  49. }
  50. suspend() {
  51. rc_procd start_service suspend
  52. }
  53. resume() {
  54. rc_procd start_service resume
  55. }
  56. query() {
  57. rc_procd "${adb_script}" query "${1}"
  58. }
  59. report() {
  60. rc_procd "${adb_script}" report "${1:-"cli"}" "${2}" "${3}" "${4}"
  61. }
  62. list() {
  63. local src_archive src_file src_enabled enabled name utc_list size focus descurl action="${1}"
  64. if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]; then
  65. shift
  66. for name in "${@}"; do
  67. case "${action}" in
  68. "add")
  69. if ! uci_get adblock global adb_sources | grep -q "${name}"; then
  70. uci_add_list adblock global adb_sources "${name}"
  71. printf "%s\n" "::: adblock source '${name}' added to config"
  72. fi
  73. ;;
  74. "remove")
  75. if uci_get adblock global adb_sources | grep -q "${name}"; then
  76. uci_remove_list adblock global adb_sources "${name}"
  77. printf "%s\n" "::: adblock source '${name}' removed from config"
  78. fi
  79. ;;
  80. "add_utc")
  81. if ! uci_get adblock global adb_utc_sources | grep -q "${name}"; then
  82. uci_add_list adblock global adb_utc_sources "${name}"
  83. printf "%s\n" "::: adblock utcapitole '${name}' added to config"
  84. fi
  85. ;;
  86. "remove_utc")
  87. if uci_get adblock global adb_utc_sources | grep -q "${name}"; then
  88. uci_remove_list adblock global adb_utc_sources "${name}"
  89. printf "%s\n" "::: adblock utcapitole '${name}' removed from config"
  90. fi
  91. ;;
  92. "add_eng")
  93. if ! uci_get adblock global adb_eng_sources | grep -q "${name}"; then
  94. uci_add_list adblock global adb_eng_sources "${name}"
  95. printf "%s\n" "::: adblock energized '${name}' added to config"
  96. fi
  97. ;;
  98. "remove_eng")
  99. if uci_get adblock global adb_eng_sources | grep -q "${name}"; then
  100. uci_remove_list adblock global adb_eng_sources "${name}"
  101. printf "%s\n" "::: adblock energized '${name}' removed from config"
  102. fi
  103. ;;
  104. "add_stb")
  105. if ! uci_get adblock global adb_stb_sources | grep -q "${name}"; then
  106. uci_add_list adblock global adb_stb_sources "${name}"
  107. printf "%s\n" "::: adblock stevenblack '${name}' added to config"
  108. fi
  109. ;;
  110. "remove_stb")
  111. if uci_get adblock global adb_stb_sources | grep -q "${name}"; then
  112. uci_remove_list adblock global adb_stb_sources "${name}"
  113. printf "%s\n" "::: adblock stevenblack '${name}' removed from config"
  114. fi
  115. ;;
  116. esac
  117. done
  118. [ -n "$(uci -q changes adblock)" ] && { uci_commit adblock; "${adb_init}" start; }
  119. else
  120. src_archive="$(uci_get adblock global adb_srcarc "/etc/adblock/adblock.sources.gz")"
  121. src_file="$(uci_get adblock global adb_srcfile "/tmp/adb_sources.json")"
  122. src_enabled="$(uci -q show adblock.global.adb_sources)"
  123. [ -r "${src_archive}" ] && zcat "${src_archive}" >"${src_file}" || printf "%s\n" "::: adblock source archive '${src_archive}' not found"
  124. if [ -r "${src_file}" ]; then
  125. src_enabled="${src_enabled#*=}"
  126. src_enabled="${src_enabled//\'}"
  127. printf "%s\n" "::: Available adblock sources"
  128. printf "%s\n" ":::"
  129. printf "%-25s%-10s%-7s%-21s%s\n" " Name" "Enabled" "Size" "Focus" "Info URL"
  130. printf "%s\n" " -------------------------------------------------------------------"
  131. json_load_file "${src_file}"
  132. json_get_keys keylist
  133. for key in ${keylist}; do
  134. json_select "${key}"
  135. json_get_var size "size"
  136. json_get_var focus "focus"
  137. json_get_var descurl "descurl"
  138. json_get_var url "url"
  139. json_get_var rule "rule"
  140. if [ -n "${url}" ] && [ -n "${rule}" ]; then
  141. if printf "%s" "${src_enabled}" | grep -q "${key}"; then
  142. enabled="x"
  143. else
  144. enabled=" "
  145. fi
  146. src_enabled="${src_enabled/${key}}"
  147. printf " + %-21s%-10s%-7s%-21s%s\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
  148. else
  149. src_enabled="${src_enabled} ${key}"
  150. fi
  151. json_select ..
  152. done
  153. utc_list="$(uci_get adblock global adb_utc_sources "-")"
  154. eng_list="$(uci_get adblock global adb_eng_sources "-")"
  155. stb_list="$(uci_get adblock global adb_stb_sources "-")"
  156. printf "%s\n" " ---------------------------------------------------------------------------"
  157. printf " * %s\n" "Configured utcapitole categories: ${utc_list// /, }"
  158. printf " * %s\n" "Configured energized variants: ${eng_list// /, }"
  159. printf " * %s\n" "Configured stevenblack variants: ${stb_list// /, }"
  160. if [ -n "${src_enabled// }" ]; then
  161. printf "%s\n" " ---------------------------------------------------------------------------"
  162. printf "%s\n" " Sources with invalid configuration"
  163. printf "%s\n" " ---------------------------------------------------------------------------"
  164. for key in ${src_enabled}; do
  165. printf " - %s\n" "${key:0:20}"
  166. done
  167. fi
  168. else
  169. printf "%s\n" "::: adblock source file '${src_file}' not found"
  170. fi
  171. fi
  172. }
  173. status() {
  174. status_service
  175. }
  176. status_service() {
  177. local key keylist value idxval values type rtfile
  178. rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
  179. json_load_file "${rtfile}" >/dev/null 2>&1
  180. json_get_keys keylist
  181. if [ -n "${keylist}" ]; then
  182. printf "%s\n" "::: adblock runtime information"
  183. for key in ${keylist}; do
  184. json_get_var value "${key}" >/dev/null 2>&1
  185. if [ "${key%_*}" = "active" ]; then
  186. printf " + %-15s : " "${key}"
  187. json_select "${key}" >/dev/null 2>&1
  188. values=""
  189. index="1"
  190. while json_get_type type "${index}" && [ "${type}" = "object" ]; do
  191. json_get_values idxval "${index}" >/dev/null 2>&1
  192. if [ "${index}" = "1" ]; then
  193. values="${idxval}"
  194. else
  195. values="${values}, ${idxval}"
  196. fi
  197. index="$((index + 1))"
  198. done
  199. values="$(printf "%s" "${values}" | awk '{NR=1;max=98;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max)}else{printf"%-22s%s\n","",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
  200. printf "%s\n" "${values:-"-"}"
  201. json_select ".."
  202. else
  203. printf " + %-15s : %s\n" "${key}" "${value:-"-"}"
  204. fi
  205. done
  206. else
  207. printf "%s\n" "::: no adblock runtime information available"
  208. fi
  209. }
  210. timer() {
  211. local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"
  212. cron_file="/etc/crontabs/root"
  213. if [ -s "${cron_file}" ] && [ "${action}" = "list" ]; then
  214. awk '{print NR "> " $0}' "${cron_file}"
  215. elif [ -x "/etc/init.d/cron" ] && [ "${action}" = "add" ]; then
  216. hour="${hour//[[:alpha:]]/}"
  217. minute="${minute//[[:alpha:]]/}"
  218. if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] &&
  219. [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] &&
  220. [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]; then
  221. printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >>"${cron_file}"
  222. /etc/init.d/cron restart
  223. fi
  224. elif [ -x "/etc/init.d/cron" ] && [ -s "${cron_file}" ] && [ "${action}" = "remove" ]; then
  225. cron_tasks="${cron_tasks//[[:alpha:]]/}"
  226. cron_lineno="$(awk 'END{print NR}' "${cron_file}")"
  227. cron_content="$(awk '{print $0}' "${cron_file}")"
  228. if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]; then
  229. printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" >"${cron_file}"
  230. /etc/init.d/cron restart
  231. fi
  232. fi
  233. }
  234. service_triggers() {
  235. local iface delay
  236. iface="$(uci_get adblock global adb_trigger)"
  237. delay="$(uci_get adblock global adb_triggerdelay "5")"
  238. PROCD_RELOAD_DELAY="$((delay * 1000))"
  239. [ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
  240. procd_add_reload_trigger "adblock"
  241. }