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.

329 lines
9.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2019-2022 Stan Grishin (stangri@melmac.ca)
  3. # shellcheck disable=SC1091,SC2039,SC3043,SC3060
  4. PKG_VERSION='dev-test'
  5. # shellcheck disable=SC2034
  6. START=80
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. if type extra_command 1>/dev/null 2>&1; then
  10. extra_command 'version' 'Show version information'
  11. else
  12. # shellcheck disable=SC2034
  13. EXTRA_COMMANDS='version'
  14. fi
  15. readonly packageName='https-dns-proxy'
  16. readonly serviceName="$packageName $PKG_VERSION"
  17. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  18. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  19. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  20. readonly PROG=/usr/sbin/https-dns-proxy
  21. readonly DEFAULT_BOOTSTRAP='1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001,8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844'
  22. dnsmasqConfig=''; forceDNS=''; forceDNSPorts='';
  23. str_contains() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
  24. is_mac_address() { expr "$1" : '[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]$' >/dev/null; }
  25. is_ipv4() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; }
  26. is_ipv6() { ! is_mac_address "$1" && str_contains "$1" ":"; }
  27. output() {
  28. local msg memmsg logmsg
  29. [ -t 1 ] && printf "%b" "$@"
  30. msg="${1//$serviceName /service }";
  31. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  32. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  33. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  34. logger -t "$packageName" "$(printf "%b" "$logmsg")"
  35. rm -f "$sharedMemoryOutput"
  36. else
  37. printf "%b" "$msg" >> "$sharedMemoryOutput"
  38. fi
  39. }
  40. output_ok() { output "$_OK_"; }
  41. output_okn() { output "${_OK_}\\n"; }
  42. output_fail() { output "$_FAIL_"; }
  43. output_failn() { output "${_FAIL_}\\n"; }
  44. uci_add_list_if_new() {
  45. local key="$1" value="$2" i
  46. if [ -z "$value" ]; then
  47. value="${key#*=}"
  48. key="${key%=*}"
  49. fi
  50. [ -n "$key" ] && [ -n "$value" ] || return 1
  51. for i in $(uci -q get "$key"); do
  52. [ "$i" = "$value" ] && return 0
  53. done
  54. uci -q add_list "${key}=${value}"
  55. }
  56. dnsmasq_restart() { [ -x /etc/init.d/dnsmasq ] || return 0; /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  57. version() { echo "$PKG_VERSION"; }
  58. xappend() { param="$param $1"; }
  59. append_bool() {
  60. local section="$1"
  61. local option="$2"
  62. local value="$3"
  63. local default="${4:-0}"
  64. local _loctmp
  65. config_get_bool _loctmp "$section" "$option" "$default"
  66. [ "$_loctmp" -ne 0 ] && xappend "$value"
  67. }
  68. append_parm() {
  69. local section="$1"
  70. local option="$2"
  71. local switch="$3"
  72. local default="$4"
  73. local _loctmp
  74. config_get _loctmp "$section" "$option" "$default"
  75. [ -n "$_loctmp" ] && xappend "$switch $_loctmp"
  76. }
  77. append_counter() {
  78. local section="$1"
  79. local option="$2"
  80. local switch="$3"
  81. local default="${4:-0}"
  82. local _loctmp i
  83. config_get _loctmp "$section" "$option" "$default"
  84. # shellcheck disable=SC2086,SC2154
  85. for i in $(seq 1 $_loctmp); do
  86. xappend '-v'
  87. done
  88. }
  89. append_bootstrap() {
  90. local section="$1"
  91. local option="$2"
  92. local switch="$3"
  93. local default="$4"
  94. local _old_ifs="$IFS"
  95. local _loctmp _newtmp i
  96. config_get _loctmp "$section" "$option" "$default"
  97. [ -z "$_loctmp" ] && return 0
  98. IFS=" ,"
  99. for i in $_loctmp; do
  100. if { [ "$ipv6_resolvers_only" -eq 0 ] && is_ipv4 "$i"; } || \
  101. { [ "$ipv6_resolvers_only" -ne 0 ] && is_ipv6 "$i"; }; then
  102. [ -z "$_newtmp" ] && _newtmp="$i" || _newtmp="${_newtmp},${i}"
  103. fi
  104. done
  105. IFS="$_old_ifs"
  106. [ -n "$_newtmp" ] && xappend "$switch $_newtmp"
  107. [ "$ipv6_resolvers_only" -eq 0 ] && xappend '-4'
  108. }
  109. start_instance() {
  110. local cfg="$1" param listen_addr listen_port ipv6_resolvers_only p
  111. config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
  112. append_parm "$cfg" 'resolver_url' '-r'
  113. append_parm "$cfg" 'polling_interval' '-i'
  114. append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
  115. append_parm "$cfg" 'listen_port' '-p' "$port"
  116. append_parm "$cfg" 'dscp_codepoint' '-c'
  117. append_bootstrap "$cfg" 'bootstrap_dns' '-b' "$DEFAULT_BOOTSTRAP"
  118. append_parm "$cfg" 'user' '-u' 'nobody'
  119. append_parm "$cfg" 'group' '-g' 'nogroup'
  120. append_parm "$cfg" 'proxy_server' '-t'
  121. append_parm "$cfg" 'logfile' '-l'
  122. append_bool "$cfg" 'use_http1' '-x'
  123. append_counter "$cfg" 'verbosity' '-v' '0'
  124. procd_open_instance
  125. # shellcheck disable=SC2086
  126. procd_set_param command $PROG $param
  127. procd_set_param stderr 1
  128. procd_set_param stdout 1
  129. procd_set_param respawn
  130. if [ "$forceDNS" -ne 0 ]; then
  131. procd_open_data
  132. json_add_array firewall
  133. for p in $forceDNSPorts; do
  134. if netstat -tuln | grep 'LISTEN' | grep ":${p}" >/dev/null 2>&1 || [ "$p" = '53' ]; then
  135. json_add_object ''
  136. json_add_string type redirect
  137. json_add_string target DNAT
  138. json_add_string src lan
  139. json_add_string proto 'tcp udp'
  140. json_add_string src_dport "$p"
  141. json_add_string dest_port "$p"
  142. json_add_boolean reflection 0
  143. json_close_object
  144. else
  145. json_add_object ''
  146. json_add_string type rule
  147. json_add_string src lan
  148. json_add_string dest '*'
  149. json_add_string proto 'tcp udp'
  150. json_add_string dest_port "$p"
  151. json_add_string target REJECT
  152. json_close_object
  153. fi
  154. done
  155. json_close_array
  156. procd_close_data
  157. fi
  158. procd_close_instance
  159. if [ "$?" ]; then
  160. forceDNS=0
  161. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  162. config_get listen_port "$cfg" 'listen_port' "$port"
  163. if [ "$dnsmasqConfig" = '*' ]; then
  164. config_load 'dhcp'
  165. config_foreach dnsmasq_doh_server 'dnsmasq' 'add' "${listen_addr}" "${listen_port}"
  166. elif [ -n "$dnsmasqConfig" ]; then
  167. for i in $dnsmasqConfig; do
  168. if [ -n "$(uci -q get "dhcp.@dnsmasq[$i]")" ]; then
  169. dnsmasq_doh_server "@dnsmasq[$i]" 'add' "${listen_addr}" "${listen_port}"
  170. elif [ -n "$(uci -q get "dhcp.${i}")" ]; then
  171. dnsmasq_doh_server "${i}" 'add' "${listen_addr}" "${listen_port}"
  172. fi
  173. done
  174. fi
  175. output_ok
  176. port="$((port+1))"
  177. else
  178. output_fail
  179. fi
  180. }
  181. start_service() {
  182. local port=5053
  183. output "Starting $serviceName "
  184. config_load "$packageName"
  185. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  186. config_get_bool forceDNS 'config' 'force_dns' '1'
  187. config_get forceDNSPorts 'config' 'force_dns_port' '53 853'
  188. dhcp_backup 'create'
  189. config_load "$packageName"
  190. config_foreach start_instance "$packageName"
  191. if [ -n "$(uci -q changes dhcp)" ]; then
  192. uci -q commit dhcp
  193. dnsmasq_restart
  194. fi
  195. output "\\n"
  196. }
  197. stop_service() {
  198. local s=0
  199. output "Stopping $serviceName "
  200. config_load "$packageName"
  201. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  202. dhcp_backup 'restore'
  203. if [ -n "$(uci -q changes dhcp)" ]; then
  204. uci -q commit dhcp
  205. dnsmasq_restart || s=1
  206. fi
  207. # shellcheck disable=SC2015
  208. [ "$s" -eq 0 ] && output_okn || output_failn
  209. }
  210. # shellcheck disable=SC1091
  211. service_triggers() {
  212. local iface
  213. . /lib/functions/network.sh
  214. network_flush_cache
  215. network_find_wan iface
  216. iface="${iface:-wan}"
  217. if [ -n "$iface" ]; then
  218. procd_add_interface_trigger "interface.*" "$iface" "/etc/init.d/${packageName}" restart
  219. fi
  220. procd_add_config_trigger "config.change" "$packageName" "/etc/init.d/${packageName}" restart
  221. }
  222. service_started() { procd_set_config_changed firewall; }
  223. service_stopped() { procd_set_config_changed firewall; }
  224. dnsmasq_doh_server() {
  225. local cfg="$1" param="$2" address="${3:-127.0.0.1}" port="$4" i
  226. case "$param" in
  227. add)
  228. case $address in
  229. 0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
  230. ::) address='::1';;
  231. esac
  232. uci_add_list_if_new "dhcp.${cfg}.server" "${address}#${port}"
  233. ;;
  234. remove)
  235. eval "$(ubus call service list "{ 'verbose': true, 'name': '$packageName' }" | jsonfilter -F '# ' -e 'TUPLES=@[*].instances[*].command[4,6]')"
  236. for i in $TUPLES; do
  237. uci -q del_list "dhcp.${cfg}.server=${i}"
  238. done
  239. ;;
  240. esac
  241. }
  242. dnsmasq_create_server_backup() {
  243. local cfg="$1" i
  244. uci -q get "dhcp.${cfg}" >/dev/null || return 1
  245. if ! uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  246. if [ -z "$(uci -q get "dhcp.${cfg}.noresolv")" ]; then
  247. uci -q set "dhcp.${cfg}.noresolv=1"
  248. uci -q set "dhcp.${cfg}.doh_backup_noresolv=-1"
  249. elif [ "$(uci -q get "dhcp.${cfg}.noresolv")" != "1" ]; then
  250. uci -q set "dhcp.${cfg}.noresolv=1"
  251. uci -q set "dhcp.${cfg}.doh_backup_noresolv=0"
  252. fi
  253. fi
  254. if ! uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  255. if [ -z "$(uci -q get "dhcp.${cfg}.server")" ]; then
  256. uci -q add_list "dhcp.${cfg}.doh_backup_server="
  257. fi
  258. for i in $(uci -q get "dhcp.${cfg}.server"); do
  259. uci -q add_list "dhcp.${cfg}.doh_backup_server=$i"
  260. if [ "$i" = "$(echo "$i" | tr -d /\#)" ]; then
  261. uci -q del_list "dhcp.${cfg}.server=$i"
  262. fi
  263. done
  264. fi
  265. return 0
  266. }
  267. dnsmasq_restore_server_backup() {
  268. local cfg="$1" i
  269. uci -q get "dhcp.${cfg}" >/dev/null || return 0
  270. if uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  271. if [ "$(uci -q get "dhcp.${cfg}.doh_backup_noresolv")" = "0" ]; then
  272. uci -q set "dhcp.${cfg}.noresolv=0"
  273. else
  274. uci -q del "dhcp.${cfg}.noresolv"
  275. fi
  276. uci -q del "dhcp.${cfg}.doh_backup_noresolv"
  277. fi
  278. if uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  279. dnsmasq_doh_server "$cfg" 'remove'
  280. for i in $(uci -q get "dhcp.${cfg}.doh_backup_server"); do
  281. uci_add_list_if_new "dhcp.${cfg}.server" "$i"
  282. done
  283. uci -q del "dhcp.${cfg}.doh_backup_server"
  284. fi
  285. }
  286. dhcp_backup() {
  287. local i
  288. config_load 'dhcp'
  289. case "$1" in
  290. create)
  291. if [ "$dnsmasqConfig" = "*" ]; then
  292. config_foreach dnsmasq_create_server_backup 'dnsmasq'
  293. elif [ -n "$dnsmasqConfig" ]; then
  294. for i in $dnsmasqConfig; do
  295. if [ -n "$(uci -q get "dhcp.@dnsmasq[$i]")" ]; then
  296. dnsmasq_create_server_backup "@dnsmasq[$i]"
  297. elif [ -n "$(uci -q get "dhcp.${i}")" ]; then
  298. dnsmasq_create_server_backup "$i"
  299. fi
  300. done
  301. fi
  302. ;;
  303. restore)
  304. config_foreach dnsmasq_restore_server_backup 'dnsmasq'
  305. ;;
  306. esac
  307. }