Интеграция прокси с Go
In Go, an http.Client goes through a proxy when its Transport has a Proxy function: build the proxy URL with url.UserPassword for the credentials and pass it to http.ProxyURL. net/http then sends the username and password to the proxy for both http and https targets, tunneling HTTPS with CONNECT. For SOCKS5, create a dialer with golang.org/x/net/proxy and plug its DialContext into the Transport. Build each client once and reuse it, since every Transport keeps its own connection pool.
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"time"
"golang.org/x/net/proxy"
)
// Residential gateway (gw-eu, gw-usa, gw-asia or gw-asia2), port 8888 or 7777 on Private 2.
// ISP proxy: take one ip:port:user:pass line and use IP:PORT, USER and PASS.
const (
proxyAddr = "gw-eu.murphyproxies.com:8888"
proxyUser = "USERNAME"
proxyPass = "PASSWORD"
)
// HTTP proxy: net/http reads the credentials from the URL and sends
// Proxy-Authorization, tunneling https:// targets with CONNECT.
func httpProxyClient() *http.Client {
proxyURL := &url.URL{
Scheme: "http",
User: url.UserPassword(proxyUser, proxyPass),
Host: proxyAddr,
}
t := http.DefaultTransport.(*http.Transport).Clone()
t.Proxy = http.ProxyURL(proxyURL)
return &http.Client{Transport: t, Timeout: 30 * time.Second}
}
// SOCKS5 proxy: same address and credentials, through golang.org/x/net/proxy.
func socks5Client() (*http.Client, error) {
auth := &proxy.Auth{User: proxyUser, Password: proxyPass}
dialer, err := proxy.SOCKS5("tcp", proxyAddr, auth, proxy.Direct)
if err != nil {
return nil, err
}
t := http.DefaultTransport.(*http.Transport).Clone()
t.Proxy = nil // the dialer handles the proxy, ignore HTTPS_PROXY
t.DialContext = dialer.(proxy.ContextDialer).DialContext
return &http.Client{Transport: t, Timeout: 30 * time.Second}, nil
}
func main() {
socks, err := socks5Client()
if err != nil {
panic(err)
}
clients := map[string]*http.Client{"http": httpProxyClient(), "socks5": socks}
for name, c := range clients {
resp, err := c.Get("https://httpbin.org/ip")
if err != nil {
fmt.Println(name, "error:", err)
continue
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(name, resp.Status, string(body))
}
}Настройка за 5 шагов
- 1Add the SOCKS package if you need it with go get golang.org/x/net/proxy. The HTTP proxy path needs only the standard library.
- 2Fill in the Murphy address and credentials: gw-eu.murphyproxies.com:8888 for residential (gw-usa, gw-asia, gw-asia2, or 7777 on Private 2), or the IP:PORT of one ISP line with its user and pass.
- 3For HTTP, build the proxy URL with url.UserPassword, clone http.DefaultTransport and set its Proxy to http.ProxyURL(proxyURL).
- 4For SOCKS5, create a dialer with proxy.SOCKS5 and a proxy.Auth, then set the cloned Transport's DialContext to the dialer's DialContext.
- 5Create each client once with a Timeout, reuse it across requests, and print the body of an IP echo endpoint to confirm the exit IP.
Вопросы о прокси для Go
Всё, что нужно, чтобы пустить Go через Murphy, от авторизации до ротации.
Yes. http.DefaultTransport uses http.ProxyFromEnvironment, so exporting HTTPS_PROXY=http://USERNAME:PASSWORD@gw-eu.murphyproxies.com:8888 is enough for code that uses the default client. Requests to localhost are never proxied that way, and the variables are read once per process, so set them before the program starts.
url.UserPassword keeps the username and password as separate values, so characters like @, : or / in a password cannot break URL parsing. net/http reads them straight from the URL struct when it builds the Proxy-Authorization header, with no manual escaping on your side.
Yes. http.ProxyURL also accepts a socks5:// URL with credentials, and net/http dials it itself. The x/net/proxy dialer is still the better tool when you need SOCKS5 outside HTTP, for raw TCP or gRPC, or want to control dialing yourself.
An HTTPS tunnel through the proxy keeps one exit IP for as long as it stays open, and Go's Transport keeps connections alive in its pool, so requests to the same host can reuse one tunnel and one IP. Set DisableKeepAlives: true on the Transport to open a new connection every time. To keep the same IP on purpose, for up to 48 hours, use a sticky session from the dashboard instead.
A 407 Proxy Authentication Required means the proxy got no credentials or the wrong ones, so check the username and password and that they sit on the proxy URL, not on the request. An error starting with proxyconnect tcp means Go could not reach the proxy at all, usually a wrong host or port. The guide at murphyproxies.com/guides/proxy-error-codes covers the rest.
Получите доступы для Go
Выберите сеть под свою задачу. ISP-прокси для быстрых и стабильных сессий, резидентные, когда против жёстких антиботов нужно много разных IP.