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:
@@ -0,0 +1,191 @@
|
||||
// Package deeplink contains deeplink parsing helpers.
|
||||
package deeplink
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
)
|
||||
|
||||
// Type is an enum type of Telegram deeplinks types.
|
||||
type Type string
|
||||
|
||||
const (
|
||||
// Resolve is deeplink like
|
||||
//
|
||||
// tg:resolve?domain={domain}
|
||||
// tg://resolve?domain={domain}
|
||||
// https://t.me/{domain}
|
||||
// https://telegram.me/{domain}
|
||||
//
|
||||
Resolve Type = "resolve"
|
||||
|
||||
// Join is deeplink like
|
||||
//
|
||||
// tg:join?invite={hash}
|
||||
// tg://join?invite={hash}
|
||||
// https://t.me/joinchat/{hash}
|
||||
// https://telegram.me/joinchat/{hash}
|
||||
// t.me/+{hash}
|
||||
//
|
||||
Join Type = "join"
|
||||
)
|
||||
|
||||
// DeepLink represents Telegram deeplink.
|
||||
type DeepLink struct {
|
||||
Type Type
|
||||
Args url.Values
|
||||
}
|
||||
|
||||
func ensureParam(query url.Values, key string) error {
|
||||
if query.Get(key) == "" {
|
||||
return errors.Errorf("should have %q query parameter", key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d DeepLink) validate() error {
|
||||
switch d.Type {
|
||||
case Resolve:
|
||||
return ensureParam(d.Args, "domain")
|
||||
case Join:
|
||||
return ensureParam(d.Args, "invite")
|
||||
default:
|
||||
return errors.Errorf("unsupported deeplink %q", d.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func parseTg(u *url.URL) (DeepLink, error) {
|
||||
query := u.Query()
|
||||
switch Type(u.Hostname()) {
|
||||
case Resolve:
|
||||
return DeepLink{
|
||||
Type: Resolve,
|
||||
Args: query,
|
||||
}, nil
|
||||
case Join:
|
||||
return DeepLink{
|
||||
Type: Join,
|
||||
Args: query,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return DeepLink{}, errors.Errorf("unsupported deeplink %q", u.String())
|
||||
}
|
||||
|
||||
func parseHTTPS(u *url.URL) (DeepLink, error) {
|
||||
cleanInviteHash := func(root string) string {
|
||||
hash := strings.Trim(root, "+ ")
|
||||
if u.RawPath == "" {
|
||||
hash = url.PathEscape(hash)
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
p := strings.TrimPrefix(u.Path, "/")
|
||||
p = strings.TrimSuffix(p, "/")
|
||||
split := strings.Split(p, "/")
|
||||
var (
|
||||
root = split[0]
|
||||
base string
|
||||
)
|
||||
if len(split) > 1 {
|
||||
base = split[1]
|
||||
}
|
||||
|
||||
switch root {
|
||||
case "joinchat":
|
||||
query.Set("invite", cleanInviteHash(base))
|
||||
return DeepLink{
|
||||
Type: Join,
|
||||
Args: query,
|
||||
}, nil
|
||||
case "":
|
||||
return DeepLink{}, errors.Errorf("unsupported deeplink %q", u.String())
|
||||
}
|
||||
|
||||
switch root[0] {
|
||||
case ' ', '+':
|
||||
query.Set("invite", cleanInviteHash(root))
|
||||
return DeepLink{
|
||||
Type: Join,
|
||||
Args: query,
|
||||
}, nil
|
||||
default:
|
||||
if err := ValidateDomain(root); err != nil {
|
||||
return DeepLink{}, err
|
||||
}
|
||||
query.Set("domain", root)
|
||||
return DeepLink{
|
||||
Type: Resolve,
|
||||
Args: query,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func hasTelegramPrefix(link string) bool {
|
||||
return strings.HasPrefix(link, "t.me") ||
|
||||
strings.HasPrefix(link, "telegram.me") ||
|
||||
strings.HasPrefix(link, "telegram.dog")
|
||||
}
|
||||
|
||||
// IsDeeplinkLike returns true if string may be a valid deeplink.
|
||||
func IsDeeplinkLike(link string) bool {
|
||||
return strings.HasPrefix(link, "tg:") ||
|
||||
hasTelegramPrefix(link) ||
|
||||
strings.HasPrefix(link, "https://")
|
||||
}
|
||||
|
||||
// Parse parses and returns deeplink.
|
||||
func Parse(link string) (DeepLink, error) {
|
||||
switch {
|
||||
// Normalize case like t.me/gotd.
|
||||
case hasTelegramPrefix(link):
|
||||
link = strings.TrimSuffix("https://"+link, "/")
|
||||
// Normalize case like tg:resolve?domain=gotd.
|
||||
case !strings.HasPrefix(link, "tg://") && strings.HasPrefix(link, "tg:"):
|
||||
link = "tg://" + strings.TrimPrefix(link, "tg:")
|
||||
}
|
||||
|
||||
u, err := url.Parse(link)
|
||||
if err != nil {
|
||||
return DeepLink{}, errors.Wrapf(err, "invalid URL %q", link)
|
||||
}
|
||||
|
||||
var d DeepLink
|
||||
switch {
|
||||
case u.Scheme == "https":
|
||||
switch strings.TrimPrefix(u.Hostname(), "www.") {
|
||||
case "t.me", "telegram.me", "telegram.dog":
|
||||
d, err = parseHTTPS(u)
|
||||
default:
|
||||
return DeepLink{}, errors.Errorf("invalid domain %q", link)
|
||||
}
|
||||
case u.Scheme == "tg":
|
||||
d, err = parseTg(u)
|
||||
default:
|
||||
return DeepLink{}, errors.Errorf("invalid deeplink %q", link)
|
||||
}
|
||||
if err != nil {
|
||||
return DeepLink{}, err
|
||||
}
|
||||
if err := d.validate(); err != nil {
|
||||
return DeepLink{}, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Expect parses deeplink and check type its type.
|
||||
func Expect(link string, typ Type) (DeepLink, error) {
|
||||
l, err := Parse(link)
|
||||
if err != nil {
|
||||
return l, err
|
||||
}
|
||||
if l.Type != typ {
|
||||
return l, errors.Errorf("unexpected deeplink type %q", l.Type)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//go:build go1.18
|
||||
|
||||
package deeplink
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func addSuites(f *testing.F, suites map[string][]testCase) {
|
||||
for _, suite := range suites {
|
||||
for _, test := range suite {
|
||||
f.Add(test.input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FuzzParse(f *testing.F) {
|
||||
for _, typeSuite := range typeSuites {
|
||||
addSuites(f, typeSuite)
|
||||
}
|
||||
|
||||
f.Fuzz(func(t *testing.T, link string) {
|
||||
_, err := Parse(link)
|
||||
if err != nil {
|
||||
t.Skip(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package deeplink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type testCase struct {
|
||||
link DeepLink
|
||||
input string
|
||||
wantErr bool
|
||||
}
|
||||
|
||||
func join(arg string) DeepLink {
|
||||
return DeepLink{
|
||||
Type: Join,
|
||||
Args: map[string][]string{
|
||||
"invite": {arg},
|
||||
},
|
||||
}
|
||||
}
|
||||
func resolve(arg string) DeepLink {
|
||||
return DeepLink{
|
||||
Type: Resolve,
|
||||
Args: map[string][]string{
|
||||
"domain": {arg},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func joinSuite() map[string][]testCase {
|
||||
expect := join("AAAAAAAAAAAAAAAAAA")
|
||||
return map[string][]testCase{
|
||||
"Test": {
|
||||
{expect, `t.me/joinchat/AAAAAAAAAAAAAAAAAA`, false},
|
||||
{expect, `t.me/joinchat/AAAAAAAAAAAAAAAAAA/`, false},
|
||||
{expect, `t.me/+AAAAAAAAAAAAAAAAAA`, false},
|
||||
{expect, `t.me/+AAAAAAAAAAAAAAAAAA/`, false},
|
||||
{expect, `t.me/ +AAAAAAAAAAAAAAAAAA/`, false},
|
||||
{expect, `https://t.me/joinchat/AAAAAAAAAAAAAAAAAA`, false},
|
||||
{expect, `https://t.me/joinchat/AAAAAAAAAAAAAAAAAA/`, false},
|
||||
{expect, `tg:join?invite=AAAAAAAAAAAAAAAAAA`, false},
|
||||
{expect, `tg://join?invite=AAAAAAAAAAAAAAAAAA`, false},
|
||||
|
||||
{DeepLink{}, `https://t.co/joinchat/AAAAAAAAAAAAAAAAAA`, true},
|
||||
{DeepLink{}, `rt://join?invite=AAAAAAAAAAAAAAAAAA`, true},
|
||||
},
|
||||
"TDLib": {
|
||||
// t.me/+<hash>
|
||||
// Positive
|
||||
{join("aba%20aba"), "t.me/+aba%20aba", false},
|
||||
{join("aba0aba"), "t.me/+aba%30aba", false},
|
||||
{join("123456a"), "t.me/+123456a", false},
|
||||
{join("12345678901"), "t.me/%2012345678901", false},
|
||||
// Negative
|
||||
{DeepLink{}, "t.me/+?invite=abcdef", true},
|
||||
{DeepLink{}, "t.me/+", true},
|
||||
{DeepLink{}, "t.me/+/abcdef", true},
|
||||
{DeepLink{}, "t.me/ ?/abcdef", true},
|
||||
{DeepLink{}, "t.me/+?abcdef", true},
|
||||
{DeepLink{}, "t.me/+#abcdef", true},
|
||||
{DeepLink{}, "t.me/ /123456/123123/12/31/a/s//21w/?asdas#test", true},
|
||||
|
||||
// t.me/joinchat/<hash>
|
||||
// Positive
|
||||
{join("abacaba"), "t.me/joinchat/abacaba", false},
|
||||
{join("aba%20aba"), "t.me/joinchat/aba%20aba", false},
|
||||
{join("aba0aba"), "t.me/joinchat/aba%30aba", false},
|
||||
{join("123456a"), "t.me/joinchat/123456a", false},
|
||||
{join("12345678901"), "t.me/joinchat/12345678901", false},
|
||||
{join("123456"), "t.me/joinchat/123456", false},
|
||||
{join("123456"), "t.me/joinchat/123456/123123/12/31/a/s//21w/?asdas#test", false},
|
||||
// Negative
|
||||
{DeepLink{}, "t.me/joinchat?invite=abcdef", true},
|
||||
{DeepLink{}, "t.me/joinchat", true},
|
||||
{DeepLink{}, "t.me/joinchat/", true},
|
||||
{DeepLink{}, "t.me/joinchat//abcdef", true},
|
||||
{DeepLink{}, "t.me/joinchat?/abcdef", true},
|
||||
{DeepLink{}, "t.me/joinchat/?abcdef", true},
|
||||
{DeepLink{}, "t.me/joinchat/#abcdef", true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resolveSuite() map[string][]testCase {
|
||||
expect := resolve("gotd_ru")
|
||||
return map[string][]testCase{
|
||||
"Test": {
|
||||
{expect, `t.me/gotd_ru`, false},
|
||||
{expect, `t.me/gotd_ru/`, false},
|
||||
{expect, `https://t.me/gotd_ru`, false},
|
||||
{expect, `https://t.me/gotd_ru/`, false},
|
||||
{expect, `tg:resolve?domain=gotd_ru`, false},
|
||||
{expect, `tg:resolve?&&&&&&&domain=gotd_ru`, false},
|
||||
{expect, `tg://resolve?domain=gotd_ru`, false},
|
||||
|
||||
{DeepLink{}, `https://t.co/gotd_ru`, true},
|
||||
{DeepLink{}, `rt://join?invite=AAAAAAAAAAAAAAAAAA`, true},
|
||||
},
|
||||
"TDLib": {
|
||||
// t.me/<domain>
|
||||
// Positive
|
||||
{resolve("a"), "t.me/a", false},
|
||||
{resolve("abcdefghijklmnopqrstuvwxyz123456"), "t.me/abcdefghijklmnopqrstuvwxyz123456", false},
|
||||
{resolve("Aasdf"), "t.me/Aasdf", false},
|
||||
{resolve("asdf0"), "t.me/asdf0", false},
|
||||
{resolve("username"), "t.me/username/0/a//s/as?gam=asd", false},
|
||||
{resolve("username"), "t.me/username/aasdas?test=1", false},
|
||||
{resolve("username"), "t.me/username/0", false},
|
||||
{resolve("telecram"), "https://telegram.dog/tele%63ram", false},
|
||||
// Negative
|
||||
{DeepLink{}, "t.me/abcdefghijklmnopqrstuvwxyz1234567", true},
|
||||
{DeepLink{}, "t.me/abcdefghijklmnop-qrstuvwxyz", true},
|
||||
{DeepLink{}, "t.me/abcdefghijklmnop~qrstuvwxyz", true},
|
||||
{DeepLink{}, "t.me/_asdf", true},
|
||||
{DeepLink{}, "t.me/0asdf", true},
|
||||
{DeepLink{}, "t.me/9asdf", true},
|
||||
{DeepLink{}, "t.me/asdf_", true},
|
||||
{DeepLink{}, "t.me/asd__fg", true},
|
||||
{DeepLink{}, "t.me//username", true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var typeSuites = map[string]map[string][]testCase{
|
||||
"Join": joinSuite(),
|
||||
"Resolve": resolveSuite(),
|
||||
}
|
||||
|
||||
func TestParseDeeplink(t *testing.T) {
|
||||
runSuite := func(suite []testCase) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
for i, test := range suite {
|
||||
t.Run(fmt.Sprintf("Test%d (%s)", i, test.input), func(t *testing.T) {
|
||||
a := require.New(t)
|
||||
d, err := Parse(test.input)
|
||||
|
||||
if test.wantErr {
|
||||
a.Error(err, test.input)
|
||||
} else {
|
||||
a.NoError(err, test.input)
|
||||
a.Equal(test.link, d, test.input)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for typeName, typeSuite := range typeSuites {
|
||||
t.Run(typeName, func(t *testing.T) {
|
||||
for suiteName, suite := range typeSuite {
|
||||
t.Run(suiteName, runSuite(suite))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package deeplink
|
||||
|
||||
import (
|
||||
"github.com/go-faster/errors"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/ascii"
|
||||
)
|
||||
|
||||
// ValidateDomain validate given domain (user) name
|
||||
func ValidateDomain(domain string) error {
|
||||
return checkDomainSymbols(domain)
|
||||
}
|
||||
|
||||
// checkDomainSymbols check that domain contains only a-z, A-Z, 0-9 and '_'
|
||||
// symbols.
|
||||
func checkDomainSymbols(domain string) error {
|
||||
switch {
|
||||
case domain == "":
|
||||
return errors.New("is empty")
|
||||
case len(domain) > 32:
|
||||
return errors.New("is too big")
|
||||
case !ascii.IsLatinLower(rune(domain[0])):
|
||||
return errors.New("must start with lower letter")
|
||||
case domain[len(domain)-1] == '_':
|
||||
return errors.New("must not end with '_'")
|
||||
}
|
||||
|
||||
for i, r := range domain {
|
||||
switch {
|
||||
case !ascii.IsLatinLetter(r) && !ascii.IsDigit(r) && r != '_':
|
||||
case i > 0 && domain[i] == '_' && domain[i] == domain[i-1]:
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.Errorf("unexpected %c at %d", r, i)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package deeplink
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestValidateDomain(t *testing.T) {
|
||||
tests := []struct {
|
||||
domain string
|
||||
wantErr bool
|
||||
}{
|
||||
{"a", false},
|
||||
{"abcdefghijklmnopqrstuvwxyz123456", false},
|
||||
{"Aasdf", false},
|
||||
{"asdf0", false},
|
||||
{"", true},
|
||||
{"asdf_", true},
|
||||
{"asd__fg", true},
|
||||
{"_asdf", true},
|
||||
{"0asdf", true},
|
||||
{"9asdf", true},
|
||||
{"abcdefghijklmnopqrstuvwxyz1234567", true},
|
||||
{"abcdefghijklmnop-qrstuvwxyz", true},
|
||||
{"abcdefghijklmnop~qrstuvwxyz", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.domain, func(t *testing.T) {
|
||||
err := ValidateDomain(tt.domain)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
"github.com/go-faster/errors"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/auth"
|
||||
)
|
||||
|
||||
func (s *Suite) createFlow(ctx context.Context) (auth.Flow, error) {
|
||||
var ua auth.UserAuthenticator
|
||||
for {
|
||||
ua = auth.Test(s.rand, s.dc)
|
||||
phone, err := ua.Phone(ctx)
|
||||
if err != nil {
|
||||
return auth.Flow{}, err
|
||||
}
|
||||
|
||||
s.usedMux.Lock()
|
||||
if _, ok := s.used[phone]; !ok {
|
||||
s.used[phone] = struct{}{}
|
||||
s.usedMux.Unlock()
|
||||
break
|
||||
}
|
||||
s.usedMux.Unlock()
|
||||
}
|
||||
|
||||
return auth.NewFlow(ua, auth.SendCodeOptions{}), nil
|
||||
}
|
||||
|
||||
// Authenticate authenticates client on test server.
|
||||
func (s *Suite) Authenticate(ctx context.Context, client auth.FlowClient) error {
|
||||
for {
|
||||
flow, err := s.createFlow(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create flow")
|
||||
}
|
||||
|
||||
if err := flow.Run(ctx, client); err != nil {
|
||||
if errors.Is(err, auth.ErrPasswordNotProvided) {
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.Wrap(err, "run flow")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// RetryAuthenticate authenticates client on test server.
|
||||
func (s *Suite) RetryAuthenticate(ctx context.Context, client auth.FlowClient) error {
|
||||
bck := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
|
||||
return backoff.Retry(func() error {
|
||||
return s.Authenticate(ctx, client)
|
||||
}, bck)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/auth"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
type mockFlow struct {
|
||||
flag bool
|
||||
}
|
||||
|
||||
var _ auth.FlowClient = &mockFlow{}
|
||||
|
||||
func (m *mockFlow) SignIn(context.Context, string, string, string) (*tg.AuthAuthorization, error) {
|
||||
// Ensure retry.
|
||||
if !m.flag {
|
||||
m.flag = true
|
||||
return nil, auth.ErrPasswordAuthNeeded
|
||||
}
|
||||
|
||||
return m.Password(context.Background(), "")
|
||||
}
|
||||
|
||||
func (m *mockFlow) SendCode(context.Context, string, auth.SendCodeOptions) (tg.AuthSentCodeClass, error) {
|
||||
return &tg.AuthSentCode{
|
||||
PhoneCodeHash: "hash",
|
||||
Type: &tg.AuthSentCodeTypeApp{},
|
||||
Timeout: 10,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *mockFlow) Password(context.Context, string) (*tg.AuthAuthorization, error) {
|
||||
return &tg.AuthAuthorization{
|
||||
User: &tg.User{
|
||||
ID: 10,
|
||||
Username: "aboba",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *mockFlow) SignUp(context.Context, auth.SignUp) (*tg.AuthAuthorization, error) {
|
||||
return nil, errors.New("must not be called")
|
||||
}
|
||||
|
||||
func TestSuite_Authenticate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
logger := zaptest.NewLogger(t)
|
||||
s := NewSuite(t, TestOptions{
|
||||
Logger: logger,
|
||||
})
|
||||
|
||||
flow := &mockFlow{}
|
||||
require.NoError(t, s.Authenticate(ctx, flow))
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// Package e2etest contains some helpers to make external E2E tests
|
||||
// using Telegram test server.
|
||||
package e2etest
|
||||
@@ -0,0 +1,190 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/message"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
|
||||
)
|
||||
|
||||
// EchoBot is a simple echo message bot.
|
||||
type EchoBot struct {
|
||||
suite *Suite
|
||||
|
||||
logger *zap.Logger
|
||||
auth chan<- *tg.User
|
||||
}
|
||||
|
||||
// NewEchoBot creates new echo bot.
|
||||
func NewEchoBot(suite *Suite, auth chan<- *tg.User) EchoBot {
|
||||
return EchoBot{
|
||||
suite: suite,
|
||||
logger: suite.logger.Named("echobot"),
|
||||
auth: auth,
|
||||
}
|
||||
}
|
||||
|
||||
type users struct {
|
||||
users map[int64]*tg.User
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func newUsers() *users {
|
||||
return &users{
|
||||
users: map[int64]*tg.User{},
|
||||
}
|
||||
}
|
||||
|
||||
func (m *users) empty() (r bool) {
|
||||
m.lock.RLock()
|
||||
r = len(m.users) < 1
|
||||
m.lock.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (m *users) add(list ...tg.UserClass) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
tg.UserClassArray(list).FillNotEmptyMap(m.users)
|
||||
}
|
||||
|
||||
func (m *users) get(id int64) (r *tg.User) {
|
||||
m.lock.RLock()
|
||||
r = m.users[id]
|
||||
m.lock.RUnlock()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (b EchoBot) login(ctx context.Context, client *telegram.Client) (*tg.User, error) {
|
||||
if err := b.suite.RetryAuthenticate(ctx, client.Auth()); err != nil {
|
||||
return nil, errors.Wrap(err, "authenticate")
|
||||
}
|
||||
|
||||
var me *tg.User
|
||||
if err := retry(ctx, func() (err error) {
|
||||
me, err = client.Self(ctx)
|
||||
return err
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
expectedUsername := "echobot" + strconv.FormatInt(me.ID, 10)
|
||||
raw := tg.NewClient(retryInvoker{prev: client})
|
||||
_, err := raw.AccountUpdateUsername(ctx, expectedUsername)
|
||||
if err != nil {
|
||||
if !tgerr.Is(err, tg.ErrUsernameNotModified) {
|
||||
return nil, errors.Wrap(err, "update username")
|
||||
}
|
||||
}
|
||||
me, err = retryResult(ctx, func() (*tg.User, error) {
|
||||
return client.Self(ctx)
|
||||
})
|
||||
if me.Username != expectedUsername {
|
||||
return nil, errors.Errorf("expected username %q, got %q", expectedUsername, me.Username)
|
||||
}
|
||||
|
||||
return me, nil
|
||||
}
|
||||
|
||||
func (b EchoBot) handler(client *telegram.Client) tg.NewMessageHandler {
|
||||
dialogsUsers := newUsers()
|
||||
|
||||
raw := tg.NewClient(client)
|
||||
sender := message.NewSender(raw)
|
||||
return func(ctx context.Context, entities tg.Entities, update *tg.UpdateNewMessage) error {
|
||||
if filterMessage(update) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m, ok := update.Message.(interface{ GetMessage() string }); ok {
|
||||
b.logger.Named("dispatcher").
|
||||
Info("Got new message update", zap.String("message", m.GetMessage()))
|
||||
}
|
||||
|
||||
if dialogsUsers.empty() {
|
||||
dialogs, err := retryResult(ctx, func() (tg.MessagesDialogsClass, error) {
|
||||
dialogs, err := raw.MessagesGetDialogs(ctx, &tg.MessagesGetDialogsRequest{
|
||||
Limit: 100,
|
||||
OffsetPeer: &tg.InputPeerEmpty{},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get dialogs")
|
||||
}
|
||||
return dialogs, nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get dialogs")
|
||||
}
|
||||
if dlg, ok := dialogs.AsModified(); ok {
|
||||
dialogsUsers.add(dlg.GetUsers()...)
|
||||
}
|
||||
}
|
||||
|
||||
switch m := update.Message.(type) {
|
||||
case *tg.Message:
|
||||
switch peer := m.PeerID.(type) {
|
||||
case *tg.PeerUser:
|
||||
user := entities.Users[peer.UserID]
|
||||
if user == nil {
|
||||
user = dialogsUsers.get(peer.UserID)
|
||||
}
|
||||
|
||||
b.logger.Info("Got message",
|
||||
zap.String("text", m.Message),
|
||||
zap.Int64("user_id", user.ID),
|
||||
zap.String("user_first_name", user.FirstName),
|
||||
zap.String("username", user.Username),
|
||||
)
|
||||
|
||||
if err := retry(ctx, func() error {
|
||||
_, err := sender.To(user.AsInputPeer()).Text(ctx, m.Message)
|
||||
return err
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "send message")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Run setups and starts echo bot.
|
||||
func (b EchoBot) Run(ctx context.Context) error {
|
||||
dispatcher := tg.NewUpdateDispatcher()
|
||||
client := b.suite.Client(b.logger, dispatcher)
|
||||
dispatcher.OnNewMessage(b.handler(client))
|
||||
|
||||
return client.Run(ctx, func(ctx context.Context) error {
|
||||
defer close(b.auth)
|
||||
|
||||
me, err := b.login(ctx, client)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "login")
|
||||
}
|
||||
|
||||
b.logger.Info("Logged in",
|
||||
zap.String("user", me.Username),
|
||||
zap.Int64("id", me.ID),
|
||||
)
|
||||
|
||||
select {
|
||||
case b.auth <- me:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
func filterMessage(update *tg.UpdateNewMessage) bool {
|
||||
if v, ok := update.Message.(interface{ GetOut() bool }); ok && v.GetOut() {
|
||||
return true
|
||||
}
|
||||
|
||||
if v, ok := update.Message.(interface{ GetPeerID() tg.PeerClass }); ok && v.GetPeerID() == nil {
|
||||
return true
|
||||
}
|
||||
if _, ok := update.Message.(*tg.MessageService); ok {
|
||||
return true
|
||||
}
|
||||
if v, ok := update.Message.(interface{ GetMessage() string }); ok && strings.HasPrefix(v.GetMessage(), "Login code:") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/constant"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/crypto"
|
||||
)
|
||||
|
||||
// TestOptions contains some common test server settings.
|
||||
type TestOptions struct {
|
||||
AppID int
|
||||
AppHash string
|
||||
DC int
|
||||
Random io.Reader
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
func (opt *TestOptions) setDefaults() {
|
||||
if opt.AppID == 0 {
|
||||
opt.AppID = constant.TestAppID
|
||||
}
|
||||
if opt.AppHash == "" {
|
||||
opt.AppHash = constant.TestAppHash
|
||||
}
|
||||
if opt.DC == 0 {
|
||||
opt.DC = 2
|
||||
}
|
||||
if opt.Random == nil {
|
||||
opt.Random = crypto.DefaultRand()
|
||||
}
|
||||
if opt.Logger == nil {
|
||||
opt.Logger = zap.NewNop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
|
||||
)
|
||||
|
||||
type retryInvoker struct {
|
||||
prev tg.Invoker
|
||||
}
|
||||
|
||||
func retryResult[T any](ctx context.Context, cb func() (T, error)) (T, error) {
|
||||
var zero T
|
||||
return backoff.RetryWithData[T](func() (T, error) {
|
||||
res, err := cb()
|
||||
if err != nil {
|
||||
if tgerr.IsCode(err, -500) {
|
||||
return zero, err
|
||||
}
|
||||
if tgerr.Is(err, "CONNECTION_NOT_INITED") {
|
||||
return zero, err
|
||||
}
|
||||
if ok, err := tgerr.FloodWait(ctx, err); ok {
|
||||
return zero, err
|
||||
}
|
||||
return zero, backoff.Permanent(err)
|
||||
}
|
||||
return res, nil
|
||||
}, backoff.WithContext(backoff.NewConstantBackOff(time.Millisecond*500), ctx))
|
||||
}
|
||||
|
||||
func retry(ctx context.Context, cb func() error) error {
|
||||
return backoff.Retry(func() error {
|
||||
if err := cb(); err != nil {
|
||||
if tgerr.IsCode(err, -500) {
|
||||
return err
|
||||
}
|
||||
if tgerr.Is(err, "CONNECTION_NOT_INITED") {
|
||||
return err
|
||||
}
|
||||
if ok, err := tgerr.FloodWait(ctx, err); ok {
|
||||
return err
|
||||
}
|
||||
return backoff.Permanent(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
|
||||
}
|
||||
|
||||
func (w retryInvoker) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
|
||||
return retry(ctx, func() error {
|
||||
return w.prev.Invoke(ctx, input, output)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/dcs"
|
||||
)
|
||||
|
||||
// Suite is struct which contains external E2E test parameters.
|
||||
type Suite struct {
|
||||
TB require.TestingT
|
||||
appID int
|
||||
appHash string
|
||||
dc int
|
||||
logger *zap.Logger
|
||||
|
||||
rand io.Reader
|
||||
// already used phone numbers
|
||||
used map[string]struct{}
|
||||
usedMux sync.Mutex
|
||||
}
|
||||
|
||||
// NewSuite creates new Suite.
|
||||
func NewSuite(tb require.TestingT, config TestOptions) *Suite {
|
||||
config.setDefaults()
|
||||
return &Suite{
|
||||
TB: tb,
|
||||
appID: config.AppID,
|
||||
appHash: config.AppHash,
|
||||
dc: config.DC,
|
||||
logger: config.Logger,
|
||||
rand: config.Random,
|
||||
used: map[string]struct{}{},
|
||||
}
|
||||
}
|
||||
|
||||
// Client creates new *telegram.Client using this suite.
|
||||
func (s *Suite) Client(logger *zap.Logger, handler telegram.UpdateHandler) *telegram.Client {
|
||||
return telegram.NewClient(s.appID, s.appHash, telegram.Options{
|
||||
DC: s.dc,
|
||||
DCList: dcs.Test(),
|
||||
Logger: logger,
|
||||
UpdateHandler: handler,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package e2etest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/message"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
|
||||
)
|
||||
|
||||
// User is a simple user bot.
|
||||
type User struct {
|
||||
suite *Suite
|
||||
text []string
|
||||
username string
|
||||
|
||||
logger *zap.Logger
|
||||
message chan string
|
||||
}
|
||||
|
||||
// NewUser creates new User bot.
|
||||
func NewUser(suite *Suite, text []string, username string) User {
|
||||
return User{
|
||||
suite: suite,
|
||||
text: text,
|
||||
username: username,
|
||||
logger: suite.logger.Named("terentyev"),
|
||||
message: make(chan string, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) messageHandler(ctx context.Context, entities tg.Entities, update *tg.UpdateNewMessage) error {
|
||||
if filterMessage(update) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m, ok := update.Message.(interface{ GetMessage() string }); ok {
|
||||
u.logger.Named("dispatcher").
|
||||
Info("Got new message update", zap.String("message", m.GetMessage()))
|
||||
}
|
||||
|
||||
msg, ok := update.Message.(*tg.Message)
|
||||
if !ok {
|
||||
return errors.Errorf("unexpected type %T", update.Message)
|
||||
}
|
||||
|
||||
select {
|
||||
case u.message <- msg.Message:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// Run setups and starts user bot.
|
||||
func (u User) Run(ctx context.Context) error {
|
||||
dispatcher := tg.NewUpdateDispatcher()
|
||||
dispatcher.OnNewMessage(u.messageHandler)
|
||||
client := u.suite.Client(u.logger, dispatcher)
|
||||
sender := message.NewSender(tg.NewClient(retryInvoker{prev: client}))
|
||||
|
||||
return client.Run(ctx, func(ctx context.Context) error {
|
||||
if err := u.suite.RetryAuthenticate(ctx, client.Auth()); err != nil {
|
||||
return errors.Wrap(err, "authenticate")
|
||||
}
|
||||
|
||||
peer, err := sender.Resolve(u.username).AsInputPeer(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "resolve bot username %q", u.username)
|
||||
}
|
||||
|
||||
for _, line := range u.text {
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
_, err = sender.To(peer).Text(ctx, line)
|
||||
if flood, err := tgerr.FloodWait(ctx, err); err != nil {
|
||||
if flood {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case gotMessage := <-u.message:
|
||||
require.Equal(u.suite.TB, line, gotMessage)
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package upconv contains updates conversion helpers.
|
||||
package upconv
|
||||
@@ -0,0 +1,122 @@
|
||||
package upconv
|
||||
|
||||
import "go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
|
||||
func convertOptional(msg *tg.Message, i tg.UpdatesClass) {
|
||||
if u, ok := i.(interface {
|
||||
GetFwdFrom() (tg.MessageFwdHeader, bool)
|
||||
}); ok {
|
||||
if v, ok := u.GetFwdFrom(); ok {
|
||||
msg.SetFwdFrom(v)
|
||||
}
|
||||
}
|
||||
if u, ok := i.(interface{ GetViaBotID() (int64, bool) }); ok {
|
||||
if v, ok := u.GetViaBotID(); ok {
|
||||
msg.SetViaBotID(v)
|
||||
}
|
||||
}
|
||||
if u, ok := i.(interface {
|
||||
GetReplyTo() (tg.MessageReplyHeader, bool)
|
||||
}); ok {
|
||||
if v, ok := u.GetReplyTo(); ok {
|
||||
msg.SetReplyTo(&v)
|
||||
}
|
||||
}
|
||||
if u, ok := i.(interface {
|
||||
GetReplyTo() (tg.MessageReplyHeaderClass, bool)
|
||||
}); ok {
|
||||
if v, ok := u.GetReplyTo(); ok {
|
||||
msg.SetReplyTo(v)
|
||||
}
|
||||
}
|
||||
if u, ok := i.(interface {
|
||||
GetEntities() ([]tg.MessageEntityClass, bool)
|
||||
}); ok {
|
||||
if v, ok := u.GetEntities(); ok {
|
||||
msg.SetEntities(v)
|
||||
}
|
||||
}
|
||||
if u, ok := i.(interface {
|
||||
GetMedia() (tg.MessageMediaClass, bool)
|
||||
}); ok {
|
||||
if v, ok := u.GetMedia(); ok {
|
||||
msg.SetMedia(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ShortMessage converts UpdateShortMessage to UpdateShort.
|
||||
func ShortMessage(u *tg.UpdateShortMessage) *tg.UpdateShort {
|
||||
msg := &tg.Message{
|
||||
ID: u.ID,
|
||||
PeerID: &tg.PeerUser{UserID: u.UserID},
|
||||
Message: u.Message,
|
||||
Date: u.Date,
|
||||
}
|
||||
// Optional fields should set by SetXXX(), so GetXXX and Flags.Has()
|
||||
// can return the right values even we hav't call .Encode()
|
||||
msg.SetOut(u.Out)
|
||||
msg.SetMentioned(u.Mentioned)
|
||||
msg.SetMediaUnread(u.MediaUnread)
|
||||
msg.SetSilent(u.Silent)
|
||||
|
||||
if !u.Out {
|
||||
msg.SetFromID(&tg.PeerUser{UserID: u.UserID})
|
||||
}
|
||||
convertOptional(msg, u)
|
||||
|
||||
return &tg.UpdateShort{
|
||||
Update: &tg.UpdateNewMessage{
|
||||
Message: msg,
|
||||
Pts: u.Pts,
|
||||
PtsCount: u.PtsCount,
|
||||
},
|
||||
Date: u.Date,
|
||||
}
|
||||
}
|
||||
|
||||
// ShortChatMessage converts UpdateShortChatMessage to UpdateShort.
|
||||
func ShortChatMessage(u *tg.UpdateShortChatMessage) *tg.UpdateShort {
|
||||
msg := &tg.Message{
|
||||
ID: u.ID,
|
||||
PeerID: &tg.PeerChat{ChatID: u.ChatID},
|
||||
Message: u.Message,
|
||||
Date: u.Date,
|
||||
}
|
||||
|
||||
msg.SetFromID(&tg.PeerUser{UserID: u.FromID})
|
||||
msg.SetOut(u.Out)
|
||||
msg.SetMentioned(u.Mentioned)
|
||||
msg.SetMediaUnread(u.MediaUnread)
|
||||
msg.SetSilent(u.Silent)
|
||||
|
||||
convertOptional(msg, u)
|
||||
|
||||
return &tg.UpdateShort{
|
||||
Update: &tg.UpdateNewMessage{
|
||||
Message: msg,
|
||||
Pts: u.Pts,
|
||||
PtsCount: u.PtsCount,
|
||||
},
|
||||
Date: u.Date,
|
||||
}
|
||||
}
|
||||
|
||||
// ShortSentMessage converts UpdateShortSentMessage to UpdateShort.
|
||||
func ShortSentMessage(u *tg.UpdateShortSentMessage) *tg.UpdateShort {
|
||||
msg := &tg.Message{
|
||||
ID: u.ID,
|
||||
Date: u.Date,
|
||||
}
|
||||
msg.SetOut(u.Out)
|
||||
convertOptional(msg, u)
|
||||
|
||||
return &tg.UpdateShort{
|
||||
Update: &tg.UpdateNewMessage{
|
||||
Message: msg,
|
||||
Pts: u.Pts,
|
||||
PtsCount: u.PtsCount,
|
||||
},
|
||||
Date: u.Date,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Package version contains gotd module version getter.
|
||||
package version
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var versionOnce struct {
|
||||
version string
|
||||
sync.Once
|
||||
}
|
||||
|
||||
// GetVersion optimistically gets current client version.
|
||||
//
|
||||
// Does not handle replace directives.
|
||||
func GetVersion() string {
|
||||
versionOnce.Do(func() {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Hard-coded package name. Probably we can generate this via parsing
|
||||
// the go.mod file.
|
||||
const pkg = "go.mau.fi/mautrix-telegram/pkg/gotd"
|
||||
for _, d := range info.Deps {
|
||||
if strings.HasPrefix(d.Path, pkg) {
|
||||
versionOnce.version = d.Version
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return versionOnce.version
|
||||
}
|
||||
Reference in New Issue
Block a user