9

So I'm looking at the net/proxy docs and there is no examples at all of how to use any of its methods. I'm looking into using socks5. This is the how the function looks:

func SOCKS5(network, addr string, auth *Auth, forward Dialer) (Dialer, error)

Now everything kinda makes sense except I'm confused about forward which is a type Dialer and the function itself returns a Dialer. Everything else makes sense network, add, auth just forward is throwing me off. How would I set my client up to use the socks5 Dialer?

Rodrigo
  • 3,129
  • 3
  • 33
  • 61

3 Answers3

17

So I was able to find the answer to my question anyone interested how to set up a socks5 client in go here it is:

dialSocksProxy, err := proxy.SOCKS5("tcp", "proxy_ip", 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,
}
Rodrigo
  • 3,129
  • 3
  • 33
  • 61
  • 4
    Dial is deprecated in 1.10. how do I get the DialContext ? – ufk May 08 '18 at 06:52
  • 1
    @ufk https://play.golang.org/p/dMGu7rdXaJ3 what do you thin about this solution? – AmaHacka Apr 07 '20 at 14:55
  • @AmaHacka I tried your code (it required some refactoring) but it did not run successfully for me. Can you check it out - [here](https://stackoverflow.com/questions/63656117/cannot-use-socks5-proxy-in-golang-read-connection-reset-by-peer) – aquaman Aug 30 '20 at 09:46
8

Recent versions of Go also have SOCKS5 proxy support via the HTTP_PROXY environment variable. You would write your http.Client code as usual, then just set the environment variable at runtime, for example:

HTTP_PROXY="socks5://127.0.0.1:1080/" ./myGoHttpClient
likebike
  • 1,167
  • 1
  • 11
  • 16
1

If you need socks5 client proxy with auth, you may use something like this:

auth := proxy.Auth{
    User:     "YOUR_PROXY_LOGIN",
    Password: "YOUR_PROXY_PASSWORD",
}

dialer, err := proxy.SOCKS5("tcp", "PROXY_IP", &auth, proxy.Direct)
if err != nil {
    fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
}

tr := &http.Transport{Dial: dialer.Dial}
myClient := &http.Client{
    Transport: tr,
}
bigspawn
  • 1,727
  • 3
  • 16
  • 16