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
- Variables
- type Checker
- type Option
- type ProxiflyOption
- type ProxiflySource
- type Repository
- type Service
- func (s *Service) Check(ctx context.Context, proxy string) error
- func (s *Service) FirstAvailable(ctx context.Context) (string, error)
- func (s *Service) HealthCheck() error
- func (s *Service) Next() string
- func (s *Service) NextChecked(ctx context.Context) (string, error)
- func (s *Service) StartPoolLoop()
- func (s *Service) Stop()
- func (s *Service) UpdateProxies() error
- type Source
- type TelegramChecker
Constants ¶
const DefaultCheckTimeout = 5 * time.Second
DefaultCheckTimeout bounds a single proxy probe.
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 ¶
var ( // ErrEmptyProxy is returned when a proxy address is empty. ErrEmptyProxy = errors.New("empty proxy") ErrProxyUnavailable = errors.New("proxy unavailable") )
var ErrNoAvailableProxy = errors.New("no available proxy")
ErrNoAvailableProxy is returned when no usable proxy can be found.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Service)
Option configures a Service.
func WithCacheTTL ¶
WithCacheTTL sets how long a fetched proxy list is considered actual. Defaults to 1 hour.
func WithChecker ¶
WithChecker overrides the proxy availability checker. Defaults to TelegramChecker.
func WithFinderWorkers ¶
WithFinderWorkers sets the concurrency of latency scans. Defaults to 128.
func WithPoolRecheck ¶
WithPoolRecheck sets how often the warm pool is revalidated in the background. Defaults to 30 seconds.
func WithPoolSize ¶
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.
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 ¶
NewService returns a Service that fetches proxies from source.
func (*Service) Check ¶
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 ¶
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 ¶
HealthCheck performs a health check on the proxy service, ensuring the proxy source is reachable and returns at least one proxy.
func (*Service) NextChecked ¶
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) UpdateProxies ¶
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 ¶
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 ¶
CheckLatency checks the availability of a proxy and returns its round-trip latency.