Add support for HTTP and SOCKS5 proxies (#127)

As mentioned in https://github.com/davegallant/vpngate/issues/126, adding support for proxies will help bypass issues when vpngate.net is not accessible directly.

This works by:

```sh
# http proxy
sudo vpngate connect --proxy "http://localhost:8080"

# socks5 proxy
sudo vpngate connect --socks5 "127.0.0.1:1080"
```
This commit is contained in:
Dave Gallant
2024-07-21 14:38:26 -04:00
committed by GitHub
parent 5850876efa
commit 16aa16c66e
7 changed files with 78 additions and 21 deletions

View File

@@ -4,9 +4,12 @@ import (
"bytes"
"io"
"net/http"
"net/url"
"os"
"github.com/jszwec/csvutil"
"github.com/rs/zerolog/log"
"golang.org/x/net/proxy"
"github.com/davegallant/vpngate/pkg/util"
"github.com/juju/errors"
@@ -55,10 +58,11 @@ func parseVpnList(r io.Reader) (*[]Server, error) {
}
// GetList returns a list of vpn servers
func GetList() (*[]Server, error) {
func GetList(httpProxy string, socks5Proxy string) (*[]Server, error) {
cacheExpired := vpnListCacheIsExpired()
var servers *[]Server
var client *http.Client
if !cacheExpired {
servers, err := getVpnListCache()
@@ -75,11 +79,43 @@ func GetList() (*[]Server, error) {
log.Info().Msg("Fetching the latest server list")
if httpProxy != "" {
proxyURL, err := url.Parse(httpProxy)
if err != nil {
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
err := util.Retry(5, 1, func() error {
var err error
r, err = http.Get(vpnList)
r, err = client.Get(vpnList)
if err != nil {
return err
}

View File

@@ -9,7 +9,7 @@ import (
// TestGetListReal tests getting the real list of vpn servers
func TestGetListReal(t *testing.T) {
_, err := GetList()
_, err := GetList("", "")
assert.NoError(t, err)
}