proxy

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package proxy provides a latency-checked pool of SOCKS5 proxies together with helpers for building Telegram bot HTTP clients through them.

Index

Constants

View Source
const DefaultCheckTimeout = 5 * time.Second

DefaultCheckTimeout bounds a single proxy probe.

View Source
const DefaultProxiflySourceURL = "https://cdn.jsdelivr.net/gh/proxifly/free-proxy-list@main/proxies/all/data.json"

DefaultProxiflySourceURL is the default mirror of the proxifly free-proxy-list.

Variables

View Source
var (
	// ErrEmptyProxy is returned when a proxy address is empty.
	ErrEmptyProxy = errors.New("empty proxy")
	// ErrProxyUnavailable is returned when a proxy is unreachable.
	ErrProxyUnavailable = errors.New("proxy unavailable")
)
View Source
var ErrNoAvailableProxy = errors.New("no available proxy")

ErrNoAvailableProxy is returned when no usable proxy can be found.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	CheckLatency(ctx context.Context, proxy string) (time.Duration, error)
}

Checker checks a single proxy and reports its round-trip latency.

type Option

type Option func(*Service)

Option configures a Service.

func WithCacheTTL

func WithCacheTTL(d time.Duration) Option

WithCacheTTL sets how long a fetched proxy list is considered actual. Defaults to 1 hour.

func WithChecker

func WithChecker(c Checker) Option

WithChecker overrides the proxy availability checker. Defaults to TelegramChecker.

func WithFinderWorkers

func WithFinderWorkers(n int) Option

WithFinderWorkers sets the concurrency of latency scans. Defaults to 128.

func WithPoolRecheck

func WithPoolRecheck(d time.Duration) Option

WithPoolRecheck sets how often the warm pool is revalidated in the background. Defaults to 30 seconds.

func WithPoolSize

func WithPoolSize(n int) Option

WithPoolSize sets the number of proxies kept warm in the pool. Defaults to 3.

func WithRepository

func WithRepository(repo Repository) Option

WithRepository overrides the proxy store. Defaults to an in-memory repository.

type ProxiflyOption

type ProxiflyOption func(*ProxiflySource)

ProxiflyOption configures a ProxiflySource.

func WithProxiflyClient

func WithProxiflyClient(c *http.Client) ProxiflyOption

WithProxiflyClient overrides the HTTP client used to fetch the list. Defaults to a client with a 30s timeout.

func WithProxiflyExcludeCountries

func WithProxiflyExcludeCountries(countries ...string) ProxiflyOption

WithProxiflyExcludeCountries excludes proxies geolocated in the given countries (ISO 3166-1 alpha-2, e.g. "RU"). Defaults to ["RU"].

func WithProxiflyProtocols

func WithProxiflyProtocols(protocols ...string) ProxiflyOption

WithProxiflyProtocols restricts the source to the given proxy protocols (e.g. "socks5"). Defaults to ["socks5"].

type ProxiflySource

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

ProxiflySource is a Source that fetches the proxifly free-proxy-list JSON. It knows the proxifly schema and applies the protocol/country filters, so the rest of the package deals only with "ip:port" strings.

func NewProxiflySource

func NewProxiflySource(url string, opts ...ProxiflyOption) *ProxiflySource

NewProxiflySource returns a ProxiflySource. Unless overridden by options, it keeps only socks5 proxies that are not geolocated in Russia.

func (*ProxiflySource) Fetch

func (s *ProxiflySource) Fetch(ctx context.Context) ([]string, error)

Fetch downloads the proxy list, filters it and returns the usable "ip:port" addresses. An error is returned when the list cannot be fetched or contains no usable proxies.

type Repository

type Repository interface {
	// Replace atomically replaces the list with the given proxies.
	Replace(proxies []string)
	// All returns all proxies in the list.
	All() []string
	// Next returns the next proxy in round-robin rotation, wrapping around.
	Next() (idx int, proxy string)
	// UpdatedAt returns the time of the last successful Replace.
	UpdatedAt() time.Time
}

Repository stores an ordered proxy list in memory.

func NewMemoryRepository

func NewMemoryRepository() Repository

NewMemoryRepository returns a thread-safe in-memory Repository.

type Service

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

Service keeps a warm pool of latency-checked proxies fetched from a Source and hands out the best available one on demand.

func NewService

func NewService(source Source, opts ...Option) *Service

NewService returns a Service that fetches proxies from source.

func (*Service) Check

func (s *Service) Check(ctx context.Context, proxy string) error

Check checks the availability of a proxy and returns an error if it is unavailable.

Returns ErrProxyUnavailable if the proxy is unavailable, or ErrEmptyProxy if the proxy is empty.

func (*Service) FirstAvailable

func (s *Service) FirstAvailable(ctx context.Context) (string, error)

FirstAvailable returns the best available proxy from the warm pool.

Falls back to a full scan of the list when the pool is empty.

func (*Service) HealthCheck

func (s *Service) HealthCheck() error

HealthCheck performs a health check on the proxy service, ensuring the proxy source is reachable and returns at least one proxy.

func (*Service) Next

func (s *Service) Next() string

Next returns the next proxy from the list

func (*Service) NextChecked

func (s *Service) NextChecked(ctx context.Context) (string, error)

NextChecked returns the next proxy from the list that is checked to be available.

Returns ErrNoAvailableProxy if no proxy is available.

func (*Service) StartPoolLoop

func (s *Service) StartPoolLoop()

StartPoolLoop starts the background goroutine that keeps the pool of verified proxies warm. It is started lazily on the first FirstAvailable call as well.

func (*Service) Stop

func (s *Service) Stop()

func (*Service) UpdateProxies

func (s *Service) UpdateProxies() error

UpdateProxies synchronously updates the proxy list from the source URL. When the list cannot be fetched or parsed, the previous list is kept and an error is returned. The update is skipped entirely when the last successful one is younger than the configured cache TTL.

type Source

type Source interface {
	Fetch(ctx context.Context) ([]string, error)
}

Source fetches a list of usable proxies as "ip:port" strings. Implementations are responsible for parsing and filtering the raw data (protocol, country, ...) and for returning an error when the list cannot be fetched or contains nothing usable, so the caller can keep the previously known proxies.

type TelegramChecker

type TelegramChecker struct{}

TelegramChecker verifies a proxy end-to-end against the Telegram Bot API. The API call uses a fake token and the proxy is considered usable when the API answers "not found" — i.e. the proxy reaches Telegram.

func (TelegramChecker) CheckLatency

func (TelegramChecker) CheckLatency(ctx context.Context, proxy string) (time.Duration, error)

CheckLatency checks the availability of a proxy and returns its round-trip latency.

Jump to

Keyboard shortcuts

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