PUBLICIP_PERIOD environment variable
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
17
internal/params/publicip.go
Normal file
17
internal/params/publicip.go
Normal 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)
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user