refactor(app): remove tgc

This commit is contained in:
iyear 2023-12-11 11:23:04 +08:00
parent 6dcb76e32f
commit e58df34627
8 changed files with 29 additions and 230 deletions

View File

@ -14,7 +14,6 @@ import (
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/downloader"
@ -23,6 +22,7 @@ import (
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
"github.com/iyear/tdl/pkg/tmessage"
"github.com/iyear/tdl/pkg/utils"
)
@ -53,12 +53,9 @@ type parser struct {
}
func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return errors.Wrap(err, "create middlewares")
}
pool := dcpool.NewPool(c, int64(viper.GetInt(consts.FlagPoolSize)), middlewares...)
pool := dcpool.NewPool(c,
int64(viper.GetInt(consts.FlagPoolSize)),
tclient.NewDefaultMiddlewares(ctx, viper.GetDuration(consts.FlagReconnectTimeout))...)
defer multierr.AppendInvoke(&rerr, multierr.Close(pool))
parsers := []parser{

View File

@ -17,13 +17,13 @@ import (
"go.uber.org/multierr"
"github.com/iyear/tdl/app/internal/tctx"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/forwarder"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
"github.com/iyear/tdl/pkg/texpr"
"github.com/iyear/tdl/pkg/tmessage"
"github.com/iyear/tdl/pkg/utils"
@ -52,12 +52,9 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
ctx = tctx.WithKV(ctx, kvd)
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return errors.Wrap(err, "create middlewares")
}
pool := dcpool.NewPool(c, int64(viper.GetInt(consts.FlagPoolSize)), middlewares...)
pool := dcpool.NewPool(c,
int64(viper.GetInt(consts.FlagPoolSize)),
tclient.NewDefaultMiddlewares(ctx, viper.GetDuration(consts.FlagReconnectTimeout))...)
defer multierr.AppendInvoke(&rerr, multierr.Close(pool))
ctx = tctx.WithPool(ctx, pool)

View File

@ -1,29 +0,0 @@
package tgc
import (
"context"
"fmt"
"github.com/gotd/td/telegram"
"go.uber.org/zap"
"github.com/iyear/tdl/pkg/logger"
)
func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx context.Context) error) error {
return client.Run(ctx, func(ctx context.Context) error {
status, err := client.Auth().Status(ctx)
if err != nil {
return err
}
if !status.Authorized {
return fmt.Errorf("not authorized. please login first")
}
logger.From(ctx).Info("Authorized",
zap.Int64("id", status.User.ID),
zap.String("username", status.User.Username))
return f(ctx)
})
}

View File

@ -1,147 +0,0 @@
package tgc
import (
"context"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/go-faster/errors"
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/contrib/middleware/ratelimit"
tdclock "github.com/gotd/td/clock"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/spf13/viper"
"go.uber.org/zap"
"golang.org/x/net/proxy"
"golang.org/x/time/rate"
"github.com/iyear/tdl/pkg/clock"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/recovery"
"github.com/iyear/tdl/pkg/retry"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
)
func NewDefaultMiddlewares(ctx context.Context) ([]telegram.Middleware, error) {
_clock, err := Clock()
if err != nil {
return nil, errors.Wrap(err, "create clock")
}
return []telegram.Middleware{
recovery.New(ctx, Backoff(_clock)),
retry.New(5),
floodwait.NewSimpleWaiter(),
}, nil
}
func New(ctx context.Context, login bool, middlewares ...telegram.Middleware) (*telegram.Client, kv.KV, error) {
kvd, err := kv.From(ctx).Open(viper.GetString(consts.FlagNamespace))
if err != nil {
return nil, nil, errors.Wrap(err, "open kv")
}
_clock, err := Clock()
if err != nil {
return nil, nil, errors.Wrap(err, "create clock")
}
mode, err := kvd.Get(key.App())
if err != nil {
mode = []byte(consts.AppBuiltin)
}
app, ok := consts.Apps[string(mode)]
if !ok {
return nil, nil, fmt.Errorf("can't find app: %s, please try re-login", mode)
}
appId, appHash := app.AppID, app.AppHash
// process proxy
var dialer dcs.DialFunc = proxy.Direct.DialContext
if p := viper.GetString(consts.FlagProxy); p != "" {
d, err := utils.Proxy.GetDial(p)
if err != nil {
return nil, nil, errors.Wrap(err, "get dialer")
}
dialer = d.DialContext
}
opts := telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: dialer,
}),
ReconnectionBackoff: func() backoff.BackOff {
return Backoff(_clock)
},
Device: consts.Device,
SessionStorage: storage.NewSession(kvd, login),
RetryInterval: 5 * time.Second,
MaxRetries: -1, // infinite retries
DialTimeout: 10 * time.Second,
Middlewares: middlewares,
Clock: _clock,
Logger: logger.From(ctx).Named("td"),
}
// test mode, hook options
if viper.GetString(consts.FlagTest) != "" {
appId, appHash = telegram.TestAppID, telegram.TestAppHash
opts.DC = 2
opts.DCList = dcs.Test()
// add rate limit to avoid frequent flood wait
opts.Middlewares = append(opts.Middlewares, ratelimit.New(rate.Every(100*time.Millisecond), 5))
}
logger.From(ctx).Info("New telegram client",
zap.Int("app", app.AppID),
zap.String("mode", string(mode)),
zap.Bool("is_login", login))
return telegram.NewClient(appId, appHash, opts), kvd, nil
}
func NoLogin(ctx context.Context, middlewares ...telegram.Middleware) (*telegram.Client, kv.KV, error) {
mid, err := NewDefaultMiddlewares(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "create default middlewares")
}
return New(ctx, false, append(middlewares, mid...)...)
}
func Login(ctx context.Context, middlewares ...telegram.Middleware) (*telegram.Client, kv.KV, error) {
mid, err := NewDefaultMiddlewares(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "create default middlewares")
}
return New(ctx, true, append(middlewares, mid...)...)
}
func Clock() (tdclock.Clock, error) {
_clock := tdclock.System
if ntp := viper.GetString(consts.FlagNTP); ntp != "" {
var err error
_clock, err = clock.New()
if err != nil {
return nil, err
}
}
return _clock, nil
}
func Backoff(_clock tdclock.Clock) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Multiplier = 1.1
b.MaxElapsedTime = viper.GetDuration(consts.FlagReconnectTimeout)
b.MaxInterval = 10 * time.Second
b.Clock = _clock
return b
}

View File

@ -7,17 +7,29 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/spf13/viper"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/tclient"
)
func Code(ctx context.Context) error {
c, kv, err := tgc.Login(ctx)
kvd, err := kv.From(ctx).Open(viper.GetString(consts.FlagNamespace))
if err != nil {
return errors.Wrap(err, "open kv")
}
c, err := tclient.New(ctx, tclient.Options{
KV: kvd,
Proxy: viper.GetString(consts.FlagProxy),
NTP: viper.GetString(consts.FlagNTP),
ReconnectTimeout: viper.GetDuration(consts.FlagReconnectTimeout),
Test: viper.GetString(consts.FlagTest) != "",
}, true)
if err != nil {
return err
}
@ -54,7 +66,7 @@ func Code(ctx context.Context) error {
return err
}
if err = kv.Set(key.App(), []byte(consts.AppBuiltin)); err != nil {
if err = kvd.Set(key.App(), []byte(tclient.AppBuiltin)); err != nil {
return err
}

View File

@ -18,6 +18,7 @@ import (
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
"github.com/iyear/tdl/pkg/tdesktop"
"github.com/iyear/tdl/pkg/tpath"
"github.com/iyear/tdl/pkg/utils"
@ -78,7 +79,7 @@ func Desktop(ctx context.Context, opts *Options) error {
return err
}
if err = kvd.Set(key.App(), []byte(consts.AppDesktop)); err != nil {
if err = kvd.Set(key.App(), []byte(tclient.AppDesktop)); err != nil {
return err
}

View File

@ -11,12 +11,12 @@ import (
"github.com/spf13/viper"
"go.uber.org/multierr"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
"github.com/iyear/tdl/pkg/uploader"
"github.com/iyear/tdl/pkg/utils"
)
@ -37,12 +37,9 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
color.Blue("Files count: %d", len(files))
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return errors.Wrap(err, "create middlewares")
}
pool := dcpool.NewPool(c, int64(viper.GetInt(consts.FlagPoolSize)), middlewares...)
pool := dcpool.NewPool(c,
int64(viper.GetInt(consts.FlagPoolSize)),
tclient.NewDefaultMiddlewares(ctx, viper.GetDuration(consts.FlagReconnectTimeout))...)
defer multierr.AppendInvoke(&rerr, multierr.Close(pool))
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(pool.Default(ctx))

View File

@ -1,32 +1,3 @@
package consts
import (
"github.com/gotd/td/telegram"
)
const (
AppBuiltin = "builtin"
AppDesktop = "desktop"
)
var Apps = map[string]struct {
AppID int
AppHash string
}{
// application created by iyear
AppBuiltin: {AppID: 15055931, AppHash: "021d433426cbb920eeb95164498fe3d3"},
// application created by tdesktop.
// https://opentele.readthedocs.io/en/latest/documentation/authorization/api/#class-telegramdesktop
AppDesktop: {AppID: 2040, AppHash: "b18441a1ff607e10a989891a5462e627"},
}
var Device = telegram.DeviceConfig{
DeviceModel: "Desktop",
SystemVersion: "Windows 10",
AppVersion: "4.2.4 x64",
LangCode: "en",
SystemLangCode: "en-US",
LangPack: "tdesktop",
}
const FileMaxSize = 4000 * 1024 * 1024