Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tgnet

Telegram Android's C++ MTProto transport, reimplemented in Python

Literally just tgnet, but in Python and compatible with Telethon.
Also reimplements FakeTLS.

  • Full support for FakeTLS (MTProxy) connections, implementing the ClientHello handshake and wrapping MTProto inside TLS ApplicationData records.
  • Sockets are configured with TCP_NODELAY (disabling Nagle's algorithm) exactly like the official app to prioritize low latency over packet efficiency.
  • Implements the 64-byte obfuscated init packet with random bytes that explicitly avoid known protocol headers (HTTP, TLS, etc.) and uses AES-256-CTR stream encryption.
  • Supports abridged (EF), intermediate (EE) and padded intermediate (DD) transports.
  • Intelligently rotates through default ports (443, 5222) before abandoning an IP address. This helps bypass port-blocking middleboxes efficiently.
  • Detects high-bit length fields signaling quick-acks instead of data packets across all framings.

How to use

To use the standard direct connection strategy (obfuscated Abridged TCP), pass TgNetConnection as the connection argument when initializing your TelegramClient.

from telethon import TelegramClient
from tgnet.connection import TgNetConnection

client = TelegramClient(
    'session',
    api_id=123,
    api_hash="DodoPizza",
    connection=TgNetConnection
)

MTProxy

If you need to connect through an MTProxy using FakeTLS (where the secret starts with ee), use TgNetConnectionTls.

from telethon import TelegramClient
from tgnet.connection.faketls import TgNetConnectionTls

proxy = (
    'proxy.example.com',
    443,
    'ee1234567890abcdef1234567890abcdef676f6f676c652e636f6d' 
)

client = TelegramClient(
    'session',
    api_id=123,
    api_hash="123123123",
    connection=TgNetConnectionTls,
    proxy=proxy
)

Architecture

  • tgnet.connection.TgNetConnection: The base connection class. Overrides Telethon's default socket establishment to forcefully disable TLS at the socket layer and apply Android-specific socket options.
  • tgnet.obfuscated_io.TgNetObfuscatedIO: Handles the AES-256-CTR continuous keystream encryption and decryption over the raw TCP stream, mirroring Connection.cpp:594-620.
  • tgnet.address_book.TgNetAddressBook: Manages the precise port/IP rotation logic, attempting ports like 443 and 5222 systematically before failing over.
  • tgnet.connection.faketls.*: The standalone Fake-TLS record layer that intercepts traffic, performs the initial proxy handshake, and wraps subsequent MTProto payloads in TLS application data.

Releases

Packages

Contributors

Languages