PUBLICIP_PERIOD environment variable

This commit is contained in:
Quentin McGaw
2020-07-16 01:12:54 +00:00
parent 002ffacd35
commit 51af8d1ab0
6 changed files with 102 additions and 11 deletions

View File

@@ -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 | | `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 | | `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 ## Connect to it
There are various ways to achieve this, depending on your use case. There are various ways to achieve this, depending on your use case.

View File

@@ -137,10 +137,12 @@ func _main(background context.Context, args []string) int {
// wait for restartUnbound // wait for restartUnbound
go unboundLooper.Run(ctx, wg) 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 restartPublicIP := publicIPLooper.Restart
setPublicIPPeriod := publicIPLooper.SetPeriod
go publicIPLooper.Run(ctx) go publicIPLooper.Run(ctx)
go publicIPLooper.RunRestartTicker(ctx) go publicIPLooper.RunRestartTicker(ctx)
setPublicIPPeriod(allSettings.PublicIPPeriod) // call after RunRestartTicker
tinyproxyLooper := tinyproxy.NewLooper(tinyProxyConf, firewallConf, allSettings.TinyProxy, logger, streamMerger, uid, gid) tinyproxyLooper := tinyproxy.NewLooper(tinyProxyConf, firewallConf, allSettings.TinyProxy, logger, streamMerger, uid, gid)
restartTinyproxy := tinyproxyLooper.Restart restartTinyproxy := tinyproxyLooper.Restart

View File

@@ -99,6 +99,9 @@ type Reader interface {
GetTinyProxyUser() (user string, err error) GetTinyProxyUser() (user string, err error)
GetTinyProxyPassword() (password string, err error) GetTinyProxyPassword() (password string, err error)
// Public IP getters
GetPublicIPPeriod() (period time.Duration, err error)
// Version getters // Version getters
GetVersion() string GetVersion() string
GetBuildDate() string GetBuildDate() string

View File

@@ -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)
}

View File

@@ -2,6 +2,7 @@ package publicip
import ( import (
"context" "context"
"sync"
"time" "time"
"github.com/qdm12/golibs/files" "github.com/qdm12/golibs/files"
@@ -14,9 +15,14 @@ type Looper interface {
Run(ctx context.Context) Run(ctx context.Context)
RunRestartTicker(ctx context.Context) RunRestartTicker(ctx context.Context)
Restart() Restart()
Stop()
GetPeriod() (period time.Duration)
SetPeriod(period time.Duration)
} }
type looper struct { type looper struct {
period time.Duration
periodMutex sync.RWMutex
getter IPGetter getter IPGetter
logger logging.Logger logger logging.Logger
fileManager files.FileManager fileManager files.FileManager
@@ -24,11 +30,16 @@ type looper struct {
uid int uid int
gid int gid int
restart chan struct{} 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, 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{ return &looper{
period: period,
getter: NewIPGetter(client), getter: NewIPGetter(client),
logger: logger.WithPrefix("ip getter: "), logger: logger.WithPrefix("ip getter: "),
fileManager: fileManager, fileManager: fileManager,
@@ -36,10 +47,32 @@ func NewLooper(client network.Client, logger logging.Logger, fileManager files.F
uid: uid, uid: uid,
gid: gid, gid: gid,
restart: make(chan struct{}), restart: make(chan struct{}),
stop: make(chan struct{}),
updateTimer: make(chan struct{}),
} }
} }
func (l *looper) Restart() { l.restart <- 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) { func (l *looper) logAndWait(ctx context.Context, err error) {
l.logger.Error(err) l.logger.Error(err)
@@ -57,7 +90,23 @@ func (l *looper) Run(ctx context.Context) {
} }
defer l.logger.Warn("loop exited") defer l.logger.Warn("loop exited")
enabled := true
for ctx.Err() == nil { 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() ip, err := l.getter.Get()
if err != nil { if err != nil {
l.logAndWait(ctx, err) l.logAndWait(ctx, err)
@@ -75,15 +124,19 @@ func (l *looper) Run(ctx context.Context) {
} }
select { select {
case <-l.restart: // triggered restart case <-l.restart: // triggered restart
case <-l.stop:
enabled = false
case <-ctx.Done(): case <-ctx.Done():
l.logger.Warn("context canceled: exiting loop")
return return
} }
} }
} }
func (l *looper) RunRestartTicker(ctx context.Context) { 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 { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@@ -91,6 +144,9 @@ func (l *looper) RunRestartTicker(ctx context.Context) {
return return
case <-ticker.C: case <-ticker.C:
l.restart <- struct{}{} l.restart <- struct{}{}
case <-l.updateTimer:
ticker.Stop()
ticker = time.NewTicker(l.GetPeriod())
} }
} }
} }

View File

@@ -2,6 +2,7 @@ package settings
import ( import (
"strings" "strings"
"time"
"github.com/qdm12/private-internet-access-docker/internal/models" "github.com/qdm12/private-internet-access-docker/internal/models"
"github.com/qdm12/private-internet-access-docker/internal/params" "github.com/qdm12/private-internet-access-docker/internal/params"
@@ -9,13 +10,14 @@ import (
// Settings contains all settings for the program to run // Settings contains all settings for the program to run
type Settings struct { type Settings struct {
VPNSP models.VPNProvider VPNSP models.VPNProvider
OpenVPN OpenVPN OpenVPN OpenVPN
System System System System
DNS DNS DNS DNS
Firewall Firewall Firewall Firewall
TinyProxy TinyProxy TinyProxy TinyProxy
ShadowSocks ShadowSocks ShadowSocks ShadowSocks
PublicIPPeriod time.Duration
} }
func (s *Settings) String() string { func (s *Settings) String() string {
@@ -27,6 +29,7 @@ func (s *Settings) String() string {
s.Firewall.String(), s.Firewall.String(),
s.TinyProxy.String(), s.TinyProxy.String(),
s.ShadowSocks.String(), s.ShadowSocks.String(),
"Public IP check period: " + s.PublicIPPeriod.String(),
"", // new line at the end "", // new line at the end
}, "\n") }, "\n")
} }
@@ -62,5 +65,9 @@ func GetAllSettings(paramsReader params.Reader) (settings Settings, err error) {
if err != nil { if err != nil {
return settings, err return settings, err
} }
settings.PublicIPPeriod, err = paramsReader.GetPublicIPPeriod()
if err != nil {
return settings, err
}
return settings, nil return settings, nil
} }