mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-06 00:33:40 +00:00
Run gofumpt on the codebase (#58)
This commit is contained in:
@@ -2,13 +2,12 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"time"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -17,8 +16,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var flagRandom bool
|
var (
|
||||||
var flagReconnect bool
|
flagRandom bool
|
||||||
|
flagReconnect bool
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
connectCmd.Flags().BoolVarP(&flagRandom, "random", "r", false, "connect to a random server")
|
connectCmd.Flags().BoolVarP(&flagRandom, "random", "r", false, "connect to a random server")
|
||||||
@@ -32,9 +33,7 @@ var connectCmd = &cobra.Command{
|
|||||||
Long: `Connect to a vpn from a list of relay servers`,
|
Long: `Connect to a vpn from a list of relay servers`,
|
||||||
Args: cobra.RangeArgs(0, 1),
|
Args: cobra.RangeArgs(0, 1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
vpnServers, err := vpn.GetList()
|
vpnServers, err := vpn.GetList()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf(err.Error())
|
log.Fatal().Msgf(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -120,6 +119,5 @@ var connectCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -20,9 +20,7 @@ var listCmd = &cobra.Command{
|
|||||||
Short: "List all available vpn servers",
|
Short: "List all available vpn servers",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
vpnServers, err := vpn.GetList()
|
vpnServers, err := vpn.GetList()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf(err.Error())
|
log.Fatal().Msgf(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -35,6 +33,5 @@ var listCmd = &cobra.Command{
|
|||||||
table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
|
table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
|
||||||
}
|
}
|
||||||
table.Render() // Send output
|
table.Render() // Send output
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -25,20 +25,18 @@ func getCacheDir() string {
|
|||||||
|
|
||||||
func createCacheDir() error {
|
func createCacheDir() error {
|
||||||
cacheDir := getCacheDir()
|
cacheDir := getCacheDir()
|
||||||
var AppFs = afero.NewOsFs()
|
AppFs := afero.NewOsFs()
|
||||||
return AppFs.MkdirAll(cacheDir, 0700)
|
return AppFs.MkdirAll(cacheDir, 0o700)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVpnListCache() (*[]Server, error) {
|
func getVpnListCache() (*[]Server, error) {
|
||||||
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
||||||
serversFile, err := os.Open(cacheFile)
|
serversFile, err := os.Open(cacheFile)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
byteValue, err := ioutil.ReadAll(serversFile)
|
byteValue, err := ioutil.ReadAll(serversFile)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -55,30 +53,25 @@ func getVpnListCache() (*[]Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeVpnListToCache(servers []Server) error {
|
func writeVpnListToCache(servers []Server) error {
|
||||||
|
|
||||||
err := createCacheDir()
|
err := createCacheDir()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := json.MarshalIndent(servers, "", " ")
|
f, err := json.MarshalIndent(servers, "", " ")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
||||||
|
|
||||||
err = ioutil.WriteFile(cacheFile, f, 0644)
|
err = ioutil.WriteFile(cacheFile, f, 0o644)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func vpnListCacheIsExpired() bool {
|
func vpnListCacheIsExpired() bool {
|
||||||
file, err := os.Stat(path.Join(getCacheDir(), serverCachefile))
|
file, err := os.Stat(path.Join(getCacheDir(), serverCachefile))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
// Connect to a specified OpenVPN configuration
|
// Connect to a specified OpenVPN configuration
|
||||||
func Connect(configPath string) error {
|
func Connect(configPath string) error {
|
||||||
|
|
||||||
tmpLogFile, err := ioutil.TempFile("", "vpngate-openvpn-log-")
|
tmpLogFile, err := ioutil.TempFile("", "vpngate-openvpn-log-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Annotate(err, "Unable to create a temporary log file")
|
return errors.Annotate(err, "Unable to create a temporary log file")
|
||||||
|
@@ -1,10 +1,9 @@
|
|||||||
package vpn
|
package vpn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/jszwec/csvutil"
|
"github.com/jszwec/csvutil"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -38,7 +37,6 @@ func streamToBytes(stream io.Reader) []byte {
|
|||||||
|
|
||||||
// parse csv
|
// parse csv
|
||||||
func parseVpnList(r io.Reader) (*[]Server, error) {
|
func parseVpnList(r io.Reader) (*[]Server, error) {
|
||||||
|
|
||||||
var servers []Server
|
var servers []Server
|
||||||
|
|
||||||
serverList := streamToBytes(r)
|
serverList := streamToBytes(r)
|
||||||
@@ -52,12 +50,10 @@ func parseVpnList(r io.Reader) (*[]Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &servers, nil
|
return &servers, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetList returns a list of vpn servers
|
// GetList returns a list of vpn servers
|
||||||
func GetList() (*[]Server, error) {
|
func GetList() (*[]Server, error) {
|
||||||
|
|
||||||
cacheExpired := vpnListCacheIsExpired()
|
cacheExpired := vpnListCacheIsExpired()
|
||||||
|
|
||||||
var servers *[]Server
|
var servers *[]Server
|
||||||
@@ -78,7 +74,6 @@ func GetList() (*[]Server, error) {
|
|||||||
log.Info().Msg("Fetching the latest server list")
|
log.Info().Msg("Fetching the latest server list")
|
||||||
|
|
||||||
r, err := http.Get(vpnList)
|
r, err := http.Get(vpnList)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Unable to retrieve vpn list")
|
return nil, errors.Annotate(err, "Unable to retrieve vpn list")
|
||||||
}
|
}
|
||||||
|
@@ -12,12 +12,10 @@ func TestGetListReal(t *testing.T) {
|
|||||||
_, err := GetList()
|
_, err := GetList()
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestParseVpnList parses a local copy of vpn list csv
|
// TestParseVpnList parses a local copy of vpn list csv
|
||||||
func TestParseVpnList(t *testing.T) {
|
func TestParseVpnList(t *testing.T) {
|
||||||
|
|
||||||
dat, err := os.Open("../../test_data/vpn_list.csv")
|
dat, err := os.Open("../../test_data/vpn_list.csv")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
@@ -31,5 +29,4 @@ func TestParseVpnList(t *testing.T) {
|
|||||||
assert.Equal(t, (*servers)[0].HostName, "public-vpn-227")
|
assert.Equal(t, (*servers)[0].HostName, "public-vpn-227")
|
||||||
assert.Equal(t, (*servers)[0].Ping, "13")
|
assert.Equal(t, (*servers)[0].Ping, "13")
|
||||||
assert.Equal(t, (*servers)[0].Score, 2086924)
|
assert.Equal(t, (*servers)[0].Score, 2086924)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user