luci.https-dns-proxy 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/sh
  2. # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
  3. # shellcheck disable=SC1091,SC2039,SC3043
  4. # TechRef: https://openwrt.org/docs/techref/rpcd
  5. # ubus -v list luci.https-dns-proxy
  6. # ubus -S call luci.https-dns-proxy getInitList '{"name": "https-dns-proxy" }'
  7. # ubus -S call luci.https-dns-proxy getInitStatus '{"name": "https-dns-proxy" }'
  8. # ubus -S call luci.https-dns-proxy getPlatformSupport '{"name": "https-dns-proxy" }'
  9. # ubus -S call luci.https-dns-proxy getProviders '{"name": "https-dns-proxy" }'
  10. # ubus -S call luci.https-dns-proxy getRuntime '{"name": "https-dns-proxy" }'
  11. readonly packageName="https-dns-proxy"
  12. readonly providersDir="/usr/share/${packageName}/providers"
  13. . /lib/functions.sh
  14. . /usr/share/libubox/jshn.sh
  15. is_enabled() { "/etc/init.d/${1}" enabled; }
  16. is_running() { [ "$(ubus call service list "{ 'name': '$1' }" | jsonfilter -q -e "@['$1'].instances[*].running" | uniq)" = 'true' ]; }
  17. get_version() { /usr/sbin/https-dns-proxy -V; }
  18. check_http2() { curl --version | grep -q 'nghttp2'; }
  19. check_http3() { curl --version | grep -q 'nghttp3'; }
  20. ubus_get_ports() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances[*].data.firewall.*.dest_port"; }
  21. logger() { /usr/bin/logger -t "$packageName" "$@"; }
  22. print_json_bool() { json_init; json_add_boolean "$1" "$2"; json_dump; json_cleanup; }
  23. get_init_list() {
  24. local name="$1"
  25. json_init
  26. json_add_object "$name"
  27. if is_enabled "$name"; then
  28. json_add_boolean 'enabled' '1'
  29. else
  30. json_add_boolean 'enabled' '0'
  31. fi
  32. if is_running "$name"; then
  33. json_add_boolean 'running' '1'
  34. else
  35. json_add_boolean 'running' '0'
  36. fi
  37. json_close_object
  38. json_dump
  39. json_cleanup
  40. }
  41. get_init_status() {
  42. local name
  43. local i ports
  44. local version
  45. name="$(basename "$1")"
  46. name="${name:-$packageName}"
  47. ports="$(ubus_get_ports)"
  48. [ -z "$version" ] && version="$(get_version "$name")"
  49. json_init
  50. json_add_object "$name"
  51. if is_enabled "$name"; then
  52. json_add_boolean 'enabled' '1'
  53. else
  54. json_add_boolean 'enabled' '0'
  55. fi
  56. if is_running "$name"; then
  57. json_add_boolean 'running' '1'
  58. else
  59. json_add_boolean 'running' '0'
  60. fi
  61. if [ -n "$ports" ]; then
  62. json_add_boolean 'force_dns_active' '1'
  63. json_add_array 'force_dns_ports'
  64. for i in $ports; do json_add_int '' "$i"; done
  65. json_close_array
  66. else
  67. json_add_boolean 'force_dns_active' '0'
  68. fi
  69. json_add_string 'version' "$version"
  70. json_close_array
  71. json_close_object
  72. json_dump
  73. json_cleanup
  74. }
  75. get_platform_support() {
  76. local name
  77. name="$(basename "$1")"
  78. name="${name:-$packageName}"
  79. json_init
  80. json_add_object "$name"
  81. if check_http2; then
  82. json_add_boolean 'http2_support' '1'
  83. else
  84. json_add_boolean 'http2_support' '0'
  85. fi
  86. if check_http3; then
  87. json_add_boolean 'http3_support' '1'
  88. else
  89. json_add_boolean 'http3_support' '0'
  90. fi
  91. json_close_object
  92. json_dump
  93. json_cleanup
  94. }
  95. get_providers() {
  96. local f
  97. echo '{"https-dns-proxy":['
  98. for f in "$providersDir"/*; do
  99. cat "$f"
  100. echo ','
  101. done
  102. # echo '{ "title": "Custom", "template": "{option}", "params": { "option": { "type": "text", }, }, },'
  103. echo ']}'
  104. }
  105. get_runtime() { ubus call service list "{ 'verbose': true, 'name': '$1' }"; }
  106. set_init_action() {
  107. local name="$1" action="$2" cmd
  108. case $action in
  109. enable|disable|start|stop|restart)
  110. cmd="/etc/init.d/${name} ${action}"
  111. ;;
  112. esac
  113. if [ -n "$cmd" ] && eval "$cmd" >/dev/null 2>&1; then
  114. print_json_bool "result" '1'
  115. else
  116. print_json_bool "result" '0'
  117. fi
  118. }
  119. case "$1" in
  120. list)
  121. json_init
  122. json_add_object "getInitList"
  123. json_add_string 'name' "name"
  124. json_close_object
  125. json_add_object "getInitStatus"
  126. json_add_string 'name' 'name'
  127. json_close_object
  128. json_add_object "getPlatformSupport"
  129. json_add_string 'name' 'name'
  130. json_close_object
  131. json_add_object "getProviders"
  132. json_add_string 'name' "name"
  133. json_close_object
  134. json_add_object "getRuntime"
  135. json_add_string 'name' "name"
  136. json_close_object
  137. json_add_object "setInitAction"
  138. json_add_string 'name' "name"
  139. json_add_string 'action' "action"
  140. json_close_object
  141. json_dump
  142. json_cleanup
  143. ;;
  144. call)
  145. case "$2" in
  146. getInitList)
  147. read -r input
  148. json_load "$input"
  149. json_get_var name "name"
  150. json_cleanup
  151. get_init_list "$name"
  152. ;;
  153. getInitStatus)
  154. read -r input
  155. json_load "$input"
  156. json_get_var name 'name'
  157. json_cleanup
  158. get_init_status "$name"
  159. ;;
  160. getPlatformSupport)
  161. read -r input
  162. json_load "$input"
  163. json_get_var name 'name'
  164. json_cleanup
  165. get_platform_support "$name"
  166. ;;
  167. getProviders)
  168. read -r input
  169. json_load "$input"
  170. json_get_var name "name"
  171. json_cleanup
  172. get_providers "$name"
  173. ;;
  174. getRuntime)
  175. read -r input
  176. json_load "$input"
  177. json_get_var name "name"
  178. json_cleanup
  179. get_runtime "$name"
  180. ;;
  181. setInitAction)
  182. read -r input
  183. json_load "$input"
  184. json_get_var name "name"
  185. json_get_var action "action"
  186. json_cleanup
  187. set_init_action "$name" "$action"
  188. ;;
  189. esac
  190. ;;
  191. esac