session

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: MIT Imports: 13 Imported by: 81

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 Data

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

Data of session.

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"
	"fmt"

	"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.
	telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{
		SessionStorage: storage,
	})

	// Load saved gotd session.
	saved, err := loader.Load(ctx)
	if err != nil {
		panic(err)
	}

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

2 192.168.0.1:443

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.

Jump to

Keyboard shortcuts

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