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:
115
cmd/connect.go
Normal file
115
cmd/connect.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/davegallant/vpngate/pkg/vpn"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var flagRandom bool
|
||||
|
||||
func init() {
|
||||
connectCmd.Flags().BoolVarP(&flagRandom, "random", "r", false, "connect to a random server")
|
||||
rootCmd.AddCommand(connectCmd)
|
||||
}
|
||||
|
||||
var connectCmd = &cobra.Command{
|
||||
Use: "connect",
|
||||
Short: "Connect to a vpn server (survey selection appears if hostname is not provided)",
|
||||
Long: `Connect to a vpn from a list of relay servers`,
|
||||
Args: cobra.RangeArgs(0, 1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
vpnServers, err := vpn.GetList()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
serverSelection := []string{}
|
||||
serverSelected := vpn.Server{}
|
||||
|
||||
for _, s := range *vpnServers {
|
||||
serverSelection = append(serverSelection, fmt.Sprintf("%s (%s)", s.HostName, s.CountryShort))
|
||||
}
|
||||
|
||||
selection := ""
|
||||
prompt := &survey.Select{
|
||||
Message: "Choose a server:",
|
||||
Options: serverSelection,
|
||||
}
|
||||
|
||||
if flagRandom {
|
||||
// Select a random server
|
||||
serverSelected = (*vpnServers)[rand.Intn(len(*vpnServers))]
|
||||
} else {
|
||||
|
||||
if len(args) > 0 {
|
||||
selection = args[0]
|
||||
} else {
|
||||
err := survey.AskOne(prompt, &selection, survey.WithPageSize(10))
|
||||
if err != nil {
|
||||
log.Error().Msg("Unable to obtain hostname from survey")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Server lookup from selection could be more optimized with a hash map
|
||||
for _, s := range *vpnServers {
|
||||
if strings.Contains(selection, s.HostName) {
|
||||
serverSelected = s
|
||||
}
|
||||
}
|
||||
|
||||
if serverSelected.HostName == "" {
|
||||
log.Fatal().Msgf("Server '%s' was not found", selection)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
decodedConfig, err := base64.StdEncoding.DecodeString(serverSelected.OpenVpnConfigData)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "vpngate-openvpn-config-")
|
||||
if err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
if _, err := tmpfile.Write(decodedConfig); err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Info().Msgf("Connecting to %s (%s) in %s", serverSelected.HostName, serverSelected.IPAddr, serverSelected.CountryLong)
|
||||
|
||||
err = vpn.Connect(tmpfile.Name())
|
||||
|
||||
if err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
40
cmd/list.go
Normal file
40
cmd/list.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/davegallant/vpngate/pkg/vpn"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all available vpn servers",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
vpnServers, err := vpn.GetList()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal().Msgf(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetHeader([]string{"#", "HostName", "Country", "Ping", "Score"})
|
||||
|
||||
for i, v := range *vpnServers {
|
||||
table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
|
||||
}
|
||||
table.Render() // Send output
|
||||
|
||||
},
|
||||
}
|
26
cmd/root.go
Normal file
26
cmd/root.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "vpngate",
|
||||
Short: "vpngate is a client for vpngate.net",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
_ = cmd.Help()
|
||||
os.Exit(0)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user