Maint: minor changes to openvpn/config package
- Constructor returns concrete struct instead of interface - Rename conf to openvpnConf in openvpn loop
This commit is contained in:
@@ -143,7 +143,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
|
|||||||
httpClient := &http.Client{Timeout: clientTimeout}
|
httpClient := &http.Client{Timeout: clientTimeout}
|
||||||
// Create configurators
|
// Create configurators
|
||||||
alpineConf := alpine.New()
|
alpineConf := alpine.New()
|
||||||
ovpnConf := openvpnconfig.NewConfigurator(
|
ovpnConf := openvpnconfig.New(
|
||||||
logger.NewChild(logging.Settings{Prefix: "openvpn configurator: "}),
|
logger.NewChild(logging.Settings{Prefix: "openvpn configurator: "}),
|
||||||
cmder, puid, pgid)
|
cmder, puid, pgid)
|
||||||
dnsCrypto := dnscrypto.New(httpClient, "", "")
|
dnsCrypto := dnscrypto.New(httpClient, "", "")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ type AuthWriter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteAuthFile writes the OpenVPN auth file to disk with the right permissions.
|
// WriteAuthFile writes the OpenVPN auth file to disk with the right permissions.
|
||||||
func (c *configurator) WriteAuthFile(user, password string) error {
|
func (c *Configurator) WriteAuthFile(user, password string) error {
|
||||||
file, err := os.Open(c.authFilePath)
|
file, err := os.Open(c.authFilePath)
|
||||||
|
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type Starter interface {
|
|||||||
stdoutLines, stderrLines chan string, waitError chan error, err error)
|
stdoutLines, stderrLines chan string, waitError chan error, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) Start(ctx context.Context, version string, flags []string) (
|
func (c *Configurator) Start(ctx context.Context, version string, flags []string) (
|
||||||
stdoutLines, stderrLines chan string, waitError chan error, err error) {
|
stdoutLines, stderrLines chan string, waitError chan error, err error) {
|
||||||
var bin string
|
var bin string
|
||||||
switch version {
|
switch version {
|
||||||
@@ -50,17 +50,17 @@ type VersionGetter interface {
|
|||||||
Version25(ctx context.Context) (version string, err error)
|
Version25(ctx context.Context) (version string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) Version24(ctx context.Context) (version string, err error) {
|
func (c *Configurator) Version24(ctx context.Context) (version string, err error) {
|
||||||
return c.version(ctx, binOpenvpn24)
|
return c.version(ctx, binOpenvpn24)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) Version25(ctx context.Context) (version string, err error) {
|
func (c *Configurator) Version25(ctx context.Context) (version string, err error) {
|
||||||
return c.version(ctx, binOpenvpn25)
|
return c.version(ctx, binOpenvpn25)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrVersionTooShort = errors.New("version output is too short")
|
var ErrVersionTooShort = errors.New("version output is too short")
|
||||||
|
|
||||||
func (c *configurator) version(ctx context.Context, binName string) (version string, err error) {
|
func (c *Configurator) version(ctx context.Context, binName string) (version string, err error) {
|
||||||
cmd := exec.CommandContext(ctx, binName, "--version")
|
cmd := exec.CommandContext(ctx, binName, "--version")
|
||||||
output, err := c.cmder.Run(cmd)
|
output, err := c.cmder.Run(cmd)
|
||||||
if err != nil && err.Error() != "exit status 1" {
|
if err != nil && err.Error() != "exit status 1" {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type Writer interface {
|
|||||||
WriteConfig(lines []string) error
|
WriteConfig(lines []string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) 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)
|
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
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ import (
|
|||||||
"github.com/qdm12/golibs/logging"
|
"github.com/qdm12/golibs/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configurator interface {
|
type Interface interface {
|
||||||
VersionGetter
|
VersionGetter
|
||||||
AuthWriter
|
AuthWriter
|
||||||
Starter
|
Starter
|
||||||
Writer
|
Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type configurator struct {
|
type Configurator struct {
|
||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
cmder command.RunStarter
|
cmder command.RunStarter
|
||||||
configPath string
|
configPath string
|
||||||
@@ -21,9 +21,9 @@ type configurator struct {
|
|||||||
puid, pgid int
|
puid, pgid int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigurator(logger logging.Logger,
|
func New(logger logging.Logger,
|
||||||
cmder command.RunStarter, puid, pgid int) Configurator {
|
cmder command.RunStarter, puid, pgid int) *Configurator {
|
||||||
return &configurator{
|
return &Configurator{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cmder: cmder,
|
cmder: cmder,
|
||||||
configPath: constants.OpenVPNConf,
|
configPath: constants.OpenVPNConf,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ type Loop struct {
|
|||||||
buildInfo models.BuildInformation
|
buildInfo models.BuildInformation
|
||||||
versionInfo bool
|
versionInfo bool
|
||||||
// Configurators
|
// Configurators
|
||||||
conf config.Configurator
|
openvpnConf config.Interface
|
||||||
fw firewallConfigurer
|
fw firewallConfigurer
|
||||||
routing routing.VPNGetter
|
routing routing.VPNGetter
|
||||||
portForward portforward.StartStopper
|
portForward portforward.StartStopper
|
||||||
@@ -65,7 +65,7 @@ const (
|
|||||||
|
|
||||||
func NewLoop(openVPNSettings configuration.OpenVPN,
|
func NewLoop(openVPNSettings configuration.OpenVPN,
|
||||||
providerSettings configuration.Provider,
|
providerSettings configuration.Provider,
|
||||||
allServers models.AllServers, conf config.Configurator,
|
allServers models.AllServers, openvpnConf config.Interface,
|
||||||
fw firewallConfigurer, routing routing.VPNGetter,
|
fw firewallConfigurer, routing routing.VPNGetter,
|
||||||
portForward portforward.StartStopper,
|
portForward portforward.StartStopper,
|
||||||
publicip publicip.Looper, dnsLooper dns.Looper,
|
publicip publicip.Looper, dnsLooper dns.Looper,
|
||||||
@@ -84,7 +84,7 @@ func NewLoop(openVPNSettings configuration.OpenVPN,
|
|||||||
state: state,
|
state: state,
|
||||||
buildInfo: buildInfo,
|
buildInfo: buildInfo,
|
||||||
versionInfo: versionInfo,
|
versionInfo: versionInfo,
|
||||||
conf: conf,
|
openvpnConf: openvpnConf,
|
||||||
fw: fw,
|
fw: fw,
|
||||||
routing: routing,
|
routing: routing,
|
||||||
portForward: portForward,
|
portForward: portForward,
|
||||||
|
|||||||
@@ -44,13 +44,13 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := l.conf.WriteConfig(lines); err != nil {
|
if err := l.openvpnConf.WriteConfig(lines); err != nil {
|
||||||
l.crashed(ctx, err)
|
l.crashed(ctx, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if openVPNSettings.User != "" {
|
if openVPNSettings.User != "" {
|
||||||
err := l.conf.WriteAuthFile(openVPNSettings.User, openVPNSettings.Password)
|
err := l.openvpnConf.WriteAuthFile(openVPNSettings.User, openVPNSettings.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.crashed(ctx, err)
|
l.crashed(ctx, err)
|
||||||
continue
|
continue
|
||||||
@@ -64,7 +64,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
|
|
||||||
openvpnCtx, openvpnCancel := context.WithCancel(context.Background())
|
openvpnCtx, openvpnCancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
stdoutLines, stderrLines, waitError, err := l.conf.Start(
|
stdoutLines, stderrLines, waitError, err := l.openvpnConf.Start(
|
||||||
openvpnCtx, openVPNSettings.Version, openVPNSettings.Flags)
|
openvpnCtx, openVPNSettings.Version, openVPNSettings.Flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
openvpnCancel()
|
openvpnCancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user