2021-07-23 19:25:48 +00:00
|
|
|
package httpproxy
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-24 18:50:17 +00:00
|
|
|
type Runner interface {
|
|
|
|
|
Run(ctx context.Context, done chan<- struct{})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 18:52:19 +00:00
|
|
|
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
2021-07-23 19:25:48 +00:00
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
|
|
crashed := false
|
|
|
|
|
|
2021-07-24 18:52:19 +00:00
|
|
|
if l.state.GetSettings().Enabled {
|
2021-07-23 19:25:48 +00:00
|
|
|
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)
|
|
|
|
|
|
2021-07-24 18:52:19 +00:00
|
|
|
settings := l.state.GetSettings()
|
2021-07-23 19:25:48 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|