teldrive/utils/tgc/tgc.go

121 lines
3.3 KiB
Go
Raw Normal View History

2023-09-20 03:20:44 +08:00
package tgc
import (
"context"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/utils"
"github.com/divyam234/teldrive/utils/kv"
2023-11-25 12:31:29 +08:00
"github.com/divyam234/teldrive/utils/recovery"
"github.com/divyam234/teldrive/utils/retry"
2023-09-20 03:20:44 +08:00
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/contrib/middleware/ratelimit"
tdclock "github.com/gotd/td/clock"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"golang.org/x/time/rate"
)
func deviceConfig(appConfig *utils.Config) telegram.DeviceConfig {
config := telegram.DeviceConfig{
DeviceModel: appConfig.TgClientDeviceModel,
SystemVersion: appConfig.TgClientSystemVersion,
AppVersion: appConfig.TgClientAppVersion,
SystemLangCode: appConfig.TgClientSystemLangCode,
LangPack: appConfig.TgClientLangPack,
LangCode: appConfig.TgClientLangCode,
}
return config
}
2023-11-25 12:31:29 +08:00
func NewDefaultMiddlewares(ctx context.Context) ([]telegram.Middleware, error) {
_clock := tdclock.System
return []telegram.Middleware{
recovery.New(ctx, Backoff(_clock)),
retry.New(5),
floodwait.NewSimpleWaiter(),
}, nil
}
2023-09-20 03:20:44 +08:00
2023-11-25 12:31:29 +08:00
func New(ctx context.Context, handler telegram.UpdateHandler, storage session.Storage, middlewares ...telegram.Middleware) *telegram.Client {
2023-09-20 03:20:44 +08:00
_clock := tdclock.System
config := utils.GetConfig()
noUpdates := true
if handler != nil {
noUpdates = false
}
opts := telegram.Options{
ReconnectionBackoff: func() backoff.BackOff {
2023-11-25 12:31:29 +08:00
return Backoff(_clock)
2023-09-20 03:20:44 +08:00
},
Device: deviceConfig(config),
SessionStorage: storage,
2023-11-25 12:31:29 +08:00
RetryInterval: time.Second,
MaxRetries: 10,
2023-09-20 03:20:44 +08:00
DialTimeout: 10 * time.Second,
Middlewares: middlewares,
Clock: _clock,
NoUpdates: noUpdates,
UpdateHandler: handler,
}
return telegram.NewClient(config.AppId, config.AppHash, opts)
}
2023-11-25 12:31:29 +08:00
func NoLogin(ctx context.Context, handler telegram.UpdateHandler, storage session.Storage) *telegram.Client {
middlewares, _ := NewDefaultMiddlewares(ctx)
2023-09-20 03:20:44 +08:00
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5))
2023-11-25 12:31:29 +08:00
return New(ctx, handler, storage, middlewares...)
2023-09-20 03:20:44 +08:00
}
2023-11-25 12:31:29 +08:00
func UserLogin(ctx context.Context, sessionStr string) (*telegram.Client, error) {
2023-09-20 03:20:44 +08:00
data, err := session.TelethonSession(sessionStr)
if err != nil {
return nil, err
}
var (
storage = new(session.StorageMemory)
loader = session.Loader{Storage: storage}
)
if err := loader.Save(context.TODO(), data); err != nil {
return nil, err
}
2023-11-25 12:31:29 +08:00
middlewares, _ := NewDefaultMiddlewares(ctx)
config := utils.GetConfig()
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*time.Duration(config.Rate)), config.RateBurst))
}
return New(ctx, nil, storage, middlewares...), nil
2023-09-20 03:20:44 +08:00
}
2023-11-25 12:31:29 +08:00
func BotLogin(ctx context.Context, token string) (*telegram.Client, error) {
2023-09-20 03:20:44 +08:00
config := utils.GetConfig()
storage := kv.NewSession(database.KV, kv.Key("botsession", token))
2023-11-25 12:31:29 +08:00
middlewares, _ := NewDefaultMiddlewares(ctx)
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*time.Duration(config.Rate)), config.RateBurst))
}
return New(ctx, nil, storage, middlewares...), nil
}
func Backoff(_clock tdclock.Clock) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Multiplier = 1.1
b.MaxElapsedTime = time.Duration(120) * time.Second
b.Clock = _clock
return b
2023-09-20 03:20:44 +08:00
}