Maint: move Openvpn package files
- Move internal/openvpn/config/*.go to internal/openvpn/ - Move internal/openvpn/setup.go to internal/vpn/openvpn.go
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/firewall"
|
||||
"github.com/qdm12/gluetun/internal/loopstate"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/openvpn/config"
|
||||
"github.com/qdm12/gluetun/internal/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/portforward"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
"github.com/qdm12/gluetun/internal/routing"
|
||||
@@ -35,7 +35,7 @@ type Loop struct {
|
||||
buildInfo models.BuildInformation
|
||||
versionInfo bool
|
||||
// Configurators
|
||||
openvpnConf config.Interface
|
||||
openvpnConf openvpn.Interface
|
||||
fw firewallConfigurer
|
||||
routing routing.VPNGetter
|
||||
portForward portforward.StartStopper
|
||||
@@ -65,7 +65,7 @@ const (
|
||||
|
||||
func NewLoop(vpnSettings configuration.VPN,
|
||||
providerSettings configuration.Provider,
|
||||
allServers models.AllServers, openvpnConf config.Interface,
|
||||
allServers models.AllServers, openvpnConf openvpn.Interface,
|
||||
fw firewallConfigurer, routing routing.VPNGetter,
|
||||
portForward portforward.StartStopper,
|
||||
publicip publicip.Looper, dnsLooper dns.Looper,
|
||||
|
||||
59
internal/vpn/openvpn.go
Normal file
59
internal/vpn/openvpn.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/firewall"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/openvpn/custom"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
)
|
||||
|
||||
var (
|
||||
errBuildConfig = errors.New("failed building configuration")
|
||||
errWriteConfig = errors.New("failed writing configuration to file")
|
||||
errWriteAuth = errors.New("failed writing auth to file")
|
||||
errFirewall = errors.New("failed allowing VPN connection through firewall")
|
||||
)
|
||||
|
||||
// setupOpenVPN sets OpenVPN up using the configurators and settings given.
|
||||
// It returns a serverName for port forwarding (PIA) and an error if it fails.
|
||||
func setupOpenVPN(ctx context.Context, fw firewall.VPNConnectionSetter,
|
||||
openvpnConf openvpn.Interface, providerConf provider.Provider,
|
||||
openVPNSettings configuration.OpenVPN, providerSettings configuration.Provider) (
|
||||
serverName string, err error) {
|
||||
var connection models.OpenVPNConnection
|
||||
var lines []string
|
||||
if openVPNSettings.Config == "" {
|
||||
connection, err = providerConf.GetOpenVPNConnection(providerSettings.ServerSelection)
|
||||
if err == nil {
|
||||
lines = providerConf.BuildConf(connection, openVPNSettings)
|
||||
}
|
||||
} else {
|
||||
lines, connection, err = custom.BuildConfig(openVPNSettings)
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %s", errBuildConfig, err)
|
||||
}
|
||||
|
||||
if err := openvpnConf.WriteConfig(lines); err != nil {
|
||||
return "", fmt.Errorf("%w: %s", errWriteConfig, err)
|
||||
}
|
||||
|
||||
if openVPNSettings.User != "" {
|
||||
err := openvpnConf.WriteAuthFile(openVPNSettings.User, openVPNSettings.Password)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %s", errWriteAuth, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := fw.SetVPNConnection(ctx, connection); err != nil {
|
||||
return "", fmt.Errorf("%w: %s", errFirewall, err)
|
||||
}
|
||||
|
||||
return connection.Hostname, nil
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
)
|
||||
|
||||
@@ -27,7 +26,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||
|
||||
providerConf := provider.New(providerSettings.Name, allServers, time.Now)
|
||||
|
||||
serverName, err := openvpn.Setup(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings)
|
||||
serverName, err := setupOpenVPN(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings)
|
||||
if err != nil {
|
||||
l.crashed(ctx, err)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user