Maint: move routeReadyEvents to openvpn package

This commit is contained in:
Quentin McGaw (desktop)
2021-08-16 19:19:41 +00:00
parent ba16270059
commit 836412b032
4 changed files with 85 additions and 103 deletions

View File

@@ -46,7 +46,7 @@ func (l *Loop) collectLines(ctx context.Context, done chan<- struct{},
l.logger.Error(line)
}
if strings.Contains(line, "Initialization Sequence Completed") {
l.tunnelReady <- struct{}{}
l.onTunnelUp(ctx)
l.startPFCh <- struct{}{}
}
}

View File

@@ -6,11 +6,13 @@ import (
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/firewall"
"github.com/qdm12/gluetun/internal/loopstate"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/state"
"github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/routing"
"github.com/qdm12/golibs/logging"
)
@@ -33,15 +35,18 @@ type Loop struct {
puid int
pgid int
targetConfPath string
buildInfo models.BuildInformation
versionInfo bool
// Configurators
conf StarterAuthWriter
fw firewallConfigurer
routing routing.VPNLocalGatewayIPGetter
routing routing.VPNGetter
portForward portforward.StartStopper
publicip publicip.Looper
dnsLooper dns.Looper
// Other objects
logger logging.Logger
client *http.Client
tunnelReady chan<- struct{}
logger logging.Logger
client *http.Client
// Internal channels and values
stop <-chan struct{}
stopped chan<- struct{}
@@ -64,9 +69,11 @@ const (
func NewLoop(settings configuration.OpenVPN, username string,
puid, pgid int, allServers models.AllServers, conf Configurator,
fw firewallConfigurer, routing routing.VPNLocalGatewayIPGetter,
portForward portforward.StartStopper, logger logging.Logger,
client *http.Client, tunnelReady chan<- struct{}) *Loop {
fw firewallConfigurer, routing routing.VPNGetter,
portForward portforward.StartStopper,
publicip publicip.Looper, dnsLooper dns.Looper,
logger logging.Logger, client *http.Client,
buildInfo models.BuildInformation, versionInfo bool) *Loop {
start := make(chan struct{})
running := make(chan models.LoopStatus)
stop := make(chan struct{})
@@ -82,13 +89,16 @@ func NewLoop(settings configuration.OpenVPN, username string,
puid: puid,
pgid: pgid,
targetConfPath: constants.OpenVPNConf,
buildInfo: buildInfo,
versionInfo: versionInfo,
conf: conf,
fw: fw,
routing: routing,
portForward: portForward,
publicip: publicip,
dnsLooper: dnsLooper,
logger: logger,
client: client,
tunnelReady: tunnelReady,
start: start,
running: running,
stop: stop,

View File

@@ -0,0 +1,33 @@
package openvpn
import (
"context"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/version"
)
func (l *Loop) onTunnelUp(ctx context.Context) {
vpnDestination, err := l.routing.VPNDestinationIP()
if err != nil {
l.logger.Warn(err.Error())
} else {
l.logger.Info("VPN routing IP address: " + vpnDestination.String())
}
if l.dnsLooper.GetSettings().Enabled {
_, _ = l.dnsLooper.ApplyStatus(ctx, constants.Running)
}
// Runs the Public IP getter job once
_, _ = l.publicip.ApplyStatus(ctx, constants.Running)
if l.versionInfo {
l.versionInfo = false // only get the version information once
message, err := version.GetMessage(ctx, l.buildInfo, l.client)
if err != nil {
l.logger.Error("cannot get version information: " + err.Error())
} else {
l.logger.Info(message)
}
}
}