File size: 4,600 Bytes
36dde81
 
1404692
36dde81
1404692
36dde81
07f63a8
36dde81
 
07f63a8
 
 
1404692
36dde81
1404692
 
07f63a8
 
 
 
 
36dde81
07f63a8
1404692
 
 
 
 
 
 
14af9d0
1404692
14af9d0
1404692
07f63a8
0ba2f8e
 
 
 
 
 
 
07f63a8
1404692
 
 
 
 
 
07f63a8
36dde81
07f63a8
 
 
 
 
36dde81
07f63a8
 
 
 
 
 
 
 
 
36dde81
07f63a8
1404692
07f63a8
0ba2f8e
 
 
 
 
 
 
07f63a8
 
1404692
 
 
 
 
07f63a8
 
 
 
 
 
 
0ba2f8e
1404692
 
 
07f63a8
 
 
36dde81
07f63a8
 
 
 
 
 
36dde81
07f63a8
 
 
36dde81
07f63a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from asyncio import create_task, gather

from pyrogram import Client

from Backend.config import Telegram
from Backend.fastapi.routes.stream_routes import _streamer_by_client
from Backend.helper.settings_manager import SettingsManager
from Backend.logger import LOGGER
from Backend.pyrofork.bot import StreamBot, client_dc_map, multi_clients, work_loads

client_tokens: dict[int, str] = {}


#----- Read the configured multi-client tokens from settings
class TokenParser:
    @staticmethod
    def parse_from_settings() -> dict[int, str]:
        tokens = SettingsManager.current().multi_tokens
        return {i + 1: tok.strip() for i, tok in enumerate(tokens) if tok and tok.strip()}


#----- Start a single bot client and register its DC/workload
async def start_client(client_id: int, token: str):
    try:
        LOGGER.info(f"Starting - Bot Client {client_id}")
        client = await Client(
            name=str(client_id),
            api_id=Telegram.API_ID,
            api_hash=Telegram.API_HASH,
            bot_token=token,
            sleep_threshold=100,
            no_updates=True,
            in_memory=True
        ).start()

        try:
            client_dc = await client.storage.dc_id()
            client_dc_map[client_id] = client_dc
            LOGGER.info(f"Client {client_id} connected to DC {client_dc}")
        except Exception as e:
            LOGGER.warning(f"Could not get DC for Client {client_id}: {e}")
            client_dc_map[client_id] = None

        work_loads[client_id] = 0
        return client_id, client
    except Exception as e:
        LOGGER.error(f"Failed to start Client - {client_id} Error: {e}", exc_info=True)
        return None


#----- Stop a client and purge all of its registry entries
async def stop_client(client_id: int) -> None:
    client = multi_clients.pop(client_id, None)
    work_loads.pop(client_id, None)
    client_dc_map.pop(client_id, None)
    client_tokens.pop(client_id, None)
    _streamer_by_client.pop(client, None)

    if client:
        try:
            await client.stop()
            LOGGER.info(f"Stopped Bot Client {client_id}")
        except Exception as e:
            LOGGER.warning(f"Error stopping Client {client_id}: {e}")


#----- Bring up the main client plus every configured extra client
async def initialize_clients() -> None:
    multi_clients[0], work_loads[0] = StreamBot, 0

    try:
        main_dc = await StreamBot.storage.dc_id()
        client_dc_map[0] = main_dc
        LOGGER.info(f"Main StreamBot connected to DC {main_dc}")
    except Exception as e:
        LOGGER.warning(f"Could not get DC for StreamBot: {e}")
        client_dc_map[0] = None

    all_tokens = TokenParser.parse_from_settings()
    if not all_tokens:
        LOGGER.info("No additional Bot Clients found, Using default client")
        return

    tasks = [create_task(start_client(i, token)) for i, token in all_tokens.items()]
    results = await gather(*tasks)

    started = {client_id: client for client_id, client in results if client}
    multi_clients.update(started)
    for client_id, token in all_tokens.items():
        if client_id in started:
            client_tokens[client_id] = token

    if len(multi_clients) != 1:
        LOGGER.info(f"Multi-Client Mode Enabled with {len(multi_clients)} clients")
    else:
        LOGGER.info("No additional clients were initialized, using default client")


#----- Reconcile running clients with the current token settings
async def reload_multi_token_clients() -> dict:
    new_tokens = TokenParser.parse_from_settings()

    old_ids = set(client_tokens.keys())
    new_ids = set(new_tokens.keys())

    to_stop = [cid for cid in old_ids if cid not in new_ids or client_tokens.get(cid) != new_tokens.get(cid)]
    for cid in to_stop:
        await stop_client(cid)

    to_start = {cid: tok for cid, tok in new_tokens.items() if cid not in old_ids or client_tokens.get(cid) != tok}

    if to_start:
        tasks = [create_task(start_client(cid, tok)) for cid, tok in to_start.items()]
        results = await gather(*tasks)
        started = {cid: client for cid, client in results if client}
        multi_clients.update(started)
        for cid, tok in to_start.items():
            if cid in started:
                client_tokens[cid] = tok

    LOGGER.info(
        f"Multi-token reload complete — {len(to_stop)} stopped, "
        f"{len(to_start)} (re)started, {len(multi_clients)} total clients active."
    )
    return {
        "stopped": len(to_stop),
        "started": len(to_start),
        "total_clients": len(multi_clients),
    }