session

package
v0.113.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 14 Imported by: 83

Documentation

Overview

Package session implements session storage.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("session storage: not found")

ErrNotFound means that session is not found in storage.

Functions

This section is empty.

Types

type Config added in v0.69.1

type Config struct {
	// Indicates that telegram is probably censored by governments/ISPs in the current region
	BlockedMode 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
}

Config is subset of tg.Config.

func ConfigFromTG added in v0.69.1

func ConfigFromTG(c tg.Config) Config

ConfigFromTG converts tg.Config to Config.

Note that Config is the subset of tg.Config, so data loss is possible.

func (Config) TG added in v0.69.1

func (c Config) TG() tg.Config

TG returns tg.Config from Config.

Note that config is the subset of tg.Config, so some fields will be unset.

type Data

type Data struct {
	Config    Config
	DC        int
	Addr      string
	AuthKey   []byte
	AuthKeyID []byte
	Salt      int64
}

Data of session.

func TDesktopSession added in v0.51.0

func TDesktopSession(account tdesktop.Account) (*Data, error)

TDesktopSession converts TDesktop's Account to Data.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/gotd/td/session"
	"github.com/gotd/td/session/tdesktop"
)

func main() {
	home, err := os.UserHomeDir()
	if err != nil {
		panic(err)
	}

	root := filepath.Join(home, "Downloads", "Telegram", "tdata")
	accounts, err := tdesktop.Read(root, nil)
	if err != nil {
		panic(err)
	}

	data, err := session.TDesktopSession(accounts[0])
	if err != nil {
		panic(err)
	}

	fmt.Println(data.DC, data.Addr)
}
Output:

Example (Convert)
package main

import (
	"context"
	"os"
	"path/filepath"

	"github.com/gotd/td/session"
	"github.com/gotd/td/session/tdesktop"
	"github.com/gotd/td/telegram"
)

func main() {
	ctx := context.Background()

	home, err := os.UserHomeDir()
	if err != nil {
		panic(err)
	}

	root := filepath.Join(home, "Downloads", "Telegram", "tdata")
	accounts, err := tdesktop.Read(root, nil)
	if err != nil {
		panic(err)
	}

	data, err := session.TDesktopSession(accounts[0])
	if err != nil {
		panic(err)
	}

	var (
		storage = new(session.StorageMemory)
		loader  = session.Loader{Storage: storage}
	)

	// Save decoded Telegram Desktop session as gotd session.
	if err := loader.Save(ctx, data); err != nil {
		panic(err)
	}

	// Create client.
	client := telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{
		SessionStorage: storage,
	})
	if err := client.Run(ctx, func(ctx context.Context) error {
		// Use Telegram Desktop session.
		return nil
	}); err != nil {
		panic(err)
	}
}
Output:

func TelethonSession added in v0.45.0

func TelethonSession(hx string) (*Data, error)

TelethonSession decodes Telethon's StringSession string to the Data. Notice that Telethon does not save tg.Config and server salt.

See https://docs.telethon.dev/en/latest/modules/sessions.html#telethon.sessions.string.StringSession.

See https://github.com/LonamiWebs/Telethon/blob/master/telethon/sessions/string.py#L29-L46.

Example
package main

import (
	"fmt"

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

func main() {
	// Get a session from Telethon.
	str := `1AsCoAAEBu2FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYW
FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY
WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWE=`

	data, err := session.TelethonSession(str)
	if err != nil {
		panic(err)
	}

	fmt.Println(data.DC, data.Addr)
}
Output:

2 192.168.0.1:443
Example (Convert)
package main

import (
	"context"

	"github.com/gotd/td/session"
	"github.com/gotd/td/telegram"
)

func main() {
	ctx := context.Background()
	// Get a session from Telethon.
	str := `1AsCoAAEBu2FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYW
FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY
WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWE=`

	data, err := session.TelethonSession(str)
	if err != nil {
		panic(err)
	}

	var (
		storage = new(session.StorageMemory)
		loader  = session.Loader{Storage: storage}
	)

	// Save decoded Telethon session as gotd session.
	if err := loader.Save(ctx, data); err != nil {
		panic(err)
	}

	// Create client.
	client := telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{
		SessionStorage: storage,
	})
	if err := client.Run(ctx, func(ctx context.Context) error {
		// Use Telethon session.
		return nil
	}); err != nil {
		panic(err)
	}
}
Output:

type FileStorage

type FileStorage struct {
	Path string
	// contains filtered or unexported fields
}

FileStorage implements SessionStorage for file system as file stored in Path.

func (*FileStorage) LoadSession

func (f *FileStorage) LoadSession(_ context.Context) ([]byte, error)

LoadSession loads session from file.

func (*FileStorage) StoreSession

func (f *FileStorage) StoreSession(_ context.Context, data []byte) error

StoreSession stores session to file.

type Loader

type Loader struct {
	Storage Storage
}

Loader wraps Storage implementing Data (un-)marshaling.

func (*Loader) Load

func (l *Loader) Load(ctx context.Context) (*Data, error)

Load loads Data from Storage.

func (*Loader) Save

func (l *Loader) Save(ctx context.Context, data *Data) error

Save saves Data to Storage.

type Storage

type Storage interface {
	LoadSession(ctx context.Context) ([]byte, error)
	StoreSession(ctx context.Context, data []byte) error
}

Storage is secure persistent storage for client session.

NB: Implementation security is important, attacker can abuse it not only for connecting as authenticated user or bot, but even decrypting previous messages in some situations.

type StorageMemory

type StorageMemory struct {
	// contains filtered or unexported fields
}

StorageMemory implements in-memory session storage. Goroutine-safe.

func (*StorageMemory) Bytes added in v0.45.0

func (s *StorageMemory) Bytes(to []byte) ([]byte, error)

Bytes appends raw session data to the given slice. Returns ErrNotFound if storage is nil or if underlying session is empty.

func (*StorageMemory) Clone added in v0.45.0

func (s *StorageMemory) Clone() *StorageMemory

Clone creates a clone of existing StorageMemory,

func (*StorageMemory) Dump added in v0.45.0

func (s *StorageMemory) Dump(w io.Writer) error

Dump dumps raw session data to the given writer. Returns ErrNotFound if storage is nil or if underlying session is empty.

func (*StorageMemory) LoadSession

func (s *StorageMemory) LoadSession(context.Context) ([]byte, error)

LoadSession loads session from memory.

func (*StorageMemory) StoreSession

func (s *StorageMemory) StoreSession(ctx context.Context, data []byte) error

StoreSession stores session to memory.

func (*StorageMemory) WriteFile added in v0.45.0

func (s *StorageMemory) WriteFile(name string, perm os.FileMode) error

WriteFile dumps raw session data to the named file, creating it if necessary. Returns ErrNotFound if storage is nil or if underlying session is empty.

Directories

Path Synopsis
Package tdesktop contains Telegram Desktop session decoder.
Package tdesktop contains Telegram Desktop session decoder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL