Maint: decouple OpenVPN config writer from loop
This commit is contained in:
@@ -5,8 +5,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *Loop) writeOpenvpnConf(lines []string) error {
|
type ConfigWriter interface {
|
||||||
file, err := os.OpenFile(l.targetConfPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
WriteConfig(lines []string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *configurator) WriteConfig(lines []string) error {
|
||||||
|
file, err := os.OpenFile(c.configPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,11 +34,10 @@ type Loop struct {
|
|||||||
username string
|
username string
|
||||||
puid int
|
puid int
|
||||||
pgid int
|
pgid int
|
||||||
targetConfPath string
|
|
||||||
buildInfo models.BuildInformation
|
buildInfo models.BuildInformation
|
||||||
versionInfo bool
|
versionInfo bool
|
||||||
// Configurators
|
// Configurators
|
||||||
conf StarterAuthWriter
|
conf Configurator
|
||||||
fw firewallConfigurer
|
fw firewallConfigurer
|
||||||
routing routing.VPNGetter
|
routing routing.VPNGetter
|
||||||
portForward portforward.StartStopper
|
portForward portforward.StartStopper
|
||||||
@@ -90,7 +89,6 @@ func NewLoop(openVPNSettings configuration.OpenVPN,
|
|||||||
username: username,
|
username: username,
|
||||||
puid: puid,
|
puid: puid,
|
||||||
pgid: pgid,
|
pgid: pgid,
|
||||||
targetConfPath: constants.OpenVPNConf,
|
|
||||||
buildInfo: buildInfo,
|
buildInfo: buildInfo,
|
||||||
versionInfo: versionInfo,
|
versionInfo: versionInfo,
|
||||||
conf: conf,
|
conf: conf,
|
||||||
|
|||||||
@@ -12,16 +12,13 @@ type Configurator interface {
|
|||||||
VersionGetter
|
VersionGetter
|
||||||
AuthWriter
|
AuthWriter
|
||||||
Starter
|
Starter
|
||||||
}
|
ConfigWriter
|
||||||
|
|
||||||
type StarterAuthWriter interface {
|
|
||||||
Starter
|
|
||||||
AuthWriter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type configurator struct {
|
type configurator struct {
|
||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
cmder command.RunStarter
|
cmder command.RunStarter
|
||||||
|
configPath string
|
||||||
authFilePath string
|
authFilePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +27,7 @@ func NewConfigurator(logger logging.Logger,
|
|||||||
return &configurator{
|
return &configurator{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cmder: cmder,
|
cmder: cmder,
|
||||||
|
configPath: constants.OpenVPNConf,
|
||||||
authFilePath: constants.OpenVPNAuthConf,
|
authFilePath: constants.OpenVPNAuthConf,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := l.writeOpenvpnConf(lines); err != nil {
|
if err := l.conf.WriteConfig(lines); err != nil {
|
||||||
l.signalOrSetStatus(constants.Crashed)
|
l.signalOrSetStatus(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user