Files
gluetun/internal/openvpn/run.go

106 lines
2.6 KiB
Go
Raw Normal View History

2021-07-23 20:46:57 +00:00
package openvpn
import (
"context"
"time"
"github.com/qdm12/gluetun/internal/constants"
"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{}) {
2021-07-23 20:46:57 +00:00
defer close(done)
select {
case <-l.start:
case <-ctx.Done():
return
}
for ctx.Err() == nil {
openVPNSettings, providerSettings, allServers := l.state.GetSettingsAndServers()
2021-07-23 20:46:57 +00:00
providerConf := provider.New(providerSettings.Name, allServers, time.Now)
2021-07-23 20:46:57 +00:00
2021-08-18 20:43:47 +00:00
serverName, err := setup(ctx, l.fw, l.openvpnConf, providerConf, openVPNSettings, providerSettings)
if err != nil {
l.crashed(ctx, err)
continue
2021-07-23 20:46:57 +00:00
}
openvpnCtx, openvpnCancel := context.WithCancel(context.Background())
stdoutLines, stderrLines, waitError, err := l.openvpnConf.Start(
openvpnCtx, openVPNSettings.Version, openVPNSettings.Flags)
2021-07-23 20:46:57 +00:00
if err != nil {
openvpnCancel()
l.crashed(ctx, err)
2021-07-23 20:46:57 +00:00
continue
}
linesCollectionCtx, linesCollectionCancel := context.WithCancel(context.Background())
2021-07-23 20:46:57 +00:00
lineCollectionDone := make(chan struct{})
tunnelUpData := tunnelUpData{
portForwarding: providerSettings.PortForwarding.Enabled,
2021-08-18 20:43:47 +00:00
serverName: serverName,
portForwarder: providerConf,
}
go l.collectLines(linesCollectionCtx, lineCollectionDone,
stdoutLines, stderrLines, tunnelUpData)
2021-07-23 20:46:57 +00:00
closeStreams := func() {
linesCollectionCancel()
2021-07-23 20:46:57 +00:00
<-lineCollectionDone
}
l.backoffTime = defaultBackoffTime
l.signalOrSetStatus(constants.Running)
stayHere := true
for stayHere {
select {
case <-ctx.Done():
const pfTimeout = 100 * time.Millisecond
l.stopPortForwarding(context.Background(),
providerSettings.PortForwarding.Enabled, pfTimeout)
2021-07-23 20:46:57 +00:00
openvpnCancel()
<-waitError
close(waitError)
closeStreams()
return
case <-l.stop:
l.userTrigger = true
l.logger.Info("stopping")
l.stopPortForwarding(ctx, providerSettings.PortForwarding.Enabled, 0)
2021-07-23 20:46:57 +00:00
openvpnCancel()
<-waitError
// do not close waitError or the waitError
// select case will trigger
closeStreams()
l.stopped <- struct{}{}
case <-l.start:
l.userTrigger = true
l.logger.Info("starting")
stayHere = false
case err := <-waitError: // unexpected error
close(waitError)
closeStreams()
l.statusManager.Lock() // prevent SetStatus from running in parallel
l.stopPortForwarding(ctx, providerSettings.PortForwarding.Enabled, 0)
2021-07-23 20:46:57 +00:00
openvpnCancel()
l.statusManager.SetStatus(constants.Crashed)
l.logAndWait(ctx, err)
stayHere = false
l.statusManager.Unlock()
}
}
openvpnCancel()
}
}