mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-06 00:33:40 +00:00
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:
48
pkg/vpn/client.go
Normal file
48
pkg/vpn/client.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user