From 1b316817d130975dc9ec40f39f2d9e40b1efefee Mon Sep 17 00:00:00 2001 From: Mateus Miranda Date: Wed, 1 Dec 2021 09:53:12 +0100 Subject: [PATCH] Testing if socks5 proxy is runnning --- cfdtunnel/cfdtunnel_test.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 29 insertions(+) diff --git a/cfdtunnel/cfdtunnel_test.go b/cfdtunnel/cfdtunnel_test.go index 19e4194..8cbac12 100644 --- a/cfdtunnel/cfdtunnel_test.go +++ b/cfdtunnel/cfdtunnel_test.go @@ -1,15 +1,19 @@ package cfdtunnel import ( + "fmt" "io/ioutil" + "net/http" "os" "os/exec" "path" + "strings" "testing" "log" "github.com/stretchr/testify/assert" + "golang.org/x/net/proxy" "gopkg.in/ini.v1" ) @@ -219,3 +223,25 @@ func TestNewTunnel(t *testing.T) { assert.Equal(t, []string{"arg", "arg"}, tunnel.Args) } + +// TestSock5ProxyRunning launches the proxy tunnel and try to use it calling google.com +// If the result does not have "connection refused" we assume the proxy is running and responding +func TestSocks5ProxyRunning(t *testing.T) { + tunnelConfig := TunnelConfig{"foo.bar", "1234", nil} + cmd := tunnelConfig.startProxyTunnel() + dialSocksProxy, err := proxy.SOCKS5("tcp", "127.0.0.1:1234", nil, proxy.Direct) + if err != nil { + fmt.Println("Error connecting to proxy:", err) + } + tr := &http.Transport{Dial: dialSocksProxy.Dial} + + // Create client + myClient := &http.Client{ + Transport: tr, + } + + _, err = myClient.Get("https://google.com") + commandKill(cmd) + assert.False(t, strings.Contains(err.Error(), "connect: connection refused")) + +} diff --git a/go.mod b/go.mod index 03feb94..6ab1eda 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 + golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 gopkg.in/ini.v1 v1.64.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 8febc9a..d5c1c2b 100644 --- a/go.sum +++ b/go.sum @@ -396,6 +396,8 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI= +golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=