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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # In case an ip has been provided in ovpn conf
  39. new_server_ip6=""
  40. new_server_ip4=""
  41. for i in $ynh_server_names; do
  42. if [[ "${i}" =~ : ]]; then
  43. new_server_ip6+=" ${i}"
  44. elif [[ "${i}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  45. new_server_ip4+=" ${i}"
  46. fi
  47. done
  48. if [[ -z "${new_server_ip6}" && -z "${new_server_ip4}" ]]; then
  49. new_server_ip6=$(dig AAAA +short $ynh_server_names @127.0.0.1 | grep -v '\.$' | grep -v "timed out" | sort | uniq)
  50. fi
  51. echo "[INFO] Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
  52. echo "[INFO] Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
  53. # Check old state of the server ipv6 route
  54. if [[ -n "${old_server_ip6}" && -n "${old_ip6_gw}" && -n "${old_wired_device}" ]]; then
  55. if [[ "${new_server_ip6}" != "${old_server_ip6}" || "${new_ip6_gw}" != "${old_ip6_gw}" || "${new_wired_device}" != "${old_wired_device}" ]]; then
  56. if is_serverip6route_set "${old_server_ip6}"; then
  57. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  58. fi
  59. fi
  60. fi
  61. # Set the new server ipv6 route
  62. if has_nativeip6; then
  63. if ! is_serverip6route_set "${new_server_ip6}"; then
  64. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  65. fi
  66. echo "[INFO] Native IPv6 detected"
  67. echo "[INFO] Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
  68. if is_serverip6route_set "${new_server_ip6}"; then
  69. echo "[ OK ] IPv6 server route correctly set"
  70. else
  71. echo "[FAIL] No IPv6 server route set" >&2
  72. exit 1
  73. fi
  74. else
  75. echo "[INFO] No native IPv6 detected"
  76. echo "[INFO] No IPv6 server route to set"
  77. fi
  78. yunohost app setting vpnclient server_ip6 --value "${new_server_ip6}"
  79. yunohost app setting vpnclient ip6_gw --value "${new_ip6_gw}"
  80. yunohost app setting vpnclient wired_device --value "${new_wired_device}"