Session persistence types and pluggable storage backends for ferogram.
Session persistence for ferogram. Extracted in v0.3.0 so that session storage can be used, extended, or replaced without pulling in the full client.
ferogram re-exports everything from here existing code needs no changes.
[dependencies]
ferogram-session = "0.3.0"- DC address table with per-DC auth keys, salts, and capability flags
- MTProto update counters: pts, qts, seq, date, and per-channel pts
- Peer access-hash cache for users, channels, and groups
- Min-user message contexts for
InputPeerUserFromMessage
The binary format is versioned. load() handles all previous versions without error. save() always writes the current version. Saves are atomic: written to a .tmp file first, then renamed into place.
The main serializable struct. Holds the full DC table, update state, and peer cache.
use ferogram_session::PersistedSession;
use std::path::Path;
let session = PersistedSession::load(Path::new("my.session"))?;
session.save(Path::new("my.session"))?;
// String (base64) round-trip
let s = session.to_string();
let session2 = PersistedSession::from_string(&s)?;use ferogram_session::{DcEntry, DcFlags};
let entry = DcEntry::from_parts(2, "149.154.167.51", 443, DcFlags::NONE);
let ipv6 = DcEntry::from_parts(2, "2001:b28:f23d:f001::a", 443, DcFlags::IPV6);Stores the session as a binary file on disk. Default backend used by ferogram.
use ferogram_session::BinaryFileBackend;
let backend = BinaryFileBackend::new("ferogram.session");Stores the session in memory only. Useful for testing or short-lived bots.
use ferogram_session::InMemoryBackend;
let backend = InMemoryBackend::new();Stores the session as a base64 string. Useful for deployments where file I/O is unavailable (e.g. environment variable sessions).
use ferogram_session::StringSessionBackend;
let backend = StringSessionBackend::new(std::env::var("SESSION").unwrap_or_default());ferogram-session = { version = "0.3.0", features = ["sqlite-session"] }use ferogram_session::SqliteBackend;
let backend = SqliteBackend::open("sessions.db")?;ferogram-session = { version = "0.3.0", features = ["libsql-session"] }use ferogram_session::LibSqlBackend;
let backend = LibSqlBackend::open_local("sessions.db")?;Implement SessionBackend to add your own storage:
use ferogram_session::{SessionBackend, PersistedSession};
use std::io;
struct RedisBackend { /* ... */ }
impl SessionBackend for RedisBackend {
fn save(&self, session: &PersistedSession) -> io::Result<()> { todo!() }
fn load(&self) -> io::Result<Option<PersistedSession>> { todo!() }
fn delete(&self) -> io::Result<()> { todo!() }
fn name(&self) -> &str { "redis" }
}| Flag | What it enables |
|---|---|
sqlite-session |
SqliteBackend via rusqlite |
libsql-session |
LibSqlBackend via libsql |
serde |
Serialize/Deserialize on session types |
ferogram
└ ferogram-session <-- here
MIT or Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.
Ankit Chaubey - github.com/ankit-chaubey