Add initial prototype (#1)

* Add survey library to select server

* Add speedtest

* Add --random flag to connect

* Add list command

* Cache server list

* Tail the openvpn logs so that it appears in vpngate logs

* Add goreleaser action

* Add golangci-lint action
This commit is contained in:
Dave Gallant
2020-12-31 02:56:01 -05:00
committed by GitHub
parent c52d1e990a
commit a2afbc1e35
17 changed files with 1355 additions and 0 deletions

48
pkg/vpn/client.go Normal file
View File

@@ -0,0 +1,48 @@
package vpn
import (
"io/ioutil"
"os"
"time"
"github.com/davegallant/vpngate/pkg/exec"
"github.com/davegallant/vpngate/pkg/network"
"github.com/hpcloud/tail"
"github.com/juju/errors"
"github.com/rs/zerolog/log"
)
// Connect to a specified OpenVPN configuration
func Connect(configPath string) error {
tmpLogFile, err := ioutil.TempFile("", "vpngate-openvpn-log-")
if err != nil {
return errors.Annotate(err, "Unable to create a temporary log file")
}
defer os.Remove(tmpLogFile.Name())
go func() {
for {
err = network.TestSpeed()
if err != nil {
log.Error().Msg("Failed to test network speed")
}
time.Sleep(time.Minute)
}
}()
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)
}
}()
_, err = exec.Run("openvpn", ".", "--verb", "4", "--log", tmpLogFile.Name(), "--config", configPath)
return err
}