Files
gluetun/internal/httpproxy/run.go

73 lines
1.4 KiB
Go
Raw Normal View History

2021-07-23 19:25:48 +00:00
package httpproxy
import (
"context"
"fmt"
"github.com/qdm12/gluetun/internal/constants"
)
func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
crashed := false
if l.GetSettings().Enabled {
go func() {
2021-07-23 20:47:36 +00:00
_, _ = l.statusManager.ApplyStatus(ctx, constants.Running)
2021-07-23 19:25:48 +00:00
}()
}
select {
case <-l.start:
case <-ctx.Done():
return
}
for ctx.Err() == nil {
runCtx, runCancel := context.WithCancel(ctx)
settings := l.GetSettings()
address := fmt.Sprintf(":%d", settings.Port)
server := New(runCtx, address, l.logger, settings.Stealth, settings.Log, settings.User, settings.Password)
errorCh := make(chan error)
go server.Run(runCtx, errorCh)
// TODO stable timer, check Shadowsocks
if !crashed {
l.running <- constants.Running
crashed = false
} else {
l.backoffTime = defaultBackoffTime
2021-07-23 20:47:36 +00:00
l.statusManager.SetStatus(constants.Running)
2021-07-23 19:25:48 +00:00
}
stayHere := true
for stayHere {
select {
case <-ctx.Done():
runCancel()
<-errorCh
return
case <-l.start:
l.logger.Info("starting")
runCancel()
<-errorCh
stayHere = false
case <-l.stop:
l.logger.Info("stopping")
runCancel()
<-errorCh
l.stopped <- struct{}{}
case err := <-errorCh:
2021-07-23 20:47:36 +00:00
l.statusManager.SetStatus(constants.Crashed)
2021-07-23 19:25:48 +00:00
l.logAndWait(ctx, err)
crashed = true
stayHere = false
}
}
runCancel() // repetition for linter only
}
}