1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// Licensed under either the MIT License or the Apache License 2.0.
// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
// https://github.com/ankit-chaubey/ferogram
//
// Feel free to use, modify, and share this code.
// Please keep this notice when redistributing.
use std::sync::Arc;
use crate::{
Client, Config, ExperimentalFeatures, InvocationError, ShutdownToken, TransportKind,
restart::{ConnectionRestartPolicy, NeverRestart},
retry::{AutoSleep, RetryPolicy},
session_backend::{BinaryFileBackend, InMemoryBackend, SessionBackend, StringSessionBackend},
socks5::Socks5Config,
};
/// Fluent builder for [`Config`] + [`Client::connect`].
///
/// Obtain one via [`Client::builder()`].
pub struct ClientBuilder {
api_id: i32,
api_hash: String,
dc_addr: Option<String>,
retry_policy: Arc<dyn RetryPolicy>,
restart_policy: Arc<dyn ConnectionRestartPolicy>,
socks5: Option<Socks5Config>,
mtproxy: Option<crate::proxy::MtProxyConfig>,
allow_ipv6: bool,
transport: TransportKind,
session_backend: Arc<dyn SessionBackend>,
catch_up: bool,
device_model: String,
system_version: String,
app_version: String,
system_lang_code: String,
lang_pack: String,
lang_code: String,
probe_transport: bool,
resilient_connect: bool,
experimental_features: ExperimentalFeatures,
use_pfs: bool,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self {
api_id: 0,
api_hash: String::new(),
dc_addr: None,
retry_policy: Arc::new(AutoSleep::default()),
restart_policy: Arc::new(NeverRestart),
socks5: None,
mtproxy: None,
allow_ipv6: false,
transport: TransportKind::Abridged,
session_backend: Arc::new(BinaryFileBackend::new("ferogram.session")),
catch_up: false,
device_model: "Linux".to_string(),
system_version: "1.0".to_string(),
app_version: env!("CARGO_PKG_VERSION").to_string(),
system_lang_code: "en".to_string(),
lang_pack: String::new(),
lang_code: "en".to_string(),
probe_transport: false,
resilient_connect: false,
experimental_features: ExperimentalFeatures::default(),
use_pfs: false,
}
}
}
impl ClientBuilder {
// Credentials
/// Set the Telegram API ID (from <https://my.telegram.org>).
pub fn api_id(mut self, id: i32) -> Self {
self.api_id = id;
self
}
/// Set the Telegram API hash (from <https://my.telegram.org>).
pub fn api_hash(mut self, hash: impl Into<String>) -> Self {
self.api_hash = hash.into();
self
}
// Session
/// Use a binary file session at `path`.
///
/// Mutually exclusive with [`session_string`](Self::session_string) and
/// [`in_memory`](Self::in_memory): last call wins.
pub fn session(mut self, path: impl AsRef<std::path::Path>) -> Self {
self.session_backend = Arc::new(BinaryFileBackend::new(path.as_ref()));
self
}
/// Use a portable base64 string session.
///
/// Pass an empty string to start fresh: the exported session string
/// from [`Client::export_session_string`] can be injected here directly
/// (e.g. via an environment variable).
///
/// Mutually exclusive with [`session`](Self::session) and
/// [`in_memory`](Self::in_memory): last call wins.
pub fn session_string(mut self, s: impl Into<String>) -> Self {
self.session_backend = Arc::new(StringSessionBackend::new(s));
self
}
/// Use a non-persistent in-memory session (useful for tests).
///
/// Mutually exclusive with [`session`](Self::session) and
/// [`session_string`](Self::session_string): last call wins.
pub fn in_memory(mut self) -> Self {
self.session_backend = Arc::new(InMemoryBackend::new());
self
}
/// Inject a fully custom [`SessionBackend`] implementation.
///
/// Useful for [`LibSqlBackend`] (bundled SQLite, no system dep) or any
/// custom persistence layer:
/// ```rust,no_run
/// # use ferogram::{Client};
/// # #[cfg(feature = "libsql-session")] {
/// # use ferogram::LibSqlBackend;
/// use std::sync::Arc;
/// let (client, _) = Client::builder()
/// .api_id(12345).api_hash("abc")
/// .session_backend(Arc::new(LibSqlBackend::new("my.db")))
/// .connect().await?;
/// # }
/// ```
pub fn session_backend(mut self, backend: Arc<dyn SessionBackend>) -> Self {
self.session_backend = backend;
self
}
// Update catch-up
/// When `true`, replay missed updates via `updates.getDifference` on connect.
///
/// Default: `false`.
pub fn catch_up(mut self, enabled: bool) -> Self {
self.catch_up = enabled;
self
}
/// Enable Perfect Forward Secrecy via `auth.bindTempAuthKey`.
///
/// Adds one extra DH round-trip per connection. Off by default.
/// Enable only if your threat model requires it.
///
/// Default: `false`.
pub fn pfs(mut self, enabled: bool) -> Self {
self.use_pfs = enabled;
self
}
// Network
/// Override the first DC address (e.g. `"149.154.167.51:443"`).
pub fn dc_addr(mut self, addr: impl Into<String>) -> Self {
self.dc_addr = Some(addr.into());
self
}
/// Route all connections through a SOCKS5 proxy.
pub fn socks5(mut self, addr: impl Into<String>) -> Self {
self.socks5 = Some(crate::socks5::Socks5Config::new(addr));
self
}
/// Route all connections through an MTProxy.
///
/// The proxy `transport` is set automatically from the secret prefix;
/// you do not need to also call `.transport()`.
/// Build the [`MtProxyConfig`] with [`crate::parse_proxy_link`].
pub fn mtproxy(mut self, proxy: crate::proxy::MtProxyConfig) -> Self {
// Override transport to match what the proxy requires.
self.transport = proxy.transport.clone();
self.mtproxy = Some(proxy);
self
}
/// Set an MTProxy from a `https://t.me/proxy?...` or `tg://proxy?...` link.
///
/// Empty string is a no-op; proxy stays unset.
/// Transport is selected from the secret prefix automatically.
pub fn proxy_link(mut self, url: &str) -> Self {
if url.is_empty() {
return self;
}
let cfg = crate::proxy::parse_proxy_link(url)
.unwrap_or_else(|| panic!("invalid MTProxy link: {url:?}"));
self.mtproxy = Some(cfg);
self
}
/// Allow IPv6 DC addresses (default: `false`).
pub fn allow_ipv6(mut self, allow: bool) -> Self {
self.allow_ipv6 = allow;
self
}
/// Choose the MTProto transport framing (default: [`TransportKind::Abridged`]).
pub fn transport(mut self, kind: TransportKind) -> Self {
self.transport = kind;
self
}
// Retry
/// Override the flood-wait / retry policy.
pub fn retry_policy(mut self, policy: Arc<dyn RetryPolicy>) -> Self {
self.retry_policy = policy;
self
}
pub fn restart_policy(mut self, policy: Arc<dyn ConnectionRestartPolicy>) -> Self {
self.restart_policy = policy;
self
}
// InitConnection identity
/// Set the device model string sent in `InitConnection` (default: `"Linux"`).
///
/// This shows up in Telegram's active sessions list as the device name.
pub fn device_model(mut self, model: impl Into<String>) -> Self {
self.device_model = model.into();
self
}
/// Set the system/OS version string sent in `InitConnection` (default: `"1.0"`).
pub fn system_version(mut self, version: impl Into<String>) -> Self {
self.system_version = version.into();
self
}
/// Set the app version string sent in `InitConnection` (default: crate version from `CARGO_PKG_VERSION`).
pub fn app_version(mut self, version: impl Into<String>) -> Self {
self.app_version = version.into();
self
}
/// Set the system language code sent in `InitConnection` (default: `"en"`).
pub fn system_lang_code(mut self, code: impl Into<String>) -> Self {
self.system_lang_code = code.into();
self
}
/// Set the language pack name sent in `InitConnection` (default: `""`).
pub fn lang_pack(mut self, pack: impl Into<String>) -> Self {
self.lang_pack = pack.into();
self
}
/// Set the language code sent in `InitConnection` (default: `"en"`).
pub fn lang_code(mut self, code: impl Into<String>) -> Self {
self.lang_code = code.into();
self
}
/// Race Obfuscated / Abridged / HTTP transports in parallel and pick the
/// fastest. Ideal when you don't know which transport your network allows.
/// Incompatible with MTProxy (proxy enforces a specific transport).
/// Default: `false`.
pub fn probe_transport(mut self, enabled: bool) -> Self {
self.probe_transport = enabled;
self
}
/// If direct TCP fails, retry via DNS-over-HTTPS (Mozilla + Google DoH),
/// then fall back to Firebase / Google special-config.
/// Default: `false`.
pub fn resilient_connect(mut self, enabled: bool) -> Self {
self.resilient_connect = enabled;
self
}
/// Opt in to experimental behaviours.
///
/// All flags default to `false`. Read [`ExperimentalFeatures`] docs before
/// enabling anything here.
///
/// # Example
/// ```rust,no_run
/// use ferogram::{Client, ExperimentalFeatures};
///
/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let (client, _sd) = Client::builder()
/// .api_id(12345)
/// .api_hash("abc")
/// .experimental_features(ExperimentalFeatures {
/// allow_zero_hash: true, // bots only
/// ..Default::default()
/// })
/// .connect().await?;
/// # Ok(()) }
/// ```
pub fn experimental_features(mut self, features: ExperimentalFeatures) -> Self {
self.experimental_features = features;
self
}
// Terminal
/// Build the [`Config`] without connecting.
pub fn build(self) -> Result<Config, BuilderError> {
if self.api_id == 0 {
return Err(BuilderError::MissingApiId);
}
if self.api_hash.is_empty() {
return Err(BuilderError::MissingApiHash);
}
Ok(Config {
api_id: self.api_id,
api_hash: self.api_hash,
dc_addr: self.dc_addr,
retry_policy: self.retry_policy,
restart_policy: self.restart_policy,
socks5: self.socks5,
mtproxy: self.mtproxy,
allow_ipv6: self.allow_ipv6,
transport: self.transport,
session_backend: self.session_backend,
catch_up: self.catch_up,
device_model: self.device_model,
system_version: self.system_version,
app_version: self.app_version,
system_lang_code: self.system_lang_code,
lang_pack: self.lang_pack,
lang_code: self.lang_code,
probe_transport: self.probe_transport,
resilient_connect: self.resilient_connect,
experimental_features: self.experimental_features,
use_pfs: self.use_pfs,
})
}
/// Build and connect in one step.
///
/// Returns `Err(BuilderError::MissingApiId)` / `Err(BuilderError::MissingApiHash)`
/// before attempting any network I/O if the required fields are absent.
pub async fn connect(self) -> Result<(Client, ShutdownToken), BuilderError> {
let cfg = self.build()?;
Client::connect(cfg).await.map_err(BuilderError::Connect)
}
}
// BuilderError
/// Errors that can be returned by [`ClientBuilder::build`] or
/// [`ClientBuilder::connect`].
#[derive(Debug)]
pub enum BuilderError {
/// `api_id` was not set (or left at 0).
MissingApiId,
/// `api_hash` was not set (or left empty).
MissingApiHash,
/// The underlying [`Client::connect`] call failed.
Connect(InvocationError),
}
impl std::fmt::Display for BuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingApiId => f.write_str("ClientBuilder: api_id not set"),
Self::MissingApiHash => f.write_str("ClientBuilder: api_hash not set"),
Self::Connect(e) => write!(f, "ClientBuilder: connect failed: {e}"),
}
}
}
impl std::error::Error for BuilderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Connect(e) => Some(e),
_ => None,
}
}
}