mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-06 00:33:40 +00:00
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:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user