Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(session): fix encoding, use tg.Config subset #886

Merged
merged 2 commits into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 83 additions & 1 deletion session/session.go
Expand Up @@ -10,9 +10,91 @@ import (
"github.com/gotd/td/tg"
)

// Config is subset of tg.Config.
type Config struct {
// Indicates that telegram is probably censored by governments/ISPs in the current region
BlockedMode bool
// Whether pfs¹ was used
//
// Links:
// 1) https://core.telegram.org/api/pfs
PFSEnabled bool
// Whether to forcefully try connecting using IPv6 dcOptions¹
//
// Links:
// 1) https://core.telegram.org/type/DcOption
ForceTryIpv6 bool
// Current date at the server
Date int
// Expiration date of this config: when it expires it'll have to be refetched using help
// getConfig¹
//
// Links:
// 1) https://core.telegram.org/method/help.getConfig
Expires int
// Whether we're connected to the test DCs
TestMode bool
// ID of the DC that returned the reply
ThisDC int
// DC IP list
DCOptions []tg.DCOption
// Domain name for fetching encrypted DC list from DNS TXT record
DCTxtDomainName string
// Temporary passport¹ sessions
//
// Links:
// 1) https://core.telegram.org/passport
//
// Use SetTmpSessions and GetTmpSessions helpers.
TmpSessions int
// DC ID to use to download webfiles¹
//
// Links:
// 1) https://core.telegram.org/api/files#downloading-webfiles
WebfileDCID int
}

// ConfigFromTG converts tg.Config to Config.
//
// Note that Config is the subset of tg.Config, so data loss is possible.
func ConfigFromTG(c tg.Config) Config {
return Config{
BlockedMode: c.BlockedMode,
ForceTryIpv6: c.ForceTryIpv6,
PFSEnabled: c.PFSEnabled,
Date: c.Date,
Expires: c.Expires,
TestMode: c.TestMode,
ThisDC: c.ThisDC,
DCOptions: c.DCOptions,
DCTxtDomainName: c.DCTxtDomainName,
WebfileDCID: c.WebfileDCID,
TmpSessions: c.TmpSessions,
}
}

// TG returns tg.Config from Config.
//
// Note that config is the subset of tg.Config, so some fields will be unset.
func (c Config) TG() tg.Config {
return tg.Config{
BlockedMode: c.BlockedMode,
ForceTryIpv6: c.ForceTryIpv6,
PFSEnabled: c.PFSEnabled,
Date: c.Date,
Expires: c.Expires,
TestMode: c.TestMode,
ThisDC: c.ThisDC,
DCOptions: c.DCOptions,
DCTxtDomainName: c.DCTxtDomainName,
WebfileDCID: c.WebfileDCID,
TmpSessions: c.TmpSessions,
}
}

// Data of session.
type Data struct {
Config tg.Config
Config Config
DC int
Addr string
AuthKey []byte
Expand Down
4 changes: 1 addition & 3 deletions session/session_test.go
Expand Up @@ -6,8 +6,6 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/gotd/td/tg"
)

func testStorage(storage Storage) func(t *testing.T) {
Expand All @@ -23,7 +21,7 @@ func testStorage(storage Storage) func(t *testing.T) {
a.ErrorIs(err, ErrNotFound)

data := &Data{
Config: tg.Config{},
Config: Config{},
DC: 2,
Addr: "localhost:8080",
AuthKey: bytes.Repeat([]byte{'a'}, 256),
Expand Down
40 changes: 7 additions & 33 deletions session/tdesktop.go
Expand Up @@ -25,7 +25,7 @@ func findDCAddr(list []tg.DCOption, dcID int) string {
return ""
}

func mapConfig(mainDC int, cfg tdesktop.MTPConfig) tg.Config {
func mapConfig(mainDC int, cfg tdesktop.MTPConfig) Config {
var opts []tg.DCOption
for _, dc := range cfg.DCOptions.Options {
opts = append(opts, tg.DCOption{
Expand All @@ -41,38 +41,12 @@ func mapConfig(mainDC int, cfg tdesktop.MTPConfig) tg.Config {
Secret: dc.Secret,
})
}
return tg.Config{
DCOptions: opts,
ThisDC: mainDC,
ChatSizeMax: int(cfg.ChatSizeMax),
MegagroupSizeMax: int(cfg.MegagroupSizeMax),
ForwardedCountMax: int(cfg.ForwardedCountMax),
OnlineUpdatePeriodMs: int(cfg.OnlineUpdatePeriod),
OfflineBlurTimeoutMs: int(cfg.OfflineBlurTimeout),
OfflineIdleTimeoutMs: int(cfg.OfflineIdleTimeout),
OnlineCloudTimeoutMs: int(cfg.OnlineCloudTimeout),
NotifyCloudDelayMs: int(cfg.NotifyCloudDelay),
NotifyDefaultDelayMs: int(cfg.NotifyDefaultDelay),
SavedGifsLimit: int(cfg.SavedGifsLimit),
EditTimeLimit: int(cfg.EditTimeLimit),
RevokeTimeLimit: int(cfg.RevokeTimeLimit),
RevokePmTimeLimit: int(cfg.RevokePrivateTimeLimit),
RevokePmInbox: cfg.RevokePrivateInbox,
StickersRecentLimit: int(cfg.StickersRecentLimit),
StickersFavedLimit: int(cfg.StickersFavedLimit),
PinnedDialogsCountMax: int(cfg.PinnedDialogsCountMax),
PinnedInfolderCountMax: int(cfg.PinnedDialogsInFolderMax),
MeURLPrefix: cfg.InternalLinksDomain,
ChannelsReadMediaPeriod: int(cfg.ChannelsReadMediaPeriod),
CallReceiveTimeoutMs: int(cfg.CallReceiveTimeoutMs),
CallRingTimeoutMs: int(cfg.CallRingTimeoutMs),
CallConnectTimeoutMs: int(cfg.CallConnectTimeoutMs),
CallPacketTimeoutMs: int(cfg.CallPacketTimeoutMs),
WebfileDCID: int(cfg.WebFileDCID),
DCTxtDomainName: cfg.TxtDomainString,
PhonecallsEnabled: cfg.PhoneCallsEnabled,
BlockedMode: cfg.BlockedMode,
CaptionLengthMax: int(cfg.CaptionLengthMax),
return Config{
DCOptions: opts,
ThisDC: mainDC,
WebfileDCID: int(cfg.WebFileDCID),
DCTxtDomainName: cfg.TxtDomainString,
BlockedMode: cfg.BlockedMode,
}
}

Expand Down
3 changes: 1 addition & 2 deletions session/tdesktop_test.go
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/gotd/td/internal/crypto"
"github.com/gotd/td/session/tdesktop"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"
)

func TestTDesktopSession(t *testing.T) {
Expand All @@ -30,7 +29,7 @@ func TestTDesktopSession(t *testing.T) {
},
}, &Data{
DC: 2,
Config: tg.Config{
Config: Config{
ThisDC: 2,
},
Addr: findDCAddr(dcs.Prod().Options, 2),
Expand Down
2 changes: 1 addition & 1 deletion telegram/session.go
Expand Up @@ -76,7 +76,7 @@ func (c *Client) saveSession(cfg tg.Config, s mtproto.Session) error {
}

// Updating previous data.
data.Config = cfg
data.Config = session.ConfigFromTG(cfg)
data.AuthKey = s.Key.Value[:]
data.AuthKeyID = s.Key.ID[:]
data.DC = cfg.ThisDC
Expand Down