← 返回
开发者工具 中文

Teamgram MTProto Protocol

Documents the MTProto protocol handling layer in Teamgram Server, including handshake, AES-IGE decryption, QuickAck, auth_key caching, and message forwarding...
记录 Teamgram Server 中 MTProto 协议处理层的实现,涵盖握手、AES-IGE 解密、QuickAck、auth_key 缓存及消息转发...
zhihang9978
开发者工具 clawhub v1.0.1 1 版本 100000 Key: 无需
★ 0
Stars
📥 461
下载
💾 10
安装
1
版本
#latest

概述

MTProto Protocol Layer in Teamgram Server

Skill Type

This is a reference/knowledge skill. It contains no executable code. It documents the MTProto protocol handling internals of Teamgram Server (open-source, Apache-2.0 licensed) for developers who need to understand, debug, or extend the protocol layer.

Overview

KHF client and Teamgram Server communicate via Telegram's MTProto binary protocol. The Teamgram gateway layer (gnetway) performs 3 key operations:

  1. Handshake / auth_key generation (unauthenticated phase: auth_key_id=0 -> handshake)
  2. Authenticated message decryption (AES-IGE)
  3. Forward decrypted payload (TLMessage2.Object) to session service via gRPC

Runtime Environment (for Teamgram Server operators)

If you are deploying Teamgram Server (not just reading this skill), the MTProto layer requires:

ComponentPurposeConfiguration
---------
gnetway serviceTCP/WS gateway, listens on ports 10443/5222/11443app/interface/gnetway/etc/gnetway.yaml
session serviceManages auth sessions, routes RPCsapp/interface/session/etc/session.yaml
MySQLStores auth_key records (auth_keys, auth_key_infos tables)Configured in service YAML files
RedisCaches auth_key for fast lookupConfigured in service YAML files
etcdService discovery between gnetway and sessionetcd.hosts in YAML config

No credentials are required to read or install this skill. The above table is for reference only when operating the actual Teamgram Server.

Core Data Structure

The decrypted data structure is mtproto.TLMessage2, whose Object field contains the specific TL RPC call (e.g., auth.sendCode, messages.sendMessage, etc.).

QuickAck Mechanism

gnetway implements QuickAck token computation. ACKs must be encoded through the codec to avoid obfuscated CTR counter desync (otherwise Android clients will fail at decryptServerResponse).

Reference function (from app/interface/gnetway/internal/server/gnet/server_gnet.go:298-305 in teamgram-server):

func computeQuickAckToken(authKey []byte, encryptedData []byte) uint32 {
    h := sha256.New()
    h.Write(authKey[88 : 88+32])
    h.Write(encryptedData)
    var sum [32]byte
    h.Sum(sum[:0])
    return binary.LittleEndian.Uint32(sum[:4]) | 0x80000000
}

Encrypted Message Processing Flow

  1. Decrypt via authKey.AesIgeDecrypt
  2. Extract from decrypted payload header: salt, sessionId, msgId
  3. Forward payload[16:] to session service via SessionDispatcher.SendData (gRPC)

auth_key Caching and QueryAuthKey

When connContext lacks an authKey, gnetway asynchronously calls the session service's QueryAuthKey RPC, then proceeds with onEncryptedMessage after receiving the key.

Key management note: auth_keys are generated during the DH handshake, stored in MySQL (auth_keys table), and cached in Redis. They are never exposed outside the server process. The gnetway service holds keys only in memory during active connections.

KHF Client Protocol Constants (must match server)

Extracted from KHF client source code:

  • TLRPC.LAYER = 222
  • BuildVars.APP_ID = 4
  • BuildVars.APP_HASH = "014b35b6184100b085b0d0572f9b5103"

These are public application identifiers, not secrets. They must match between client and server for TL schema compatibility.

Important Notes

  • QuickAck must be encoded through codec, otherwise obfuscated CTR counter will desync
  • Messages with auth_key_id=0 go through handshake flow; non-zero go through decryption flow
  • After AES-IGE decryption, the first 16 bytes of payload are salt+sessionId, followed by TLMessage2 data

Source Code References

All code referenced in this skill is from the open-source Teamgram Server project:

  • Repository: https://github.com/teamgram/teamgram-server
  • License: Apache-2.0
  • Gateway code: app/interface/gnetway/
  • Session code: app/interface/session/
  • MTProto codec: mtproto/ package

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-03-30 07:21 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 65 📥 179,753
security-compliance

MTProto 2.0

zhihang9978
MTProto 2.0协议实现指南,用于Telegram后端开发。包括MTProto加密、握手、消息序列化等实现指导。
★ 0 📥 525
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 666 📥 323,714