Tinyproxy set and get settings
This commit is contained in:
@@ -14,19 +14,24 @@ import (
|
|||||||
type Looper interface {
|
type Looper interface {
|
||||||
Run(ctx context.Context, wg *sync.WaitGroup)
|
Run(ctx context.Context, wg *sync.WaitGroup)
|
||||||
Restart()
|
Restart()
|
||||||
|
Start()
|
||||||
|
Stop()
|
||||||
|
GetSettings() (settings settings.TinyProxy)
|
||||||
|
SetSettings(settings settings.TinyProxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
type looper struct {
|
type looper struct {
|
||||||
conf Configurator
|
conf Configurator
|
||||||
firewallConf firewall.Configurator
|
firewallConf firewall.Configurator
|
||||||
settings settings.TinyProxy
|
settings settings.TinyProxy
|
||||||
logger logging.Logger
|
settingsMutex sync.RWMutex
|
||||||
streamMerger command.StreamMerger
|
logger logging.Logger
|
||||||
uid int
|
streamMerger command.StreamMerger
|
||||||
gid int
|
uid int
|
||||||
restart chan struct{}
|
gid int
|
||||||
start chan struct{}
|
restart chan struct{}
|
||||||
stop chan struct{}
|
start chan struct{}
|
||||||
|
stop chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *looper) logAndWait(ctx context.Context, err error) {
|
func (l *looper) logAndWait(ctx context.Context, err error) {
|
||||||
@@ -53,6 +58,30 @@ func NewLooper(conf Configurator, firewallConf firewall.Configurator, settings s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *looper) GetSettings() (settings settings.TinyProxy) {
|
||||||
|
l.settingsMutex.RLock()
|
||||||
|
defer l.settingsMutex.RUnlock()
|
||||||
|
return l.settings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *looper) SetSettings(settings settings.TinyProxy) {
|
||||||
|
l.settingsMutex.Lock()
|
||||||
|
defer l.settingsMutex.Unlock()
|
||||||
|
l.settings = settings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *looper) isEnabled() bool {
|
||||||
|
l.settingsMutex.RLock()
|
||||||
|
defer l.settingsMutex.RUnlock()
|
||||||
|
return l.settings.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *looper) setEnabled(enabled bool) {
|
||||||
|
l.settingsMutex.Lock()
|
||||||
|
defer l.settingsMutex.Unlock()
|
||||||
|
l.settings.Enabled = enabled
|
||||||
|
}
|
||||||
|
|
||||||
func (l *looper) Restart() { l.restart <- struct{}{} }
|
func (l *looper) Restart() { l.restart <- struct{}{} }
|
||||||
func (l *looper) Start() { l.start <- struct{}{} }
|
func (l *looper) Start() { l.start <- struct{}{} }
|
||||||
func (l *looper) Stop() { l.stop <- struct{}{} }
|
func (l *looper) Stop() { l.stop <- struct{}{} }
|
||||||
@@ -77,27 +106,22 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
|
|
||||||
var previousPort uint16
|
var previousPort uint16
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
for !l.settings.Enabled {
|
for !l.isEnabled() {
|
||||||
// wait for a signal to re-enable
|
// wait for a signal to re-enable
|
||||||
select {
|
select {
|
||||||
case <-l.stop:
|
case <-l.stop:
|
||||||
l.logger.Info("already disabled")
|
l.logger.Info("already disabled")
|
||||||
case <-l.restart:
|
case <-l.restart:
|
||||||
l.settings.Enabled = true
|
l.setEnabled(true)
|
||||||
case <-l.start:
|
case <-l.start:
|
||||||
l.settings.Enabled = true
|
l.setEnabled(true)
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := l.conf.MakeConf(
|
settings := l.GetSettings()
|
||||||
l.settings.LogLevel,
|
err := l.conf.MakeConf(settings.LogLevel, settings.Port, settings.User, settings.Password, l.uid, l.gid)
|
||||||
l.settings.Port,
|
|
||||||
l.settings.User,
|
|
||||||
l.settings.Password,
|
|
||||||
l.uid,
|
|
||||||
l.gid)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
continue
|
continue
|
||||||
@@ -109,11 +133,11 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := l.firewallConf.SetAllowedPort(ctx, l.settings.Port); err != nil {
|
if err := l.firewallConf.SetAllowedPort(ctx, settings.Port); err != nil {
|
||||||
l.logger.Error(err)
|
l.logger.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
previousPort = l.settings.Port
|
previousPort = settings.Port
|
||||||
|
|
||||||
tinyproxyCtx, tinyproxyCancel := context.WithCancel(context.Background())
|
tinyproxyCtx, tinyproxyCancel := context.WithCancel(context.Background())
|
||||||
stream, waitFn, err := l.conf.Start(tinyproxyCtx)
|
stream, waitFn, err := l.conf.Start(tinyproxyCtx)
|
||||||
@@ -150,7 +174,7 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
tinyproxyCancel()
|
tinyproxyCancel()
|
||||||
<-waitError
|
<-waitError
|
||||||
close(waitError)
|
close(waitError)
|
||||||
l.settings.Enabled = false
|
l.setEnabled(false)
|
||||||
stayHere = false
|
stayHere = false
|
||||||
case err := <-waitError: // unexpected error
|
case err := <-waitError: // unexpected error
|
||||||
tinyproxyCancel()
|
tinyproxyCancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user