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 ¶
- func NewPrivateKey(k *rsa.PrivateKey) exchange.PrivateKey
- type Handler
- type HandlerFunc
- type InMemoryKeys
- type KeyStorage
- type Request
- type Server
- func (s *Server) ForceDisconnect(k Session)
- func (s *Server) Key() exchange.PublicKey
- func (s *Server) Send(ctx context.Context, k Session, t proto.MessageType, message bin.Encoder) error
- func (s *Server) SendAck(ctx context.Context, k Session, ids ...int64) error
- func (s *Server) SendBool(req *Request, r bool) error
- func (s *Server) SendErr(req *Request, e *tgerr.Error) error
- func (s *Server) SendEternalSalt(req *Request) error
- func (s *Server) SendFutureSalts(req *Request, salts ...mt.FutureSalt) error
- func (s *Server) SendPong(req *Request, pingID int64) error
- func (s *Server) SendResult(req *Request, msg bin.Encoder) error
- func (s *Server) SendUpdates(ctx context.Context, k Session, updates ...tg.UpdateClass) error
- func (s *Server) Serve(ctx context.Context, l transport.Listener) error
- type ServerOptions
- type Session
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 ¶
Handler is an RPC request handler.
func UnpackInvoke ¶
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 ¶
HandlerFunc is a functional adapter for 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.
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 ¶
ForceDisconnect drops the live connection for session k. The auth key is kept.
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) SendEternalSalt ¶
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) SendResult ¶
SendResult sends an RPC result for req.
func (*Server) SendUpdates ¶
SendUpdates pushes updates to the live connection of session k.
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
}
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.