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.

258 lines
6.7 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (c) 2015-2020 Dirk Brenken (dev@brenken.org)
  3. # This is free software, licensed under the GNU General Public License v3.
  4. # set (s)hellcheck exceptions
  5. # shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
  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" "[<search>] Print DNS statistics with an optional search parameter"
  12. extra_command "list" "[[<add>|<remove>] [source(s)]] List available adblock sources or add/remove them from config"
  13. extra_command "timer" "<action> <hour> [<minute>] [<weekday>] Set a cron based update interval"
  14. extra_command "version" "Print version information"
  15. adb_init="/etc/init.d/adblock"
  16. adb_script="/usr/bin/adblock.sh"
  17. adb_pidfile="/var/run/adblock.pid"
  18. if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || \
  19. [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || \
  20. [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || \
  21. { [ "${action}" = "list" ] && [ -n "${1}" ]; }; }
  22. then
  23. exit 0
  24. fi
  25. version()
  26. {
  27. rc_procd "${adb_script}" version
  28. }
  29. boot()
  30. {
  31. [ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
  32. rc_procd start_service
  33. }
  34. start_service()
  35. {
  36. if [ "$("${adb_init}" enabled; printf "%u" ${?})" -eq 0 ]
  37. then
  38. if [ "${action}" = "boot" ]
  39. then
  40. if [ -n "$(uci_get adblock global adb_trigger)" ]
  41. then
  42. return 0
  43. fi
  44. fi
  45. procd_open_instance "adblock"
  46. procd_set_param command "${adb_script}" "${@}"
  47. procd_set_param pidfile "${adb_pidfile}"
  48. procd_set_param nice "$(uci_get adblock global adb_nice "0")"
  49. procd_set_param stdout 1
  50. procd_set_param stderr 1
  51. procd_close_instance
  52. fi
  53. }
  54. reload_service()
  55. {
  56. rc_procd start_service reload
  57. }
  58. stop_service()
  59. {
  60. rc_procd "${adb_script}" stop
  61. }
  62. restart()
  63. {
  64. rc_procd start_service restart
  65. }
  66. suspend()
  67. {
  68. rc_procd start_service suspend
  69. }
  70. resume()
  71. {
  72. rc_procd start_service resume
  73. }
  74. query()
  75. {
  76. rc_procd "${adb_script}" query "${1}"
  77. }
  78. report()
  79. {
  80. rc_procd "${adb_script}" report "${1:-"+"}" "${2:-"50"}" "${3:-"true"}" "${4:-"cli"}"
  81. }
  82. list()
  83. {
  84. local src_archive src_file src_enabled enabled name action="${1}"
  85. if [ "${action}" = "add" ] || [ "${action}" = "remove" ]
  86. then
  87. shift
  88. for name in "${@}"
  89. do
  90. if [ "${action}" = "add" ]
  91. then
  92. if [ -z "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
  93. then
  94. uci_add_list adblock global adb_sources "${name}"
  95. printf "%s\\n" "::: adblock source '${name}' added to config"
  96. fi
  97. else
  98. if [ -n "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
  99. then
  100. uci_remove_list adblock global adb_sources "${name}"
  101. printf "%s\\n" "::: adblock source '${name}' removed from config"
  102. fi
  103. fi
  104. done
  105. if [ -n "$(uci -q changes adblock)" ]
  106. then
  107. uci_commit adblock
  108. "${adb_init}" start
  109. fi
  110. else
  111. src_archive="$(uci_get adblock global adb_srcarc "/etc/adblock/adblock.sources.gz")"
  112. src_file="$(uci_get adblock global adb_srcfile "/tmp/adb_sources.json")"
  113. src_enabled="$(uci -q show adblock.global.adb_sources)"
  114. if [ -r "${src_archive}" ]
  115. then
  116. zcat "${src_archive}" > "${src_file}"
  117. else
  118. printf "%s\\n" "::: adblock source archive '${src_archive}' not found"
  119. fi
  120. if [ -r "${src_file}" ]
  121. then
  122. src_enabled="${src_enabled#*=}"
  123. src_enabled="${src_enabled//\'}"
  124. printf "%s\\n" "::: Available adblock sources"
  125. printf "%s\\n" ":::"
  126. printf "%-25s%-10s%-7s%-21s%s\\n" " Name" "Enabled" "Size" "Focus" "Info URL"
  127. printf "%s\\n" " -------------------------------------------------------------------"
  128. json_load_file "${src_file}"
  129. json_get_keys keylist
  130. for key in ${keylist}
  131. do
  132. json_select "${key}"
  133. json_get_var size "size"
  134. json_get_var focus "focus"
  135. json_get_var descurl "descurl"
  136. json_get_var url "url"
  137. json_get_var rule "rule"
  138. if [ -n "${url}" ] && [ -n "${rule}" ]
  139. then
  140. if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
  141. 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. if [ -n "${src_enabled// }" ]
  154. then
  155. printf "%s\\n" " ----------------------------------------------"
  156. printf "%s\\n" " Sources without valid configuration"
  157. printf "%s\\n" " ----------------------------------------------"
  158. for key in ${src_enabled}
  159. do
  160. printf " - %s\\n" "${key:0:20}"
  161. done
  162. fi
  163. else
  164. printf "%s\\n" "::: adblock source file '${src_file}' not found"
  165. fi
  166. fi
  167. }
  168. status_service()
  169. {
  170. local key keylist value rtfile
  171. rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
  172. if [ -s "${rtfile}" ]
  173. then
  174. printf "%s\\n" "::: adblock runtime information"
  175. json_load_file "${rtfile}"
  176. json_select data
  177. json_get_keys keylist
  178. for key in ${keylist}
  179. do
  180. json_get_var value "${key}"
  181. if [ "${key}" = "active_sources" ]
  182. then
  183. printf " + %-15s : " "${key}"
  184. json_select "${key}"
  185. index=1
  186. while json_get_type status "${index}" && [ "${status}" = "object" ]
  187. do
  188. json_get_values source "${index}"
  189. printf "%s " "${source}"
  190. index=$((index+1))
  191. done
  192. printf "\\n"
  193. json_select ".."
  194. else
  195. printf " + %-15s : %s\\n" "${key}" "${value}"
  196. fi
  197. done
  198. else
  199. printf "%s\\n" "::: no adblock runtime information available"
  200. fi
  201. }
  202. timer()
  203. {
  204. local action="${1}" hour="${2}" minute="${3:-0}" weekday="${4:-"*"}"
  205. hour="${hour//[[:alpha:]]/}"
  206. minute="${minute//[[:alpha:]]/}"
  207. if [ -n "${action}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
  208. [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
  209. [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
  210. then
  211. if [ -r "/etc/crontabs/root" ]
  212. then
  213. search="${adb_init//\//\\/}"
  214. search="${search//./\\.}"
  215. sed -i "/${search}/d" "/etc/crontabs/root"
  216. fi
  217. printf "%02d %02d %s\\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${action}" >> "/etc/crontabs/root"
  218. /etc/init.d/cron restart
  219. else
  220. printf "%s\\n" "::: the refresh timer could not been updated"
  221. fi
  222. }
  223. service_triggers()
  224. {
  225. local trigger delay type
  226. PROCD_RELOAD_DELAY=$((delay*1000))
  227. trigger="$(uci_get adblock global adb_trigger)"
  228. delay="$(uci_get adblock global adb_triggerdelay "2")"
  229. type="$(uci_get adblock global adb_starttype "start")"
  230. if [ -n "${trigger}" ]
  231. then
  232. procd_add_interface_trigger "interface.*.up" "${trigger}" "${adb_init}" "${type}"
  233. fi
  234. procd_add_reload_trigger "adblock"
  235. }