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.

78 lines
3.3 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. vpn="${1}"
  12. vpn_action="${2}"
  13. vpn_service="${3}"
  14. vpn_iface="${4}"
  15. vpn_instance="${5}"
  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. if [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ]; then
  31. if [ "${vpn_action}" = "enable_keep" ]; then
  32. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  33. fi
  34. if [ "${vpn_action}" = "enable" ] || [ "${vpn_status}" != "true" ]; then
  35. if [ "${vpn_service}" = "openvpn" ] && [ -n "${vpn_instance}" ] && [ -x "/etc/init.d/openvpn" ] && ! /etc/init.d/openvpn running "${vpn_instance}"; then
  36. /etc/init.d/openvpn start "${vpn_instance}"
  37. fi
  38. ifup "${vpn_iface}"
  39. cnt=0
  40. while true; do
  41. vpn_status="$(ubus -S call network.interface."${vpn_iface}" status 2>/dev/null | jsonfilter -q -l1 -e '@.up')"
  42. if [ "${vpn_status}" = "true" ]; then
  43. net_status="$(f_net)"
  44. if [ "${net_status}" = "net ok" ]; then
  45. : >"${trm_vpnfile}"
  46. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection enabled '${vpn_iface}/${vpn_instance:-"-"}'" 2>/dev/null
  47. break
  48. fi
  49. fi
  50. if [ "${cnt}" -ge "$((trm_maxwait / 3))" ]; then
  51. ifdown "${vpn_iface}"
  52. if [ "${vpn_service}" = "openvpn" ] && [ -n "${vpn_instance}" ] && [ -x "/etc/init.d/openvpn" ] && /etc/init.d/openvpn running "${vpn_instance}"; then
  53. /etc/init.d/openvpn stop "${vpn_instance}"
  54. fi
  55. rm -f "${trm_vpnfile}"
  56. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection can't be established '${vpn_iface}/${vpn_instance:-"-"}'" 2>/dev/null
  57. return 1
  58. fi
  59. sleep 1
  60. cnt="$((cnt + 1))"
  61. done
  62. fi
  63. elif { [ "${vpn}" != "1" ] && [ "${vpn_action%_*}" = "enable" ]; } || [ "${vpn_action}" = "disable" ]; then
  64. ifdown "${vpn_iface}"
  65. if [ "${vpn_service}" = "openvpn" ] && [ -n "${vpn_instance}" ] && [ -x "/etc/init.d/openvpn" ] && /etc/init.d/openvpn running "${vpn_instance}"; then
  66. /etc/init.d/openvpn stop "${vpn_instance}"
  67. fi
  68. rm -f "${trm_vpnfile}"
  69. "${trm_logger}" -p "info" -t "trm-vpn [${$}]" "${vpn_service} client connection disabled '${vpn_iface}/${vpn_instance:-"-"}'" 2>/dev/null
  70. fi