diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index 00835c6d..b4975139 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -22,7 +22,6 @@ import ( "github.com/qdm12/gluetun/internal/healthcheck" "github.com/qdm12/gluetun/internal/httpproxy" "github.com/qdm12/gluetun/internal/models" - "github.com/qdm12/gluetun/internal/openvpn" openvpnconfig "github.com/qdm12/gluetun/internal/openvpn/config" "github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/publicip" @@ -32,6 +31,7 @@ import ( "github.com/qdm12/gluetun/internal/storage" "github.com/qdm12/gluetun/internal/tun" "github.com/qdm12/gluetun/internal/updater" + "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/command" "github.com/qdm12/golibs/logging" "github.com/qdm12/golibs/params" @@ -355,18 +355,17 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, go publicIPLooper.RunRestartTicker(pubIPTickerCtx, pubIPTickerDone) tickersGroupHandler.Add(pubIPTickerHandler) - openvpnLogger := logger.NewChild(logging.Settings{Prefix: "openvpn: "}) - openvpnLooper := openvpn.NewLoop(allSettings.VPN, allSettings.VPN.Provider, + vpnLogger := logger.NewChild(logging.Settings{Prefix: "vpn: "}) + vpnLooper := vpn.NewLoop(allSettings.VPN, allSettings.VPN.Provider, allServers, ovpnConf, firewallConf, routingConf, portForwardLooper, - publicIPLooper, unboundLooper, openvpnLogger, httpClient, + publicIPLooper, unboundLooper, vpnLogger, httpClient, buildInfo, allSettings.VersionInformation) openvpnHandler, openvpnCtx, openvpnDone := goshutdown.NewGoRoutineHandler( "openvpn", goshutdown.GoRoutineSettings{Timeout: time.Second}) - // wait for restartOpenvpn - go openvpnLooper.Run(openvpnCtx, openvpnDone) + go vpnLooper.Run(openvpnCtx, openvpnDone) updaterLooper := updater.NewLooper(allSettings.Updater, - allServers, storage, openvpnLooper.SetServers, httpClient, + allServers, storage, vpnLooper.SetServers, httpClient, logger.NewChild(logging.Settings{Prefix: "updater: "})) updaterHandler, updaterCtx, updaterDone := goshutdown.NewGoRoutineHandler( "updater", defaultGoRoutineSettings) @@ -400,12 +399,12 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, "http server", defaultGoRoutineSettings) httpServer := server.New(httpServerCtx, controlServerAddress, controlServerLogging, logger.NewChild(logging.Settings{Prefix: "http server: "}), - buildInfo, openvpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper) + buildInfo, vpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper) go httpServer.Run(httpServerCtx, httpServerDone) controlGroupHandler.Add(httpServerHandler) 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( "HTTP health server", defaultGoRoutineSettings) go healthcheckServer.Run(healthServerCtx, healthServerDone) @@ -420,9 +419,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, orderHandler.Append(controlGroupHandler, tickersGroupHandler, healthServerHandler, openvpnHandler, portForwardHandler, otherGroupHandler) - // Start openvpn for the first time in a blocking call - // until openvpn is launched - _, _ = openvpnLooper.ApplyStatus(ctx, constants.Running) // TODO option to disable with variable + // Start VPN for the first time in a blocking call + // until the VPN is launched + _, _ = vpnLooper.ApplyStatus(ctx, constants.Running) // TODO option to disable with variable <-ctx.Done() diff --git a/internal/healthcheck/health.go b/internal/healthcheck/health.go index a2502640..c34e3ea1 100644 --- a/internal/healthcheck/health.go +++ b/internal/healthcheck/health.go @@ -12,7 +12,7 @@ import ( func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { defer close(done) - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) + s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait) for { previousErr := s.handler.getErr() @@ -22,12 +22,12 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { if previousErr != nil && err == nil { s.logger.Info("healthy!") - s.openvpn.healthyTimer.Stop() - s.openvpn.healthyWait = s.config.OpenVPN.Initial + s.vpn.healthyTimer.Stop() + s.vpn.healthyWait = s.config.OpenVPN.Initial } else if previousErr == nil && err != nil { s.logger.Info("unhealthy: " + err.Error()) - s.openvpn.healthyTimer.Stop() - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) + s.vpn.healthyTimer.Stop() + s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait) } if err != nil { // try again after 1 second @@ -39,7 +39,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { } return case <-timer.C: - case <-s.openvpn.healthyTimer.C: + case <-s.vpn.healthyTimer.C: s.onUnhealthyOpenvpn(ctx) } continue diff --git a/internal/healthcheck/openvpn.go b/internal/healthcheck/openvpn.go index 7e618ea6..a86f11c8 100644 --- a/internal/healthcheck/openvpn.go +++ b/internal/healthcheck/openvpn.go @@ -5,20 +5,20 @@ import ( "time" "github.com/qdm12/gluetun/internal/constants" - "github.com/qdm12/gluetun/internal/openvpn" + "github.com/qdm12/gluetun/internal/vpn" ) -type openvpnHealth struct { - looper openvpn.Looper +type vpnHealth struct { + looper vpn.Looper healthyWait time.Duration healthyTimer *time.Timer } func (s *Server) onUnhealthyOpenvpn(ctx context.Context) { s.logger.Info("program has been unhealthy for " + - s.openvpn.healthyWait.String() + ": restarting OpenVPN") - _, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Stopped) - _, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Running) - s.openvpn.healthyWait += s.config.OpenVPN.Addition - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) + s.vpn.healthyWait.String() + ": restarting OpenVPN") + _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped) + _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running) + s.vpn.healthyWait += s.config.OpenVPN.Addition + s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait) } diff --git a/internal/healthcheck/server.go b/internal/healthcheck/server.go index 34f45a59..22f5dda0 100644 --- a/internal/healthcheck/server.go +++ b/internal/healthcheck/server.go @@ -5,7 +5,7 @@ import ( "net" "github.com/qdm12/gluetun/internal/configuration" - "github.com/qdm12/gluetun/internal/openvpn" + "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/logging" ) @@ -20,18 +20,18 @@ type Server struct { handler *handler resolver *net.Resolver config configuration.Health - openvpn openvpnHealth + vpn vpnHealth } func NewServer(config configuration.Health, - logger logging.Logger, openvpnLooper openvpn.Looper) *Server { + logger logging.Logger, vpnLooper vpn.Looper) *Server { return &Server{ logger: logger, handler: newHandler(logger), resolver: net.DefaultResolver, config: config, - openvpn: openvpnHealth{ - looper: openvpnLooper, + vpn: vpnHealth{ + looper: vpnLooper, healthyWait: config.OpenVPN.Initial, }, } diff --git a/internal/openvpn/setup.go b/internal/openvpn/setup.go index 7ccadfaa..22865847 100644 --- a/internal/openvpn/setup.go +++ b/internal/openvpn/setup.go @@ -20,9 +20,9 @@ var ( 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. -func setup(ctx context.Context, fw firewall.VPNConnectionSetter, +func Setup(ctx context.Context, fw firewall.VPNConnectionSetter, openvpnConf config.Interface, providerConf provider.Provider, openVPNSettings configuration.OpenVPN, providerSettings configuration.Provider) ( serverName string, err error) { diff --git a/internal/server/handler.go b/internal/server/handler.go index aed0a318..092fa2a3 100644 --- a/internal/server/handler.go +++ b/internal/server/handler.go @@ -7,16 +7,16 @@ import ( "github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/models" - "github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/updater" + "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/logging" ) func newHandler(ctx context.Context, logger logging.Logger, logging bool, buildInfo models.BuildInformation, - openvpnLooper openvpn.Looper, + vpnLooper vpn.Looper, pfGetter portforward.Getter, unboundLooper dns.Looper, updaterLooper updater.Looper, @@ -24,12 +24,12 @@ func newHandler(ctx context.Context, logger logging.Logger, logging bool, ) http.Handler { handler := &handler{} - openvpn := newOpenvpnHandler(ctx, openvpnLooper, pfGetter, logger) + openvpn := newOpenvpnHandler(ctx, vpnLooper, pfGetter, logger) dns := newDNSHandler(ctx, unboundLooper, logger) updater := newUpdaterHandler(ctx, updaterLooper, 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) handlerWithLog := withLogMiddleware(handler, logger, logging) diff --git a/internal/server/handlerv0.go b/internal/server/handlerv0.go index 6a45bf51..ee15a95a 100644 --- a/internal/server/handlerv0.go +++ b/internal/server/handlerv0.go @@ -6,17 +6,17 @@ import ( "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/dns" - "github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/updater" + "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/logging" ) 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{ ctx: ctx, logger: logger, - openvpn: openvpn, + vpn: vpn, dns: dns, updater: updater, } @@ -25,7 +25,7 @@ func newHandlerV0(ctx context.Context, logger logging.Logger, type handlerV0 struct { ctx context.Context logger logging.Logger - openvpn openvpn.Looper + vpn vpn.Looper dns dns.Looper updater updater.Looper } @@ -39,9 +39,9 @@ func (h *handlerV0) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/version": http.Redirect(w, r, "/v1/version", http.StatusPermanentRedirect) 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) - outcome, _ = h.openvpn.ApplyStatus(h.ctx, constants.Running) + outcome, _ = h.vpn.ApplyStatus(h.ctx, constants.Running) h.logger.Info("openvpn: " + outcome) if _, err := w.Write([]byte("openvpn restarted, please consider using the /v1/ API in the future.")); err != nil { h.logger.Warn(err.Error()) diff --git a/internal/server/openvpn.go b/internal/server/openvpn.go index 732f8163..6687b60f 100644 --- a/internal/server/openvpn.go +++ b/internal/server/openvpn.go @@ -6,12 +6,12 @@ import ( "net/http" "strings" - "github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/portforward" + "github.com/qdm12/gluetun/internal/vpn" "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 { return &openvpnHandler{ ctx: ctx, @@ -23,7 +23,7 @@ func newOpenvpnHandler(ctx context.Context, looper openvpn.Looper, type openvpnHandler struct { ctx context.Context - looper openvpn.Looper + looper vpn.Looper pf portforward.Getter logger logging.Logger } diff --git a/internal/server/server.go b/internal/server/server.go index f396049e..8a864984 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -9,10 +9,10 @@ import ( "github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/models" - "github.com/qdm12/gluetun/internal/openvpn" "github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/updater" + "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/logging" ) @@ -27,7 +27,7 @@ type server struct { } 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, updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server { handler := newHandler(ctx, logger, logEnabled, buildInfo, diff --git a/internal/openvpn/helpers.go b/internal/vpn/helpers.go similarity index 98% rename from internal/openvpn/helpers.go rename to internal/vpn/helpers.go index 7484771a..851fb230 100644 --- a/internal/openvpn/helpers.go +++ b/internal/vpn/helpers.go @@ -1,4 +1,4 @@ -package openvpn +package vpn import ( "context" diff --git a/internal/openvpn/loop.go b/internal/vpn/loop.go similarity index 97% rename from internal/openvpn/loop.go rename to internal/vpn/loop.go index 041bbb92..5d23f9f9 100644 --- a/internal/openvpn/loop.go +++ b/internal/vpn/loop.go @@ -1,4 +1,4 @@ -package openvpn +package vpn import ( "net/http" @@ -11,10 +11,10 @@ import ( "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/state" "github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/routing" + "github.com/qdm12/gluetun/internal/vpn/state" "github.com/qdm12/golibs/logging" ) diff --git a/internal/openvpn/portforward.go b/internal/vpn/portforward.go similarity index 98% rename from internal/openvpn/portforward.go rename to internal/vpn/portforward.go index 0c02f34a..c711fa12 100644 --- a/internal/openvpn/portforward.go +++ b/internal/vpn/portforward.go @@ -1,4 +1,4 @@ -package openvpn +package vpn import ( "context" diff --git a/internal/openvpn/run.go b/internal/vpn/run.go similarity index 93% rename from internal/openvpn/run.go rename to internal/vpn/run.go index b7631022..8004530e 100644 --- a/internal/openvpn/run.go +++ b/internal/vpn/run.go @@ -1,10 +1,11 @@ -package openvpn +package vpn import ( "context" "time" "github.com/qdm12/gluetun/internal/constants" + "github.com/qdm12/gluetun/internal/openvpn" "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) - 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 { l.crashed(ctx, err) continue diff --git a/internal/openvpn/servers.go b/internal/vpn/servers.go similarity index 81% rename from internal/openvpn/servers.go rename to internal/vpn/servers.go index 3f03a814..61d8fb68 100644 --- a/internal/openvpn/servers.go +++ b/internal/vpn/servers.go @@ -1,8 +1,8 @@ -package openvpn +package vpn import ( "github.com/qdm12/gluetun/internal/models" - "github.com/qdm12/gluetun/internal/openvpn/state" + "github.com/qdm12/gluetun/internal/vpn/state" ) type ServersGetterSetter = state.ServersGetterSetter diff --git a/internal/openvpn/settings.go b/internal/vpn/settings.go similarity index 86% rename from internal/openvpn/settings.go rename to internal/vpn/settings.go index 9aa7e34c..3c3f7033 100644 --- a/internal/openvpn/settings.go +++ b/internal/vpn/settings.go @@ -1,10 +1,10 @@ -package openvpn +package vpn import ( "context" "github.com/qdm12/gluetun/internal/configuration" - "github.com/qdm12/gluetun/internal/openvpn/state" + "github.com/qdm12/gluetun/internal/vpn/state" ) type SettingsGetSetter = state.SettingsGetSetter diff --git a/internal/openvpn/state/servers.go b/internal/vpn/state/servers.go similarity index 100% rename from internal/openvpn/state/servers.go rename to internal/vpn/state/servers.go diff --git a/internal/openvpn/state/state.go b/internal/vpn/state/state.go similarity index 100% rename from internal/openvpn/state/state.go rename to internal/vpn/state/state.go diff --git a/internal/openvpn/state/openvpn.go b/internal/vpn/state/vpn.go similarity index 100% rename from internal/openvpn/state/openvpn.go rename to internal/vpn/state/vpn.go diff --git a/internal/openvpn/status.go b/internal/vpn/status.go similarity index 95% rename from internal/openvpn/status.go rename to internal/vpn/status.go index bc1b327a..5e1d94b9 100644 --- a/internal/openvpn/status.go +++ b/internal/vpn/status.go @@ -1,4 +1,4 @@ -package openvpn +package vpn import ( "context" diff --git a/internal/openvpn/tunnelup.go b/internal/vpn/tunnelup.go similarity index 98% rename from internal/openvpn/tunnelup.go rename to internal/vpn/tunnelup.go index 0c21ef25..c777b609 100644 --- a/internal/openvpn/tunnelup.go +++ b/internal/vpn/tunnelup.go @@ -1,4 +1,4 @@ -package openvpn +package vpn import ( "context"