Proxy setup in Go

Proxies in Go with net/http: http.ProxyURL on the Transport, credentials in the URL, SOCKS5 via the socks5:// scheme with no extra modules, and timeouts.

Updated · Prokso editorial team

Markdown, ready to paste into ChatGPT or another AI assistant
Connection details
Host
gw.prokso.com
Port
823 HTTP · 824 SOCKS5 - new IP per request · 10000–20000 sticky session, HTTP(S) and SOCKS5
Username and password
in your dashboard, on the plan page

Your dashboard generates ready-made lines in the format you need: choose the country, IP type and protocol.

Key takeaways
  • Transport.Proxy + http.ProxyURL; username and password via url.UserPassword
  • SOCKS5 in net/http with the socks5:// scheme on port 824, no third-party packages
  • One Transport per program; read the response body to the end and close it

In Go the proxy is the Proxy field in http.Transport: a function that returns the proxy address for each request. The standard library understands HTTP, HTTPS and SOCKS5 on its own and sends the username and password from the address by itself, so for most tasks you don't need third-party modules. The golang.org/x/net/proxy package is only needed when you want SOCKS5 not for HTTP but for a plain TCP connection.

Step 1. Build a url.URL for Transport.Proxy

Go expects the proxy address as a *url.URL. The most reliable way is to build it from parts rather than parse a string: the scheme (http or socks5), the host with port, and the username and password via url.UserPassword.

Why from parts: url.UserPassword stores the password as is and escapes special characters itself when the address is turned into a string. If you put a password with @, : or # directly into the string for url.Parse, the address will be parsed incorrectly, and you'd have to encode those characters by hand.

Take the values from your plan: host gw.prokso.com, username and password from the “Access” block. Port 823 gives HTTP and a new IP per request, 824 does the same for SOCKS5, and 10000-20000 give a sticky IP for 1-120 minutes for both protocols. The country is set by the username.

Step 2. http.ProxyURL in your own Transport

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"
)

func main() {
	proxyURL := &url.URL{
		Scheme: "http",
		User:   url.UserPassword("USERNAME", "PASSWORD"),
		Host:   "gw.prokso.com:823",
	}
	// one client for the whole app
	client := &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
		Timeout:   30 * time.Second,
	}

	resp, err := client.Get("https://api.ipify.org")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body)) // prints the IP the site sees
}

http.ProxyURL returns a function that gives the same address for every request. If the address contains a username and password, Go, according to the documentation, passes them to the proxy in the Proxy-Authorization header - both for regular http requests and during CONNECT, when a tunnel to an https site is opened.

Pay attention to Timeout: a zero value in http.Client means “no timeout”, and a request to a proxy that doesn't respond will wait forever. The client timeout covers the connection, redirects and reading the response body.

Step 3. One Transport for all goroutines

The net/http documentation states plainly: Transport and Client should be reused rather than created for each request, and both are safe for concurrent use by many goroutines. A Transport keeps a pool of connections to the proxy; a new Transport per request means a new connection every time and needless delays.

The second condition for reuse is the response body. If resp.Body isn't read to the end and closed, the Transport can't reuse the connection. That's why the example has defer resp.Body.Close() and io.ReadAll.

For several sticky addresses, make one Transport per port and hand them out to workers. Or use one Transport with your own Proxy function that picks the port based on something in the request: the field accepts any function with the signature func(*http.Request) (*url.URL, error). A plan allows up to 1000 concurrent connections, so cap your worker pool size accordingly.

Step 4. SOCKS5 without third-party packages

The Transport determines the proxy type from the address scheme: http, https, socks5 and socks5h are supported, and with no scheme http is assumed. SOCKS5 in Transport.Proxy has worked since Go 1.9. In the example, just change Scheme to "socks5" and the port to 824.

The Go 1.23+ documentation clarifies that socks5 is handled the same way as socks5h: site names are resolved by the proxy, not by your computer. The Transport also uses the username and password from url.UserPassword for SOCKS5.

SOCKS5 for TCP: golang.org/x/net/proxy

If you need to send your own protocol over TCP through the proxy rather than HTTP, you'll need the golang.org/x/net/proxy package. The proxy.SOCKS5 function creates a dialer with the username and password in proxy.Auth:

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

// SOCKS5 connection with username and password
auth := &proxy.Auth{User: "USERNAME", Password: "PASSWORD"}
dialer, err := proxy.SOCKS5("tcp", "gw.prokso.com:824", auth, proxy.Direct)
if err != nil {
	panic(err)
}
conn, err := dialer.Dial("tcp", "example.com:443")

The package can also do proxy.FromURL - a dialer from the same socks5:// address, and dialers that support a context implement proxy.ContextDialer. Remember that only TCP goes through Prokso: UDP protocols can't be sent this way.

HTTP_PROXY and HTTPS_PROXY variables in Go

The standard http.DefaultTransport (and so http.Get without your own client) gets the proxy through http.ProxyFromEnvironment from the HTTP_PROXY, HTTPS_PROXY and NO_PROXY variables; the lowercase versions take precedence.

export HTTPS_PROXY="http://USERNAME:PASSWORD@gw.prokso.com:823"
export HTTP_PROXY="http://USERNAME:PASSWORD@gw.prokso.com:823"
export NO_PROXY="localhost,127.0.0.1"
go run .

A few quirks. Go treats a value without a scheme, like gw.prokso.com:823, as an http address. Requests to localhost and loopback addresses never go through the proxy. And in the current implementation the variables are read once per process lifetime, so changing them from code after the first request is pointless.

In your own Transport with http.ProxyURL, as in step 2, environment variables have no effect. That is handy on a server where they may be set for other programs.

How to read net/http errors

  • proxyconnect tcp: … - Go couldn't connect to the proxy itself: a wrong host or port, or the network to gw.prokso.com is blocked (connection refused).
  • Proxy Authentication Required - on CONNECT Go returns the status text the proxy sent. Check the username, password and remaining traffic: when the traffic is used up, the response is the same (code 407).
  • context deadline exceeded / Client.Timeout exceeded - the client timeout has expired. Often this is the http scheme on port 824 (proxy timeout).
  • Code 403 or 429 in resp.StatusCode - the proxy worked; the site is the one responding. On port 823 a retry goes out from a new address.

Checking: the IP in the program output

Run the example: go run . prints the IP the site saw the request coming from. It shouldn't match your machine's address. Two runs in a row on port 823 give two different addresses; on a sticky port, the same one.

Check the program output, not the browser: What is my IP shows the IP of the browser, which goes direct. If the program can't connect, first check the string in the proxy checker: that will show whether the mistake is in the data or in the code.

If the proxy does not work

FAQ

Do I need a third-party package for SOCKS5 in Go?

For HTTP requests, no: http.Transport has understood the socks5:// scheme since Go 1.9. The golang.org/x/net/proxy package is only needed for SOCKS5 in plain TCP connections.

Why does Go ignore HTTPS_PROXY?

Only http.ProxyFromEnvironment reads the variables. If your Transport uses http.ProxyURL or the Proxy field is empty, the variables have no effect. They also never apply to localhost.

How do I pick different proxies for different requests?

Write your own function for Transport.Proxy: it receives the *http.Request and returns the address you need, for example a sticky port depending on the account. You still keep a single Transport.

Why aren't proxy connections being reused?

Most often the response body isn't read to the end or isn't closed. Use defer resp.Body.Close() and read the body to the end, even if you don't need it.

How many goroutines can I run at once?

As many as you like, but a plan allows up to 1000 concurrent connections. Limit the number of workers or MaxConnsPerHost in the Transport so you don't hit the limit.

Was this guide helpful?

Proxy not working? Check it in 6 steps

Install the Prokso app