Maint: create OpenVPN runner in VPN run loop

This commit is contained in:
Quentin McGaw (desktop)
2021-08-19 14:45:57 +00:00
parent 3d8e61900b
commit 9218c7ef19
7 changed files with 48 additions and 26 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/routing"
"github.com/qdm12/gluetun/internal/vpn/state"
"github.com/qdm12/golibs/command"
"github.com/qdm12/golibs/logging"
)
@@ -42,8 +43,9 @@ type Loop struct {
publicip publicip.Looper
dnsLooper dns.Looper
// Other objects
logger logging.Logger
client *http.Client
starter command.Starter // for OpenVPN
logger logging.Logger
client *http.Client
// Internal channels and values
stop <-chan struct{}
stopped chan<- struct{}
@@ -67,7 +69,7 @@ func NewLoop(vpnSettings configuration.VPN,
providerSettings configuration.Provider,
allServers models.AllServers, openvpnConf openvpn.Interface,
fw firewallConfigurer, routing routing.VPNGetter,
portForward portforward.StartStopper,
portForward portforward.StartStopper, starter command.Starter,
publicip publicip.Looper, dnsLooper dns.Looper,
logger logging.Logger, client *http.Client,
buildInfo models.BuildInformation, versionInfo bool) *Loop {
@@ -90,6 +92,7 @@ func NewLoop(vpnSettings configuration.VPN,
portForward: portForward,
publicip: publicip,
dnsLooper: dnsLooper,
starter: starter,
logger: logger,
client: client,
start: start,

View File

@@ -11,6 +11,8 @@ import (
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/openvpn/custom"
"github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/golibs/command"
"github.com/qdm12/golibs/logging"
)
var (
@@ -24,8 +26,9 @@ var (
// 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) {
openVPNSettings configuration.OpenVPN, providerSettings configuration.Provider,
starter command.Starter, logger logging.Logger) (
runner vpnRunner, serverName string, err error) {
var connection models.Connection
var lines []string
if openVPNSettings.Config == "" {
@@ -37,23 +40,25 @@ func setupOpenVPN(ctx context.Context, fw firewall.VPNConnectionSetter,
lines, connection, err = custom.BuildConfig(openVPNSettings)
}
if err != nil {
return "", fmt.Errorf("%w: %s", errBuildConfig, err)
return nil, "", fmt.Errorf("%w: %s", errBuildConfig, err)
}
if err := openvpnConf.WriteConfig(lines); err != nil {
return "", fmt.Errorf("%w: %s", errWriteConfig, err)
return nil, "", 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)
return nil, "", fmt.Errorf("%w: %s", errWriteAuth, err)
}
}
if err := fw.SetVPNConnection(ctx, connection); err != nil {
return "", fmt.Errorf("%w: %s", errFirewall, err)
return nil, "", fmt.Errorf("%w: %s", errFirewall, err)
}
return connection.Hostname, nil
runner = openvpn.NewRunner(openVPNSettings, starter, logger)
return runner, connection.Hostname, nil
}

View File

@@ -12,6 +12,10 @@ type Runner interface {
Run(ctx context.Context, done chan<- struct{})
}
type vpnRunner interface {
Run(ctx context.Context, errCh chan<- error, ready chan<- struct{})
}
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
@@ -26,7 +30,10 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
providerConf := provider.New(providerSettings.Name, allServers, time.Now)
serverName, err := setupOpenVPN(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings)
vpnRunner, serverName, err := setupOpenVPN(ctx, l.fw,
l.openvpnConf, providerConf,
VPNSettings.OpenVPN, providerSettings,
l.starter, l.logger)
if err != nil {
l.crashed(ctx, err)
continue
@@ -41,8 +48,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
waitError := make(chan error)
tunnelReady := make(chan struct{})
go l.openvpnConf.Run(openvpnCtx, waitError, tunnelReady,
l.logger, VPNSettings.OpenVPN)
go vpnRunner.Run(openvpnCtx, waitError, tunnelReady)
if err := l.waitForError(ctx, waitError); err != nil {
openvpnCancel()