proxy

package
v0.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package proxy manages HTTP/SOCKS proxy pools, health checking, and rotation strategies for the Foxhound scraping framework.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoProxy = errors.New("proxy: no proxy available")

ErrNoProxy is returned when the pool has no usable proxy available.

Functions

This section is empty.

Types

type HealthChecker

type HealthChecker struct {
	// contains filtered or unexported fields
}

HealthChecker probes proxies against a test endpoint to determine liveness and measure latency.

func NewHealthChecker

func NewHealthChecker(checkURL string, timeout time.Duration) *HealthChecker

NewHealthChecker creates a HealthChecker that probes proxies by requesting checkURL. timeout is applied to each individual probe.

func (*HealthChecker) Check

func (hc *HealthChecker) Check(ctx context.Context, p *Proxy) ProxyHealth

Check probes a single proxy and returns its updated health. The ProxyHealth.Score is set to 1.0 for alive proxies and 0.0 for dead ones.

The request is routed through the proxy under test so that the latency and reachability measurement reflects actual proxy connectivity.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool manages a set of proxies sourced from one or more Provider instances. It tracks health, enforces cooldowns, and selects the best proxy on each Get.

func NewPool

func NewPool(providers ...Provider) *Pool

NewPool creates a Pool pre-loaded with proxies from the given providers. Providers are queried once during construction using a background context. If a provider fails it is logged and skipped.

func (*Pool) Ban

func (p *Pool) Ban(px *Proxy, domain string)

Ban marks a proxy as banned for a specific domain and puts it on cooldown.

func (*Pool) Close

func (p *Pool) Close() error

Close shuts down the pool and releases resources.

func (*Pool) Get

func (p *Pool) Get(ctx context.Context) (*Proxy, error)

Get returns a proxy according to the configured rotation strategy. It blocks until a proxy is available or the context is cancelled.

When all proxies are on cooldown it sleeps until the earliest cooldown expiry rather than busy-waiting. If the pool is empty it returns ErrNoProxy immediately.

func (*Pool) GetForDomain

func (p *Pool) GetForDomain(ctx context.Context, domain string) (*Proxy, error)

GetForDomain returns a sticky proxy for the given domain. The same proxy is returned on every subsequent call with the same domain as long as the proxy remains available. If the sticky proxy is no longer available a new one is assigned.

func (*Pool) GetForGeo

func (p *Pool) GetForGeo(ctx context.Context, country, city string) (*Proxy, error)

GetForGeo returns a proxy matching the requested country (and optionally city). Falls back to any available proxy if no geo match is found.

func (*Pool) GetForSession

func (p *Pool) GetForSession(ctx context.Context, sessionID string) (*Proxy, error)

GetForSession returns a sticky proxy for the given sessionID. The same proxy is returned on every subsequent call with the same sessionID as long as the proxy remains available. If the sticky proxy is no longer available a new one is assigned.

func (*Pool) Health

func (p *Pool) Health(px *Proxy) ProxyHealth

Health returns a snapshot of the ProxyHealth for the given proxy.

func (*Pool) Len

func (p *Pool) Len() int

Len returns the total number of proxies in the pool (including on cooldown).

func (*Pool) Release

func (p *Pool) Release(px *Proxy, success bool)

Release returns a proxy to the pool after use, updating its health score. success=true increments the success rate; success=false decreases the score. When maxRequests > 0 and the proxy has now reached that limit it is placed on cooldown automatically.

func (*Pool) SetCooldown

func (p *Pool) SetCooldown(d time.Duration)

SetCooldown changes the duration proxies remain on cooldown after being banned.

func (*Pool) SetMaxRequests

func (p *Pool) SetMaxRequests(n int)

SetMaxRequests sets the maximum number of requests a proxy handles before automatic cooldown. 0 means unlimited.

func (*Pool) SetRotation

func (p *Pool) SetRotation(strategy RotationStrategy)

SetRotation changes the rotation strategy. Safe to call at any time.

type Provider

type Provider interface {
	// Proxies returns the current list of available proxies.
	Proxies(ctx context.Context) ([]*Proxy, error)
}

Provider is the source of proxy addresses for a pool.

func Static

func Static(urls []string) Provider

Static creates a Provider backed by a hardcoded list of proxy URL strings. Invalid URLs are logged and skipped rather than causing an error.

type Proxy

type Proxy struct {
	// URL is the full proxy URL, e.g. "http://user:pass@host:port".
	URL string
	// Protocol is one of "http", "https", or "socks5".
	Protocol string
	// Host is the proxy hostname or IP address.
	Host string
	// Port is the proxy port number as a string.
	Port string
	// Username is the proxy auth username (may be empty).
	Username string
	// Password is the proxy auth password (may be empty).
	Password string
	// Country is the ISO 3166-1 alpha-2 country code of the proxy (optional).
	Country string
	// City is the city name of the proxy (optional).
	City string
}

Proxy holds the parsed coordinates of a single proxy server.

func Parse

func Parse(raw string) (*Proxy, error)

Parse converts a raw proxy string into a Proxy value.

Accepted formats (in order of detection):

  • protocol://user:pass@host:port — standard URL (already had scheme)
  • user:pass@host:port — URL without scheme (http assumed)
  • host:port — bare address (http, no auth)
  • host:port:user:pass — colon-separated, port is numeric
  • user:pass:host:port — colon-separated, port is last field
  • protocol:host:port:user:pass — five colon-separated fields

type ProxyHealth

type ProxyHealth struct {
	// Alive indicates whether the proxy is reachable.
	Alive bool
	// Latency is the round-trip time to the proxy check endpoint.
	Latency time.Duration
	// SuccessRate is the fraction of requests that succeeded (0.0–1.0).
	SuccessRate float64
	// BlockRate is the fraction of requests that were blocked (0.0–1.0).
	BlockRate float64
	// BanCount is the total number of domain bans recorded for this proxy.
	BanCount int
	// CooldownUntil is the time after which the proxy may be used again.
	CooldownUntil time.Time
	// Score is an aggregate quality score from 0.0 (dead) to 1.0 (perfect).
	Score float64
}

ProxyHealth records the current health state of a proxy.

type RotationStrategy

type RotationStrategy int

RotationStrategy controls when the pool selects a new proxy.

const (
	// PerRequest rotates the proxy on every outbound request.
	PerRequest RotationStrategy = iota
	// PerSession keeps the same proxy for the lifetime of a walker session.
	PerSession
	// PerDomain keeps the same proxy per target domain.
	PerDomain
	// OnBlock only rotates when a proxy is detected as blocked.
	OnBlock
)

type StaticProvider

type StaticProvider struct {
	// contains filtered or unexported fields
}

StaticProvider serves a fixed list of proxy URLs.

func (*StaticProvider) Proxies

func (s *StaticProvider) Proxies(_ context.Context) ([]*Proxy, error)

Proxies parses and returns all valid proxies from the static list. Entries that cannot be parsed are logged at WARN level and omitted.

Directories

Path Synopsis
Package providers contains third-party proxy provider adapters that implement the proxy.Provider interface.
Package providers contains third-party proxy provider adapters that implement the proxy.Provider interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL