File size: 7,041 Bytes
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import secrets
import time

from pyrogram import Client
from pyrogram.errors import (
    BadRequest,
    FloodWait,
    PasswordHashInvalid,
    PhoneCodeExpired,
    PhoneCodeInvalid,
    PhoneNumberInvalid,
    SessionPasswordNeeded,
)

import Backend.pyrofork.bot as botmod
from Backend import db
from Backend.config import Telegram
from Backend.helper import global_search, task_manager
from Backend.helper.encrypt import decode_string, encode_string
from Backend.logger import LOGGER

_PENDING = {}
_PENDING_TTL = 600


async def _cleanup_pending():
    now = time.time()
    for lid in [k for k, v in _PENDING.items() if now - v["ts"] > _PENDING_TTL]:
        entry = _PENDING.pop(lid, None)
        if entry:
            try:
                await entry["client"].disconnect()
            except Exception:
                pass


def _profile(me) -> dict:
    name = " ".join(p for p in [me.first_name, me.last_name] if p) or "Telegram User"
    phone = me.phone_number or ""
    if phone and not phone.startswith("+"):
        phone = "+" + phone
    return {
        "name": name,
        "username": me.username or "",
        "phone": phone,
        "user_id": me.id,
    }


async def _store_session(session_string: str, profile: dict) -> None:
    encoded = await encode_string(session_string)
    doc = {
        "session": encoded,
        "active": True,
        "created_at": time.time(),
        **profile,
    }
    await db.dbs["tracking"]["state"].update_one(
        {"_id": "user_session"}, {"$set": doc}, upsert=True
    )


async def _read_stored() -> dict:
    return await db.dbs["tracking"]["state"].find_one({"_id": "user_session"}) or {}


async def get_active_session_string() -> str:
    doc = await _read_stored()
    if not doc or not doc.get("active") or not doc.get("session"):
        return ""
    try:
        return await decode_string(doc["session"])
    except Exception:
        return ""


async def _activate(session_string: str) -> None:
    try:
        if botmod.Userbot is None:
            botmod.build_userbot(session_string)
            await botmod.Userbot.start()
            botmod.Userbot.username = getattr(botmod.Userbot.me, "username", None)
            LOGGER.info("Userbot session activated live from stored session.")
        for mod in (global_search, task_manager):
            try:
                mod._userbot_session_dead = False
            except Exception:
                pass
    except Exception as e:
        LOGGER.warning(f"[SESSION] Live Userbot activation failed (restart to apply): {e}")


#----- The Cloudflare Worker streams global results with this session; tell it to reload
def _sync_worker() -> None:
    from Backend.helper.cf_stream import sync_worker_soon
    sync_worker_soon()


async def _deactivate() -> None:
    try:
        if botmod.Userbot is not None:
            await botmod.Userbot.stop()
    except Exception:
        pass
    botmod.Userbot = None


async def start_login(phone: str) -> dict:
    await _cleanup_pending()
    phone = (phone or "").strip()
    if not phone:
        raise ValueError("Enter a valid phone number with country code (e.g. +12025550123).")
    if not Telegram.API_ID or not Telegram.API_HASH:
        raise ValueError("API_ID / API_HASH are not configured.")

    client = Client(f"login_{secrets.token_hex(6)}", api_id=Telegram.API_ID, api_hash=Telegram.API_HASH, in_memory=True)
    await client.connect()
    try:
        sent = await client.send_code(phone)
    except (PhoneNumberInvalid, BadRequest):
        await client.disconnect()
        raise ValueError("That phone number was rejected by Telegram. Check the country code and try again.")
    except FloodWait as e:
        await client.disconnect()
        raise ValueError(f"Too many attempts. Try again in {e.value} seconds.")

    login_id = secrets.token_hex(12)
    _PENDING[login_id] = {"client": client, "phone": phone, "hash": sent.phone_code_hash, "ts": time.time()}
    return {"login_id": login_id}


async def submit_code(login_id: str, code: str) -> dict:
    entry = _PENDING.get(login_id)
    if not entry:
        raise ValueError("Login session expired. Start again.")
    client = entry["client"]
    code = (code or "").strip().replace(" ", "")
    try:
        await client.sign_in(entry["phone"], entry["hash"], code)
    except SessionPasswordNeeded:
        return {"status": "password_needed"}
    except PhoneCodeInvalid:
        raise ValueError("The code you entered is incorrect.")
    except PhoneCodeExpired:
        _PENDING.pop(login_id, None)
        try:
            await client.disconnect()
        except Exception:
            pass
        raise ValueError("The code has expired. Please request a new one.")
    return await _finalize(login_id)


async def submit_password(login_id: str, password: str) -> dict:
    entry = _PENDING.get(login_id)
    if not entry:
        raise ValueError("Login session expired. Start again.")
    client = entry["client"]
    try:
        await client.check_password((password or "").strip())
    except PasswordHashInvalid:
        raise ValueError("Incorrect two-step verification password.")
    return await _finalize(login_id)


async def _finalize(login_id: str) -> dict:
    entry = _PENDING.pop(login_id, None)
    client = entry["client"]
    me = await client.get_me()
    profile = _profile(me)
    session_string = await client.export_session_string()
    try:
        await client.disconnect()
    except Exception:
        pass
    await _store_session(session_string, profile)
    await _activate(session_string)
    _sync_worker()
    return {"status": "ok", "profile": profile}


async def get_session_status() -> dict:
    doc = await _read_stored()
    if not doc:
        return {"connected": False, "profile": None}
    return {
        "connected": bool(doc.get("active")),
        "live": botmod.Userbot is not None,
        "profile": {
            "name": doc.get("name"),
            "username": doc.get("username"),
            "phone": doc.get("phone"),
            "user_id": doc.get("user_id"),
        },
    }


async def disconnect_session() -> dict:
    await db.dbs["tracking"]["state"].update_one({"_id": "user_session"}, {"$set": {"active": False}})
    await _deactivate()
    _sync_worker()
    return {"ok": True}


async def reconnect_session() -> dict:
    session_string = None
    doc = await _read_stored()
    if doc and doc.get("session"):
        try:
            session_string = await decode_string(doc["session"])
        except Exception:
            session_string = None
    if not session_string:
        raise ValueError("No stored session to reconnect.")
    await db.dbs["tracking"]["state"].update_one({"_id": "user_session"}, {"$set": {"active": True}})
    await _activate(session_string)
    _sync_worker()
    return {"ok": True}


async def remove_session() -> dict:
    await _deactivate()
    await db.dbs["tracking"]["state"].delete_one({"_id": "user_session"})
    _sync_worker()
    return {"ok": True}