package mtproto

import (
	
	

	

	

	
	
	
	
	
	
	
)

// Options of Conn.
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 the structured logger. No logs by default.
	Logger log.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 the duration between get_future_salts requests.
	//
	// Zero (default) never sends get_future_salts, like Telegram Desktop:
	// the server salt is taken from new_session_created and bad_server_salt
	// (a request with an expired salt is re-sent with the new one). A
	// positive value fetches future salts every interval, the first time
	// one interval after the session starts.
	SaltFetchInterval time.Duration
	// PingTimeout is the time to wait for a pong, after which the connection
	// is considered dead. Default is 15 seconds.
	PingTimeout time.Duration
	// PingInterval is the duration between ping_delay_disconnect requests.
	//
	// Zero (default) pings like Telegram Desktop: every 30–45 seconds (a
	// random interval in [30s, 45s]) with disconnect_delay 60. A positive
	// value pings at that fixed interval with disconnect_delay
	// PingInterval + PingTimeout.
	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
	// ServerTime tracks the server time offset; share it between connections
	// of a client. Defaults to a new ServerTime over Clock. Message ids of the
	// default MessageID source and the msg_id time check use it.
	ServerTime *ServerTime
	// Types map, used in verbose logging of incoming message.
	Types *tmap.Map
	// Key that can be used to restore previous connection.
	Key crypto.AuthKey
	// PermKey is permanent auth key for PFS mode.
	PermKey crypto.AuthKey
	// Salt from server that can be used to restore previous connection.
	Salt int64
	// SessionState keeps the server session (session_id and seqno) across
	// reconnects of a connection slot with the same key. Nil starts a new
	// session on every connect. Not used in PFS mode, where every connection
	// has a new temporary key.
	SessionState *SessionState
	// KeyCreatedAt is the creation time of the permanent key (Key, or PermKey
	// in PFS mode), Unix seconds; zero if unknown. It is reported back in
	// Session.KeyCreatedAt and used by the PFS key recovery policy.
	KeyCreatedAt int64

	// EnablePFS enables Perfect Forward Secrecy using temporary auth keys.
	EnablePFS bool
	// TempKeyTTL is temporary auth key lifetime in seconds.
	// Default: 86400 (24h). Minimum: 60.
	TempKeyTTL int

	// Tracer for OTEL.
	Tracer trace.Tracer

	// WireTracer observes plaintext messages and lifecycle events of the
	// connection. Nil (default) disables wire tracing.
	WireTracer WireTracer

	// Private options.

	// Cipher defines message crypto.
	Cipher Cipher
	// engine for replacing RPC engine.
	engine *rpc.Engine
}

const (
	// Telegram recommends short-lived temp keys; we keep 24h as practical
	// default to balance reconnect frequency and security.
	defaultTempKeyTTL = 24 * 60 * 60
	// Prevent obviously invalid ttl values that can break renewal math.
	minTempKeyTTL = 60
)

type nopHandler struct{}

func (nopHandler) ( *bin.Buffer) error   { return nil }
func (nopHandler) ( Session) error { return nil }

func ( *Options) () {
	// Using public keys that are included with distribution if not
	// provided.
	//
	// This should never fail and keys should be valid for recent
	// library versions.
	.PublicKeys = vendoredKeys()
}

func ( *Options) () {
	if .DC == 0 {
		.DC = 2
	}
	if .Random == nil {
		.Random = crypto.DefaultRand()
	}
	if .Logger == nil {
		.Logger = log.Nop
	}
	if .AckBatchSize == 0 {
		.AckBatchSize = 20
	}
	if .AckInterval == 0 {
		.AckInterval = 15 * time.Second
	}
	if .RetryInterval == 0 {
		.RetryInterval = 5 * time.Second
	}
	if .MaxRetries == 0 {
		.MaxRetries = 5
	}
	if .DialTimeout == 0 {
		.DialTimeout = 35 * time.Second
	}
	if .ExchangeTimeout == 0 {
		.ExchangeTimeout = exchange.DefaultTimeout
	}
	if .PingTimeout == 0 {
		.PingTimeout = 15 * time.Second
	}
	if .RequestTimeout == nil {
		.RequestTimeout = func( uint32) time.Duration {
			return 15 * time.Second
		}
	}
	if .CompressThreshold == 0 {
		.CompressThreshold = 1024
	}
	if .Clock == nil {
		.Clock = clock.System
	}
	if .ServerTime == nil {
		.ServerTime = NewServerTime(.Clock, 0)
	}
	if .MessageID == nil {
		.MessageID = proto.NewMessageIDGen(.ServerTime.Now)
	}
	if .TempKeyTTL == 0 {
		.TempKeyTTL = defaultTempKeyTTL
	}
	if .TempKeyTTL < minTempKeyTTL {
		.TempKeyTTL = minTempKeyTTL
	}
	// Fallback for callers that still pass restored key via Key.
	if .EnablePFS && .PermKey.Zero() && !.Key.Zero() {
		.PermKey = .Key
	}
	if len(.PublicKeys) == 0 {
		.setDefaultPublicKeys()
	}
	if .Handler == nil {
		.Handler = nopHandler{}
	}
	if .Cipher == nil {
		.Cipher = crypto.NewClientCipher(.Random)
	}
}