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.

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