Skip to content

Commit

Permalink
Maint: internal/vpn package for vpn loop
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 18, 2021
1 parent 05018ec commit d4ca5cf
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 60 deletions.
23 changes: 11 additions & 12 deletions cmd/gluetun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
12 changes: 6 additions & 6 deletions internal/healthcheck/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions internal/healthcheck/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions internal/healthcheck/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/openvpn/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ 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,
publicIPLooper publicip.Looper,
) 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)
Expand Down
12 changes: 6 additions & 6 deletions internal/server/handlerv0.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
}
Expand All @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions internal/server/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/openvpn/helpers.go → internal/vpn/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package openvpn
package vpn

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions internal/openvpn/loop.go → internal/vpn/loop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package openvpn
package vpn

import (
"net/http"
Expand All @@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package openvpn
package vpn

import (
"context"
Expand Down
5 changes: 3 additions & 2 deletions internal/openvpn/run.go → internal/vpn/run.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/openvpn/servers.go → internal/vpn/servers.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/openvpn/settings.go → internal/vpn/settings.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/openvpn/status.go → internal/vpn/status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package openvpn
package vpn

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion internal/openvpn/tunnelup.go → internal/vpn/tunnelup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package openvpn
package vpn

import (
"context"
Expand Down

0 comments on commit d4ca5cf

Please sign in to comment.