mtproto

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 28 Imported by: 6

Documentation

Overview

Package mtproto implements MTProto connection.

Index

Constants

View Source
const ErrFloodWait = "FLOOD_WAIT"

ErrFloodWait is error type of "FLOOD_WAIT" error.

Variables

This section is empty.

Functions

func AsFloodWait added in v0.22.0

func AsFloodWait(err error) (d time.Duration, ok bool)

AsFloodWait returns wait duration and true boolean if err is the "FLOOD_WAIT" error.

Client should wait for that duration before issuing new requests with same method.

func IsErr added in v0.22.0

func IsErr(err error, tt ...string) bool

IsErr returns true if err type is t.

func IsErrCode added in v0.22.0

func IsErrCode(err error, code ...int) bool

IsErrCode returns true of error code is as provided.

Types

type Cipher added in v0.19.0

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 added in v0.18.0

func New(addr string, opt Options) *Conn

New creates new unstarted connection.

func (*Conn) InvokeRaw

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

InvokeRaw sens 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 added in v0.18.0

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 Error

type Error struct {
	Code     int    // 420
	Message  string // FLOOD_WAIT_3
	Type     string // FLOOD_WAIT
	Argument int    // 3
}

Error represents RPC error returned to request.

func AsErr added in v0.22.0

func AsErr(err error) (rpcErr *Error, ok bool)

AsErr extracts *Error from err if possible.

func AsTypeErr added in v0.22.0

func AsTypeErr(err error, t string) (rpcErr *Error, ok bool)

AsTypeErr returns *Error from err if rpc error type is t.

func NewError added in v0.22.0

func NewError(code int, msg string) *Error

NewError creates new *Error from code and message, extracting argument and type.

func (*Error) Error

func (e *Error) Error() string

func (*Error) IsCode added in v0.22.0

func (e *Error) IsCode(code int) bool

IsCode reports whether error Code is equal to code.

func (*Error) IsCodeOneOf added in v0.22.0

func (e *Error) IsCodeOneOf(codes ...int) bool

IsCodeOneOf returns true if error code is one of codes.

func (*Error) IsOneOf added in v0.22.0

func (e *Error) IsOneOf(tt ...string) bool

IsOneOf returns true if error type is in tt.

func (*Error) IsType added in v0.22.0

func (e *Error) IsType(t string) bool

IsType reports whether error has type t.

type Handler

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

Handler will be called on received message from Telegram.

type MessageIDSource

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

MessageIDSource is message id generator.

type Options

type Options struct {
	// PublicKeys of telegram.
	//
	// If not provided, embedded public keys will be used.
	PublicKeys []*rsa.PublicKey
	// Transport to use. Default dialer will be used if not provided.
	Transport Transport
	// Network to use. Defaults to tcp.
	Network string
	// 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
	// DialTimeout is timeout of creating connection.
	DialTimeout time.Duration
	// ExchangeTimeout is timeout of every key exchange request.
	ExchangeTimeout time.Duration
	// MaxRetries is max retry count until rpc request failure.
	MaxRetries 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
	// ReadConcurrency limits maximum concurrently handled messages.
	// Can be CPU or IO bound depending on message handlers.
	// Defaults to GOMAXPROCS if it is not less than 10.
	ReadConcurrency int
	// 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.

type Transport

type Transport interface {
	Codec() transport.Codec
	DialContext(ctx context.Context, network, address string) (transport.Conn, error)
}

Transport is MTProto connection creator.

Jump to

Keyboard shortcuts

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