cligram.proxy_manager

Proxy management and testing for Telegram connections.

class cligram.proxy_manager.Proxy(url, type, host, port, secret=None, username=None, password=None)[source]

Bases: object

Proxy connection configuration.

Supports both MTProto and SOCKS5 protocols with their respective authentication methods. For MTProto, requires secret key; for SOCKS5, supports optional username/password authentication.

host: str

Proxy server hostname or IP address

property is_direct: bool

Check if the proxy is a direct connection.

password: Optional[str] = None

SOCKS5 authentication password

port: int

Proxy server port number

secret: Optional[str] = None

MTProto secret key (hex or base64 encoded)

type: ProxyType

Type of proxy protocol to use

url: str

Original proxy URL string

username: Optional[str] = None

SOCKS5 authentication username

class cligram.proxy_manager.ProxyManager[source]

Bases: object

Manages proxy connections and testing.

Provides functionality to add, test, and select working proxies for Telegram connections.

add_proxy(proxy_url)[source]

Add new proxy from URL string.

Parameters:

proxy_url (str) – URL string in supported format

Return type:

Optional[Proxy]

Returns:

Configured Proxy instance if parsing successful

current_proxy: Optional[Proxy]

Currently selected working proxy

classmethod from_config(config, exclude_direct=False)[source]

Create ProxyManager instance from application config.

Parameters:
  • config (Config) – Application configuration object

  • exclude_direct (bool) – Whether to exclude direct connection from proxy list even if it is enabled in config

Return type:

ProxyManager

Returns:

ProxyManager instance

proxies: List[Proxy]

List of configured proxy connections

async test_proxies(filter=None, exclusion=[], shutdown_event=None, timeout=30.0, oneshot=False)[source]

Test configured proxies.

The best proxy will be set as current_proxy.

Parameters:
  • filter (Optional[ProxyType]) – Optional proxy type to filter for testing

  • exclusion (List[Proxy]) – List of proxies to exclude from testing

  • shutdown_event (Optional[Event]) – Optional asyncio event to signal shutdown

  • timeout (float) – Timeout for proxy test in seconds

  • oneshot (bool) – If True, stop testing after first successful proxy

Return type:

List[ProxyTestResult]

Returns:

List of ProxyTestResult objects with test outcomes

class cligram.proxy_manager.ProxyTestResult(proxy, success, latency=None, error=None)[source]

Bases: object

Result of a proxy test, including latency and status.

property is_good: bool

Check if the proxy test was successful.

property score: float

Calculate proxy score (lower is better).

class cligram.proxy_manager.ProxyType(*values)[source]

Bases: Enum

Type of proxy connection supported by the application.

DIRECT = 'direct'

Direct connection without proxy.

MTPROTO = 'mtproto'

MTProto proxy protocol used by Telegram.

SOCKS5 = 'socks5'

SOCKS5 proxy protocol with optional authentication.