Add initial support and docs for Windows (#132)

- Add initial support for Windows
- Update docs
- Fix issue with openvpn 2.6 data-ciphers
This commit is contained in:
Dave Gallant
2024-07-28 21:09:57 -04:00
committed by GitHub
parent 885f73db1c
commit 3e819c5c55
7 changed files with 47 additions and 67 deletions

View File

@@ -1,19 +1,18 @@
package exec
import (
"bytes"
"bufio"
"os"
"os/exec"
"strings"
"github.com/juju/errors"
"github.com/rs/zerolog/log"
)
// Run executes a command in workDir and returns stdout and error.
// The spawned process will exit upon termination of this application
// to ensure a clean exit
func Run(path string, workDir string, args ...string) (string, error) {
func Run(path string, workDir string, args ...string) error {
_, err := exec.LookPath(path)
if err != nil {
log.Error().Msgf("%s is required, please install it", path)
@@ -21,25 +20,28 @@ func Run(path string, workDir string, args ...string) (string, error) {
}
cmd := exec.Command(path, args...)
cmd.Dir = workDir
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
log.Debug().Msgf("Executing " + strings.Join(cmd.Args, " "))
err = cmd.Run()
output := strings.TrimSpace(stdout.String())
errOut := strings.TrimSpace(stderr.String())
if output != "" {
log.Debug().Msgf(output)
}
if errOut != "" {
log.Debug().Msgf(errOut)
}
if _, ok := err.(*exec.ExitError); !ok {
return output, errors.Trace(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return output, errors.Annotatef(err, path, cmd.Args, errOut)
log.Fatal().Msgf("Failed to get stdout pipe: %v", err)
}
return output, nil
if err := cmd.Start(); err != nil {
log.Fatal().Msgf("Failed to start command: %v", err)
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
log.Debug().Msg(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal().Msgf("Error reading stdout: %v", err)
}
if err := cmd.Wait(); err != nil {
log.Fatal().Msgf("Command finished with error: %v", err)
return err
}
return nil
}

View File

@@ -2,11 +2,10 @@ package vpn
import (
"os"
"runtime"
"github.com/davegallant/vpngate/pkg/exec"
"github.com/juju/errors"
"github.com/nxadm/tail"
"github.com/rs/zerolog/log"
)
// Connect to a specified OpenVPN configuration
@@ -17,17 +16,11 @@ func Connect(configPath string) error {
}
defer os.Remove(tmpLogFile.Name())
go func() {
// Tail the temporary openvpn log file
t, err := tail.TailFile(tmpLogFile.Name(), tail.Config{Follow: true})
if err != nil {
log.Error().Msgf("%s", err)
}
for line := range t.Lines {
log.Debug().Msg(line.Text)
}
}()
executable := "openvpn"
if runtime.GOOS == "windows" {
executable = "C:\\Program Files\\OpenVPN\\bin\\openvpn.exe"
}
_, err = exec.Run("openvpn", ".", "--verb", "4", "--log", tmpLogFile.Name(), "--config", configPath)
err = exec.Run(executable, ".", "--verb", "4", "--config", configPath, "--data-ciphers", "AES-128-CBC")
return err
}