mtproto

package
v0.96.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package mtproto implements MTProto connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cipher

type Cipher interface {
	DecryptFromBuffer(k crypto.AuthKey, buf *bin.Buffer) (*crypto.EncryptedMessageData, error)
	Encrypt(key crypto.AuthKey, data crypto.EncryptedMessageData, b *bin.Buffer) error
}

Cipher handles message encryption and decryption.

type Conn

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

Conn represents a MTProto client to Telegram.

func New

func New(dialer Dialer, opt Options) *Conn

New creates new unstarted connection.

func (*Conn) Invoke added in v0.43.0

func (c *Conn) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error

Invoke sends input and decodes result into output.

NOTE: Assuming that call contains content message (seqno increment).

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping sends ping request to server and waits until pong is received or context is canceled.

func (*Conn) Run

func (c *Conn) Run(ctx context.Context, f func(ctx context.Context) error) error

Run initializes MTProto connection to server and blocks until disconnection.

When connection is ready, Handler.OnSession is called.

type Dialer added in v0.30.0

type Dialer func(ctx context.Context) (transport.Conn, error)

Dialer is an abstraction for MTProto transport connection creator.

type Handler

type Handler interface {
	OnMessage(b *bin.Buffer) error
	OnSession(session Session) error
}

Handler will be called on received message from Telegram.

type MessageBuf added in v0.43.0

type MessageBuf interface {
	Consume(id int64) bool
}

MessageBuf is message id buffer.

type MessageIDSource

type MessageIDSource interface {
	New(t proto.MessageType) int64
}

MessageIDSource is message id generator.

type Options

type Options struct {
	// DC is datacenter ID for key exchange.
	// Defaults to 2.
	DC int

	// PublicKeys of telegram.
	//
	// If not provided, embedded public keys will be used.
	PublicKeys []exchange.PublicKey

	// Random is random source. Defaults to crypto.
	Random io.Reader
	// Logger is instance of zap.Logger. No logs by default.
	Logger *zap.Logger
	// Handler will be called on received message.
	Handler Handler

	// AckBatchSize is maximum ack-s to buffer.
	AckBatchSize int
	// AckInterval is maximum time to buffer ack.
	AckInterval time.Duration

	// RetryInterval is duration between retries.
	RetryInterval time.Duration
	// MaxRetries is max retry count until rpc request failure.
	MaxRetries int

	// DialTimeout is timeout of creating connection.
	DialTimeout time.Duration
	// ExchangeTimeout is timeout of every key exchange request.
	ExchangeTimeout time.Duration
	// SaltFetchInterval is duration between get_future_salts request.
	SaltFetchInterval time.Duration
	// PingTimeout sets ping_delay_disconnect timeout.
	PingTimeout time.Duration
	// PingInterval is duration between ping_delay_disconnect request.
	PingInterval time.Duration
	// RequestTimeout is function which returns request timeout for given type ID.
	RequestTimeout func(req uint32) time.Duration

	// CompressThreshold is a threshold in bytes to determine that message
	// is large enough to be compressed using GZIP.
	// If < 0, compression will be disabled.
	// If == 0, default value will be used.
	CompressThreshold int
	// MessageID is message id source. Share source between connection to
	// reduce collision probability.
	MessageID MessageIDSource
	// Clock is current time source. Defaults to system time.
	Clock clock.Clock
	// Types map, used in verbose logging of incoming message.
	Types *tmap.Map
	// Key that can be used to restore previous connection.
	Key crypto.AuthKey
	// Salt from server that can be used to restore previous connection.
	Salt int64

	// Tracer for OTEL.
	Tracer trace.Tracer

	// Cipher defines message crypto.
	Cipher Cipher
	// contains filtered or unexported fields
}

Options of Conn.

type Session

type Session struct {
	ID   int64
	Key  crypto.AuthKey
	Salt int64
}

Session represents connection state.

Directories

Path Synopsis
Package salts contains MTProto server salt storage.
Package salts contains MTProto server salt storage.

Jump to

Keyboard shortcuts

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