Try something like this
You wish to change ufw
rules based on interface
status.
Nice place for this kind of action is in /etc/network/if-down.d/
and /etc/network/if-up.d/
You can in this folder put some script and make action. For example put this script in /etc/network/if-down.d/
to change ufw
rules every time when tun0
go down.
Make script called script
with execute permissions 755
sudo nano /etc/network/if-down.d/script
Script is
# Check interface
[ "$IFACE" != "tun0" ] || exit 0
# Do something
sudo ufw default deny outgoing
sudo ufw allow out to xxx.xxx.xxx.xxx
Change permision of script
chmod 755 /etc/network/if-down.d/script
In script
xxx.xxx.xxx.xxx
represent ip of your vpn
server
When vpn
reconnect you need again to change ufw
rules.
Make one more script aka script2
sudo nano /etc/network/if-up.d/script2
Script is something like this
# Check interface
[ "$IFACE" != "tun0" ] || exit 0
# Do something
sudo ufw default allow outgoing
#also you can add more ufw rules ...
sudo ufw ....
Change permision of script
chmod 755 /etc/network/if-up.d/script2
First script will if tun0
go down make default outgoing police to deny but will allow access to vpn
server. Second script will change default outgoing police to allow