Documentation ¶
Overview ¶
Package session implements session storage.
Index ¶
- Variables
- type Config
- type Data
- type FileStorage
- type Loader
- type Storage
- type StorageMemory
- func (s *StorageMemory) Bytes(to []byte) ([]byte, error)
- func (s *StorageMemory) Clone() *StorageMemory
- func (s *StorageMemory) Dump(w io.Writer) error
- func (s *StorageMemory) LoadSession(context.Context) ([]byte, error)
- func (s *StorageMemory) StoreSession(ctx context.Context, data []byte) error
- func (s *StorageMemory) WriteFile(name string, perm os.FileMode) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 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 }
Config is subset of tg.Config.
func ConfigFromTG ¶ added in v0.69.1
ConfigFromTG converts tg.Config to Config.
Note that Config is the subset of tg.Config, so data loss is possible.
type Data ¶
Data of session.
func TDesktopSession ¶ added in v0.51.0
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
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.
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.