2023-08-16 20:39:02 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-11-18 19:18:15 +01:00
|
|
|
is_nat_set() {
|
|
|
|
local gateway_interface=${1}
|
|
|
|
iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
|
|
|
|
}
|
|
|
|
|
|
|
|
unset_nat() {
|
|
|
|
local gateway_interface=${1}
|
|
|
|
iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
|
|
|
|
}
|
|
|
|
|
|
|
|
set_nat() {
|
|
|
|
local gateway_interface=${1}
|
|
|
|
iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
|
|
|
|
}
|
|
|
|
|
2023-12-19 10:42:31 +01:00
|
|
|
ynh_hotspot_state=$(systemctl is-active __SERVICE_NAME__)
|
2023-12-18 14:33:53 +01:00
|
|
|
if [[ "${ynh_hotspot_state}" == "active" || "${ynh_hotspot_state}" == "activating" ]]; then
|
2023-11-20 11:54:47 +01:00
|
|
|
old_gateway_interface=$(yunohost app setting __APP__ gateway_interface)
|
2023-11-18 21:42:48 +01:00
|
|
|
new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
|
2023-11-18 19:18:15 +01:00
|
|
|
|
2023-11-18 21:42:48 +01:00
|
|
|
if [[ -n "$old_gateway_interface" ]] && [[ "$old_gateway_interface" != "$new_gateway_interface" ]] && is_nat_set "$old_gateway_interface"; then
|
|
|
|
unset_nat "${old_gateway_interface}"
|
|
|
|
fi
|
2023-11-18 19:18:15 +01:00
|
|
|
|
2023-11-18 21:45:01 +01:00
|
|
|
if [[ -n "$new_gateway_interface" ]] && ! is_nat_set $new_gateway_interface; then
|
|
|
|
set_nat "${new_gateway_interface}"
|
|
|
|
fi
|
2023-11-18 19:18:15 +01:00
|
|
|
|
2023-11-20 11:54:47 +01:00
|
|
|
yunohost app setting __APP__ gateway_interface --value "${new_gateway_interface}"
|
2023-11-18 21:42:48 +01:00
|
|
|
fi
|