集成

将Shifter与以下工具配合使用 Go

几分钟内即可将 Shifter 的住宅代理和 ISP 代理接入 Go 项目。支持标准 net/http、Resty、GoQuery、Colly 及任何接受 *url.URL 的客户端,无需 SDK。

快速入门

安装

go get github.com/go-resty/resty/v2

基本用法

package main

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

func main() {
	proxyURL, _ := url.Parse(
		"customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443",
	)

	client := &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	}

	resp, _ := client.Get("https://ipinfo.io/json")
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
	// {"ip": "154.16.xxx.xxx", "city": "New York", "country": "US", ...}
}

功能特性

开箱即用,支持 net/http、Resty、GoQuery、Colly、fasthttp 以及任何接受代理 *url.URL 的客户端
默认按请求轮换,使用`sid`实现粘性会话,使用`ttl-N`实现N秒定时固定
通过 chromedp 和 Rod 支持无头浏览器,用于处理 JavaScript 渲染的目标页面
同一网关端点支持 HTTP、HTTPS 和 SOCKS5 协议
通过用户名参数在 195+ 个国家/地区进行地理定位 — 无需额外 SDK
兼容 Go 1.18 及以上所有版本,包括模块、go-workspaces 和无 CGO 构建

示例

标准 net/http 粘性会话配置

零依赖方案。构建一个带有代理 URL 的 *http.Transport——同一客户端可在多个 goroutine 中安全复用。

package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"
)

func newClient(country, city string) *http.Client {
	sidBytes := make([]byte, 4)
	rand.Read(sidBytes)
	sid := hex.EncodeToString(sidBytes)

	proxyURL, _ := url.Parse(fmt.Sprintf(
		"customer-USERNAME-country-%s-city-%s-sid-%s-ttl-300:PASSWORD@p.shifter.io:443",
		country, city, sid,
	))

	return &http.Client{
		Timeout: 30 * time.Second,
		Transport: &http.Transport{
			Proxy:                 http.ProxyURL(proxyURL),
			MaxIdleConnsPerHost:   16,
			IdleConnTimeout:       90 * time.Second,
			TLSHandshakeTimeout:   10 * time.Second,
			ResponseHeaderTimeout: 20 * time.Second,
		},
	}
}

func main() {
	c := newClient("uk", "london")

	for _, path := range []string{"/login", "/dashboard", "/orders"} {
		req, _ := http.NewRequest("GET", "https://example.co.uk"+path, nil)
		req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")

		resp, err := c.Do(req)
		if err != nil {
			fmt.Println("error:", err)
			continue
		}
		body, _ := io.ReadAll(resp.Body)
		resp.Body.Close()
		fmt.Println(path, resp.StatusCode, len(body), "bytes")
	}
}

Resty(更符合人体工程学)

Resty 在 net/http 基础上提供流式构建器、自动重试和响应绑定,非常适合需要清晰错误处理的抓取管道。

package main

import (
	"fmt"

	"github.com/go-resty/resty/v2"
)

type Product struct {
	ID    int     `json:"id"`
	Name  string  `json:"name"`
	Price float64 `json:"price"`
}

func main() {
	client := resty.New().
		SetProxy("customer-USERNAME-country-de-sid-456DEF:PASSWORD@p.shifter.io:443").
		SetRetryCount(3).
		SetRetryWaitTime(2_000_000_000). // 2s in ns
		SetHeader("User-Agent", "Mozilla/5.0 (Macintosh) AppleWebKit/537.36")

	var products []Product
	resp, err := client.R().
		SetResult(&products).
		Get("https://api.example.de/products")

	if err != nil || !resp.IsSuccess() {
		fmt.Println("error:", err, resp.Status())
		return
	}

	for _, p := range products {
		fmt.Printf("%d %s — %.2f EUR\n", p.ID, p.Name, p.Price)
	}
}

Colly(爬取框架)

Colly 是事实上的 Go 爬取库。通过 SetProxy() 配置代理——爬虫发出的每个请求都将通过 Shifter 路由。

package main

import (
	"fmt"
	"log"

	"github.com/gocolly/colly/v2"
)

func main() {
	c := colly.NewCollector(
		colly.AllowedDomains("example.com"),
		colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"),
	)

	if err := c.SetProxy(
		"customer-USERNAME-country-fr-city-paris-sid-789GHI:PASSWORD@p.shifter.io:443",
	); err != nil {
		log.Fatal(err)
	}

	c.OnHTML("article.post", func(e *colly.HTMLElement) {
		fmt.Println(e.ChildText("h2"), "->", e.ChildAttr("a", "href"))
	})

	c.OnError(func(r *colly.Response, err error) {
		fmt.Printf("error %d: %v\n", r.StatusCode, err)
	})

	c.Visit("https://example.com/blog")
}

chromedp(无头 Chromium)

通过 Shifter 驱动真实的 Chromium 实例,用于处理 JavaScript 渲染的目标页面。chromedp 使用 Chrome 的 CDP 协议,无需 Selenium 依赖。

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/chromedp/chromedp"
)

func main() {
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.ProxyServer("http://p.shifter.io:443"),
		chromedp.Flag("headless", true),
	)

	allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	ctx, cancel := chromedp.NewContext(allocCtx)
	defer cancel()

	ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
	defer cancel()

	var html string
	err := chromedp.Run(ctx,
		// chromedp doesn't natively prompt for proxy auth — use a Chrome
		// extension or run a sidecar that injects credentials, or use
		// Shifter's IP-whitelist auth instead of user/pass.
		chromedp.Navigate("https://example.com"),
		chromedp.WaitVisible("body", chromedp.ByQuery),
		chromedp.OuterHTML("html", &html),
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(len(html), "bytes")
}
常见问题

常见问题

关于将 Shifter 与 Go 搭配使用的常见问题。

使用 url.Parse 解析代理 URL,然后通过 http.ProxyURL(proxyURL) 将其传递给 *http.Transport。将该 transport 用于 *http.Client。该客户端可安全地在多个 goroutine 间共享,并自动复用连接。

立即开始

开始将Shifter与以下工具配合使用 Go

在 5 分钟内将 Shifter 的 205M+ 住宅和 ISP 代理添加到您的 Go 服务中。支持 net/http、Resty、Colly 和 chromedp。

免费试用 Shifter几分钟内完成设置,随时可取消。