go-tg-proxy

module
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

README

go-tg-proxy

Reusable building blocks for running go-telegram/bot bots over SOCKS5 proxies: a latency-checked proxy pool with a pluggable source, and a bot lifecycle manager that restarts the bot on a new proxy when the connection degrades.

Built for Telegram bots that run from restricted environments (e.g. Russia), where direct access to api.telegram.org is blocked and proxy rotation is a must. Extracted from raspishika-gx.

Packages

Package Purpose
proxyutil NewHTTPProxyClient — an *http.Client dialing through a SOCKS5 proxy (context-aware).
proxy Source interface + ProxiflySource (default provider), in-memory Repository, latency Checker + TelegramChecker, and a Service that keeps a warm pool of verified proxies.
botservice BotService — starts/stops/restarts a *bot.Bot, picking the best available proxy, with a watchdog that rotates the proxy when Telegram stops answering.

Quick start

source := proxy.NewProxiflySource(proxy.DefaultProxiflySourceURL)
proxies := proxy.NewService(source)
proxies.StartPoolLoop()
defer proxies.Stop()

proxyAddr, err := proxies.FirstAvailable(ctx)
if err != nil {
    return err
}

httpClient, err := proxyutil.NewHTTPProxyClient(proxyAddr)
if err != nil {
    return err
}
b, err := bot.New(token,
    bot.WithHTTPClient(10*time.Second, httpClient),
    bot.WithCheckInitTimeout(10*time.Second),
)
if err != nil {
    return err
}

Or let botservice own the whole lifecycle:

bs := botservice.NewBotService(func(proxy string, onActivity func()) (*bot.Bot, error) {
    httpClient, err := proxyutil.NewHTTPProxyClient(proxy)
    if err != nil {
        return nil, err
    }
    return bot.New(token,
        bot.WithHTTPClient(10*time.Second, httpClient),
        bot.WithCheckInitTimeout(10*time.Second),
        bot.WithMiddlewares(func(next bot.HandlerFunc) bot.HandlerFunc {
            return func(ctx context.Context, b *bot.Bot, u *models.Update) { onActivity(); next(ctx, b, u) }
        }),
    )
}, proxies)
bs.Start(ctx)

Configuration

  • The proxy source is injected via the proxy.Source interface — swap ProxiflySource for any provider that returns ip:port strings.
  • ProxiflySource defaults to keeping only non-Russian socks5 proxies from the proxifly jsdelivr mirror. Tune it with options:
proxy.NewProxiflySource(proxy.DefaultProxiflySourceURL,
    proxy.WithProxiflyProtocols("socks5", "socks4"),
    proxy.WithProxiflyExcludeCountries("RU", "CN"),
    proxy.WithProxiflyClient(&http.Client{Timeout: time.Minute}),
)
  • proxy.NewService(source, ...) accepts WithChecker, WithRepository, WithPoolSize, WithFinderWorkers, WithPoolRecheck, WithCacheTTL.

How proxies are verified

TelegramChecker probes api.telegram.org end-to-end with a fake token: a proxy is usable when the API replies "not found" (i.e. the request actually reached Telegram). The probe measures round-trip latency, so the pool always prefers the fastest live proxy.

BotService runs a watchdog that probes getMe every 30 seconds: when the API becomes unreachable it restarts the bot on the next best proxy. A quiet period with no incoming updates is not treated as a failure — many bots simply have no traffic at night, and restarting on inactivity kept rotating a healthy proxy.

License

MIT.

Directories

Path Synopsis
Package botservice manages the lifecycle of a go-telegram/bot instance that runs over a SOCKS5 proxy: it builds the bot through the given builder, keeps the connection warm with a watchdog and restarts the bot with a new proxy when the current one becomes unreachable.
Package botservice manages the lifecycle of a go-telegram/bot instance that runs over a SOCKS5 proxy: it builds the bot through the given builder, keeps the connection warm with a watchdog and restarts the bot with a new proxy when the current one becomes unreachable.
Package proxy provides a latency-checked pool of SOCKS5 proxies together with helpers for building Telegram bot HTTP clients through them.
Package proxy provides a latency-checked pool of SOCKS5 proxies together with helpers for building Telegram bot HTTP clients through them.

Jump to

Keyboard shortcuts

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