Maint: internal/vpn package for vpn loop

This commit is contained in:
Quentin McGaw (desktop)
2021-08-18 22:01:04 +00:00
parent 05018ec971
commit d4ca5cf257
20 changed files with 60 additions and 60 deletions

View File

@@ -22,7 +22,6 @@ import (
"github.com/qdm12/gluetun/internal/healthcheck" "github.com/qdm12/gluetun/internal/healthcheck"
"github.com/qdm12/gluetun/internal/httpproxy" "github.com/qdm12/gluetun/internal/httpproxy"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn"
openvpnconfig "github.com/qdm12/gluetun/internal/openvpn/config" openvpnconfig "github.com/qdm12/gluetun/internal/openvpn/config"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
@@ -32,6 +31,7 @@ import (
"github.com/qdm12/gluetun/internal/storage" "github.com/qdm12/gluetun/internal/storage"
"github.com/qdm12/gluetun/internal/tun" "github.com/qdm12/gluetun/internal/tun"
"github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/command" "github.com/qdm12/golibs/command"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/params" "github.com/qdm12/golibs/params"
@@ -355,18 +355,17 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
go publicIPLooper.RunRestartTicker(pubIPTickerCtx, pubIPTickerDone) go publicIPLooper.RunRestartTicker(pubIPTickerCtx, pubIPTickerDone)
tickersGroupHandler.Add(pubIPTickerHandler) tickersGroupHandler.Add(pubIPTickerHandler)
openvpnLogger := logger.NewChild(logging.Settings{Prefix: "openvpn: "}) vpnLogger := logger.NewChild(logging.Settings{Prefix: "vpn: "})
openvpnLooper := openvpn.NewLoop(allSettings.VPN, allSettings.VPN.Provider, vpnLooper := vpn.NewLoop(allSettings.VPN, allSettings.VPN.Provider,
allServers, ovpnConf, firewallConf, routingConf, portForwardLooper, allServers, ovpnConf, firewallConf, routingConf, portForwardLooper,
publicIPLooper, unboundLooper, openvpnLogger, httpClient, publicIPLooper, unboundLooper, vpnLogger, httpClient,
buildInfo, allSettings.VersionInformation) buildInfo, allSettings.VersionInformation)
openvpnHandler, openvpnCtx, openvpnDone := goshutdown.NewGoRoutineHandler( openvpnHandler, openvpnCtx, openvpnDone := goshutdown.NewGoRoutineHandler(
"openvpn", goshutdown.GoRoutineSettings{Timeout: time.Second}) "openvpn", goshutdown.GoRoutineSettings{Timeout: time.Second})
// wait for restartOpenvpn go vpnLooper.Run(openvpnCtx, openvpnDone)
go openvpnLooper.Run(openvpnCtx, openvpnDone)
updaterLooper := updater.NewLooper(allSettings.Updater, updaterLooper := updater.NewLooper(allSettings.Updater,
allServers, storage, openvpnLooper.SetServers, httpClient, allServers, storage, vpnLooper.SetServers, httpClient,
logger.NewChild(logging.Settings{Prefix: "updater: "})) logger.NewChild(logging.Settings{Prefix: "updater: "}))
updaterHandler, updaterCtx, updaterDone := goshutdown.NewGoRoutineHandler( updaterHandler, updaterCtx, updaterDone := goshutdown.NewGoRoutineHandler(
"updater", defaultGoRoutineSettings) "updater", defaultGoRoutineSettings)
@@ -400,12 +399,12 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
"http server", defaultGoRoutineSettings) "http server", defaultGoRoutineSettings)
httpServer := server.New(httpServerCtx, controlServerAddress, controlServerLogging, httpServer := server.New(httpServerCtx, controlServerAddress, controlServerLogging,
logger.NewChild(logging.Settings{Prefix: "http server: "}), logger.NewChild(logging.Settings{Prefix: "http server: "}),
buildInfo, openvpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper) buildInfo, vpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper)
go httpServer.Run(httpServerCtx, httpServerDone) go httpServer.Run(httpServerCtx, httpServerDone)
controlGroupHandler.Add(httpServerHandler) controlGroupHandler.Add(httpServerHandler)
healthLogger := logger.NewChild(logging.Settings{Prefix: "healthcheck: "}) healthLogger := logger.NewChild(logging.Settings{Prefix: "healthcheck: "})
healthcheckServer := healthcheck.NewServer(allSettings.Health, healthLogger, openvpnLooper) healthcheckServer := healthcheck.NewServer(allSettings.Health, healthLogger, vpnLooper)
healthServerHandler, healthServerCtx, healthServerDone := goshutdown.NewGoRoutineHandler( healthServerHandler, healthServerCtx, healthServerDone := goshutdown.NewGoRoutineHandler(
"HTTP health server", defaultGoRoutineSettings) "HTTP health server", defaultGoRoutineSettings)
go healthcheckServer.Run(healthServerCtx, healthServerDone) go healthcheckServer.Run(healthServerCtx, healthServerDone)
@@ -420,9 +419,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
orderHandler.Append(controlGroupHandler, tickersGroupHandler, healthServerHandler, orderHandler.Append(controlGroupHandler, tickersGroupHandler, healthServerHandler,
openvpnHandler, portForwardHandler, otherGroupHandler) openvpnHandler, portForwardHandler, otherGroupHandler)
// Start openvpn for the first time in a blocking call // Start VPN for the first time in a blocking call
// until openvpn is launched // until the VPN is launched
_, _ = openvpnLooper.ApplyStatus(ctx, constants.Running) // TODO option to disable with variable _, _ = vpnLooper.ApplyStatus(ctx, constants.Running) // TODO option to disable with variable
<-ctx.Done() <-ctx.Done()

View File

@@ -12,7 +12,7 @@ import (
func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
defer close(done) defer close(done)
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
for { for {
previousErr := s.handler.getErr() previousErr := s.handler.getErr()
@@ -22,12 +22,12 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
if previousErr != nil && err == nil { if previousErr != nil && err == nil {
s.logger.Info("healthy!") s.logger.Info("healthy!")
s.openvpn.healthyTimer.Stop() s.vpn.healthyTimer.Stop()
s.openvpn.healthyWait = s.config.OpenVPN.Initial s.vpn.healthyWait = s.config.OpenVPN.Initial
} else if previousErr == nil && err != nil { } else if previousErr == nil && err != nil {
s.logger.Info("unhealthy: " + err.Error()) s.logger.Info("unhealthy: " + err.Error())
s.openvpn.healthyTimer.Stop() s.vpn.healthyTimer.Stop()
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
} }
if err != nil { // try again after 1 second if err != nil { // try again after 1 second
@@ -39,7 +39,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
} }
return return
case <-timer.C: case <-timer.C:
case <-s.openvpn.healthyTimer.C: case <-s.vpn.healthyTimer.C:
s.onUnhealthyOpenvpn(ctx) s.onUnhealthyOpenvpn(ctx)
} }
continue continue

View File

@@ -5,20 +5,20 @@ import (
"time" "time"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/vpn"
) )
type openvpnHealth struct { type vpnHealth struct {
looper openvpn.Looper looper vpn.Looper
healthyWait time.Duration healthyWait time.Duration
healthyTimer *time.Timer healthyTimer *time.Timer
} }
func (s *Server) onUnhealthyOpenvpn(ctx context.Context) { func (s *Server) onUnhealthyOpenvpn(ctx context.Context) {
s.logger.Info("program has been unhealthy for " + s.logger.Info("program has been unhealthy for " +
s.openvpn.healthyWait.String() + ": restarting OpenVPN") s.vpn.healthyWait.String() + ": restarting OpenVPN")
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Stopped) _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped)
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Running) _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running)
s.openvpn.healthyWait += s.config.OpenVPN.Addition s.vpn.healthyWait += s.config.OpenVPN.Addition
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
} }

View File

@@ -5,7 +5,7 @@ import (
"net" "net"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )
@@ -20,18 +20,18 @@ type Server struct {
handler *handler handler *handler
resolver *net.Resolver resolver *net.Resolver
config configuration.Health config configuration.Health
openvpn openvpnHealth vpn vpnHealth
} }
func NewServer(config configuration.Health, func NewServer(config configuration.Health,
logger logging.Logger, openvpnLooper openvpn.Looper) *Server { logger logging.Logger, vpnLooper vpn.Looper) *Server {
return &Server{ return &Server{
logger: logger, logger: logger,
handler: newHandler(logger), handler: newHandler(logger),
resolver: net.DefaultResolver, resolver: net.DefaultResolver,
config: config, config: config,
openvpn: openvpnHealth{ vpn: vpnHealth{
looper: openvpnLooper, looper: vpnLooper,
healthyWait: config.OpenVPN.Initial, healthyWait: config.OpenVPN.Initial,
}, },
} }

View File

@@ -20,9 +20,9 @@ var (
errFirewall = errors.New("failed allowing VPN connection through firewall") errFirewall = errors.New("failed allowing VPN connection through firewall")
) )
// setup sets OpenVPN up using the configurators and settings given. // Setup sets OpenVPN up using the configurators and settings given.
// It returns a serverName for port forwarding (PIA) and an error if it fails. // It returns a serverName for port forwarding (PIA) and an error if it fails.
func setup(ctx context.Context, fw firewall.VPNConnectionSetter, func Setup(ctx context.Context, fw firewall.VPNConnectionSetter,
openvpnConf config.Interface, providerConf provider.Provider, openvpnConf config.Interface, providerConf provider.Provider,
openVPNSettings configuration.OpenVPN, providerSettings configuration.Provider) ( openVPNSettings configuration.OpenVPN, providerSettings configuration.Provider) (
serverName string, err error) { serverName string, err error) {

View File

@@ -7,16 +7,16 @@ import (
"github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )
func newHandler(ctx context.Context, logger logging.Logger, logging bool, func newHandler(ctx context.Context, logger logging.Logger, logging bool,
buildInfo models.BuildInformation, buildInfo models.BuildInformation,
openvpnLooper openvpn.Looper, vpnLooper vpn.Looper,
pfGetter portforward.Getter, pfGetter portforward.Getter,
unboundLooper dns.Looper, unboundLooper dns.Looper,
updaterLooper updater.Looper, updaterLooper updater.Looper,
@@ -24,12 +24,12 @@ func newHandler(ctx context.Context, logger logging.Logger, logging bool,
) http.Handler { ) http.Handler {
handler := &handler{} handler := &handler{}
openvpn := newOpenvpnHandler(ctx, openvpnLooper, pfGetter, logger) openvpn := newOpenvpnHandler(ctx, vpnLooper, pfGetter, logger)
dns := newDNSHandler(ctx, unboundLooper, logger) dns := newDNSHandler(ctx, unboundLooper, logger)
updater := newUpdaterHandler(ctx, updaterLooper, logger) updater := newUpdaterHandler(ctx, updaterLooper, logger)
publicip := newPublicIPHandler(publicIPLooper, logger) publicip := newPublicIPHandler(publicIPLooper, logger)
handler.v0 = newHandlerV0(ctx, logger, openvpnLooper, unboundLooper, updaterLooper) handler.v0 = newHandlerV0(ctx, logger, vpnLooper, unboundLooper, updaterLooper)
handler.v1 = newHandlerV1(logger, buildInfo, openvpn, dns, updater, publicip) handler.v1 = newHandlerV1(logger, buildInfo, openvpn, dns, updater, publicip)
handlerWithLog := withLogMiddleware(handler, logger, logging) handlerWithLog := withLogMiddleware(handler, logger, logging)

View File

@@ -6,17 +6,17 @@ import (
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )
func newHandlerV0(ctx context.Context, logger logging.Logger, func newHandlerV0(ctx context.Context, logger logging.Logger,
openvpn openvpn.Looper, dns dns.Looper, updater updater.Looper) http.Handler { vpn vpn.Looper, dns dns.Looper, updater updater.Looper) http.Handler {
return &handlerV0{ return &handlerV0{
ctx: ctx, ctx: ctx,
logger: logger, logger: logger,
openvpn: openvpn, vpn: vpn,
dns: dns, dns: dns,
updater: updater, updater: updater,
} }
@@ -25,7 +25,7 @@ func newHandlerV0(ctx context.Context, logger logging.Logger,
type handlerV0 struct { type handlerV0 struct {
ctx context.Context ctx context.Context
logger logging.Logger logger logging.Logger
openvpn openvpn.Looper vpn vpn.Looper
dns dns.Looper dns dns.Looper
updater updater.Looper updater updater.Looper
} }
@@ -39,9 +39,9 @@ func (h *handlerV0) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "/version": case "/version":
http.Redirect(w, r, "/v1/version", http.StatusPermanentRedirect) http.Redirect(w, r, "/v1/version", http.StatusPermanentRedirect)
case "/openvpn/actions/restart": case "/openvpn/actions/restart":
outcome, _ := h.openvpn.ApplyStatus(h.ctx, constants.Stopped) outcome, _ := h.vpn.ApplyStatus(h.ctx, constants.Stopped)
h.logger.Info("openvpn: " + outcome) h.logger.Info("openvpn: " + outcome)
outcome, _ = h.openvpn.ApplyStatus(h.ctx, constants.Running) outcome, _ = h.vpn.ApplyStatus(h.ctx, constants.Running)
h.logger.Info("openvpn: " + outcome) h.logger.Info("openvpn: " + outcome)
if _, err := w.Write([]byte("openvpn restarted, please consider using the /v1/ API in the future.")); err != nil { if _, err := w.Write([]byte("openvpn restarted, please consider using the /v1/ API in the future.")); err != nil {
h.logger.Warn(err.Error()) h.logger.Warn(err.Error())

View File

@@ -6,12 +6,12 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )
func newOpenvpnHandler(ctx context.Context, looper openvpn.Looper, func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
pfGetter portforward.Getter, logger logging.Logger) http.Handler { pfGetter portforward.Getter, logger logging.Logger) http.Handler {
return &openvpnHandler{ return &openvpnHandler{
ctx: ctx, ctx: ctx,
@@ -23,7 +23,7 @@ func newOpenvpnHandler(ctx context.Context, looper openvpn.Looper,
type openvpnHandler struct { type openvpnHandler struct {
ctx context.Context ctx context.Context
looper openvpn.Looper looper vpn.Looper
pf portforward.Getter pf portforward.Getter
logger logging.Logger logger logging.Logger
} }

View File

@@ -9,10 +9,10 @@ import (
"github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )
@@ -27,7 +27,7 @@ type server struct {
} }
func New(ctx context.Context, address string, logEnabled bool, logger logging.Logger, func New(ctx context.Context, address string, logEnabled bool, logger logging.Logger,
buildInfo models.BuildInformation, openvpnLooper openvpn.Looper, buildInfo models.BuildInformation, openvpnLooper vpn.Looper,
pfGetter portforward.Getter, unboundLooper dns.Looper, pfGetter portforward.Getter, unboundLooper dns.Looper,
updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server { updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server {
handler := newHandler(ctx, logger, logEnabled, buildInfo, handler := newHandler(ctx, logger, logEnabled, buildInfo,

View File

@@ -1,4 +1,4 @@
package openvpn package vpn
import ( import (
"context" "context"

View File

@@ -1,4 +1,4 @@
package openvpn package vpn
import ( import (
"net/http" "net/http"
@@ -11,10 +11,10 @@ import (
"github.com/qdm12/gluetun/internal/loopstate" "github.com/qdm12/gluetun/internal/loopstate"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/config" "github.com/qdm12/gluetun/internal/openvpn/config"
"github.com/qdm12/gluetun/internal/openvpn/state"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/routing" "github.com/qdm12/gluetun/internal/routing"
"github.com/qdm12/gluetun/internal/vpn/state"
"github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/logging"
) )

View File

@@ -1,4 +1,4 @@
package openvpn package vpn
import ( import (
"context" "context"

View File

@@ -1,10 +1,11 @@
package openvpn package vpn
import ( import (
"context" "context"
"time" "time"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/provider" "github.com/qdm12/gluetun/internal/provider"
) )
@@ -26,7 +27,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
providerConf := provider.New(providerSettings.Name, allServers, time.Now) providerConf := provider.New(providerSettings.Name, allServers, time.Now)
serverName, err := setup(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings) serverName, err := openvpn.Setup(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings)
if err != nil { if err != nil {
l.crashed(ctx, err) l.crashed(ctx, err)
continue continue

View File

@@ -1,8 +1,8 @@
package openvpn package vpn
import ( import (
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/state" "github.com/qdm12/gluetun/internal/vpn/state"
) )
type ServersGetterSetter = state.ServersGetterSetter type ServersGetterSetter = state.ServersGetterSetter

View File

@@ -1,10 +1,10 @@
package openvpn package vpn
import ( import (
"context" "context"
"github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/openvpn/state" "github.com/qdm12/gluetun/internal/vpn/state"
) )
type SettingsGetSetter = state.SettingsGetSetter type SettingsGetSetter = state.SettingsGetSetter

View File

@@ -1,4 +1,4 @@
package openvpn package vpn
import ( import (
"context" "context"

View File

@@ -1,4 +1,4 @@
package openvpn package vpn
import ( import (
"context" "context"