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.

250 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. "${adb_init}" start
  103. fi
  104. else
  105. src_archive="$(uci_get adblock global adb_srcarc "/etc/adblock/adblock.sources.gz")"
  106. src_file="$(uci_get adblock global adb_srcfile "/tmp/adb_sources.json")"
  107. src_enabled="$(uci -q show adblock.global.adb_sources)"
  108. if [ -r "${src_archive}" ]
  109. then
  110. zcat "${src_archive}" > "${src_file}"
  111. else
  112. printf "%s\\n" "::: adblock source archive '${src_archive}' not found"
  113. fi
  114. if [ -r "${src_file}" ]
  115. then
  116. src_enabled="${src_enabled#*=}"
  117. src_enabled="${src_enabled//\'}"
  118. printf "%s\\n" "::: Available adblock sources"
  119. printf "%s\\n" ":::"
  120. printf "%-25s%-10s%-7s%-21s%s\\n" " Name" "Enabled" "Size" "Focus" "Info URL"
  121. printf "%s\\n" " -------------------------------------------------------------------"
  122. json_load_file "${src_file}"
  123. json_get_keys keylist
  124. for key in ${keylist}
  125. do
  126. json_select "${key}"
  127. json_get_var size "size"
  128. json_get_var focus "focus"
  129. json_get_var descurl "descurl"
  130. json_get_var url "url"
  131. json_get_var rule "rule"
  132. if [ -n "${url}" ] && [ -n "${rule}" ]
  133. then
  134. if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
  135. then
  136. enabled="x"
  137. else
  138. enabled=" "
  139. fi
  140. src_enabled="${src_enabled/${key}}"
  141. printf " + %-21s%-10s%-7s%-21s%s\\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
  142. else
  143. src_enabled="${src_enabled} ${key}"
  144. fi
  145. json_select ..
  146. done
  147. if [ -n "${src_enabled// }" ]
  148. then
  149. printf "%s\\n" " ----------------------------------------------"
  150. printf "%s\\n" " Sources without valid configuration"
  151. printf "%s\\n" " ----------------------------------------------"
  152. for key in ${src_enabled}
  153. do
  154. printf " - %s\\n" "${key:0:20}"
  155. done
  156. fi
  157. else
  158. printf "%s\\n" "::: adblock source file '${src_file}' not found"
  159. fi
  160. fi
  161. }
  162. status_service()
  163. {
  164. local key keylist value rtfile
  165. rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
  166. if [ -s "${rtfile}" ]
  167. then
  168. printf "%s\\n" "::: adblock runtime information"
  169. json_load_file "${rtfile}"
  170. json_select data
  171. json_get_keys keylist
  172. for key in ${keylist}
  173. do
  174. json_get_var value "${key}"
  175. if [ "${key}" = "active_sources" ]
  176. then
  177. printf " + %-15s : " "${key}"
  178. json_select "${key}"
  179. index=1
  180. while json_get_type status "${index}" && [ "${status}" = "object" ]
  181. do
  182. json_get_values source "${index}"
  183. printf "%s " "${source}"
  184. index=$((index+1))
  185. done
  186. printf "\\n"
  187. json_select ".."
  188. else
  189. printf " + %-15s : %s\\n" "${key}" "${value}"
  190. fi
  191. done
  192. else
  193. printf "%s\\n" "::: no adblock runtime information available"
  194. fi
  195. }
  196. timer()
  197. {
  198. local action="${1}" hour="${2}" minute="${3:-0}" weekday="${4:-"*"}"
  199. hour="${hour//[[:alpha:]]/}"
  200. minute="${minute//[[:alpha:]]/}"
  201. if [ -n "${action}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
  202. [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
  203. [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
  204. then
  205. if [ -r "/etc/crontabs/root" ]
  206. then
  207. search="${adb_init//\//\\/}"
  208. search="${search//./\\.}"
  209. sed -i "/${search}/d" "/etc/crontabs/root"
  210. fi
  211. printf "%02d %02d %s\\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${action}" >> "/etc/crontabs/root"
  212. /etc/init.d/cron restart
  213. else
  214. printf "%s\\n" "::: the refresh timer could not been updated"
  215. fi
  216. }
  217. service_triggers()
  218. {
  219. local trigger delay type
  220. PROCD_RELOAD_DELAY=$((delay*1000))
  221. trigger="$(uci_get adblock global adb_trigger)"
  222. delay="$(uci_get adblock global adb_triggerdelay "2")"
  223. type="$(uci_get adblock global adb_starttype "start")"
  224. if [ -n "${trigger}" ]
  225. then
  226. procd_add_interface_trigger "interface.*.up" "${trigger}" "${adb_init}" "${type}"
  227. fi
  228. procd_add_reload_trigger "adblock"
  229. }