Maint: internal/vpn package for vpn loop
This commit is contained in:
64
internal/vpn/helpers.go
Normal file
64
internal/vpn/helpers.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
// waitForError waits 100ms for an error in the waitError channel.
|
||||
func (l *Loop) waitForError(ctx context.Context,
|
||||
waitError chan error) (err error) {
|
||||
const waitDurationForError = 100 * time.Millisecond
|
||||
timer := time.NewTimer(waitDurationForError)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
case err := <-waitError:
|
||||
close(waitError)
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Loop) crashed(ctx context.Context, err error) {
|
||||
l.signalOrSetStatus(constants.Crashed)
|
||||
l.logAndWait(ctx, err)
|
||||
}
|
||||
|
||||
func (l *Loop) signalOrSetStatus(status models.LoopStatus) {
|
||||
if l.userTrigger {
|
||||
l.userTrigger = false
|
||||
select {
|
||||
case l.running <- status:
|
||||
default: // receiver calling ApplyStatus dropped out
|
||||
}
|
||||
} else {
|
||||
l.statusManager.SetStatus(status)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Loop) logAndWait(ctx context.Context, err error) {
|
||||
if err != nil {
|
||||
l.logger.Error(err.Error())
|
||||
}
|
||||
l.logger.Info("retrying in " + l.backoffTime.String())
|
||||
timer := time.NewTimer(l.backoffTime)
|
||||
l.backoffTime *= 2
|
||||
select {
|
||||
case <-timer.C:
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
}
|
||||
}
|
||||
102
internal/vpn/loop.go
Normal file
102
internal/vpn/loop.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"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/config"
|
||||
"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"
|
||||
)
|
||||
|
||||
var _ Looper = (*Loop)(nil)
|
||||
|
||||
type Looper interface {
|
||||
Runner
|
||||
loopstate.Getter
|
||||
loopstate.Applier
|
||||
SettingsGetSetter
|
||||
ServersGetterSetter
|
||||
}
|
||||
|
||||
type Loop struct {
|
||||
statusManager loopstate.Manager
|
||||
state state.Manager
|
||||
// Fixed parameters
|
||||
buildInfo models.BuildInformation
|
||||
versionInfo bool
|
||||
// Configurators
|
||||
openvpnConf config.Interface
|
||||
fw firewallConfigurer
|
||||
routing routing.VPNGetter
|
||||
portForward portforward.StartStopper
|
||||
publicip publicip.Looper
|
||||
dnsLooper dns.Looper
|
||||
// Other objects
|
||||
logger logging.Logger
|
||||
client *http.Client
|
||||
// Internal channels and values
|
||||
stop <-chan struct{}
|
||||
stopped chan<- struct{}
|
||||
start <-chan struct{}
|
||||
running chan<- models.LoopStatus
|
||||
userTrigger bool
|
||||
// Internal constant values
|
||||
backoffTime time.Duration
|
||||
}
|
||||
|
||||
type firewallConfigurer interface {
|
||||
firewall.VPNConnectionSetter
|
||||
firewall.PortAllower
|
||||
}
|
||||
|
||||
const (
|
||||
defaultBackoffTime = 15 * time.Second
|
||||
)
|
||||
|
||||
func NewLoop(vpnSettings configuration.VPN,
|
||||
providerSettings configuration.Provider,
|
||||
allServers models.AllServers, openvpnConf config.Interface,
|
||||
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{})
|
||||
stopped := make(chan struct{})
|
||||
|
||||
statusManager := loopstate.New(constants.Stopped, start, running, stop, stopped)
|
||||
state := state.New(statusManager, vpnSettings, providerSettings, allServers)
|
||||
|
||||
return &Loop{
|
||||
statusManager: statusManager,
|
||||
state: state,
|
||||
buildInfo: buildInfo,
|
||||
versionInfo: versionInfo,
|
||||
openvpnConf: openvpnConf,
|
||||
fw: fw,
|
||||
routing: routing,
|
||||
portForward: portForward,
|
||||
publicip: publicip,
|
||||
dnsLooper: dnsLooper,
|
||||
logger: logger,
|
||||
client: client,
|
||||
start: start,
|
||||
running: running,
|
||||
stop: stop,
|
||||
stopped: stopped,
|
||||
userTrigger: true,
|
||||
backoffTime: defaultBackoffTime,
|
||||
}
|
||||
}
|
||||
62
internal/vpn/portforward.go
Normal file
62
internal/vpn/portforward.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/portforward"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
)
|
||||
|
||||
var (
|
||||
errObtainVPNLocalGateway = errors.New("cannot obtain VPN local gateway IP")
|
||||
errStartPortForwarding = errors.New("cannot start port forwarding")
|
||||
)
|
||||
|
||||
func (l *Loop) startPortForwarding(ctx context.Context, enabled bool,
|
||||
portForwarder provider.PortForwarder, serverName string) (err error) {
|
||||
if !enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
// only used for PIA for now
|
||||
gateway, err := l.routing.VPNLocalGatewayIP()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", errObtainVPNLocalGateway, err)
|
||||
}
|
||||
l.logger.Info("VPN gateway IP address: " + gateway.String())
|
||||
|
||||
pfData := portforward.StartData{
|
||||
PortForwarder: portForwarder,
|
||||
Gateway: gateway,
|
||||
ServerName: serverName,
|
||||
Interface: constants.TUN,
|
||||
}
|
||||
_, err = l.portForward.Start(ctx, pfData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", errStartPortForwarding, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Loop) stopPortForwarding(ctx context.Context, enabled bool,
|
||||
timeout time.Duration) {
|
||||
if !enabled {
|
||||
return // nothing to stop
|
||||
}
|
||||
|
||||
if timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
_, err := l.portForward.Stop(ctx)
|
||||
if err != nil {
|
||||
l.logger.Error("cannot stop port forwarding: " + err.Error())
|
||||
}
|
||||
}
|
||||
99
internal/vpn/run.go
Normal file
99
internal/vpn/run.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
)
|
||||
|
||||
type Runner interface {
|
||||
Run(ctx context.Context, done chan<- struct{})
|
||||
}
|
||||
|
||||
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||
defer close(done)
|
||||
|
||||
select {
|
||||
case <-l.start:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
|
||||
for ctx.Err() == nil {
|
||||
VPNSettings, providerSettings, allServers := l.state.GetSettingsAndServers()
|
||||
|
||||
providerConf := provider.New(providerSettings.Name, allServers, time.Now)
|
||||
|
||||
serverName, err := openvpn.Setup(ctx, l.fw, l.openvpnConf, providerConf, VPNSettings.OpenVPN, providerSettings)
|
||||
if err != nil {
|
||||
l.crashed(ctx, err)
|
||||
continue
|
||||
}
|
||||
tunnelUpData := tunnelUpData{
|
||||
portForwarding: providerSettings.PortForwarding.Enabled,
|
||||
serverName: serverName,
|
||||
portForwarder: providerConf,
|
||||
}
|
||||
|
||||
openvpnCtx, openvpnCancel := context.WithCancel(context.Background())
|
||||
waitError := make(chan error)
|
||||
tunnelReady := make(chan struct{})
|
||||
|
||||
go l.openvpnConf.Run(openvpnCtx, waitError, tunnelReady,
|
||||
l.logger, VPNSettings.OpenVPN)
|
||||
|
||||
if err := l.waitForError(ctx, waitError); err != nil {
|
||||
openvpnCancel()
|
||||
l.crashed(ctx, err)
|
||||
continue
|
||||
}
|
||||
|
||||
l.backoffTime = defaultBackoffTime
|
||||
l.signalOrSetStatus(constants.Running)
|
||||
|
||||
stayHere := true
|
||||
for stayHere {
|
||||
select {
|
||||
case <-tunnelReady:
|
||||
go l.onTunnelUp(openvpnCtx, tunnelUpData)
|
||||
case <-ctx.Done():
|
||||
const pfTimeout = 100 * time.Millisecond
|
||||
l.stopPortForwarding(context.Background(),
|
||||
providerSettings.PortForwarding.Enabled, pfTimeout)
|
||||
openvpnCancel()
|
||||
<-waitError
|
||||
close(waitError)
|
||||
return
|
||||
case <-l.stop:
|
||||
l.userTrigger = true
|
||||
l.logger.Info("stopping")
|
||||
l.stopPortForwarding(ctx, providerSettings.PortForwarding.Enabled, 0)
|
||||
openvpnCancel()
|
||||
<-waitError
|
||||
// do not close waitError or the waitError
|
||||
// select case will trigger
|
||||
l.stopped <- struct{}{}
|
||||
case <-l.start:
|
||||
l.userTrigger = true
|
||||
l.logger.Info("starting")
|
||||
stayHere = false
|
||||
case err := <-waitError: // unexpected error
|
||||
close(waitError)
|
||||
|
||||
l.statusManager.Lock() // prevent SetStatus from running in parallel
|
||||
|
||||
l.stopPortForwarding(ctx, providerSettings.PortForwarding.Enabled, 0)
|
||||
openvpnCancel()
|
||||
l.statusManager.SetStatus(constants.Crashed)
|
||||
l.logAndWait(ctx, err)
|
||||
stayHere = false
|
||||
|
||||
l.statusManager.Unlock()
|
||||
}
|
||||
}
|
||||
openvpnCancel()
|
||||
}
|
||||
}
|
||||
16
internal/vpn/servers.go
Normal file
16
internal/vpn/servers.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/vpn/state"
|
||||
)
|
||||
|
||||
type ServersGetterSetter = state.ServersGetterSetter
|
||||
|
||||
func (l *Loop) GetServers() (servers models.AllServers) {
|
||||
return l.state.GetServers()
|
||||
}
|
||||
|
||||
func (l *Loop) SetServers(servers models.AllServers) {
|
||||
l.state.SetServers(servers)
|
||||
}
|
||||
21
internal/vpn/settings.go
Normal file
21
internal/vpn/settings.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/vpn/state"
|
||||
)
|
||||
|
||||
type SettingsGetSetter = state.SettingsGetSetter
|
||||
|
||||
func (l *Loop) GetSettings() (
|
||||
vpn configuration.VPN, provider configuration.Provider) {
|
||||
return l.state.GetSettings()
|
||||
}
|
||||
|
||||
func (l *Loop) SetSettings(ctx context.Context,
|
||||
vpn configuration.VPN, provider configuration.Provider) (
|
||||
outcome string) {
|
||||
return l.state.SetSettings(ctx, vpn, provider)
|
||||
}
|
||||
20
internal/vpn/state/servers.go
Normal file
20
internal/vpn/state/servers.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package state
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
type ServersGetterSetter interface {
|
||||
GetServers() (servers models.AllServers)
|
||||
SetServers(servers models.AllServers)
|
||||
}
|
||||
|
||||
func (s *State) GetServers() (servers models.AllServers) {
|
||||
s.allServersMu.RLock()
|
||||
defer s.allServersMu.RUnlock()
|
||||
return s.allServers
|
||||
}
|
||||
|
||||
func (s *State) SetServers(servers models.AllServers) {
|
||||
s.allServersMu.Lock()
|
||||
defer s.allServersMu.Unlock()
|
||||
s.allServers = servers
|
||||
}
|
||||
52
internal/vpn/state/state.go
Normal file
52
internal/vpn/state/state.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/loopstate"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var _ Manager = (*State)(nil)
|
||||
|
||||
type Manager interface {
|
||||
SettingsGetSetter
|
||||
ServersGetterSetter
|
||||
GetSettingsAndServers() (vpn configuration.VPN,
|
||||
provider configuration.Provider, allServers models.AllServers)
|
||||
}
|
||||
|
||||
func New(statusApplier loopstate.Applier,
|
||||
vpn configuration.VPN, provider configuration.Provider,
|
||||
allServers models.AllServers) *State {
|
||||
return &State{
|
||||
statusApplier: statusApplier,
|
||||
vpn: vpn,
|
||||
provider: provider,
|
||||
allServers: allServers,
|
||||
}
|
||||
}
|
||||
|
||||
type State struct {
|
||||
statusApplier loopstate.Applier
|
||||
|
||||
vpn configuration.VPN
|
||||
provider configuration.Provider
|
||||
settingsMu sync.RWMutex
|
||||
|
||||
allServers models.AllServers
|
||||
allServersMu sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *State) GetSettingsAndServers() (vpn configuration.VPN,
|
||||
provider configuration.Provider, allServers models.AllServers) {
|
||||
s.settingsMu.RLock()
|
||||
s.allServersMu.RLock()
|
||||
vpn = s.vpn
|
||||
provider = s.provider
|
||||
allServers = s.allServers
|
||||
s.settingsMu.RUnlock()
|
||||
s.allServersMu.RUnlock()
|
||||
return vpn, provider, allServers
|
||||
}
|
||||
43
internal/vpn/state/vpn.go
Normal file
43
internal/vpn/state/vpn.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
type SettingsGetSetter interface {
|
||||
GetSettings() (vpn configuration.VPN,
|
||||
provider configuration.Provider)
|
||||
SetSettings(ctx context.Context, vpn configuration.VPN,
|
||||
provider configuration.Provider) (outcome string)
|
||||
}
|
||||
|
||||
func (s *State) GetSettings() (vpn configuration.VPN,
|
||||
provider configuration.Provider) {
|
||||
s.settingsMu.RLock()
|
||||
vpn = s.vpn
|
||||
provider = s.provider
|
||||
s.settingsMu.RUnlock()
|
||||
return vpn, provider
|
||||
}
|
||||
|
||||
func (s *State) SetSettings(ctx context.Context,
|
||||
vpn configuration.VPN, provider configuration.Provider) (
|
||||
outcome string) {
|
||||
s.settingsMu.Lock()
|
||||
settingsUnchanged := reflect.DeepEqual(s.vpn, vpn) &&
|
||||
reflect.DeepEqual(s.provider, provider)
|
||||
if settingsUnchanged {
|
||||
s.settingsMu.Unlock()
|
||||
return "settings left unchanged"
|
||||
}
|
||||
s.vpn = vpn
|
||||
s.provider = provider
|
||||
s.settingsMu.Unlock()
|
||||
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Stopped)
|
||||
outcome, _ = s.statusApplier.ApplyStatus(ctx, constants.Running)
|
||||
return outcome
|
||||
}
|
||||
16
internal/vpn/status.go
Normal file
16
internal/vpn/status.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func (l *Loop) GetStatus() (status models.LoopStatus) {
|
||||
return l.statusManager.GetStatus()
|
||||
}
|
||||
|
||||
func (l *Loop) ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
||||
outcome string, err error) {
|
||||
return l.statusManager.ApplyStatus(ctx, status)
|
||||
}
|
||||
46
internal/vpn/tunnelup.go
Normal file
46
internal/vpn/tunnelup.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package vpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
"github.com/qdm12/gluetun/internal/version"
|
||||
)
|
||||
|
||||
type tunnelUpData struct {
|
||||
// Port forwarding
|
||||
portForwarding bool
|
||||
serverName string
|
||||
portForwarder provider.PortForwarder
|
||||
}
|
||||
|
||||
func (l *Loop) onTunnelUp(ctx context.Context, data tunnelUpData) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
err = l.startPortForwarding(ctx, data.portForwarding, data.portForwarder, data.serverName)
|
||||
if err != nil {
|
||||
l.logger.Error(err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user