Files
gluetun/internal/httpproxy/run.go
Quentin McGaw 7d824a5179 chore(settings): refactor settings processing (#756)
- Better settings tree structure logged using `qdm12/gotree`
- Read settings from environment variables, then files, then secret files
- Settings methods to default them, merge them and override them
- `DNS_PLAINTEXT_ADDRESS` default changed to `127.0.0.1` to use DoT. Warning added if set to something else.
- `HTTPPROXY_LISTENING_ADDRESS` instead of `HTTPPROXY_PORT` (with retro-compatibility)
2022-01-06 06:40:23 -05:00

76 lines
1.5 KiB
Go

package httpproxy
import (
"context"
"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{}) {
defer close(done)
if !*l.state.GetSettings().Enabled {
select {
case <-l.start:
case <-ctx.Done():
return
}
}
for ctx.Err() == nil {
runCtx, runCancel := context.WithCancel(ctx)
settings := l.state.GetSettings()
server := New(runCtx, settings.ListeningAddress, 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 l.userTrigger {
l.running <- constants.Running
l.userTrigger = false
} else {
l.backoffTime = defaultBackoffTime
l.statusManager.SetStatus(constants.Running)
}
stayHere := true
for stayHere {
select {
case <-ctx.Done():
runCancel()
<-errorCh
close(errorCh)
return
case <-l.start:
l.userTrigger = true
l.logger.Info("starting")
runCancel()
<-errorCh
close(errorCh)
stayHere = false
case <-l.stop:
l.userTrigger = true
l.logger.Info("stopping")
runCancel()
<-errorCh
// Do not close errorCh or this for loop won't work
l.stopped <- struct{}{}
case err := <-errorCh:
close(errorCh)
l.statusManager.SetStatus(constants.Crashed)
l.logAndWait(ctx, err)
stayHere = false
}
}
runCancel() // repetition for linter only
}
}