openvpn_90-hotspot 1000 B

12345678910111213141516171819202122232425262728293031
  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. iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
  9. }
  10. set_nat() {
  11. local gateway_interface=${1}
  12. iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
  13. }
  14. if systemctl -q is-active __SERVICE_NAME__; then
  15. old_gateway_interface=$(yunohost app setting __APP__ gateway_interface)
  16. new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
  17. if [[ -n "$old_gateway_interface" ]] && [[ "$old_gateway_interface" != "$new_gateway_interface" ]] && is_nat_set "$old_gateway_interface"; then
  18. unset_nat "${old_gateway_interface}"
  19. fi
  20. if [[ -n "$new_gateway_interface" ]] && ! is_nat_set $new_gateway_interface; then
  21. set_nat "${new_gateway_interface}"
  22. fi
  23. yunohost app setting __APP__ gateway_interface --value "${new_gateway_interface}"
  24. fi