2020-07-08 22:33:28 +00:00
|
|
|
package publicip
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-07-16 01:12:54 +00:00
|
|
|
"sync"
|
2020-07-08 22:33:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2020-07-08 22:33:28 +00:00
|
|
|
"github.com/qdm12/golibs/files"
|
|
|
|
|
"github.com/qdm12/golibs/logging"
|
|
|
|
|
"github.com/qdm12/golibs/network"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Looper interface {
|
2020-09-12 14:34:15 -04:00
|
|
|
Run(ctx context.Context, wg *sync.WaitGroup)
|
|
|
|
|
RunRestartTicker(ctx context.Context, wg *sync.WaitGroup)
|
2020-07-15 01:34:46 +00:00
|
|
|
Restart()
|
2020-07-16 01:12:54 +00:00
|
|
|
Stop()
|
|
|
|
|
GetPeriod() (period time.Duration)
|
|
|
|
|
SetPeriod(period time.Duration)
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type looper struct {
|
2020-07-16 01:12:54 +00:00
|
|
|
period time.Duration
|
|
|
|
|
periodMutex sync.RWMutex
|
2020-07-08 22:33:28 +00:00
|
|
|
getter IPGetter
|
|
|
|
|
logger logging.Logger
|
|
|
|
|
fileManager files.FileManager
|
|
|
|
|
ipStatusFilepath models.Filepath
|
|
|
|
|
uid int
|
|
|
|
|
gid int
|
2020-07-15 01:34:46 +00:00
|
|
|
restart chan struct{}
|
2020-07-16 01:12:54 +00:00
|
|
|
stop chan struct{}
|
2020-07-16 01:44:48 +00:00
|
|
|
updateTicker chan struct{}
|
2020-10-15 23:20:36 +00:00
|
|
|
timeNow func() time.Time
|
|
|
|
|
timeSince func(time.Time) time.Duration
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLooper(client network.Client, logger logging.Logger, fileManager files.FileManager,
|
2020-07-16 01:12:54 +00:00
|
|
|
ipStatusFilepath models.Filepath, period time.Duration, uid, gid int) Looper {
|
2020-07-08 22:33:28 +00:00
|
|
|
return &looper{
|
2020-07-16 01:12:54 +00:00
|
|
|
period: period,
|
2020-07-08 22:33:28 +00:00
|
|
|
getter: NewIPGetter(client),
|
|
|
|
|
logger: logger.WithPrefix("ip getter: "),
|
|
|
|
|
fileManager: fileManager,
|
|
|
|
|
ipStatusFilepath: ipStatusFilepath,
|
|
|
|
|
uid: uid,
|
|
|
|
|
gid: gid,
|
2020-07-15 01:34:46 +00:00
|
|
|
restart: make(chan struct{}),
|
2020-07-16 01:12:54 +00:00
|
|
|
stop: make(chan struct{}),
|
2020-07-16 01:44:48 +00:00
|
|
|
updateTicker: make(chan struct{}),
|
2020-10-15 23:20:36 +00:00
|
|
|
timeNow: time.Now,
|
|
|
|
|
timeSince: time.Since,
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 01:34:46 +00:00
|
|
|
func (l *looper) Restart() { l.restart <- struct{}{} }
|
2020-07-16 01:12:54 +00:00
|
|
|
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.periodMutex.Lock()
|
|
|
|
|
l.period = period
|
2020-07-16 01:44:48 +00:00
|
|
|
l.periodMutex.Unlock()
|
|
|
|
|
l.updateTicker <- struct{}{}
|
2020-07-16 01:12:54 +00:00
|
|
|
}
|
2020-07-15 01:34:46 +00:00
|
|
|
|
2020-07-11 20:59:30 +00:00
|
|
|
func (l *looper) logAndWait(ctx context.Context, err error) {
|
2020-07-08 22:33:28 +00:00
|
|
|
l.logger.Error(err)
|
|
|
|
|
l.logger.Info("retrying in 5 seconds")
|
2020-07-11 20:59:30 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel() // just for the linter
|
|
|
|
|
<-ctx.Done()
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-12 14:34:15 -04:00
|
|
|
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
|
defer wg.Done()
|
2020-07-08 22:33:28 +00:00
|
|
|
select {
|
2020-07-15 01:34:46 +00:00
|
|
|
case <-l.restart:
|
2020-07-08 22:33:28 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-11 20:59:30 +00:00
|
|
|
defer l.logger.Warn("loop exited")
|
|
|
|
|
|
2020-07-16 01:12:54 +00:00
|
|
|
enabled := true
|
|
|
|
|
|
2020-07-11 20:59:30 +00:00
|
|
|
for ctx.Err() == nil {
|
2020-07-16 01:12:54 +00:00
|
|
|
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
|
|
|
|
|
|
2020-07-08 22:33:28 +00:00
|
|
|
ip, err := l.getter.Get()
|
|
|
|
|
if err != nil {
|
2020-07-11 20:59:30 +00:00
|
|
|
l.logAndWait(ctx, err)
|
2020-07-08 22:33:28 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
l.logger.Info("Public IP address is %s", ip)
|
|
|
|
|
err = l.fileManager.WriteLinesToFile(
|
|
|
|
|
string(l.ipStatusFilepath),
|
|
|
|
|
[]string{ip.String()},
|
|
|
|
|
files.Ownership(l.uid, l.gid),
|
|
|
|
|
files.Permissions(0600))
|
|
|
|
|
if err != nil {
|
2020-07-11 20:59:30 +00:00
|
|
|
l.logAndWait(ctx, err)
|
2020-07-08 22:33:28 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
select {
|
2020-07-15 01:34:46 +00:00
|
|
|
case <-l.restart: // triggered restart
|
2020-07-16 01:12:54 +00:00
|
|
|
case <-l.stop:
|
|
|
|
|
enabled = false
|
2020-07-08 22:33:28 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-08 22:47:12 +00:00
|
|
|
|
2020-09-12 14:34:15 -04:00
|
|
|
func (l *looper) RunRestartTicker(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
|
defer wg.Done()
|
2020-10-15 23:20:36 +00:00
|
|
|
timer := time.NewTimer(time.Hour)
|
|
|
|
|
timer.Stop() // 1 hour, cannot be a race condition
|
|
|
|
|
timerIsStopped := true
|
2020-07-16 01:44:48 +00:00
|
|
|
period := l.GetPeriod()
|
|
|
|
|
if period > 0 {
|
2020-10-15 23:20:36 +00:00
|
|
|
timer.Reset(period)
|
|
|
|
|
timerIsStopped = false
|
2020-07-16 01:44:48 +00:00
|
|
|
}
|
2020-10-15 23:20:36 +00:00
|
|
|
lastTick := time.Unix(0, 0)
|
2020-07-08 22:47:12 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
2020-10-15 23:20:36 +00:00
|
|
|
if !timerIsStopped && !timer.Stop() {
|
|
|
|
|
<-timer.C
|
|
|
|
|
}
|
2020-07-08 22:47:12 +00:00
|
|
|
return
|
2020-10-15 23:20:36 +00:00
|
|
|
case <-timer.C:
|
|
|
|
|
lastTick = l.timeNow()
|
2020-07-15 01:34:46 +00:00
|
|
|
l.restart <- struct{}{}
|
2020-10-15 23:20:36 +00:00
|
|
|
timer.Reset(l.GetPeriod())
|
2020-07-16 01:44:48 +00:00
|
|
|
case <-l.updateTicker:
|
2020-10-15 23:20:36 +00:00
|
|
|
if !timer.Stop() {
|
|
|
|
|
<-timer.C
|
|
|
|
|
}
|
|
|
|
|
timerIsStopped = true
|
|
|
|
|
period := l.GetPeriod()
|
|
|
|
|
if period == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var waited time.Duration
|
|
|
|
|
if lastTick.UnixNano() > 0 {
|
|
|
|
|
waited = l.timeSince(lastTick)
|
|
|
|
|
}
|
|
|
|
|
leftToWait := period - waited
|
|
|
|
|
timer.Reset(leftToWait)
|
|
|
|
|
timerIsStopped = false
|
2020-07-08 22:47:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|