mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-05 08:13:41 +00:00
* 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
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
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
|
|
}
|