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.

89 lines
3.3 KiB

  1. #!/bin/sh
  2. # vpn handler called by travelmate
  3. # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # set (s)hellcheck exceptions
  6. # shellcheck disable=1091,3040,3043
  7. # Please note: you have to setup the package 'wireguard' or 'openvpn' before using this script
  8. export LC_ALL=C
  9. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. set -o pipefail
  11. # source function library if necessary
  12. #
  13. if [ -z "${_C}" ]; then
  14. . "/lib/functions.sh"
  15. fi
  16. vpn="${1}"
  17. vpn_action="${2}"
  18. vpn_service="${3}"
  19. vpn_iface="${4}"
  20. trm_maxwait="$(uci_get travelmate global trm_maxwait "30")"
  21. trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")"
  22. trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")"
  23. trm_logger="$(command -v logger)"
  24. trm_fetch="$(command -v curl)"
  25. f_net() {
  26. local json_rc result="net nok"
  27. json_rc="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{response_code}" --silent --output /dev/null --max-time $((trm_maxwait / 6)) "${trm_captiveurl}")"
  28. if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then
  29. result="net ok"
  30. fi
  31. printf "%s" "${result}"
  32. }
  33. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  34. case "${vpn_service}" in
  35. "wireguard")
  36. if [ "${vpn_action}" = "enable" ] && [ "${vpn_status:-"false"}" != "true" ]; then
  37. ubus call network.interface."${vpn_iface}" up
  38. fi
  39. if { [ "${vpn}" = "0" ] && [ "${vpn_action}" = "enable" ]; } || { [ "${vpn_action}" = "disable" ] && [ "${vpn_status}" = "true" ]; }; then
  40. ubus call network.interface."${vpn_iface}" down
  41. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection disabled" 2>/dev/null
  42. fi
  43. ;;
  44. "openvpn")
  45. if [ "${vpn_action}" = "enable" ] && [ "${vpn_status:-"false"}" != "true" ]; then
  46. ubus call network.interface."${vpn_iface}" up
  47. /etc/init.d/openvpn restart >/dev/null 2>&1
  48. fi
  49. if { [ "${vpn}" = "0" ] && [ "${vpn_action}" = "enable" ]; } || { [ "${vpn_action}" = "disable" ] && [ "${vpn_status}" = "true" ]; }; then
  50. ubus call network.interface."${vpn_iface}" down
  51. /etc/init.d/openvpn stop >/dev/null 2>&1
  52. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection disabled" 2>/dev/null
  53. fi
  54. ;;
  55. esac
  56. if [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ "${vpn_status:-"false"}" != "true" ]; then
  57. cnt=0
  58. while true; do
  59. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  60. if [ "${vpn_status}" = "true" ]; then
  61. net_status="$(f_net)"
  62. if [ "${net_status}" = "net ok" ]; then
  63. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection enabled" 2>/dev/null
  64. break
  65. fi
  66. fi
  67. if [ "${cnt}" -ge "$((trm_maxwait / 6))" ]; then
  68. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection can't be established" 2>/dev/null
  69. ubus call network.interface."${vpn_iface}" down
  70. exit 1
  71. fi
  72. sleep 1
  73. cnt="$((cnt + 1))"
  74. done
  75. fi
  76. if [ "${vpn_action}" = "enable" ] && [ "${vpn_status}" = "true" ]; then
  77. if [ -f "/etc/init.d/sysntpd" ]; then
  78. /etc/init.d/sysntpd restart >/dev/null 2>&1
  79. fi
  80. fi