diff --git a/internal/dns/logs.go b/internal/dns/logs.go index d136ef06..53347f7b 100644 --- a/internal/dns/logs.go +++ b/internal/dns/logs.go @@ -8,7 +8,7 @@ import ( "github.com/qdm12/golibs/logging" ) -func (l *looper) collectLines(stdout, stderr <-chan string, done chan<- struct{}) { +func (l *Loop) collectLines(stdout, stderr <-chan string, done chan<- struct{}) { defer close(done) var line string var ok bool diff --git a/internal/dns/loop.go b/internal/dns/loop.go index a5b7132b..c732ebf9 100644 --- a/internal/dns/loop.go +++ b/internal/dns/loop.go @@ -14,18 +14,16 @@ import ( "github.com/qdm12/golibs/logging" ) +var _ Looper = (*Loop)(nil) + type Looper interface { - Run(ctx context.Context, done chan<- struct{}) - RunRestartTicker(ctx context.Context, done chan<- struct{}) - GetStatus() (status models.LoopStatus) - ApplyStatus(ctx context.Context, status models.LoopStatus) ( - outcome string, err error) - GetSettings() (settings configuration.DNS) - SetSettings(ctx context.Context, settings configuration.DNS) ( - outcome string) + Runner + RestartTickerRunner + StatusGetterApplier + SettingsGetterSetter } -type looper struct { +type Loop struct { state *state conf unbound.Configurator resolvConf string @@ -46,7 +44,7 @@ type looper struct { const defaultBackoffTime = 10 * time.Second func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http.Client, - logger logging.Logger) Looper { + logger logging.Logger) *Loop { start := make(chan struct{}) running := make(chan models.LoopStatus) stop := make(chan struct{}) @@ -55,7 +53,7 @@ func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http state := newState(constants.Stopped, settings, start, running, stop, stopped, updateTicker) - return &looper{ + return &Loop{ state: state, conf: conf, resolvConf: "/etc/resolv.conf", @@ -74,7 +72,7 @@ func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http } } -func (l *looper) logAndWait(ctx context.Context, err error) { +func (l *Loop) logAndWait(ctx context.Context, err error) { if err != nil { l.logger.Warn(err.Error()) } @@ -90,7 +88,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) { } } -func (l *looper) signalOrSetStatus(status models.LoopStatus) { +func (l *Loop) signalOrSetStatus(status models.LoopStatus) { if l.userTrigger { l.userTrigger = false select { diff --git a/internal/dns/plaintext.go b/internal/dns/plaintext.go index aa59960e..212d459b 100644 --- a/internal/dns/plaintext.go +++ b/internal/dns/plaintext.go @@ -2,7 +2,7 @@ package dns import "github.com/qdm12/dns/pkg/nameserver" -func (l *looper) useUnencryptedDNS(fallback bool) { +func (l *Loop) useUnencryptedDNS(fallback bool) { settings := l.GetSettings() // Try with user provided plaintext ip address diff --git a/internal/dns/run.go b/internal/dns/run.go index be08232c..58ddfa3f 100644 --- a/internal/dns/run.go +++ b/internal/dns/run.go @@ -7,7 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants" ) -func (l *looper) Run(ctx context.Context, done chan<- struct{}) { +type Runner interface { + Run(ctx context.Context, done chan<- struct{}) +} + +func (l *Loop) Run(ctx context.Context, done chan<- struct{}) { defer close(done) const fallback = false diff --git a/internal/dns/settings.go b/internal/dns/settings.go index 363d0d29..7d4a8dac 100644 --- a/internal/dns/settings.go +++ b/internal/dns/settings.go @@ -6,14 +6,23 @@ import ( "github.com/qdm12/gluetun/internal/configuration" ) -func (l *looper) GetSettings() (settings configuration.DNS) { return l.state.GetSettings() } +type SettingsGetterSetter interface { + SettingsGetter + SettingsSetter +} + +type SettingsGetter interface { + GetSettings() (settings configuration.DNS) +} + +func (l *Loop) GetSettings() (settings configuration.DNS) { return l.state.GetSettings() } type SettingsSetter interface { SetSettings(ctx context.Context, settings configuration.DNS) ( outcome string) } -func (l *looper) SetSettings(ctx context.Context, settings configuration.DNS) ( +func (l *Loop) SetSettings(ctx context.Context, settings configuration.DNS) ( outcome string) { return l.state.SetSettings(ctx, settings) } diff --git a/internal/dns/setup.go b/internal/dns/setup.go index fbeeaa6e..1bb84f74 100644 --- a/internal/dns/setup.go +++ b/internal/dns/setup.go @@ -14,7 +14,7 @@ var errUpdateFiles = errors.New("cannot update files") // Returning cancel == nil signals we want to re-run setupUnbound // Returning err == errUpdateFiles signals we should not fall back // on the plaintext DNS as DOT is still up and running. -func (l *looper) setupUnbound(ctx context.Context) ( +func (l *Loop) setupUnbound(ctx context.Context) ( cancel context.CancelFunc, waitError chan error, closeStreams func(), err error) { err = l.updateFiles(ctx) if err != nil { diff --git a/internal/dns/status.go b/internal/dns/status.go index 353e9316..ee492ab9 100644 --- a/internal/dns/status.go +++ b/internal/dns/status.go @@ -6,14 +6,23 @@ import ( "github.com/qdm12/gluetun/internal/models" ) -func (l *looper) GetStatus() (status models.LoopStatus) { return l.state.GetStatus() } +type StatusGetterApplier interface { + StatusGetter + StatusApplier +} + +type StatusGetter interface { + GetStatus() (status models.LoopStatus) +} + +func (l *Loop) GetStatus() (status models.LoopStatus) { return l.state.GetStatus() } type StatusApplier interface { ApplyStatus(ctx context.Context, status models.LoopStatus) ( outcome string, err error) } -func (l *looper) ApplyStatus(ctx context.Context, status models.LoopStatus) ( +func (l *Loop) ApplyStatus(ctx context.Context, status models.LoopStatus) ( outcome string, err error) { return l.state.ApplyStatus(ctx, status) } diff --git a/internal/dns/ticker.go b/internal/dns/ticker.go index 9c5a1352..4918903b 100644 --- a/internal/dns/ticker.go +++ b/internal/dns/ticker.go @@ -7,7 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants" ) -func (l *looper) RunRestartTicker(ctx context.Context, done chan<- struct{}) { +type RestartTickerRunner interface { + RunRestartTicker(ctx context.Context, done chan<- struct{}) +} + +func (l *Loop) RunRestartTicker(ctx context.Context, done chan<- struct{}) { defer close(done) // Timer that acts as a ticker timer := time.NewTimer(time.Hour) diff --git a/internal/dns/update.go b/internal/dns/update.go index 3f10acbf..60b10d9d 100644 --- a/internal/dns/update.go +++ b/internal/dns/update.go @@ -2,7 +2,7 @@ package dns import "context" -func (l *looper) updateFiles(ctx context.Context) (err error) { +func (l *Loop) updateFiles(ctx context.Context) (err error) { l.logger.Info("downloading DNS over TLS cryptographic files") if err := l.conf.SetupFiles(ctx); err != nil { return err