openvpn_route-up_90-hotspot 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. is_nat_set() {
  3. local gateway_interface=${1}
  4. iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
  5. }
  6. unset_nat() {
  7. local gateway_interface=${1}
  8. echo "[INFO] hotspot ${wifi_device}: Unset NAT on ${gateway_interface}"
  9. iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
  10. }
  11. set_nat() {
  12. local gateway_interface=${1}
  13. echo "[INFO] hotspot ${wifi_device}: Set NAT on ${gateway_interface}"
  14. iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
  15. }
  16. has_ip6delegatedprefix() {
  17. [[ -n "${ip6_net}" ]] && [[ "${ip6_net}" != "none" ]]
  18. }
  19. is_ip6addr_set() {
  20. ip address show dev "${wifi_device}" 2>/dev/null | grep -q "${ip6_addr}/64"
  21. }
  22. set_ip6addr() {
  23. echo "[INFO] hotspot ${wifi_device}: Set IPv6 address ${ip6_addr}"
  24. ip address delete "${ip6_addr}/64" dev "${new_gateway_interface}" &>/dev/null
  25. ip address add "${ip6_addr}/64" dev "${wifi_device}"
  26. }
  27. ynh_hotspot_state=$(systemctl is-active __SERVICE_NAME__)
  28. if [[ "${ynh_hotspot_state}" == "active" || "${ynh_hotspot_state}" == "activating" ]]; then
  29. old_gateway_interface=$(ip route | awk '/default via/ { print $5; }')
  30. new_gateway_interface=${dev}
  31. ip6_net=$(yunohost app setting __APP__ ip6_net)
  32. ip6_addr="${ip6_net}1"
  33. wifi_device=$(yunohost app setting __APP__ wifi_device)
  34. if [[ -n "$old_gateway_interface" ]] && is_nat_set "$old_gateway_interface"; then
  35. unset_nat "${old_gateway_interface}"
  36. fi
  37. if ! is_nat_set $new_gateway_interface; then
  38. set_nat "${new_gateway_interface}"
  39. fi
  40. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  41. set_ip6addr
  42. fi
  43. yunohost app setting __APP__ gateway_interface --value "${new_gateway_interface}"
  44. fi