Unable to get the Socks Proxy working Golang applications

8/30/2021

I am running a golang application on my local laptop, which connects with the Service APIs from a kubernetes cluster. To allow the communication between my laptop and Service APIs on Kubernetes Cluster, I have setup the Socks5 proxy and Able to connect with all services over CURL command.

But I am unable to set same proxy in the Golang application. I have tried various options already

1) Settings env vars - tried setting these 3 at OS level, App level but it did not work

- http_proxy 
- https_proxy 
- all_proxy

2) I am running on Ubuntu Desktop 20.X, so I tried setting Socks proxy at OS Network level as well. All other apps using proxy, but golang isnt picking that up.

3) I tried setting the http.transport level configs too, but it didnt helped.

func InsecureTLSTransport() *http.Transport {

	// START - Only for Proxy - Dev Mode.
	var dialer, err = proxy.SOCKS5("socks5", "<IP:port>", nil, proxy.Direct)

	if err != nil {
		fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
	}
	return &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		Dial: dialer.Dial,
	}
}

I want the socks5 Proxy to be used by my golang app at runtime. Any suggestions/help is really appreciated here.

-- mahesh_klp
go
kubernetes

1 Answer

8/31/2021

you should use tcp instead of socks5 in the network parameter.

package main

import (
	"fmt"

	"golang.org/x/net/proxy"
)

func main() {
	d, err := proxy.SOCKS5("tcp", "<IP:port>", nil, proxy.Direct) // <-- here
	if err != nil {
		fmt.Println(err)
		return
	}
	c, err := d.Dial("tcp", "https://google.com")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c.Close()
}
-- twiny
Source: StackOverflow