mtproto

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package mtproto implements a custom MTProto server for teled.

It composes the low-level gotd/td primitives (transport, exchange, crypto, proto/mt/mtproto) that tgtest.Server is built on, but owns the connection lifecycle so that auth keys and sessions can be persisted through a pluggable KeyStorage. See docs/architecture.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPrivateKey

func NewPrivateKey(k *rsa.PrivateKey) exchange.PrivateKey

NewPrivateKey creates a new private key from an RSA private key.

Types

type Handler

type Handler interface {
	OnMessage(server *Server, req *Request) error
}

Handler is an RPC request handler.

func UnpackInvoke

func UnpackInvoke(next Handler) Handler

UnpackInvoke is a Handler middleware that unpacks Invoke*-like wrappers:

tg.InvokeWithLayerRequest
tg.InitConnectionRequest
tg.InvokeWithoutUpdatesRequest

so the inner query reaches the next handler.

type HandlerFunc

type HandlerFunc func(server *Server, req *Request) error

HandlerFunc is a functional adapter for Handler.

func (HandlerFunc) OnMessage

func (h HandlerFunc) OnMessage(server *Server, req *Request) error

OnMessage implements Handler.

type InMemoryKeys

type InMemoryKeys struct {
	// contains filtered or unexported fields
}

InMemoryKeys is a KeyStorage backed by a map. Safe for concurrent use.

func NewInMemoryKeys

func NewInMemoryKeys() *InMemoryKeys

NewInMemoryKeys creates an empty in-memory KeyStorage.

func (*InMemoryKeys) Get

func (s *InMemoryKeys) Get(_ context.Context, id [8]byte) (crypto.AuthKey, bool, error)

Get implements KeyStorage.

func (*InMemoryKeys) Len

func (s *InMemoryKeys) Len() int

Len returns the number of stored keys.

func (*InMemoryKeys) Save

func (s *InMemoryKeys) Save(_ context.Context, key crypto.AuthKey) error

Save implements KeyStorage.

type KeyStorage

type KeyStorage interface {
	// Save persists an auth key. Saving an existing key is not an error.
	Save(ctx context.Context, key crypto.AuthKey) error
	// Get returns the key by its 8-byte ID. The boolean is false when absent.
	Get(ctx context.Context, id [8]byte) (crypto.AuthKey, bool, error)
}

KeyStorage persists MTProto auth keys.

Owning the connection loop lets the server resolve an incoming auth_key_id through this seam, so a client can reconnect with an existing key after a restart instead of re-running key exchange. The in-memory default keeps keys only for the process lifetime; a PostgreSQL-backed implementation is added in internal/db (see docs/architecture.md).

type Request

type Request struct {
	// DC is the DC ID of the server that received the request.
	DC int
	// Session is the user session the request arrived on.
	Session Session
	// MsgID is the message ID of the RPC request.
	MsgID int64
	// Buf contains the RPC request body.
	Buf *bin.Buffer
	// RequestCtx is the request context.
	RequestCtx context.Context
}

Request represents an MTProto RPC request.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a custom MTProto server.

func NewServer

func NewServer(key exchange.PrivateKey, handler Handler, opts ServerOptions) *Server

NewServer creates a new Server.

func (*Server) ForceDisconnect

func (s *Server) ForceDisconnect(k Session)

ForceDisconnect drops the live connection for session k. The auth key is kept.

func (*Server) Key

func (s *Server) Key() exchange.PublicKey

Key returns the public key of this server.

func (*Server) Send

func (s *Server) Send(ctx context.Context, k Session, t proto.MessageType, message bin.Encoder) error

Send sends a message to the live connection of session k. The message type t must be proto.MessageServerResponse or proto.MessageFromServer.

func (*Server) SendAck

func (s *Server) SendAck(ctx context.Context, k Session, ids ...int64) error

SendAck acknowledges received messages on session k.

func (*Server) SendBool

func (s *Server) SendBool(req *Request, r bool) error

SendBool sends a bool RPC result for req.

func (*Server) SendErr

func (s *Server) SendErr(req *Request, e *tgerr.Error) error

SendErr sends an RPC error result for req.

func (*Server) SendEternalSalt

func (s *Server) SendEternalSalt(req *Request) error

SendEternalSalt sends a salt valid until the maximum possible date.

func (*Server) SendFutureSalts

func (s *Server) SendFutureSalts(req *Request, salts ...mt.FutureSalt) error

SendFutureSalts responds to a get_future_salts request.

func (*Server) SendPong

func (s *Server) SendPong(req *Request, pingID int64) error

SendPong responds to a ping request.

func (*Server) SendResult

func (s *Server) SendResult(req *Request, msg bin.Encoder) error

SendResult sends an RPC result for req.

func (*Server) SendUpdates

func (s *Server) SendUpdates(ctx context.Context, k Session, updates ...tg.UpdateClass) error

SendUpdates pushes updates to the live connection of session k.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, l transport.Listener) error

Serve runs the server loop using the given listener until ctx is canceled or the listener is closed.

type ServerOptions

type ServerOptions struct {
	// DC is the DC ID of this server. Defaults to 2.
	DC int
	// Random is the random source. Defaults to crypto.DefaultRand.
	Random io.Reader
	// Logger is the zap logger. No logs by default.
	Logger *zap.Logger
	// Keys persists auth keys. Defaults to an in-memory store.
	Keys KeyStorage
	// Clock to use. Defaults to clock.System.
	Clock clock.Clock
	// MessageID generates server message IDs. Defaults to proto.NewMessageIDGen.
	MessageID mtproto.MessageIDSource
	// Types is a type map used in verbose logging of incoming messages.
	Types *tmap.Map
	// ReadTimeout is the connection read timeout.
	ReadTimeout time.Duration
	// WriteTimeout is the connection write timeout.
	WriteTimeout time.Duration
	// Providers supplies the OpenTelemetry tracer and meter for this layer.
	// The zero value yields no-op instrumentation.
	Providers obs.Providers
}

ServerOptions configures a Server.

type Session

type Session struct {
	// ID is a session ID.
	ID int64
	// AuthKey is the attached auth key.
	AuthKey crypto.AuthKey
}

Session represents a connection session.

func (Session) MarshalLogObject

func (s Session) MarshalLogObject(encoder zapcore.ObjectEncoder) error

MarshalLogObject implements zap.ObjectMarshaler.

Jump to

Keyboard shortcuts

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