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.

74 lines
2.7 KiB

  1. #!/bin/sh
  2. # vpn handler called by travelmate
  3. # Copyright (c) 2020-2022 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. . "/lib/functions.sh"
  9. export LC_ALL=C
  10. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. set -o pipefail
  12. vpn="${1}"
  13. vpn_action="${2}"
  14. vpn_service="${3}"
  15. vpn_iface="${4}"
  16. trm_maxwait="$(uci_get travelmate global trm_maxwait "30")"
  17. trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")"
  18. trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")"
  19. trm_logger="$(command -v logger)"
  20. trm_fetch="$(command -v curl)"
  21. trm_vpnfile="/var/state/travelmate.vpn"
  22. f_net() {
  23. local json_rc result="net nok"
  24. 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}")"
  25. if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then
  26. result="net ok"
  27. fi
  28. printf "%s" "${result}"
  29. }
  30. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  31. if [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ "${vpn_status}" != "true" ]; then
  32. if [ "${vpn_service}" = "openvpn" ] && [ -x "/etc/init.d/openvpn" ]; then
  33. /etc/init.d/openvpn start
  34. fi
  35. ifup "${vpn_iface}"
  36. cnt=0
  37. while true; do
  38. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  39. if [ "${vpn_status}" = "true" ]; then
  40. net_status="$(f_net)"
  41. if [ "${net_status}" = "net ok" ]; then
  42. : >"${trm_vpnfile}"
  43. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection enabled" 2>/dev/null
  44. break
  45. fi
  46. fi
  47. if [ "${cnt}" -ge "$((trm_maxwait / 3))" ]; then
  48. ifdown "${vpn_iface}"
  49. if [ "${vpn_service}" = "openvpn" ] && [ -x "/etc/init.d/openvpn" ]; then
  50. /etc/init.d/openvpn stop
  51. fi
  52. rm -f "${trm_vpnfile}"
  53. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection can't be established" 2>/dev/null
  54. exit 1
  55. fi
  56. sleep 1
  57. cnt="$((cnt + 1))"
  58. done
  59. elif { [ "${vpn}" != "1" ] && [ "${vpn_action}" = "enable" ]; } || [ "${vpn_action}" = "disable" ]; then
  60. ifdown "${vpn_iface}"
  61. if [ "${vpn_service}" = "openvpn" ] && [ -x "/etc/init.d/openvpn" ]; then
  62. /etc/init.d/openvpn stop
  63. fi
  64. rm -f "${trm_vpnfile}"
  65. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection disabled" 2>/dev/null
  66. fi