-
Notifications
You must be signed in to change notification settings - Fork 29
/
tgstore.go
657 lines (564 loc) · 13.9 KB
/
tgstore.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
Package tgstore implements an encrypted object storage system with unlimited
space backed by Telegram.
*/
package tgstore
import (
"bytes"
"compress/gzip"
"context"
"crypto/cipher"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/allegro/bigcache/v3"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/poly1305"
"gopkg.in/tucnak/telebot.v2"
)
const (
// telegramFileChunkSize is the chunk size of Telegram file.
telegramFileChunkSize = 64 << 10
// telegramFileEncryptedChunkSize is the encrypted chunk size of
// Telegram file.
telegramFileEncryptedChunkSize = chacha20poly1305.NonceSize +
telegramFileChunkSize +
poly1305.TagSize
)
// TGStore is the top-level struct of this project.
//
// It is highly recommended not to modify the value of any field of the
// `TGStore` after calling any methods of it, which will cause unpredictable
// problems.
//
// The new instances of the `TGStore` should only be created by calling the
// `New`.
type TGStore struct {
// BotAPIEndpoint is the endpoint of the Telegram Bot API.
//
// You might prefer to use your local Telegram Bot API server. See
// https://core.telegram.org/bots/api#using-a-local-bot-api-server for
// benefits.
//
// Default value: "https://api.telegram.org"
BotAPIEndpoint string `mapstructure:"bot_api_endpoint"`
// BotToken is the Telegram bot token.
//
// Default value: ""
BotToken string `mapstructure:"bot_token"`
// ChatID is the ID of the Telegram chat used to store the objects to be
// uploaded.
//
// It is ok to change the `ChatID` if you want. The objects that have
// already been uploaded are not affected.
//
// Default value: 0
ChatID int64 `mapstructure:"chat_id"`
// MaxFileBytes is the maximum number of bytes allowed for a Telegram
// file to have.
//
// The `MaxFileBytes` must be at least 20971520 (20 MiB).
//
// It is ok to change the `MaxFileBytes` if you want. The objects that
// have already been uploaded are not affected.
//
// Default value: 20971520
MaxFileBytes int `mapstructure:"max_file_bytes"`
// MaxObjectMetadataCacheBytes is the maximum number of bytes allowed
// for object metadata cache to use.
//
// The `MaxObjectMetadataCacheBytes` must be at least 1048576 (1 MiB).
//
// Default value: 1048576
MaxObjectMetadataCacheBytes int `mapstructure:"max_object_metadata_cache_bytes"`
// HTTPClient is the `http.Client` used to communicate with the Telegram
// Bot API.
//
// Default value: `http.DefaultClient`
HTTPClient *http.Client `mapstructure:"-"`
loadOnce sync.Once
loadError error
bot *telebot.Bot
chat *telebot.Chat
fileHeadPool sync.Pool
objectSplitSize int64
objectMetadataCache *bigcache.BigCache
}
// New returns a new instance of the `TGStore` with default field values.
//
// The `New` is the only function that creates new instances of the `TGStore`
// and keeps everything working.
func New() *TGStore {
return &TGStore{
BotAPIEndpoint: "https://api.telegram.org",
MaxFileBytes: 20 << 20,
MaxObjectMetadataCacheBytes: 1 << 20,
HTTPClient: http.DefaultClient,
fileHeadPool: sync.Pool{
New: func() interface{} {
return make([]byte, 1<<20)
},
},
}
}
// load loads the stuff of the tgs up.
func (tgs *TGStore) load() {
if tgs.bot, tgs.loadError = telebot.NewBot(telebot.Settings{
URL: tgs.BotAPIEndpoint,
Token: tgs.BotToken,
Reporter: func(error) {},
Client: tgs.HTTPClient,
}); tgs.loadError != nil {
tgs.loadError = fmt.Errorf(
"failed to create telegram bot: %v",
tgs.loadError,
)
return
}
if tgs.chat, tgs.loadError = tgs.bot.ChatByID(
strconv.FormatInt(tgs.ChatID, 10),
); tgs.loadError != nil {
tgs.loadError = fmt.Errorf(
"failed to get telegram chat: %v",
tgs.loadError,
)
return
}
if tgs.MaxFileBytes < 20<<20 {
tgs.loadError = errors.New("invalid max file bytes")
return
}
tgs.objectSplitSize = int64(tgs.MaxFileBytes)
tgs.objectSplitSize /= telegramFileEncryptedChunkSize
tgs.objectSplitSize *= telegramFileChunkSize
if tgs.MaxObjectMetadataCacheBytes < 1<<20 {
tgs.loadError = errors.New(
"invalid max object metadata cache bytes",
)
return
}
maxObjectMetadataCacheMB := tgs.MaxObjectMetadataCacheBytes / (1 << 20)
if tgs.objectMetadataCache, tgs.loadError = bigcache.NewBigCache(
bigcache.Config{
Shards: 1024,
LifeWindow: 24 * time.Hour,
CleanWindow: 10 * time.Second,
MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntrySize: 500,
HardMaxCacheSize: maxObjectMetadataCacheMB,
},
); tgs.loadError != nil {
tgs.loadError = fmt.Errorf(
"failed to create object metadata cache: %v",
tgs.loadError,
)
return
}
}
// Upload uploads the content to the cloud.
//
// Note that the returned id is not guaranteed for a fixed length.
//
// The lenth of the secretKey must be 16.
func (tgs *TGStore) Upload(
ctx context.Context,
secretKey []byte,
content io.Reader,
) (string, error) {
tgs.loadOnce.Do(tgs.load)
if tgs.loadError != nil {
return "", tgs.loadError
}
aead, err := chacha20poly1305.New(secretKey)
if err != nil {
return "", err
}
if content == nil {
return "0", nil
}
metadata := objectMetadata{}
for buf := bytes.NewBuffer(nil); ; {
if _, err := io.CopyN(buf, content, 1); err != nil {
if errors.Is(err, io.EOF) {
break
}
return "", err
}
part := &countReader{
r: io.LimitReader(
io.MultiReader(buf, content),
tgs.objectSplitSize,
),
}
tgfID, err := tgs.uploadTelegramFile(ctx, aead, part)
if err != nil {
return "", err
}
metadata.PartIDs = append(metadata.PartIDs, tgfID)
metadata.Size += part.c
}
switch len(metadata.PartIDs) {
case 0:
return "0", nil
case 1:
metadataJSON, err := json.Marshal(metadata)
if err != nil {
return "", err
}
id := fmt.Sprint("0", metadata.PartIDs[0])
tgs.objectMetadataCache.Set(id, metadataJSON)
return id, nil
}
metadata.SplitSize = tgs.objectSplitSize
metadataJSON, err := json.Marshal(metadata)
if err != nil {
return "", err
}
gzippedMetadataJSON := bytes.Buffer{}
if gw, err := gzip.NewWriterLevel(
&gzippedMetadataJSON,
gzip.BestCompression,
); err != nil {
return "", err
} else if _, err := io.Copy(
gw,
bytes.NewReader(metadataJSON),
); err != nil {
return "", err
} else if err := gw.Close(); err != nil {
return "", err
}
tgfID, err := tgs.uploadTelegramFile(ctx, aead, &gzippedMetadataJSON)
if err != nil {
return "", err
}
id := fmt.Sprint("1", tgfID)
tgs.objectMetadataCache.Set(id, metadataJSON)
return id, nil
}
// Download downloads the object targeted by the id from the cloud. It returns
// `fs.ErrNotExist` if not found.
//
// The lenth of the secretKey must be 16.
func (tgs *TGStore) Download(
ctx context.Context,
secretKey []byte,
id string,
) (io.ReadSeekCloser, error) {
tgs.loadOnce.Do(tgs.load)
if tgs.loadError != nil {
return nil, tgs.loadError
}
aead, err := chacha20poly1305.New(secretKey)
if err != nil {
return nil, err
}
switch id {
case "":
return nil, fs.ErrNotExist
case "0":
return &objectReader{}, nil
}
reader := &objectReader{
ctx: ctx,
aead: aead,
tgs: tgs,
}
if metadataJSON, err := tgs.objectMetadataCache.Get(id); err == nil {
if err := json.Unmarshal(
metadataJSON,
&reader.metadata,
); err != nil {
return nil, err
}
return reader, nil
}
switch id[0] {
case '0':
reader.metadata.PartIDs = []string{id[1:]}
if reader.metadata.Size, err = tgs.sizeTelegramFile(
ctx,
reader.metadata.PartIDs[0],
); err != nil {
return nil, err
}
metadataJSON, err := json.Marshal(reader.metadata)
if err != nil {
return nil, err
}
tgs.objectMetadataCache.Set(id, metadataJSON)
case '1':
tgfrc, err := tgs.downloadTelegramFile(ctx, aead, id[1:], 0)
if err != nil {
return nil, err
}
defer tgfrc.Close()
gr, err := gzip.NewReader(tgfrc)
if err != nil {
return nil, err
}
defer gr.Close()
metadataJSON, err := io.ReadAll(gr)
if err != nil {
return nil, err
}
tgs.objectMetadataCache.Set(id, metadataJSON)
if err := json.Unmarshal(
metadataJSON,
&reader.metadata,
); err != nil {
return nil, err
}
default:
return nil, fs.ErrNotExist
}
return reader, nil
}
// uploadTelegramFile uploads the content to the Telegram.
func (tgs *TGStore) uploadTelegramFile(
ctx context.Context,
aead cipher.AEAD,
content io.Reader,
) (string, error) {
pr, pw := io.Pipe()
defer pr.Close()
go func() (err error) {
defer func() {
pw.CloseWithError(err)
}()
var (
buf = make([]byte, telegramFileEncryptedChunkSize)
nonce = make([]byte, chacha20poly1305.NonceSize)
)
for counter := uint64(1); ; counter++ {
n, err := io.ReadFull(
content,
buf[:telegramFileChunkSize],
)
if err != nil {
if errors.Is(err, io.EOF) {
break
} else if !errors.Is(err, io.ErrUnexpectedEOF) {
return err
}
}
binary.LittleEndian.PutUint64(nonce, counter)
if _, err := pw.Write(nonce); err != nil {
return err
}
if _, err := pw.Write(aead.Seal(
buf[:0],
nonce,
buf[:n],
nil,
)); err != nil {
return err
}
}
return nil
}()
head := tgs.fileHeadPool.Get().([]byte)[:1<<20]
defer tgs.fileHeadPool.Put(head)
if n, err := io.ReadFull(pr, head); err != nil {
switch {
case errors.Is(err, io.EOF):
head = nil
case errors.Is(err, io.ErrUnexpectedEOF):
head = head[:n]
default:
return "", err
}
}
cr := &countReader{
r: pr,
}
Upload:
if ctx.Err() != nil {
return "", ctx.Err()
}
m, err := tgs.bot.Send(tgs.chat, &telebot.Document{
File: telebot.FromReader(io.MultiReader(
bytes.NewReader(head),
cr,
)),
})
if err != nil {
if cr.c == 0 && isRetryableTelegramBotAPIError(err) {
time.Sleep(time.Second)
goto Upload
}
return "", err
}
return m.Document.FileID, nil
}
// requestTelegramFile requests the file targeted by the id from the Telegram.
// It returns `fs.ErrNotExist` if not found.
func (tgs *TGStore) requestTelegramFile(
ctx context.Context,
id string,
header http.Header,
) (*http.Response, error) {
Ready:
if ctx.Err() != nil {
return nil, ctx.Err()
}
fileURL, err := tgs.bot.FileURLByID(id)
if err != nil {
if strings.Contains(err.Error(), "Not Found") {
return nil, fs.ErrNotExist
}
if isRetryableTelegramBotAPIError(err) {
time.Sleep(time.Second)
goto Ready
}
return nil, err
}
Request:
if ctx.Err() != nil {
return nil, ctx.Err()
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fileURL,
nil,
)
if err != nil {
return nil, err
}
if header != nil {
req.Header = header
}
res, err := tgs.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode >= http.StatusBadRequest {
switch res.StatusCode {
case http.StatusBadRequest,
http.StatusTooManyRequests,
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
res.Body.Close()
time.Sleep(time.Second)
goto Request
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
return nil, fmt.Errorf("GET %s: %s: %s", req.URL, res.Status, b)
}
return res, nil
}
// downloadTelegramFile downloads the file targeted by the id from the Telegram.
// It returns `fs.ErrNotExist` if not found.
func (tgs *TGStore) downloadTelegramFile(
ctx context.Context,
aead cipher.AEAD,
id string,
offset int64,
) (io.ReadCloser, error) {
offsetChunkCount := offset / telegramFileChunkSize
offset -= offsetChunkCount * telegramFileChunkSize
var reqHeader http.Header
if offset > 0 {
reqHeader = http.Header{}
reqHeader.Set("Range", fmt.Sprintf(
"bytes=%d-",
offsetChunkCount*telegramFileEncryptedChunkSize,
))
}
res, err := tgs.requestTelegramFile(ctx, id, reqHeader)
if err != nil {
return nil, err
}
pr, pw := io.Pipe()
go func() (err error) {
defer func() {
pw.CloseWithError(err)
res.Body.Close()
}()
buf := make([]byte, telegramFileEncryptedChunkSize)
for {
n, err := io.ReadFull(res.Body, buf)
if err != nil {
if errors.Is(err, io.EOF) {
break
} else if !errors.Is(err, io.ErrUnexpectedEOF) {
return err
}
}
nonce := buf[:chacha20poly1305.NonceSize]
chunk := buf[chacha20poly1305.NonceSize:n]
chunk, err = aead.Open(chunk[:0], nonce, chunk, nil)
if err != nil {
return err
}
if offset > 0 {
chunk = chunk[offset:]
offset = 0
}
if _, err := pw.Write(chunk); err != nil {
return err
}
}
return nil
}()
return pr, nil
}
// sizeTelegramFile sizes the file targeted by the id from the Telegram. It
// returns `fs.ErrNotExist` if not found.
func (tgs *TGStore) sizeTelegramFile(
ctx context.Context,
id string,
) (int64, error) {
res, err := tgs.requestTelegramFile(ctx, id, nil)
if err != nil {
return 0, err
}
res.Body.Close()
fullChunkCount := res.ContentLength / telegramFileEncryptedChunkSize
size := fullChunkCount * telegramFileChunkSize
if res.ContentLength%telegramFileEncryptedChunkSize != 0 {
size += res.ContentLength -
fullChunkCount*telegramFileEncryptedChunkSize -
telegramFileEncryptedChunkSize +
telegramFileChunkSize
}
return size, nil
}
// isRetryableTelegramBotAPIError reports whether the err is a retryable
// Telegram Bot API error.
func isRetryableTelegramBotAPIError(err error) bool {
em := err.Error()
return strings.Contains(em, "Bad Request") ||
strings.Contains(em, "Too Many Requests") ||
strings.Contains(em, "Internal Server Error") ||
strings.Contains(em, "Bad Gateway") ||
strings.Contains(em, "Service Unavailable") ||
strings.Contains(em, "Gateway Timeout")
}
// countReader is used to count the number of bytes read from the underlying
// `io.Reader`.
type countReader struct {
r io.Reader
c int64
}
// Read implements the `io.Reader`.
func (cr *countReader) Read(b []byte) (int, error) {
n, err := cr.r.Read(b)
cr.c += int64(n)
return n, err
}