From 51af8d1ab0c50c206ce7b9dfc55007e3ae42d92b Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 16 Jul 2020 01:12:54 +0000 Subject: [PATCH] PUBLICIP_PERIOD environment variable --- README.md | 6 ++++ cmd/gluetun/main.go | 4 ++- internal/params/params.go | 3 ++ internal/params/publicip.go | 17 ++++++++++ internal/publicip/loop.go | 62 +++++++++++++++++++++++++++++++++-- internal/settings/settings.go | 21 ++++++++---- 6 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 internal/params/publicip.go diff --git a/README.md b/README.md index 437b8bc9..17d686de 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,12 @@ That one is important if you want to connect to the container from your LAN for | `UID` | `1000` | | User ID to run as non root and for ownership of files written | | `GID` | `1000` | | Group ID to run as non root and for ownership of files written | +### Other + +| Variable | Default | Choices | Description | +| --- | --- | --- | --- | +| `PUBLICIP_PERIOD` | `12h` | Valid duration | Period to check for public IP address. Set to `0` to disable. | + ## Connect to it There are various ways to achieve this, depending on your use case. diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index 455715db..01934fa2 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -137,10 +137,12 @@ func _main(background context.Context, args []string) int { // wait for restartUnbound go unboundLooper.Run(ctx, wg) - publicIPLooper := publicip.NewLooper(client, logger, fileManager, allSettings.System.IPStatusFilepath, uid, gid) + publicIPLooper := publicip.NewLooper(client, logger, fileManager, allSettings.System.IPStatusFilepath, allSettings.PublicIPPeriod, uid, gid) restartPublicIP := publicIPLooper.Restart + setPublicIPPeriod := publicIPLooper.SetPeriod go publicIPLooper.Run(ctx) go publicIPLooper.RunRestartTicker(ctx) + setPublicIPPeriod(allSettings.PublicIPPeriod) // call after RunRestartTicker tinyproxyLooper := tinyproxy.NewLooper(tinyProxyConf, firewallConf, allSettings.TinyProxy, logger, streamMerger, uid, gid) restartTinyproxy := tinyproxyLooper.Restart diff --git a/internal/params/params.go b/internal/params/params.go index 15f755ca..03290cf9 100644 --- a/internal/params/params.go +++ b/internal/params/params.go @@ -99,6 +99,9 @@ type Reader interface { GetTinyProxyUser() (user string, err error) GetTinyProxyPassword() (password string, err error) + // Public IP getters + GetPublicIPPeriod() (period time.Duration, err error) + // Version getters GetVersion() string GetBuildDate() string diff --git a/internal/params/publicip.go b/internal/params/publicip.go new file mode 100644 index 00000000..ab47a7f3 --- /dev/null +++ b/internal/params/publicip.go @@ -0,0 +1,17 @@ +package params + +import ( + "time" + + libparams "github.com/qdm12/golibs/params" +) + +// GetPublicIPPeriod obtains the period to fetch the IP address periodically. +// Set to 0 to disable +func (r *reader) GetPublicIPPeriod() (period time.Duration, err error) { + s, err := r.envParams.GetEnv("PUBLICIP_PERIOD", libparams.Default("12h")) + if err != nil { + return 0, err + } + return time.ParseDuration(s) +} diff --git a/internal/publicip/loop.go b/internal/publicip/loop.go index 6062e95a..e610ecd6 100644 --- a/internal/publicip/loop.go +++ b/internal/publicip/loop.go @@ -2,6 +2,7 @@ package publicip import ( "context" + "sync" "time" "github.com/qdm12/golibs/files" @@ -14,9 +15,14 @@ type Looper interface { Run(ctx context.Context) RunRestartTicker(ctx context.Context) Restart() + Stop() + GetPeriod() (period time.Duration) + SetPeriod(period time.Duration) } type looper struct { + period time.Duration + periodMutex sync.RWMutex getter IPGetter logger logging.Logger fileManager files.FileManager @@ -24,11 +30,16 @@ type looper struct { uid int gid int restart chan struct{} + stop chan struct{} + updateTimer chan struct{} + tickerReady bool + tickerReadyMutex sync.Mutex } func NewLooper(client network.Client, logger logging.Logger, fileManager files.FileManager, - ipStatusFilepath models.Filepath, uid, gid int) Looper { + ipStatusFilepath models.Filepath, period time.Duration, uid, gid int) Looper { return &looper{ + period: period, getter: NewIPGetter(client), logger: logger.WithPrefix("ip getter: "), fileManager: fileManager, @@ -36,10 +47,32 @@ func NewLooper(client network.Client, logger logging.Logger, fileManager files.F uid: uid, gid: gid, restart: make(chan struct{}), + stop: make(chan struct{}), + updateTimer: make(chan struct{}), } } func (l *looper) Restart() { l.restart <- struct{}{} } +func (l *looper) Stop() { l.stop <- struct{}{} } + +func (l *looper) GetPeriod() (period time.Duration) { + l.periodMutex.RLock() + defer l.periodMutex.RUnlock() + return l.period +} + +func (l *looper) SetPeriod(period time.Duration) { + l.tickerReadyMutex.Lock() + defer l.tickerReadyMutex.Unlock() + if !l.tickerReady { + l.logger.Error("cannot set period before ticker is started!") + return + } + l.periodMutex.Lock() + defer l.periodMutex.Unlock() + l.period = period + l.updateTimer <- struct{}{} +} func (l *looper) logAndWait(ctx context.Context, err error) { l.logger.Error(err) @@ -57,7 +90,23 @@ func (l *looper) Run(ctx context.Context) { } defer l.logger.Warn("loop exited") + enabled := true + for ctx.Err() == nil { + for !enabled { + // wait for a signal to re-enable + select { + case <-l.stop: + l.logger.Info("already disabled") + case <-l.restart: + enabled = true + case <-ctx.Done(): + return + } + } + + // Enabled and has a period set + ip, err := l.getter.Get() if err != nil { l.logAndWait(ctx, err) @@ -75,15 +124,19 @@ func (l *looper) Run(ctx context.Context) { } select { case <-l.restart: // triggered restart + case <-l.stop: + enabled = false case <-ctx.Done(): - l.logger.Warn("context canceled: exiting loop") return } } } func (l *looper) RunRestartTicker(ctx context.Context) { - ticker := time.NewTicker(time.Hour) + l.tickerReadyMutex.Lock() + l.tickerReady = true + l.tickerReadyMutex.Unlock() + ticker := time.NewTicker(l.GetPeriod()) for { select { case <-ctx.Done(): @@ -91,6 +144,9 @@ func (l *looper) RunRestartTicker(ctx context.Context) { return case <-ticker.C: l.restart <- struct{}{} + case <-l.updateTimer: + ticker.Stop() + ticker = time.NewTicker(l.GetPeriod()) } } } diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 0d9e4957..003cb90d 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -2,6 +2,7 @@ package settings import ( "strings" + "time" "github.com/qdm12/private-internet-access-docker/internal/models" "github.com/qdm12/private-internet-access-docker/internal/params" @@ -9,13 +10,14 @@ import ( // Settings contains all settings for the program to run type Settings struct { - VPNSP models.VPNProvider - OpenVPN OpenVPN - System System - DNS DNS - Firewall Firewall - TinyProxy TinyProxy - ShadowSocks ShadowSocks + VPNSP models.VPNProvider + OpenVPN OpenVPN + System System + DNS DNS + Firewall Firewall + TinyProxy TinyProxy + ShadowSocks ShadowSocks + PublicIPPeriod time.Duration } func (s *Settings) String() string { @@ -27,6 +29,7 @@ func (s *Settings) String() string { s.Firewall.String(), s.TinyProxy.String(), s.ShadowSocks.String(), + "Public IP check period: " + s.PublicIPPeriod.String(), "", // new line at the end }, "\n") } @@ -62,5 +65,9 @@ func GetAllSettings(paramsReader params.Reader) (settings Settings, err error) { if err != nil { return settings, err } + settings.PublicIPPeriod, err = paramsReader.GetPublicIPPeriod() + if err != nil { + return settings, err + } return settings, nil }