30-vpnclient-set-server-ipv6-route 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. has_nativeip6() {
  3. ip -6 route | grep -q "default via"
  4. }
  5. is_serverip6route_set() {
  6. local server_ip6s=${1}
  7. if [[ -z "${server_ip6s}" ]]; then
  8. return 0
  9. fi
  10. for server_ip6 in ${server_ip6s}; do
  11. if ! ip -6 route | grep -q "^${server_ip6}"; then
  12. return 1
  13. fi
  14. done
  15. }
  16. set_serverip6route() {
  17. local server_ip6s=${1}
  18. local ip6_gw=${2}
  19. local wired_device=${3}
  20. for server_ip6 in ${server_ip6s}; do
  21. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  22. done
  23. }
  24. unset_serverip6route() {
  25. local server_ip6s=${1}
  26. local ip6_gw=${2}
  27. local wired_device=${3}
  28. for server_ip6 in ${server_ip6s}; do
  29. ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  30. done
  31. }
  32. old_ip6_gw=$(yunohost app setting vpnclient ip6_gw)
  33. old_wired_device=$(yunohost app setting vpnclient wired_device)
  34. old_server_ip6=$(yunohost app setting vpnclient server_ip6)
  35. new_ip6_gw=$(ip -6 route | awk '/default via/ { print $3 }')
  36. new_wired_device=$(ip route | awk '/default via/ { print $5; }')
  37. ynh_server_names=$(grep -o -P '^\s*remote\s+\K([^\s]+)' /etc/openvpn/client.conf | sort | uniq)
  38. new_server_ip6=$(dig AAAA +short $ynh_server_names @127.0.0.1 | grep -v '\.$' | grep -v "timed out" | sort | uniq)
  39. for i in $ynh_server_names; do
  40. if [[ "${i}" =~ : ]] && [[ ! "$new_server_ip6" == *"${i}"* ]] ; then
  41. new_server_ip6+=" ${i}"
  42. fi
  43. done
  44. echo "[INFO] Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
  45. echo "[INFO] Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
  46. # Check old state of the server ipv6 route
  47. if [[ -n "${old_server_ip6}" && -n "${old_ip6_gw}" && -n "${old_wired_device}" ]]; then
  48. if [[ "${new_server_ip6}" != "${old_server_ip6}" || "${new_ip6_gw}" != "${old_ip6_gw}" || "${new_wired_device}" != "${old_wired_device}" ]]; then
  49. if is_serverip6route_set "${old_server_ip6}"; then
  50. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  51. fi
  52. fi
  53. fi
  54. # Set the new server ipv6 route
  55. if has_nativeip6; then
  56. if ! is_serverip6route_set "${new_server_ip6}"; then
  57. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  58. fi
  59. echo "[INFO] Native IPv6 detected"
  60. echo "[INFO] Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
  61. if is_serverip6route_set "${new_server_ip6}"; then
  62. echo "[ OK ] IPv6 server route correctly set"
  63. else
  64. echo "[FAIL] No IPv6 server route set" >&2
  65. exit 1
  66. fi
  67. else
  68. echo "[INFO] No native IPv6 detected"
  69. echo "[INFO] No IPv6 server route to set"
  70. fi
  71. yunohost app setting vpnclient server_ip6 --value "${new_server_ip6}"
  72. yunohost app setting vpnclient ip6_gw --value "${new_ip6_gw}"
  73. yunohost app setting vpnclient wired_device --value "${new_wired_device}"