-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
client.go
473 lines (409 loc) · 11.3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package telegram
import (
"context"
"errors"
"fmt"
"io"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"go.uber.org/atomic"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/xerrors"
"github.com/gotd/td/bin"
"github.com/gotd/td/clock"
"github.com/gotd/td/internal/crypto"
"github.com/gotd/td/internal/mt"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/pool"
"github.com/gotd/td/internal/proto"
"github.com/gotd/td/internal/tdsync"
"github.com/gotd/td/internal/tmap"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/internal/manager"
"github.com/gotd/td/tg"
)
// UpdateHandler will be called on received updates from Telegram.
type UpdateHandler interface {
Handle(ctx context.Context, u *tg.Updates) error
HandleShort(ctx context.Context, u *tg.UpdateShort) error
}
type clientStorage interface {
Load(ctx context.Context) (*session.Data, error)
Save(ctx context.Context, data *session.Data) error
}
type clientConn interface {
Run(ctx context.Context) error
InvokeRaw(ctx context.Context, input bin.Encoder, output bin.Decoder) error
}
// Client represents a MTProto client to Telegram.
type Client struct {
// tg provides RPC calls via Client.
tg *tg.Client // immutable
// Telegram device information.
device DeviceConfig // immutable
// MTProto options.
opts mtproto.Options // immutable
// Connection state. Guarded by connMux.
session *pool.SyncSession
cfg *manager.AtomicConfig
conn clientConn
connMux sync.Mutex
// Connection factory fields.
create connConstructor // immutable
resolver dcs.Resolver // immutable
connBackoff func() backoff.BackOff // immutable
defaultMode manager.ConnMode // immutable
connsCounter atomic.Int64
// Restart signal channel.
restart chan struct{} // immutable
// Migration state.
exported chan *tg.AuthExportedAuthorization // immutable
migrationTimeout time.Duration // immutable
migration chan struct{}
// Connections to non-primary DC.
subConns map[int]CloseInvoker
subConnsMux sync.Mutex
sessions map[int]*pool.SyncSession
sessionsMux sync.Mutex
// Wrappers for external world, like logs or PRNG.
rand io.Reader // immutable
log *zap.Logger // immutable
clock clock.Clock // immutable
// Client context. Will be canceled by Run on exit.
ctx context.Context // immutable
cancel context.CancelFunc // immutable
// Client config.
appID int // immutable
appHash string // immutable
// Session storage.
storage clientStorage // immutable, nillable
// Ready signal channel, sends signal when client connection is ready.
// Resets on reconnect.
ready *tdsync.ResetReady // immutable
// Telegram updates handler.
updateHandler UpdateHandler // immutable
// Denotes that no update mode is enabled.
noUpdatesMode bool // immutable
}
func (c *Client) onMessage(b *bin.Buffer) error {
return c.handleUpdates(b)
}
// getVersion optimistically gets current client version.
//
// Does not handle replace directives.
func getVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
// Hard-coded package name. Probably we can generate this via parsing
// the go.mod file.
const pkg = "github.com/gotd/td"
for _, d := range info.Deps {
if strings.HasPrefix(d.Path, pkg) {
return d.Version
}
}
return ""
}
// Port is default port used by telegram.
const Port = 443
// NewClient creates new unstarted client.
func NewClient(appID int, appHash string, opt Options) *Client {
opt.setDefaults()
mode := manager.ConnModeUpdates
if opt.NoUpdates {
mode = manager.ConnModeData
}
clientCtx, clientCancel := context.WithCancel(context.Background())
client := &Client{
rand: opt.Random,
log: opt.Logger,
ctx: clientCtx,
cancel: clientCancel,
appID: appID,
appHash: appHash,
updateHandler: opt.UpdateHandler,
session: pool.NewSyncSession(pool.Session{
DC: opt.DC,
}),
cfg: manager.NewAtomicConfig(tg.Config{
DCOptions: opt.DCList,
}),
create: defaultConstructor(),
resolver: opt.Resolver,
defaultMode: mode,
connBackoff: opt.ReconnectionBackoff,
clock: opt.Clock,
device: opt.Device,
migrationTimeout: opt.MigrationTimeout,
noUpdatesMode: opt.NoUpdates,
}
client.init()
// Including version into client logger to help with debugging.
if v := getVersion(); v != "" {
client.log = client.log.With(zap.String("v", v))
}
if opt.SessionStorage != nil {
client.storage = &session.Loader{
Storage: opt.SessionStorage,
}
}
client.opts = mtproto.Options{
PublicKeys: opt.PublicKeys,
Random: opt.Random,
Logger: opt.Logger,
AckBatchSize: opt.AckBatchSize,
AckInterval: opt.AckInterval,
RetryInterval: opt.RetryInterval,
MaxRetries: opt.MaxRetries,
MessageID: opt.MessageID,
Clock: opt.Clock,
Types: tmap.New(
tg.TypesMap(),
mt.TypesMap(),
proto.TypesMap(),
),
}
client.conn = client.createPrimaryConn(nil)
return client
}
// init sets fields which needs explicit initialization, like maps or channels.
func (c *Client) init() {
if c.cfg == nil {
c.cfg = manager.NewAtomicConfig(tg.Config{})
}
c.ready = tdsync.NewResetReady()
c.restart = make(chan struct{})
c.migration = make(chan struct{}, 1)
c.exported = make(chan *tg.AuthExportedAuthorization, 1)
c.sessions = map[int]*pool.SyncSession{}
c.subConns = map[int]CloseInvoker{}
// Initializing internal RPC caller.
c.tg = tg.NewClient(c)
}
func (c *Client) restoreConnection(ctx context.Context) error {
if c.storage == nil {
return nil
}
data, err := c.storage.Load(ctx)
if errors.Is(err, session.ErrNotFound) {
return nil
}
if err != nil {
return xerrors.Errorf("load: %w", err)
}
// If file does not contain DC ID, so we use DC from options.
prev := c.session.Load()
if data.DC == 0 {
data.DC = prev.DC
}
// Restoring persisted auth key.
var key crypto.AuthKey
copy(key.Value[:], data.AuthKey)
copy(key.ID[:], data.AuthKeyID)
if key.Value.ID() != key.ID {
return xerrors.New("corrupted key")
}
// Re-initializing connection from persisted state.
c.log.Info("Connection restored from state",
zap.String("addr", data.Addr),
zap.String("key_id", fmt.Sprintf("%x", data.AuthKeyID)),
)
c.connMux.Lock()
c.session.Store(pool.Session{
DC: data.DC,
AuthKey: key,
Salt: data.Salt,
})
c.conn = c.createPrimaryConn(nil)
c.connMux.Unlock()
return nil
}
func (c *Client) runUntilRestart(ctx context.Context) error {
g := tdsync.NewCancellableGroup(ctx)
g.Go(c.conn.Run)
// If don't need updates, so there is no reason to subscribe for it.
if !c.noUpdatesMode {
g.Go(func(ctx context.Context) error {
// Call method which requires authorization, to subscribe for updates.
// See https://core.telegram.org/api/updates#subscribing-to-updates.
self, err := c.Self(ctx)
if err != nil {
// Ignore unauthorized errors.
if !unauthorized(err) {
c.log.Warn("Got error on self", zap.Error(err))
}
return nil
}
c.log.Info("Got self", zap.String("username", self.Username))
return nil
})
}
g.Go(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.restart:
c.log.Debug("Restart triggered")
// Should call cancel() to cancel group.
g.Cancel()
return nil
}
})
return g.Wait()
}
func (c *Client) reconnectUntilClosed(ctx context.Context) error {
// Note that we currently have no timeout on connection, so this is
// potentially eternal.
b := tdsync.SyncBackoff(backoff.WithContext(c.connBackoff(), ctx))
return backoff.RetryNotify(func() error {
return c.runUntilRestart(ctx)
}, b, func(err error, timeout time.Duration) {
c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout))
c.connMux.Lock()
setup := func(ctx context.Context, invoker tg.Invoker) error {
// Setup function call means successful connection
// initialization, so we can reset backoff.
b.Reset()
raw := tg.NewClient(invoker)
select {
case export := <-c.exported:
_, err := raw.AuthImportAuthorization(ctx, &tg.AuthImportAuthorizationRequest{
ID: export.ID,
Bytes: export.Bytes,
})
return err
default:
}
return nil
}
c.conn = c.createPrimaryConn(setup)
c.connMux.Unlock()
})
}
func (c *Client) onReady() {
c.log.Debug("Ready")
c.ready.Signal()
}
func (c *Client) resetReady() {
c.ready.Reset()
}
// Run starts client session and block until connection close.
// The f callback is called on successful session initialization and Run
// will return on f() result.
//
// Context of callback will be canceled if fatal error is detected.
func (c *Client) Run(ctx context.Context, f func(ctx context.Context) error) (err error) {
select {
case <-c.ctx.Done():
return xerrors.Errorf("client already closed: %w", c.ctx.Err())
default:
}
c.log.Info("Starting")
defer c.log.Info("Closed")
// Cancel client on exit.
defer c.cancel()
defer func() {
c.subConnsMux.Lock()
defer c.subConnsMux.Unlock()
for _, conn := range c.subConns {
if closeErr := conn.Close(); !xerrors.Is(closeErr, context.Canceled) {
multierr.AppendInto(&err, closeErr)
}
}
}()
c.resetReady()
if err := c.restoreConnection(ctx); err != nil {
return err
}
g := tdsync.NewCancellableGroup(ctx)
g.Go(c.reconnectUntilClosed)
g.Go(func(ctx context.Context) error {
select {
case <-ctx.Done():
c.cancel()
return ctx.Err()
case <-c.ctx.Done():
return c.ctx.Err()
}
})
g.Go(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ready.Ready():
if err := f(ctx); err != nil {
return xerrors.Errorf("callback: %w", err)
}
// Should call cancel() to cancel ctx.
// This will terminate c.conn.Run().
c.log.Debug("Callback returned, stopping")
g.Cancel()
return nil
}
})
if err := g.Wait(); !xerrors.Is(err, context.Canceled) {
return err
}
return nil
}
func (c *Client) saveSession(cfg tg.Config, s mtproto.Session) error {
if c.storage == nil {
return nil
}
data, err := c.storage.Load(c.ctx)
if errors.Is(err, session.ErrNotFound) {
// Initializing new state.
err = nil
data = &session.Data{}
}
if err != nil {
return xerrors.Errorf("load: %w", err)
}
// Updating previous data.
data.Config = cfg
data.AuthKey = s.Key.Value[:]
data.AuthKeyID = s.Key.ID[:]
data.DC = cfg.ThisDC
data.Salt = s.Salt
if err := c.storage.Save(c.ctx, data); err != nil {
return xerrors.Errorf("save: %w", err)
}
c.log.Debug("Data saved",
zap.String("key_id", fmt.Sprintf("%x", data.AuthKeyID)),
)
return nil
}
func (c *Client) onSession(cfg tg.Config, s mtproto.Session) error {
c.sessionsMux.Lock()
c.sessions[cfg.ThisDC] = pool.NewSyncSession(pool.Session{
DC: cfg.ThisDC,
Salt: s.Salt,
AuthKey: s.Key,
})
c.sessionsMux.Unlock()
primaryDC := c.session.Load().DC
// Do not save session for non-primary DC.
if cfg.ThisDC != 0 && primaryDC != 0 && primaryDC != cfg.ThisDC {
return nil
}
if err := c.saveSession(cfg, s); err != nil {
return xerrors.Errorf("save: %w", err)
}
c.connMux.Lock()
c.session.Store(pool.Session{
DC: cfg.ThisDC,
Salt: s.Salt,
AuthKey: s.Key,
})
c.cfg.Store(cfg)
c.onReady()
c.connMux.Unlock()
return nil
}