move gotd fork into repo. (#111)

- update to latest telegram layer
- remove some references to fields in tg.Entities that don't exist in
the schema
- originally added here:
https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e
  - referenced here
-
https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3
-
https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
Adam Van Ymeren
2025-06-27 20:03:37 -07:00
committed by GitHub
parent 0952df0244
commit 7a04f298d2
19264 changed files with 1539697 additions and 84 deletions
+257
View File
@@ -0,0 +1,257 @@
package manager
import (
"context"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/go-faster/errors"
"go.uber.org/zap"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/clock"
"go.mau.fi/mautrix-telegram/pkg/gotd/mtproto"
"go.mau.fi/mautrix-telegram/pkg/gotd/pool"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdsync"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)
type protoConn interface {
Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error
Run(ctx context.Context, f func(ctx context.Context) error) error
Ping(ctx context.Context) error
}
//go:generate go run -modfile=../../../_tools/go.mod golang.org/x/tools/cmd/stringer -type=ConnMode
// ConnMode represents connection mode.
type ConnMode byte
const (
// ConnModeUpdates is update connection mode.
ConnModeUpdates ConnMode = iota
// ConnModeData is data connection mode.
ConnModeData
// ConnModeCDN is CDN connection mode.
ConnModeCDN
)
// Conn is a Telegram client connection.
type Conn struct {
// Connection parameters.
mode ConnMode // immutable
// MTProto connection.
proto protoConn // immutable
// InitConnection parameters.
appID int // immutable
device DeviceConfig // immutable
// setup is callback which called after initConnection, but before ready signaling.
// This is necessary to transfer auth from previous connection to another DC.
setup SetupCallback // nilable
// onDead is called on connection death.
onDead func()
// Wrappers for external world, like logs or PRNG.
// Should be immutable.
clock clock.Clock // immutable
log *zap.Logger // immutable
// Handler passed by client.
handler Handler // immutable
// State fields.
cfg tg.Config
ongoing int
latest time.Time
mux sync.Mutex
sessionInit *tdsync.Ready // immutable
gotConfig *tdsync.Ready // immutable
dead *tdsync.Ready // immutable
connBackoff func(ctx context.Context) backoff.BackOff // immutable
}
// OnSession implements mtproto.Handler.
func (c *Conn) OnSession(session mtproto.Session) error {
c.log.Info("SessionInit")
c.sessionInit.Signal()
// Waiting for config, because OnSession can occur before we set config.
select {
case <-c.gotConfig.Ready():
case <-c.dead.Ready():
return nil
}
c.mux.Lock()
cfg := c.cfg
c.mux.Unlock()
return c.handler.OnSession(cfg, session)
}
func (c *Conn) trackInvoke() func() {
start := c.clock.Now()
c.mux.Lock()
defer c.mux.Unlock()
c.ongoing++
c.latest = start
return func() {
c.mux.Lock()
defer c.mux.Unlock()
c.ongoing--
end := c.clock.Now()
c.latest = end
c.log.Debug("Invoke",
zap.Duration("duration", end.Sub(start)),
zap.Int("ongoing", c.ongoing),
)
}
}
// Run initialize connection.
func (c *Conn) Run(ctx context.Context) (err error) {
defer c.dead.Signal()
defer func() {
if err != nil && ctx.Err() == nil {
c.log.Debug("Connection dead", zap.Error(err))
if c.onDead != nil {
c.onDead()
}
}
}()
return c.proto.Run(ctx, func(ctx context.Context) error {
// Signal death on init error. Otherwise connection shutdown
// deadlocks in OnSession that occurs before init fails.
err := c.init(ctx)
if err != nil {
c.dead.Signal()
}
return err
})
}
func (c *Conn) waitSession(ctx context.Context) error {
select {
case <-c.sessionInit.Ready():
return nil
case <-c.dead.Ready():
return pool.ErrConnDead
case <-ctx.Done():
return ctx.Err()
}
}
// Ready returns channel to determine connection readiness.
// Useful for pooling.
func (c *Conn) Ready() <-chan struct{} {
return c.sessionInit.Ready()
}
// Invoke implements Invoker.
func (c *Conn) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
// Tracking ongoing invokes.
defer c.trackInvoke()()
if err := c.waitSession(ctx); err != nil {
return errors.Wrap(err, "waitSession")
}
return c.proto.Invoke(ctx, c.wrapRequest(noopDecoder{input}), output)
}
// OnMessage implements mtproto.Handler.
func (c *Conn) OnMessage(b *bin.Buffer) error {
return c.handler.OnMessage(b)
}
type noopDecoder struct {
bin.Encoder
}
func (n noopDecoder) Decode(b *bin.Buffer) error {
return errors.New("not implemented")
}
func (c *Conn) wrapRequest(req bin.Object) bin.Object {
if c.mode != ConnModeUpdates {
return &tg.InvokeWithoutUpdatesRequest{
Query: req,
}
}
return req
}
func (c *Conn) init(ctx context.Context) error {
c.log.Debug("Initializing")
q := c.wrapRequest(&tg.InitConnectionRequest{
APIID: c.appID,
DeviceModel: c.device.DeviceModel,
SystemVersion: c.device.SystemVersion,
AppVersion: c.device.AppVersion,
SystemLangCode: c.device.SystemLangCode,
LangPack: c.device.LangPack,
LangCode: c.device.LangCode,
Proxy: c.device.Proxy,
Params: c.device.Params,
Query: c.wrapRequest(&tg.HelpGetConfigRequest{}),
})
req := c.wrapRequest(&tg.InvokeWithLayerRequest{
Layer: tg.Layer,
Query: q,
})
var cfg tg.Config
if err := backoff.RetryNotify(func() error {
if err := c.proto.Invoke(ctx, req, &cfg); err != nil {
if tgerr.Is(err, tgerr.ErrFloodWait) {
// Server sometimes returns FLOOD_WAIT(0) if you create
// multiple connections in short period of time.
//
// See https://github.com/gotd/td/issues/388.
return errors.Wrap(err, "flood wait")
}
// Not retrying other errors.
return backoff.Permanent(errors.Wrap(err, "invoke"))
}
return nil
}, c.connBackoff(ctx), func(err error, duration time.Duration) {
c.log.Debug("Retrying connection initialization",
zap.Error(err), zap.Duration("duration", duration),
)
}); err != nil {
return errors.Wrap(err, "initConnection")
}
if c.setup != nil {
if err := c.setup(ctx, c); err != nil {
return errors.Wrap(err, "setup")
}
}
c.mux.Lock()
c.latest = c.clock.Now()
c.cfg = cfg
c.mux.Unlock()
c.gotConfig.Signal()
return nil
}
// Ping calls ping for underlying protocol connection.
func (c *Conn) Ping(ctx context.Context) error {
return c.proto.Ping(ctx)
}
@@ -0,0 +1,25 @@
// Code generated by "stringer -type=ConnMode"; DO NOT EDIT.
package manager
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[ConnModeUpdates-0]
_ = x[ConnModeData-1]
_ = x[ConnModeCDN-2]
}
const _ConnMode_name = "ConnModeUpdatesConnModeDataConnModeCDN"
var _ConnMode_index = [...]uint8{0, 15, 27, 38}
func (i ConnMode) String() string {
if i >= ConnMode(len(_ConnMode_index)-1) {
return "ConnMode(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _ConnMode_name[_ConnMode_index[i]:_ConnMode_index[i+1]]
}
@@ -0,0 +1,96 @@
package manager
import (
"context"
"time"
"github.com/cenkalti/backoff/v4"
"go.mau.fi/mautrix-telegram/pkg/gotd/clock"
"go.mau.fi/mautrix-telegram/pkg/gotd/mtproto"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdsync"
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/auth"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)
// SetupCallback is an optional setup connection callback.
type SetupCallback = func(ctx context.Context, invoker tg.Invoker) error
// ConnOptions is a Telegram client connection options.
type ConnOptions struct {
DC int
Test bool
Device DeviceConfig
Handler Handler
Setup SetupCallback
OnDead func()
OnAuthError func(error)
Backoff func(ctx context.Context) backoff.BackOff
}
func defaultBackoff(c clock.Clock) func(ctx context.Context) backoff.BackOff {
return func(ctx context.Context) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Clock = c
b.MaxElapsedTime = time.Second * 30
b.MaxInterval = time.Second * 5
return backoff.WithContext(b, ctx)
}
}
// setDefaults sets default values.
func (c *ConnOptions) setDefaults(connClock clock.Clock) {
if c.DC == 0 {
c.DC = 2
}
// It's okay to use zero value Test.
c.Device.SetDefaults()
if c.Handler == nil {
c.Handler = NoopHandler{}
}
if c.Backoff == nil {
c.Backoff = defaultBackoff(connClock)
}
}
// CreateConn creates new connection.
func CreateConn(
create mtproto.Dialer,
mode ConnMode,
appID int,
opts mtproto.Options,
connOpts ConnOptions,
) *Conn {
connOpts.setDefaults(opts.Clock)
conn := &Conn{
mode: mode,
appID: appID,
device: connOpts.Device,
clock: opts.Clock,
handler: connOpts.Handler,
sessionInit: tdsync.NewReady(),
gotConfig: tdsync.NewReady(),
dead: tdsync.NewReady(),
setup: connOpts.Setup,
onDead: connOpts.OnDead,
connBackoff: connOpts.Backoff,
}
conn.log = opts.Logger
opts.DC = connOpts.DC
if connOpts.Test {
// New key exchange algorithm requires DC ID and uses mapping like MTProxy.
// +10000 for test DC, *-1 for media-only.
opts.DC += 10000
}
opts.Handler = conn
opts.Logger = conn.log.Named("mtproto")
opts.OnError = func(err error) {
if auth.IsUnauthorized(err) && connOpts.OnAuthError != nil {
connOpts.OnAuthError(err)
}
}
conn.proto = mtproto.New(create, opts)
return conn
}
@@ -0,0 +1,61 @@
package manager
import (
"runtime"
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/internal/version"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)
// DeviceConfig is config which send when Telegram connection session created.
type DeviceConfig struct {
// Device model.
DeviceModel string
// Operating system version.
SystemVersion string
// Application version.
AppVersion string
// Code for the language used on the device's OS, ISO 639-1 standard.
SystemLangCode string
// Language pack to use.
LangPack string
// Code for the language used on the client, ISO 639-1 standard.
LangCode string
// Info about an MTProto proxy.
Proxy tg.InputClientProxy
// Additional initConnection parameters. For now, only the tz_offset field is supported,
// for specifying timezone offset in seconds.
Params tg.JSONValueClass
}
// SetDefaults sets default values.
func (c *DeviceConfig) SetDefaults() {
const notAvailable = "n/a"
// Strings must be non-empty, so set notAvailable if default value is empty.
set := func(to *string, value string) {
if value != "" {
*to = value
} else {
*to = notAvailable
}
}
if c.DeviceModel == "" {
set(&c.DeviceModel, runtime.Version())
}
if c.SystemVersion == "" {
set(&c.SystemVersion, runtime.GOOS)
}
if c.AppVersion == "" {
set(&c.AppVersion, version.GetVersion())
}
if c.SystemLangCode == "" {
c.SystemLangCode = "en"
}
if c.LangCode == "" {
c.LangCode = "en"
}
// It's okay to use zero value Proxy.
// It's okay to use zero value Params.
}
@@ -0,0 +1,2 @@
// Package manager contains connection management utilities.
package manager
@@ -0,0 +1,26 @@
package manager
import (
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/mtproto"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)
// Handler abstracts updates and session handler.
type Handler interface {
OnSession(cfg tg.Config, s mtproto.Session) error
OnMessage(b *bin.Buffer) error
}
// NoopHandler is a noop handler.
type NoopHandler struct{}
// OnSession implements Handler.
func (n NoopHandler) OnSession(cfg tg.Config, s mtproto.Session) error {
return nil
}
// OnMessage implements Handler
func (n NoopHandler) OnMessage(b *bin.Buffer) error {
return nil
}
@@ -0,0 +1,29 @@
package manager
import (
"sync/atomic"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)
// AtomicConfig is atomic tg.Config.
type AtomicConfig struct {
atomic.Value
}
// NewAtomicConfig creates new AtomicConfig.
func NewAtomicConfig(cfg tg.Config) *AtomicConfig {
a := &AtomicConfig{}
a.Store(cfg)
return a
}
// Load loads atomically config and returns it.
func (c *AtomicConfig) Load() tg.Config {
return c.Value.Load().(tg.Config)
}
// Store saves given config atomically.
func (c *AtomicConfig) Store(cfg tg.Config) {
c.Value.Store(cfg)
}