session

package
v1.0.0-beta24 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeGramjsSession

func DecodeGramjsSession(hx string) (*session.Data, error)

func DecodeMtcuteSession

func DecodeMtcuteSession(sessionStr string) (*session.Data, error)

DecodeMtcuteSession decodes an mtcute v3 string session into session.Data.

mtcute session strings are URL-safe base64 encoded binary data using TL-style serialization with the following layout:

| Field            | Size     | Description                        |
|------------------|----------|------------------------------------|
| version          | 1 byte   | Must be 3                          |
| flags            | 4 bytes  | LE int32 (bit0=hasSelf, bit2=hasMedia) |
| primaryDC        | variable | TL bytes-wrapped DC option         |
| mediaDC          | variable | TL bytes-wrapped DC option (if hasMedia) |
| selfUserID       | 8 bytes  | LE int64 (if hasSelf)              |
| selfIsBot        | 4 bytes  | TL bool (if hasSelf)               |
| authKey          | variable | TL bytes-wrapped (256 bytes)       |

func DecodePyrogramSession

func DecodePyrogramSession(hx string) (*session.Data, error)

func EncodeGramjsSession

func EncodeGramjsSession(data *session.Data) (string, error)

EncodeGramjsSession encodes session data into a GramJS-compatible session string.

GramJS session strings use version prefix '1' followed by base64 standard-encoded binary data with big-endian byte order:

| Size     |  Type  | Description       |
|----------|--------|-------------------|
| 1        | uint8  | DC ID             |
| 2        | uint16 | Address length    |
| variable | bytes  | Server address    |
| 2        | uint16 | Port              |
| 256      | bytes  | Auth Key          |

Parameters:

  • data: The session data containing DC, Addr (ip:port), and AuthKey

Returns the GramJS session string (prefixed with '1') or an error.

func EncodeMtcuteSession

func EncodeMtcuteSession(data *session.Data, userID int64, isBot bool) (string, error)

EncodeMtcuteSession encodes session data into an mtcute v3-compatible session string.

mtcute session strings are URL-safe base64 encoded binary data using TL-style serialization with the following layout:

| Field            | Size     | Description                                    |
|------------------|----------|------------------------------------------------|
| version          | 1 byte   | Always 3                                       |
| flags            | 4 bytes  | LE int32 (bit0=hasSelf, bit2=hasMedia)         |
| primaryDC        | variable | TL bytes-wrapped DC option                     |
| selfUserID       | 8 bytes  | LE int64 (if hasSelf)                          |
| selfIsBot        | 4 bytes  | TL bool (if hasSelf)                           |
| authKey          | variable | TL bytes-wrapped (256 bytes)                   |

Parameters:

  • data: The session data containing DC, Addr (ip:port), and AuthKey
  • userID: The authenticated user's Telegram ID (0 to omit self info)
  • isBot: Whether the session belongs to a bot

Returns the URL-safe base64-encoded session string or an error.

func EncodePyrogramSession

func EncodePyrogramSession(data *session.Data, appID int32, userID int64, isBot bool) (string, error)

EncodePyrogramSession encodes session data into a Pyrogram-compatible session string.

Pyrogram session strings use the format '>BI?256sQ?' (big-endian packed):

| Size |  Type  | Description |
|------|--------|-------------|
| 1    | uint8  | DC ID       |
| 4    | uint32 | App ID      |
| 1    | bool   | Test Mode   |
| 256  | bytes  | Auth Key    |
| 8    | uint64 | User ID     |
| 1    | bool   | Is Bot      |

Parameters:

  • data: The session data containing DC, AuthKey, and Config
  • appID: The Telegram API application ID
  • userID: The authenticated user's Telegram ID
  • isBot: Whether the session belongs to a bot

Returns the base64 URL-encoded session string or an error.

func EncodeTelethonSession

func EncodeTelethonSession(data *session.Data) (string, error)

EncodeTelethonSession encodes session data into a Telethon-compatible session string.

Telethon session strings use version prefix '1' followed by base64 URL-encoded binary data with big-endian byte order:

| Size |  Type  | Description |
|------|--------|-------------|
| 1    | byte   | DC ID       |
| 4/16 | bytes  | IP address  |
| 2    | uint16 | Port        |
| 256  | bytes  | Auth Key    |

Parameters:

  • data: The session data containing DC, Addr (ip:port), and AuthKey

Returns the Telethon session string (prefixed with '1') or an error.

func NewSessionStorage

func NewSessionStorage(ctx context.Context, sessionType SessionConstructor, inMemory bool) (*storage.PeerStorage, telegram.SessionStorage, error)

NewSessionStorage creates a session storage for Telegram client authentication.

It creates both a PeerStorage (for peer caching) and a SessionStorage (for persisting auth session data like auth keys, DC ID, etc.).

Parameters:

  • ctx: Context for session operations
  • sessionType: Session type to load (SqlSession, MemorySession, StringSession)
  • inMemory: If true, only in-memory storage is used (no database persistence)

Returns:

  • PeerStorage: For caching peers (users, chats, channels)
  • SessionStorage: For storing Telegram session data
  • error: If session loading fails

Example:

// Create session with SQLite database
peerStorage, sessionStorage, err := session.NewSessionStorage(
    ctx,
    session.SqlSession(sqlite.Open("telegram.db")),
    false,
)

// Create in-memory only session
peerStorage, sessionStorage, err := session.NewSessionStorage(
    ctx,
    session.MemorySession(),
    true,
)

Types

type AdapterSessionConstructor

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

func WithAdapter

func WithAdapter(adapter storage.Adapter) *AdapterSessionConstructor

type AuthKey

type AuthKey struct {
	Value Key
	ID    [8]byte
}

AuthKey is a Key with cached id.

type GramjsSessionConstructor

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

func GramjsSession

func GramjsSession(value string) *GramjsSessionConstructor

GramjsSession creates a constructor for Gram.js string session format.

Gram.js session strings use a specific hex encoding format.

Parameters:

  • value: The Gram.js session string to encode

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.GramjsSession("v12345...67890abcdef")

func (*GramjsSessionConstructor) Name

type JsonFileSessionConstructor

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

func JsonFileSession

func JsonFileSession(filePath string) *JsonFileSessionConstructor

JsonFileSession creates a constructor for JSON file session format.

This allows loading and saving sessions from a local JSON file.

Parameters:

  • filePath: Path to the JSON session file

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.JsonFileSession("/path/to/session.json")

func (*JsonFileSessionConstructor) Name

type Key

type Key [256]byte

func (Key) ID

func (k Key) ID() [8]byte

ID returns auth_key_id.

func (Key) WithID

func (k Key) WithID() AuthKey

WithID creates new AuthKey from Key.

type MtcuteSessionConstructor

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

func MtcuteSession

func MtcuteSession(value string) *MtcuteSessionConstructor

MtcuteSession creates a constructor for mtcute string session format.

mtcute session strings use URL-safe base64 encoding with TL-style binary serialization (version 3).

Parameters:

  • value: The mtcute session string to decode

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.MtcuteSession("AwQAAAAXAgIA...")

func (*MtcuteSessionConstructor) Name

type PyrogramSessionConstructor

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

func PyrogramSession

func PyrogramSession(value string) *PyrogramSessionConstructor

PyrogramSession creates a constructor for Pyrogram string session format.

Pyrogram session strings use a specific hex encoding format ( '>BI?256sQ?' prefix followed by encoded session data).

Parameters:

  • value: The Pyrogram session string to encode

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.PyrogramSession("v12345...67890abcdef")

func (*PyrogramSessionConstructor) Name

type SessionConstructor

type SessionConstructor interface {
	// contains filtered or unexported methods
}

type SessionStorage

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

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

func (*SessionStorage) LoadSession

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

LoadSession loads session from file.

func (*SessionStorage) StoreSession

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

StoreSession stores session to sqlite storage.

type SimpleSessionConstructor

type SimpleSessionConstructor int8

func SimpleSession

func SimpleSession() *SimpleSessionConstructor

type SqlSessionConstructor

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

func SqlSession

func SqlSession(dialector gorm.Dialector) *SqlSessionConstructor

SqlSession creates a constructor for SQLite-based session storage.

This allows loading and saving sessions from a SQLite database file.

Parameters:

  • dialector: The GORM dialector for database connection

Returns:

  • A constructor that implements SessionConstructor interface

Example:

dialector := sqlite.Open("telegram.db")
constructor := session.SqlSession(dialector)

type StringSessionConstructor

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

func StringSession

func StringSession(value string) *StringSessionConstructor

StringSession creates a constructor for plain string session format.

This allows using simple string-encoded session data without hex encoding.

Parameters:

  • value: The session string value

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.StringSession(sessionString)

func (*StringSessionConstructor) Name

type TdataSessionConstructor

type TdataSessionConstructor struct {
	Account tdesktop.Account
	// contains filtered or unexported fields
}

func TdataSession

func TdataSession(account tdesktop.Account) *TdataSessionConstructor

TdataSession creates a constructor for Telegram Desktop session format.

Telegram Desktop session uses a specific binary format for session storage.

Parameters:

  • account: The tdesktop.Account containing session data

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.TdataSession(account)

func (*TdataSessionConstructor) Name

type TelethonSessionConstructor

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

func TelethonSession

func TelethonSession(value string) *TelethonSessionConstructor

TelethonSession creates a constructor for Telethon string session format.

Telethon session strings use a specific hex encoding format ( '>BI?256sQ?' prefix followed by encoded session data).

Parameters:

  • value: The Telethon session string to encode

Returns:

  • A constructor that implements SessionConstructor interface

Example:

constructor := session.TelethonSession("v12345...67890abcdef")

func (*TelethonSessionConstructor) Name

Jump to

Keyboard shortcuts

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