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.

main.gogo
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ステップで設定

  1. 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.
  2. 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.
  3. 3For HTTP, build the proxy URL with url.UserPassword, clone http.DefaultTransport and set its Proxy to http.ProxyURL(proxyURL).
  4. 4For SOCKS5, create a dialer with proxy.SOCKS5 and a proxy.Auth, then set the cloned Transport's DialContext to the dialer's DialContext.
  5. 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経由で使うためのポイントをまとめました。

Go用の認証情報を取得

ターゲットに合ったネットワークを選びましょう。高速で安定したセッションにはISPプロキシ、厳しいボット対策には幅広いIPを持つレジデンシャルプロキシ。