Files
gluetun/internal/httpproxy/run.go

76 lines
1.5 KiB
Go
Raw Normal View History

2021-07-23 19:25:48 +00:00
package httpproxy
import (
"context"
2021-07-26 01:42:37 +00:00
"strconv"
2021-07-23 19:25:48 +00:00
"github.com/qdm12/gluetun/internal/constants"
)
type Runner interface {
Run(ctx context.Context, done chan<- struct{})
}
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
2021-07-23 19:25:48 +00:00
defer close(done)
2021-07-26 01:42:37 +00:00
if !l.state.GetSettings().Enabled {
select {
case <-l.start:
case <-ctx.Done():
return
}
2021-07-23 19:25:48 +00:00
}
for ctx.Err() == nil {
runCtx, runCancel := context.WithCancel(ctx)
settings := l.state.GetSettings()
2021-07-26 01:42:37 +00:00
address := ":" + strconv.Itoa(int(settings.Port))
2021-07-23 19:25:48 +00:00
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
2021-07-26 01:42:37 +00:00
if l.userTrigger {
2021-07-23 19:25:48 +00:00
l.running <- constants.Running
2021-07-26 01:42:37 +00:00
l.userTrigger = false
2021-07-23 19:25:48 +00:00
} 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
2021-07-26 01:42:37 +00:00
close(errorCh)
2021-07-23 19:25:48 +00:00
return
case <-l.start:
2021-07-26 01:42:37 +00:00
l.userTrigger = true
2021-07-23 19:25:48 +00:00
l.logger.Info("starting")
runCancel()
<-errorCh
2021-07-26 01:42:37 +00:00
close(errorCh)
2021-07-23 19:25:48 +00:00
stayHere = false
case <-l.stop:
2021-07-26 01:42:37 +00:00
l.userTrigger = true
2021-07-23 19:25:48 +00:00
l.logger.Info("stopping")
runCancel()
<-errorCh
2021-07-26 01:42:37 +00:00
// Do not close errorCh or this for loop won't work
2021-07-23 19:25:48 +00:00
l.stopped <- struct{}{}
case err := <-errorCh:
2021-07-26 01:42:37 +00:00
close(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)
stayHere = false
}
}
runCancel() // repetition for linter only
}
}