2020-02-06 20:42:46 -05:00
|
|
|
package openvpn
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-19 18:13:48 +00:00
|
|
|
"context"
|
2021-05-30 16:14:08 +00:00
|
|
|
"errors"
|
2020-02-06 20:42:46 -05:00
|
|
|
"fmt"
|
2021-07-16 20:04:17 +00:00
|
|
|
"os/exec"
|
2020-02-06 20:42:46 -05:00
|
|
|
"strings"
|
2021-07-16 20:04:17 +00:00
|
|
|
"syscall"
|
2020-02-06 20:42:46 -05:00
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2020-02-06 20:42:46 -05:00
|
|
|
)
|
|
|
|
|
|
2021-05-31 18:54:36 +00:00
|
|
|
var ErrVersionUnknown = errors.New("OpenVPN version is unknown")
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
binOpenvpn24 = "openvpn2.4"
|
|
|
|
|
binOpenvpn25 = "openvpn"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (c *configurator) Start(ctx context.Context, version string) (
|
2021-01-26 01:09:09 +00:00
|
|
|
stdoutLines, stderrLines chan string, waitError chan error, err error) {
|
2021-05-31 18:54:36 +00:00
|
|
|
var bin string
|
|
|
|
|
switch version {
|
|
|
|
|
case constants.Openvpn24:
|
|
|
|
|
bin = binOpenvpn24
|
|
|
|
|
case constants.Openvpn25:
|
|
|
|
|
bin = binOpenvpn25
|
|
|
|
|
default:
|
|
|
|
|
return nil, nil, nil, fmt.Errorf("%w: %s", ErrVersionUnknown, version)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.logger.Info("starting OpenVPN " + version)
|
|
|
|
|
|
2021-07-16 20:04:17 +00:00
|
|
|
cmd := exec.CommandContext(ctx, bin, "--config", constants.OpenVPNConf)
|
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
|
|
|
|
|
|
|
|
return c.commander.Start(cmd)
|
2021-05-31 18:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *configurator) Version24(ctx context.Context) (version string, err error) {
|
|
|
|
|
return c.version(ctx, binOpenvpn24)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *configurator) Version25(ctx context.Context) (version string, err error) {
|
|
|
|
|
return c.version(ctx, binOpenvpn25)
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 16:14:08 +00:00
|
|
|
var ErrVersionTooShort = errors.New("version output is too short")
|
|
|
|
|
|
2021-05-31 18:54:36 +00:00
|
|
|
func (c *configurator) version(ctx context.Context, binName string) (version string, err error) {
|
2021-07-16 20:04:17 +00:00
|
|
|
cmd := exec.CommandContext(ctx, binName, "--version")
|
|
|
|
|
output, err := c.commander.Run(cmd)
|
2020-02-06 20:42:46 -05:00
|
|
|
if err != nil && err.Error() != "exit status 1" {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
firstLine := strings.Split(output, "\n")[0]
|
|
|
|
|
words := strings.Fields(firstLine)
|
2020-10-20 02:45:28 +00:00
|
|
|
const minWords = 2
|
|
|
|
|
if len(words) < minWords {
|
2021-05-30 16:14:08 +00:00
|
|
|
return "", fmt.Errorf("%w: %s", ErrVersionTooShort, firstLine)
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
|
|
|
|
return words[1], nil
|
|
|
|
|
}
|