Go

Go

標準の net/http パッケージを使って Go から RaxyProxy を利用します。

基本 HTTP

追加パッケージ不要 — Go の標準ライブラリが HTTP プロキシをネイティブに処理します。

main.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

func main() {
	proxyURL, _ := url.Parse("http://USERNAME:PASSWORD@proxy.raxyproxy.com:823")

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

	resp, err := client.Get("https://httpbin.org/ip")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var result map[string]string
	json.NewDecoder(resp.Body).Decode(&result)
	fmt.Println("IP:", result["origin"])
}

Geo ターゲティング

geo.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

func proxyClient(username, password string) *http.Client {
	rawURL  := fmt.Sprintf("http://%s:%s@proxy.raxyproxy.com:823", username, password)
	proxyURL, _ := url.Parse(rawURL)
	return &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	}
}

func getIP(client *http.Client) string {
	resp, _ := client.Get("https://httpbin.org/ip")
	defer resp.Body.Close()
	var data map[string]string
	json.NewDecoder(resp.Body).Decode(&data)
	return data["origin"]
}

func main() {
	// Country only — free
	clientUS := proxyClient("USERNAME__cr.us", "PASSWORD")
	fmt.Println("US:", getIP(clientUS))

	// City — 2× billing for residential/mobile/datacenter
	clientLA := proxyClient("USERNAME__cr.us;city.losangeles", "PASSWORD")
	fmt.Println("LA:", getIP(clientLA))

	// Sticky sessid
	clientSticky := proxyClient("USERNAME__sessid.session-42", "PASSWORD")
	fmt.Println("Sticky:", getIP(clientSticky))
}

並行リクエスト

concurrent.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"sync"
)

func newClient() *http.Client {
	proxyURL, _ := url.Parse("http://USERNAME:PASSWORD@proxy.raxyproxy.com:823")
	return &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	}
}

func fetchIP(client *http.Client, id int, wg *sync.WaitGroup) {
	defer wg.Done()
	resp, err := client.Get("https://httpbin.org/ip")
	if err != nil {
		fmt.Printf("Request %d error: %v
", id, err)
		return
	}
	defer resp.Body.Close()
	var data map[string]string
	json.NewDecoder(resp.Body).Decode(&data)
	fmt.Printf("Request %d: %s
", id, data["origin"])
}

func main() {
	client := newClient()
	var wg sync.WaitGroup

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go fetchIP(client, i, &wg)
	}
	wg.Wait()
}

スティッキーセッション — ポートベース

sticky.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"sync"
)

func stickyClient(port int) *http.Client {
	rawURL   := fmt.Sprintf("http://USERNAME:PASSWORD@proxy.raxyproxy.com:%d", port)
	proxyURL, _ := url.Parse(rawURL)
	return &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	}
}

func main() {
	// 5 independent sticky slots, each on a different port
	var wg sync.WaitGroup
	for slot := 0; slot < 5; slot++ {
		wg.Add(1)
		go func(slot int) {
			defer wg.Done()
			client := stickyClient(10001 + slot)
			resp, _ := client.Get("https://httpbin.org/ip")
			defer resp.Body.Close()
			var data map[string]string
			json.NewDecoder(resp.Body).Decode(&data)
			fmt.Printf("Slot %d port %d: %s
", slot, 10001+slot, data["origin"])
		}(slot)
	}
	wg.Wait()
}