Set and get settings for openvpn
This commit is contained in:
@@ -21,12 +21,15 @@ type Looper interface {
|
|||||||
Run(ctx context.Context, wg *sync.WaitGroup)
|
Run(ctx context.Context, wg *sync.WaitGroup)
|
||||||
Restart()
|
Restart()
|
||||||
PortForward()
|
PortForward()
|
||||||
|
GetSettings() (settings settings.OpenVPN)
|
||||||
|
SetSettings(settings settings.OpenVPN)
|
||||||
}
|
}
|
||||||
|
|
||||||
type looper struct {
|
type looper struct {
|
||||||
// Variable parameters
|
// Variable parameters
|
||||||
provider models.VPNProvider
|
provider models.VPNProvider
|
||||||
settings settings.OpenVPN
|
settings settings.OpenVPN
|
||||||
|
settingsMutex sync.RWMutex
|
||||||
// Fixed parameters
|
// Fixed parameters
|
||||||
uid int
|
uid int
|
||||||
gid int
|
gid int
|
||||||
@@ -69,6 +72,18 @@ func NewLooper(provider models.VPNProvider, settings settings.OpenVPN,
|
|||||||
func (l *looper) Restart() { l.restart <- struct{}{} }
|
func (l *looper) Restart() { l.restart <- struct{}{} }
|
||||||
func (l *looper) PortForward() { l.portForwardSignals <- struct{}{} }
|
func (l *looper) PortForward() { l.portForwardSignals <- struct{}{} }
|
||||||
|
|
||||||
|
func (l *looper) GetSettings() (settings settings.OpenVPN) {
|
||||||
|
l.settingsMutex.RLock()
|
||||||
|
defer l.settingsMutex.RUnlock()
|
||||||
|
return l.settings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *looper) SetSettings(settings settings.OpenVPN) {
|
||||||
|
l.settingsMutex.Lock()
|
||||||
|
defer l.settingsMutex.Unlock()
|
||||||
|
l.settings = settings
|
||||||
|
}
|
||||||
|
|
||||||
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@@ -80,23 +95,24 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
defer l.logger.Warn("loop exited")
|
defer l.logger.Warn("loop exited")
|
||||||
|
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
|
settings := l.GetSettings()
|
||||||
providerConf := provider.New(l.provider)
|
providerConf := provider.New(l.provider)
|
||||||
connections, err := providerConf.GetOpenVPNConnections(l.settings.Provider.ServerSelection)
|
connections, err := providerConf.GetOpenVPNConnections(settings.Provider.ServerSelection)
|
||||||
l.fatalOnError(err)
|
l.fatalOnError(err)
|
||||||
lines := providerConf.BuildConf(
|
lines := providerConf.BuildConf(
|
||||||
connections,
|
connections,
|
||||||
l.settings.Verbosity,
|
settings.Verbosity,
|
||||||
l.uid,
|
l.uid,
|
||||||
l.gid,
|
l.gid,
|
||||||
l.settings.Root,
|
settings.Root,
|
||||||
l.settings.Cipher,
|
settings.Cipher,
|
||||||
l.settings.Auth,
|
settings.Auth,
|
||||||
l.settings.Provider.ExtraConfigOptions,
|
settings.Provider.ExtraConfigOptions,
|
||||||
)
|
)
|
||||||
err = l.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(l.uid, l.gid), files.Permissions(0400))
|
err = l.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(l.uid, l.gid), files.Permissions(0400))
|
||||||
l.fatalOnError(err)
|
l.fatalOnError(err)
|
||||||
|
|
||||||
err = l.conf.WriteAuthFile(l.settings.User, l.settings.Password, l.uid, l.gid)
|
err = l.conf.WriteAuthFile(settings.User, settings.Password, l.uid, l.gid)
|
||||||
l.fatalOnError(err)
|
l.fatalOnError(err)
|
||||||
|
|
||||||
if err := l.fw.SetVPNConnections(ctx, connections); err != nil {
|
if err := l.fw.SetVPNConnections(ctx, connections); err != nil {
|
||||||
@@ -158,7 +174,8 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *looper) portForward(ctx context.Context, providerConf provider.Provider, client network.Client) {
|
func (l *looper) portForward(ctx context.Context, providerConf provider.Provider, client network.Client) {
|
||||||
if !l.settings.Provider.PortForwarding.Enabled {
|
settings := l.GetSettings()
|
||||||
|
if !settings.Provider.PortForwarding.Enabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var port uint16
|
var port uint16
|
||||||
@@ -175,7 +192,7 @@ func (l *looper) portForward(ctx context.Context, providerConf provider.Provider
|
|||||||
l.logger.Info("port forwarded is %d", port)
|
l.logger.Info("port forwarded is %d", port)
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath := l.settings.Provider.PortForwarding.Filepath
|
filepath := settings.Provider.PortForwarding.Filepath
|
||||||
l.logger.Info("writing forwarded port to %s", filepath)
|
l.logger.Info("writing forwarded port to %s", filepath)
|
||||||
err = l.fileManager.WriteLinesToFile(
|
err = l.fileManager.WriteLinesToFile(
|
||||||
string(filepath), []string{fmt.Sprintf("%d", port)},
|
string(filepath), []string{fmt.Sprintf("%d", port)},
|
||||||
|
|||||||
Reference in New Issue
Block a user