Files
gluetun/internal/firewall/vpn.go
Quentin McGaw (desktop) 3d8e61900b Maint: make VPN connection not specific to OpenVPN
- Add VPN field to ServerSelection struct
- Set VPN type to server selection at start using VPN_TYPE
- Change OpenVPNConnection to Connection with Type field
- Rename Provider GetOpenVPNConnection to GetConnection
- Rename GetTargetIPOpenVPNConnection to GetTargetIPConnection
- Rename PickRandomOpenVPNConnection to PickRandomConnection
- Add 'OpenVPN' prefix to OpenVPN specific methods on connection
2021-08-19 14:09:41 +00:00

44 lines
1.1 KiB
Go

package firewall
import (
"context"
"fmt"
"github.com/qdm12/gluetun/internal/models"
)
type VPNConnectionSetter interface {
SetVPNConnection(ctx context.Context, connection models.Connection) error
}
func (c *Config) SetVPNConnection(ctx context.Context, connection models.Connection) (err error) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
if !c.enabled {
c.logger.Info("firewall disabled, only updating internal VPN connection")
c.vpnConnection = connection
return nil
}
c.logger.Info("setting VPN connection through firewall...")
if c.vpnConnection.Equal(connection) {
return nil
}
remove := true
if c.vpnConnection.IP != nil {
if err := c.acceptOutputTrafficToVPN(ctx, c.defaultInterface, c.vpnConnection, remove); err != nil {
c.logger.Error("cannot remove outdated VPN connection through firewall: " + err.Error())
}
}
c.vpnConnection = models.Connection{}
remove = false
if err := c.acceptOutputTrafficToVPN(ctx, c.defaultInterface, connection, remove); err != nil {
return fmt.Errorf("cannot set VPN connection through firewall: %w", err)
}
c.vpnConnection = connection
return nil
}