Documentation ¶
Overview ¶
Package session implements session storage.
Index ¶
- Variables
- 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 Data ¶
Data of session.
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" "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.
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.