mirror of
https://github.com/davegallant/vpngate.git
synced 2026-03-03 10:06:36 +00:00
Compare commits
2 Commits
323709b0a1
...
7948580d1d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7948580d1d | ||
| bb88db92c1 |
@@ -30,8 +30,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var connectCmd = &cobra.Command{
|
var connectCmd = &cobra.Command{
|
||||||
Use: "connect",
|
Use: "connect",
|
||||||
|
|
||||||
Short: "Connect to a vpn server (survey selection appears if hostname is not provided)",
|
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`,
|
Long: `Connect to a vpn from a list of relay servers`,
|
||||||
Args: cobra.RangeArgs(0, 1),
|
Args: cobra.RangeArgs(0, 1),
|
||||||
@@ -39,49 +38,43 @@ var connectCmd = &cobra.Command{
|
|||||||
vpnServers, err := vpn.GetList(flagProxy, flagSocks5Proxy)
|
vpnServers, err := vpn.GetList(flagProxy, flagSocks5Proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serverSelection := []string{}
|
// Build server selection options and hostname lookup map
|
||||||
serverSelected := vpn.Server{}
|
serverSelection := make([]string, len(*vpnServers))
|
||||||
|
serverMap := make(map[string]vpn.Server, len(*vpnServers))
|
||||||
for _, s := range *vpnServers {
|
for i, s := range *vpnServers {
|
||||||
serverSelection = append(serverSelection, fmt.Sprintf("%s (%s)", s.HostName, s.CountryLong))
|
serverSelection[i] = fmt.Sprintf("%s (%s)", s.HostName, s.CountryLong)
|
||||||
|
serverMap[s.HostName] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
selection := ""
|
selection := ""
|
||||||
prompt := &survey.Select{
|
var serverSelected vpn.Server
|
||||||
Message: "Choose a server:",
|
|
||||||
Options: serverSelection,
|
|
||||||
}
|
|
||||||
|
|
||||||
if !flagRandom {
|
if !flagRandom {
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
selection = args[0]
|
selection = args[0]
|
||||||
} else {
|
} else {
|
||||||
|
prompt := &survey.Select{
|
||||||
|
Message: "Choose a server:",
|
||||||
|
Options: serverSelection,
|
||||||
|
}
|
||||||
err := survey.AskOne(prompt, &selection, survey.WithPageSize(10))
|
err := survey.AskOne(prompt, &selection, survey.WithPageSize(10))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg("Unable to obtain hostname from survey")
|
log.Fatal().Msg("Unable to obtain hostname from survey")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server lookup from selection could be more optimized with a hash map
|
// Lookup server from selection using map for O(1) lookup
|
||||||
for _, s := range *vpnServers {
|
hostname := extractHostname(selection)
|
||||||
if strings.Contains(selection, s.HostName) {
|
if server, exists := serverMap[hostname]; exists {
|
||||||
serverSelected = s
|
serverSelected = server
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
if serverSelected.HostName == "" {
|
|
||||||
log.Fatal().Msgf("Server '%s' was not found", selection)
|
log.Fatal().Msgf("Server '%s' was not found", selection)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
if flagRandom {
|
if flagRandom {
|
||||||
// Select a random server
|
// Select a random server
|
||||||
serverSelected = (*vpnServers)[rand.Intn(len(*vpnServers))]
|
serverSelected = (*vpnServers)[rand.Intn(len(*vpnServers))]
|
||||||
@@ -90,23 +83,19 @@ var connectCmd = &cobra.Command{
|
|||||||
decodedConfig, err := base64.StdEncoding.DecodeString(serverSelected.OpenVpnConfigData)
|
decodedConfig, err := base64.StdEncoding.DecodeString(serverSelected.OpenVpnConfigData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpfile, err := os.CreateTemp("", "vpngate-openvpn-config-")
|
tmpfile, err := os.CreateTemp("", "vpngate-openvpn-config-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := tmpfile.Write(decodedConfig); err != nil {
|
if _, err := tmpfile.Write(decodedConfig); err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tmpfile.Close(); err != nil {
|
if err := tmpfile.Close(); err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msgf("Connecting to %s (%s) in %s", serverSelected.HostName, serverSelected.IPAddr, serverSelected.CountryLong)
|
log.Info().Msgf("Connecting to %s (%s) in %s", serverSelected.HostName, serverSelected.IPAddr, serverSelected.CountryLong)
|
||||||
@@ -114,16 +103,22 @@ var connectCmd = &cobra.Command{
|
|||||||
err = vpn.Connect(tmpfile.Name())
|
err = vpn.Connect(tmpfile.Name())
|
||||||
|
|
||||||
if err != nil && !flagReconnect {
|
if err != nil && !flagReconnect {
|
||||||
log.Fatal().Msg(err.Error())
|
// VPN connection failed and reconnect is disabled
|
||||||
os.Exit(1)
|
_ = os.Remove(tmpfile.Name())
|
||||||
} else {
|
log.Fatal().Msg("VPN connection failed")
|
||||||
err = os.Remove(tmpfile.Name())
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Msg(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always try to clean up temporary file
|
||||||
|
_ = os.Remove(tmpfile.Name())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractHostname extracts the hostname from the selection string (format: "hostname (country)")
|
||||||
|
func extractHostname(selection string) string {
|
||||||
|
parts := strings.Split(selection, " (")
|
||||||
|
if len(parts) > 0 {
|
||||||
|
return parts[0]
|
||||||
|
}
|
||||||
|
return selection
|
||||||
|
}
|
||||||
@@ -27,7 +27,6 @@ var listCmd = &cobra.Command{
|
|||||||
vpnServers, err := vpn.GetList(flagProxy, flagSocks5Proxy)
|
vpnServers, err := vpn.GetList(flagProxy, flagSocks5Proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
table := tw.NewWriter(os.Stdout)
|
table := tw.NewWriter(os.Stdout)
|
||||||
@@ -37,13 +36,11 @@ var listCmd = &cobra.Command{
|
|||||||
err := table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
|
err := table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = table.Render()
|
err = table.Render()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
pkgs.buildGo125Module rec {
|
pkgs.buildGo125Module rec {
|
||||||
name = "vpngate";
|
name = "vpngate";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
vendorHash = "sha256-ODAco5xEvhF8elvfeCawLubSIJe9Y/sKOPGivcQJlqE=";
|
vendorHash = "sha256-FNpeIIIrINm/3neCkuX/kFWWGCCEN8Duz1iSFAki+54=";
|
||||||
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.makeWrapper ];
|
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.makeWrapper ];
|
||||||
env.CGO_ENABLED = 0;
|
env.CGO_ENABLED = 0;
|
||||||
doCheck = false;
|
doCheck = false;
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -10,7 +10,6 @@ require (
|
|||||||
github.com/juju/errors v1.0.0
|
github.com/juju/errors v1.0.0
|
||||||
github.com/olekukonko/tablewriter v1.1.3
|
github.com/olekukonko/tablewriter v1.1.3
|
||||||
github.com/rs/zerolog v1.34.0
|
github.com/rs/zerolog v1.34.0
|
||||||
github.com/spf13/afero v1.15.0
|
|
||||||
github.com/spf13/cobra v1.10.2
|
github.com/spf13/cobra v1.10.2
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
golang.org/x/net v0.50.0
|
golang.org/x/net v0.50.0
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -66,8 +66,6 @@ github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
|||||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
|
||||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
|
||||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
|||||||
@@ -2,46 +2,58 @@ package exec
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"os"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run executes a command in workDir and returns stdout and error.
|
// Run executes a command in workDir and logs its output.
|
||||||
// The spawned process will exit upon termination of this application
|
// If the command fails to start or setup fails, an error is logged and returned.
|
||||||
// to ensure a clean exit
|
// If the command exits with a non-zero status, the error is returned without logging
|
||||||
|
// (this allows the caller to decide how to handle it).
|
||||||
func Run(path string, workDir string, args ...string) error {
|
func Run(path string, workDir string, args ...string) error {
|
||||||
_, err := exec.LookPath(path)
|
_, err := exec.LookPath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msgf("%s is required, please install it", path)
|
log.Error().Msgf("%s is required, please install it", path)
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(path, args...)
|
cmd := exec.Command(path, args...)
|
||||||
cmd.Dir = workDir
|
cmd.Dir = workDir
|
||||||
log.Debug().Msg("Executing " + strings.Join(cmd.Args, " "))
|
|
||||||
|
log.Debug().Strs("command", cmd.Args).Msg("Executing command")
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf("Failed to get stdout pipe: %v", err)
|
log.Error().Msgf("Failed to get stdout pipe: %v", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Msgf("Failed to get stderr pipe: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
log.Fatal().Msgf("Failed to start command: %v", err)
|
log.Error().Msgf("Failed to start command: %v", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner := bufio.NewScanner(stdout)
|
// Combine stdout and stderr into a single reader
|
||||||
|
combined := io.MultiReader(stdout, stderr)
|
||||||
|
scanner := bufio.NewScanner(combined)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
log.Debug().Msg(scanner.Text())
|
log.Debug().Msg(scanner.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
log.Fatal().Msgf("Error reading stdout: %v", err)
|
log.Error().Msgf("Error reading output: %v", err)
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Wait(); err != nil {
|
|
||||||
log.Fatal().Msgf("Command finished with error: %v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
// cmd.Wait() returns an error if the command exits with non-zero status
|
||||||
|
// We return this without logging since it's expected behavior for some commands
|
||||||
|
return cmd.Wait()
|
||||||
|
}
|
||||||
@@ -2,17 +2,18 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Retry(attempts int, delay time.Duration,fn func() error) error {
|
func Retry(attempts int, delay time.Duration, fn func() error) error {
|
||||||
var err error
|
var err error
|
||||||
for i := 0; i < attempts; i++ {
|
for i := 0; i < attempts; i++ {
|
||||||
if err = fn(); err == nil {
|
if err = fn(); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Error().Msgf("Retrying after %d seconds. An error occured: %s", delay, err)
|
log.Error().Msgf("Retrying after %v. An error occurred: %s", delay, err)
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -4,37 +4,42 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const serverCachefile = "servers.json"
|
const serverCachefile = "servers.json"
|
||||||
|
|
||||||
func getCacheDir() string {
|
func getCacheDir() (string, error) {
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msgf("Failed to get user's home directory: %s ", err)
|
return "", err
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
cacheDir := path.Join(homeDir, ".vpngate", "cache")
|
cacheDir := filepath.Join(homeDir, ".vpngate", "cache")
|
||||||
return cacheDir
|
return cacheDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCacheDir() error {
|
func createCacheDir() error {
|
||||||
cacheDir := getCacheDir()
|
cacheDir, err := getCacheDir()
|
||||||
AppFs := afero.NewOsFs()
|
if err != nil {
|
||||||
return AppFs.MkdirAll(cacheDir, 0o700)
|
return err
|
||||||
|
}
|
||||||
|
return os.MkdirAll(cacheDir, 0o700)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVpnListCache() (*[]Server, error) {
|
func getVpnListCache() (*[]Server, error) {
|
||||||
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
cacheDir, err := getCacheDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cacheFile := filepath.Join(cacheDir, serverCachefile)
|
||||||
serversFile, err := os.Open(cacheFile)
|
serversFile, err := os.Open(cacheFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = serversFile.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
byteValue, err := io.ReadAll(serversFile)
|
byteValue, err := io.ReadAll(serversFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -44,7 +49,6 @@ func getVpnListCache() (*[]Server, error) {
|
|||||||
var servers []Server
|
var servers []Server
|
||||||
|
|
||||||
err = json.Unmarshal(byteValue, &servers)
|
err = json.Unmarshal(byteValue, &servers)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -53,8 +57,7 @@ func getVpnListCache() (*[]Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeVpnListToCache(servers []Server) error {
|
func writeVpnListToCache(servers []Server) error {
|
||||||
err := createCacheDir()
|
if err := createCacheDir(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,20 +66,26 @@ func writeVpnListToCache(servers []Server) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheFile := path.Join(getCacheDir(), serverCachefile)
|
cacheDir, err := getCacheDir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cacheFile := filepath.Join(cacheDir, serverCachefile)
|
||||||
|
|
||||||
err = os.WriteFile(cacheFile, f, 0o644)
|
return os.WriteFile(cacheFile, f, 0o644)
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func vpnListCacheIsExpired() bool {
|
func vpnListCacheIsExpired() bool {
|
||||||
file, err := os.Stat(path.Join(getCacheDir(), serverCachefile))
|
cacheDir, err := getCacheDir()
|
||||||
|
if err != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
file, err := os.Stat(filepath.Join(cacheDir, serverCachefile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
lastModified := file.ModTime()
|
lastModified := file.ModTime()
|
||||||
|
|
||||||
return (time.Since(lastModified)) > time.Duration(24*time.Hour)
|
return time.Since(lastModified) > 24*time.Hour
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,17 @@
|
|||||||
package vpn
|
package vpn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/davegallant/vpngate/pkg/exec"
|
"github.com/davegallant/vpngate/pkg/exec"
|
||||||
"github.com/juju/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Connect to a specified OpenVPN configuration
|
// Connect to a specified OpenVPN configuration
|
||||||
func Connect(configPath string) error {
|
func Connect(configPath string) error {
|
||||||
tmpLogFile, err := os.CreateTemp("", "vpngate-openvpn-log-")
|
|
||||||
if err != nil {
|
|
||||||
return errors.Annotate(err, "Unable to create a temporary log file")
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = os.Remove(tmpLogFile.Name())
|
|
||||||
}()
|
|
||||||
|
|
||||||
executable := "openvpn"
|
executable := "openvpn"
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
executable = "C:\\Program Files\\OpenVPN\\bin\\openvpn.exe"
|
executable = "C:\\Program Files\\OpenVPN\\bin\\openvpn.exe"
|
||||||
}
|
}
|
||||||
|
|
||||||
err = exec.Run(executable, ".", "--verb", "4", "--config", configPath, "--data-ciphers", "AES-128-CBC")
|
return exec.Run(executable, ".", "--verb", "4", "--config", configPath, "--data-ciphers", "AES-128-CBC")
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
161
pkg/vpn/list.go
161
pkg/vpn/list.go
@@ -2,10 +2,12 @@ package vpn
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"time"
|
||||||
|
|
||||||
"github.com/jszwec/csvutil"
|
"github.com/jszwec/csvutil"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -16,10 +18,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
vpnList = "https://www.vpngate.net/api/iphone/"
|
vpnList = "https://www.vpngate.net/api/iphone/"
|
||||||
|
httpClientTimeout = 30 * time.Second
|
||||||
|
dialTimeout = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server holds in formation about a vpn relay server
|
// Server holds information about a vpn relay server
|
||||||
type Server struct {
|
type Server struct {
|
||||||
HostName string `csv:"#HostName"`
|
HostName string `csv:"#HostName"`
|
||||||
CountryLong string `csv:"CountryLong"`
|
CountryLong string `csv:"CountryLong"`
|
||||||
@@ -30,20 +34,14 @@ type Server struct {
|
|||||||
Ping string `csv:"Ping"`
|
Ping string `csv:"Ping"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamToBytes(stream io.Reader) []byte {
|
// parseVpnList parses the VPN server list from CSV format
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
_, err := buf.ReadFrom(stream)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Msg("Unable to stream bytes")
|
|
||||||
}
|
|
||||||
return buf.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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, err := io.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotate(err, "Unable to read stream")
|
||||||
|
}
|
||||||
|
|
||||||
// Trim known invalid rows
|
// Trim known invalid rows
|
||||||
serverList = bytes.TrimPrefix(serverList, []byte("*vpn_servers\r\n"))
|
serverList = bytes.TrimPrefix(serverList, []byte("*vpn_servers\r\n"))
|
||||||
@@ -57,86 +55,119 @@ func parseVpnList(r io.Reader) (*[]Server, error) {
|
|||||||
return &servers, nil
|
return &servers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createHTTPClient creates an HTTP client with optional proxy configuration
|
||||||
|
func createHTTPClient(httpProxy string, socks5Proxy string) (*http.Client, error) {
|
||||||
|
if httpProxy != "" {
|
||||||
|
proxyURL, err := url.Parse(httpProxy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "Error parsing HTTP proxy: %s", httpProxy)
|
||||||
|
}
|
||||||
|
transport := &http.Transport{
|
||||||
|
Proxy: http.ProxyURL(proxyURL),
|
||||||
|
}
|
||||||
|
return &http.Client{
|
||||||
|
Transport: transport,
|
||||||
|
Timeout: httpClientTimeout,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if socks5Proxy != "" {
|
||||||
|
dialer, err := proxy.SOCKS5("tcp", socks5Proxy, nil, proxy.Direct)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "Error creating SOCKS5 dialer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a DialContext function from the SOCKS5 dialer
|
||||||
|
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
// Check if context is already done
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the dialer with a timeout
|
||||||
|
conn, err := dialer.Dial(network, addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Respect context cancellation after connection
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
_ = conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
httpTransport := &http.Transport{
|
||||||
|
DialContext: dialContext,
|
||||||
|
}
|
||||||
|
return &http.Client{
|
||||||
|
Transport: httpTransport,
|
||||||
|
Timeout: httpClientTimeout,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &http.Client{
|
||||||
|
Timeout: httpClientTimeout,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
DialContext: (&net.Dialer{
|
||||||
|
Timeout: dialTimeout,
|
||||||
|
}).DialContext,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetList returns a list of vpn servers
|
// GetList returns a list of vpn servers
|
||||||
func GetList(httpProxy string, socks5Proxy string) (*[]Server, error) {
|
func GetList(httpProxy string, socks5Proxy string) (*[]Server, error) {
|
||||||
cacheExpired := vpnListCacheIsExpired()
|
cacheExpired := vpnListCacheIsExpired()
|
||||||
|
|
||||||
var servers *[]Server
|
// Try to use cached list if not expired
|
||||||
var client *http.Client
|
|
||||||
|
|
||||||
if !cacheExpired {
|
if !cacheExpired {
|
||||||
servers, err := getVpnListCache()
|
servers, err := getVpnListCache()
|
||||||
|
if err == nil {
|
||||||
if err != nil {
|
|
||||||
log.Info().Msg("Unable to retrieve vpn list from cache")
|
|
||||||
} else {
|
|
||||||
return servers, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
|
log.Info().Msg("Unable to retrieve vpn list from cache")
|
||||||
} else {
|
} else {
|
||||||
log.Info().Msg("The vpn server list cache has expired")
|
log.Info().Msg("The vpn server list cache has expired")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msg("Fetching the latest server list")
|
log.Info().Msg("Fetching the latest server list")
|
||||||
|
|
||||||
if httpProxy != "" {
|
client, err := createHTTPClient(httpProxy, socks5Proxy)
|
||||||
proxyURL, err := url.Parse(httpProxy)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
log.Error().Msgf("Error parsing proxy: %s", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
transport := &http.Transport{
|
|
||||||
Proxy: http.ProxyURL(proxyURL),
|
|
||||||
}
|
|
||||||
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: transport,
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if socks5Proxy != "" {
|
|
||||||
dialer, err := proxy.SOCKS5("tcp", socks5Proxy, nil, proxy.Direct)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Msgf("Error creating SOCKS5 dialer: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
httpTransport := &http.Transport{
|
|
||||||
Dial: dialer.Dial,
|
|
||||||
}
|
|
||||||
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: httpTransport,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
client = &http.Client{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var r *http.Response
|
var servers *[]Server
|
||||||
|
|
||||||
err := util.Retry(5, 1, func() error {
|
err = util.Retry(5, 1, func() error {
|
||||||
var err error
|
resp, err := client.Get(vpnList)
|
||||||
r, err = client.Get(vpnList)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = r.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if r.StatusCode != 200 {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return errors.Annotatef(err, "Unexpected status code when retrieving vpn list: %d", r.StatusCode)
|
return errors.Annotatef(err, "Unexpected status code when retrieving vpn list: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
servers, err = parseVpnList(r.Body)
|
parsedServers, err := parseVpnList(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeVpnListToCache(*servers)
|
servers = parsedServers
|
||||||
|
|
||||||
if err != nil {
|
// Cache the servers for future use
|
||||||
log.Warn().Msgf("Unable to write servers to cache: %s", err)
|
cacheErr := writeVpnListToCache(*servers)
|
||||||
|
if cacheErr != nil {
|
||||||
|
log.Warn().Msgf("Unable to write servers to cache: %s", cacheErr)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -146,4 +177,4 @@ func GetList(httpProxy string, socks5Proxy string) (*[]Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return servers, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user