-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtelegram_admin.py
More file actions
493 lines (434 loc) · 25 KB
/
Copy pathtelegram_admin.py
File metadata and controls
493 lines (434 loc) · 25 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
from __future__ import annotations
import asyncio
import math
from typing import Any
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
PAGE_SIZE = 6
CREATE_FIELDS = [
("name", "Client name", ""),
("phone_number", "Phone number, or - to leave empty", ""),
("telegram_id", "Telegram username/ID, or - to leave empty", ""),
("data_limit_value", "Shared data limit value, 0 for no data cap", "0"),
("data_limit_unit", "Data unit: Mi or Gi", "Gi"),
("time_limit_days", "Active days, 0 for no time limit", "0"),
("start_on_first_use", "Start timer on first use? yes/no", "yes"),
("unlimited", "Unlimited subscription mode? yes/no", "no"),
("note", "Administrative note, or - to leave empty", ""),
]
EDIT_FIELDS = [
("name", "Client name"),
("phone_number", "Phone number"),
("telegram_id", "Telegram username/ID"),
("data_limit_value", "Shared data limit"),
("data_limit_unit", "Data unit"),
("time_limit_days", "Active days"),
("start_on_first_use", "Start on first use"),
("unlimited", "Unlimited mode"),
("note", "Administrative note"),
]
def _html(g, value):
return g["html"](value)
def _api(g, method, path, payload=None, timeout=25):
return g["_api_data"](method, path, payload=payload, timeout=timeout)
def _err(data):
if not isinstance(data, dict):
return str(data)
return str(data.get("detail") or data.get("message") or data.get("error") or "Unknown error")
def _bool(value):
return str(value or "").strip().lower() in {"1", "true", "yes", "on", "y", "enabled"}
def _value(key, raw):
value = str(raw or "").strip()
if value == "-":
value = ""
if key == "data_limit_value":
return max(0, int(float(value or 0)))
if key == "time_limit_days":
return max(0.0, float(value or 0))
if key in {"start_on_first_use", "unlimited", "enabled"}:
return _bool(value)
if key == "data_limit_unit":
return "Mi" if value.lower().startswith("mi") else "Gi"
return value
def _bytes(value):
try:
size = float(max(0, int(value or 0)))
except Exception:
size = 0.0
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
if size < 1024 or unit == "TiB":
return f"{int(size)} {unit}" if unit == "B" else f"{size:.1f} {unit}"
size /= 1024.0
return "0 B"
def clients(g):
data = _api(g, "GET", "/api/subscriptions", timeout=30)
rows = data.get("subscriptions") if isinstance(data, dict) else []
return rows if isinstance(rows, list) else []
def client(g, sid):
data = _api(g, "GET", f"/api/subscriptions/{int(sid)}", timeout=20)
row = data.get("subscription") if isinstance(data, dict) else None
return row if isinstance(row, dict) else {}
def status(row):
counts = row.get("runtime_counts") or {}
if int(counts.get("blocked") or 0) > 0 or str(row.get("status") or "").lower() == "blocked":
return "🔴", "Blocked"
if row.get("enabled"):
return "🟢", "Enabled"
return "🟡", "Disabled"
def time_text(g, row):
if row.get("unlimited"):
return "Unlimited"
ttl = row.get("ttl_seconds")
if ttl is None:
if row.get("start_on_first_use") and not row.get("first_used_at"):
return "Waiting for first use"
return "No limit"
try:
ttl = int(ttl)
except Exception:
return str(ttl)
if ttl <= 0:
return "Expired"
return g["human_ttl"](ttl)
def data_text(row):
used = _bytes(row.get("used_bytes"))
if row.get("data_limit_bytes") in (None, 0, "0"):
return f"{used} used · no cap"
return f"{used} used · {_bytes(row.get('remaining_bytes'))} left"
def render_list(g, page=1, query=""):
rows = clients(g)
needle = str(query or "").strip().lower()
if needle:
rows = [r for r in rows if needle in " ".join(str(r.get(k) or "") for k in ("id","name","phone_number","telegram_id","note")).lower()]
pages = max(1, math.ceil(len(rows) / PAGE_SIZE))
page = max(1, min(int(page or 1), pages))
visible = rows[(page-1)*PAGE_SIZE:page*PAGE_SIZE]
enabled = sum(1 for r in rows if r.get("enabled"))
blocked = sum(1 for r in rows if status(r)[1] == "Blocked")
lines = ["👤 <b>Clients & Subscriptions</b>", "", f"Total <code>{len(rows)}</code> · Enabled <code>{enabled}</code> · Blocked <code>{blocked}</code>"]
if needle:
lines.append(f"Search: <code>{_html(g, query)}</code>")
lines += ["", f"Page <code>{page}/{pages}</code>" if visible else "No matching clients."]
kb = []
for r in visible:
sid = int(r.get("id") or 0)
icon, _ = status(r)
name = str(r.get("name") or f"Client {sid}")
count = int((r.get("runtime_counts") or {}).get("total") or len(r.get("locations") or []))
kb.append([InlineKeyboardButton(f"{icon} {name} · {count} config{'s' if count != 1 else ''}", callback_data=f"client:open:{sid}")])
nav = []
if page > 1: nav.append(InlineKeyboardButton("‹ Prev", callback_data=f"client:list:{page-1}"))
if page < pages: nav.append(InlineKeyboardButton("Next ›", callback_data=f"client:list:{page+1}"))
if nav: kb.append(nav)
kb += [
[InlineKeyboardButton("➕ New client", callback_data="client:new"), InlineKeyboardButton("🔎 Search", callback_data="client:search")],
[InlineKeyboardButton("🔄 Refresh", callback_data=f"client:list:{page}"), InlineKeyboardButton("⬅️ Home", callback_data="home:main")],
]
return "\n".join(lines), InlineKeyboardMarkup(kb)
def render_client(g, sid):
r = client(g, sid)
if not r:
return "Client not found.", InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Clients", callback_data="client:list:1")]])
icon, state = status(r)
counts = r.get("runtime_counts") or {}
locations = r.get("locations") or []
name = str(r.get("name") or f"Client {sid}")
lines = [
f"👤 <b>{_html(g, name)}</b>",
f"ID <code>{int(r.get('id') or sid)}</code> · {icon} <b>{state}</b>", "",
f"📦 <b>Data</b>: {_html(g, data_text(r))}",
f"⏳ <b>Time</b>: {_html(g, time_text(g, r))}",
f"🖧 <b>Configs</b>: <code>{int(counts.get('total') or len(locations))}</code> (🟢 {int(counts.get('enabled') or 0)} · 🟡 {int(counts.get('disabled') or 0)} · 🔴 {int(counts.get('blocked') or 0)})",
]
contact = []
if r.get("phone_number"): contact.append(f"☎️ {_html(g, r.get('phone_number'))}")
if r.get("telegram_id"): contact.append(f"Telegram {_html(g, r.get('telegram_id'))}")
if contact: lines += ["", " · ".join(contact)]
if r.get("note"): lines += ["", f"📝 {_html(g, r.get('note'))}"]
keyboard = []
if r.get("enabled"):
keyboard.append([InlineKeyboardButton("⏸ Disable", callback_data=f"client:disable:confirm:{sid}")])
else:
keyboard.append([InlineKeyboardButton("▶️ Enable + reset", callback_data=f"client:enable:confirm:{sid}")])
keyboard += [
[InlineKeyboardButton("✏️ Edit", callback_data=f"client:edit:{sid}"), InlineKeyboardButton("🖧 Configs", callback_data=f"client:configs:{sid}:1")],
[InlineKeyboardButton("♻️ Reset data", callback_data=f"client:reset_data:confirm:{sid}"), InlineKeyboardButton("⏱ Reset timer", callback_data=f"client:reset_timer:confirm:{sid}")],
]
links = _api(g, "GET", f"/api/subscriptions/{int(sid)}/shortlink", timeout=15)
linkrow = []
if links.get("url") or links.get("public_url"): linkrow.append(InlineKeyboardButton("🌐 Portal", url=links.get("url") or links.get("public_url")))
if links.get("config_url"): linkrow.append(InlineKeyboardButton("📥 Config", url=links.get("config_url")))
if linkrow: keyboard.append(linkrow)
keyboard += [
[InlineKeyboardButton("🗑 Delete", callback_data=f"client:delete:confirm:{sid}"), InlineKeyboardButton("🔄 Refresh", callback_data=f"client:open:{sid}")],
[InlineKeyboardButton("⬅️ Clients", callback_data="client:list:1")],
]
return "\n".join(lines), InlineKeyboardMarkup(keyboard)
def render_edit(g, sid):
r = client(g, sid)
if not r: return "Client not found.", InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Clients", callback_data="client:list:1")]])
rows = []
for key, label in EDIT_FIELDS:
value = r.get(key)
shown = "Yes" if value is True else "No" if value is False else str(value if value not in (None, "") else "—")
if len(shown) > 18: shown = shown[:17] + "…"
rows.append([InlineKeyboardButton(f"{label}: {shown}", callback_data=f"client:field:{sid}:{key}")])
rows.append([InlineKeyboardButton("⬅️ Client", callback_data=f"client:open:{sid}")])
return f"✏️ <b>Edit {_html(g, r.get('name') or sid)}</b>\n\nChoose a field.", InlineKeyboardMarkup(rows)
def render_configs(g, sid, page=1):
r = client(g, sid)
if not r: return "Client not found.", InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Clients", callback_data="client:list:1")]])
locs = r.get("locations") or []
pages = max(1, math.ceil(len(locs)/PAGE_SIZE)); page=max(1,min(int(page),pages)); visible=locs[(page-1)*PAGE_SIZE:page*PAGE_SIZE]
lines=[f"🖧 <b>Configs · {_html(g, r.get('name') or sid)}</b>","",f"Attached: <code>{len(locs)}</code>"]
rows=[]
for loc in visible:
link=int(loc.get("link_id") or 0); name=str(loc.get("name") or f"Peer {loc.get('peer_id')}"); iface=str(loc.get("iface") or "—"); node=str(loc.get("node_name") or "Local")
icon={"online":"🟢","blocked":"🔴"}.get(str(loc.get("status") or "").lower(),"🟡")
rows.append([InlineKeyboardButton(f"{icon} {name} · {node} · {iface}",callback_data=f"client:config:{sid}:{link}")])
nav=[]
if page>1: nav.append(InlineKeyboardButton("‹ Prev",callback_data=f"client:configs:{sid}:{page-1}"))
if page<pages: nav.append(InlineKeyboardButton("Next ›",callback_data=f"client:configs:{sid}:{page+1}"))
if nav: rows.append(nav)
rows += [[InlineKeyboardButton("➕ Attach existing config",callback_data=f"client:add:{sid}:1")],[InlineKeyboardButton("⬅️ Client",callback_data=f"client:open:{sid}")]]
return "\n".join(lines),InlineKeyboardMarkup(rows)
def render_config(g, sid, link):
r=client(g,sid); loc=next((x for x in (r.get("locations") or []) if int(x.get("link_id") or 0)==int(link)),None)
if not loc: return "Config not found.",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Configs",callback_data=f"client:configs:{sid}:1")]])
lines=[f"🖧 <b>{_html(g,loc.get('name') or 'Config')}</b>","",f"Scope: <code>{_html(g,loc.get('scope') or 'local')}</code>",f"Node: <code>{_html(g,loc.get('node_name') or 'local')}</code>",f"Interface: <code>{_html(g,loc.get('iface') or '—')}</code>",f"Address: <code>{_html(g,loc.get('address') or '—')}</code>",f"Status: <code>{_html(g,loc.get('status') or '—')}</code>",f"Used: <code>{_html(g,_bytes(loc.get('used_bytes')))}</code>"]
kb=InlineKeyboardMarkup([[InlineKeyboardButton("🔗 Detach only",callback_data=f"client:detachq:{sid}:{link}:0"),InlineKeyboardButton("🗑 Detach + delete",callback_data=f"client:detachq:{sid}:{link}:1")],[InlineKeyboardButton("⬅️ Configs",callback_data=f"client:configs:{sid}:1")]])
return "\n".join(lines),kb
def render_catalog(g,sid,page=1):
data=_api(g,"GET","/api/subscriptions/inbounds_catalog",timeout=30); catalog=[x for x in (data.get("inbounds") or []) if not x.get("already_linked")]
pages=max(1,math.ceil(len(catalog)/PAGE_SIZE));page=max(1,min(int(page),pages));visible=catalog[(page-1)*PAGE_SIZE:page*PAGE_SIZE]
lines=["➕ <b>Attach existing config</b>","",f"Available: <code>{len(catalog)}</code>"]
rows=[]
for x in visible:
pid=int(x.get("peer_id") or 0); name=str(x.get("name") or f"Peer {pid}"); iface=str(x.get("iface") or "—"); node=str(x.get("node_name") or "")
rows.append([InlineKeyboardButton(f"{name} · {node+' · ' if node else ''}{iface}",callback_data=f"client:attach:{sid}:{pid}")])
nav=[]
if page>1: nav.append(InlineKeyboardButton("‹ Prev",callback_data=f"client:add:{sid}:{page-1}"))
if page<pages: nav.append(InlineKeyboardButton("Next ›",callback_data=f"client:add:{sid}:{page+1}"))
if nav: rows.append(nav)
rows.append([InlineKeyboardButton("⬅️ Configs",callback_data=f"client:configs:{sid}:1")])
return "\n".join(lines),InlineKeyboardMarkup(rows)
def _local_telegram_settings(g):
path = g.get("TELEGRAM_SETTINGS_FILE")
if not path:
return {}
try:
return g["_load_json"](path)
except Exception:
return {}
def diagnostics(g):
version = g["panel_version_info"](
fresh=False,
)
update = g["panel_update_status"]()
tg = _local_telegram_settings(g)
admins = g["current_admins_full"]()
notify = (
tg.get("notify")
or {}
) if isinstance(tg, dict) else {}
installed = str(
version.get("current")
or g.get("PROJECT_VERSION")
or "unknown"
)
latest = str(
version.get("latest")
or ""
)
source = str(
version.get("source")
or "main"
).strip()
current_revision = str(
version.get("current_revision_short")
or version.get("current_revision")
or ""
).strip()[:8]
latest_revision = str(
version.get("latest_revision_short")
or version.get("latest_revision")
or ""
).strip()[:8]
update_state = str(
update.get("status")
or "idle"
).lower()
updater_text = (
"Ready"
if update_state == "idle"
else update_state.replace("_", " ").title()
)
telegram_text = (
"Enabled"
if tg.get("enabled")
else "Disabled"
)
token_text = (
"Configured"
if tg.get("bot_token")
else "Missing"
)
def enabled_text(key):
return (
"On"
if notify.get(key)
else "Off"
)
lines = [
"<b>Bot Administration</b>",
"",
"<b>System</b>",
(
f"Panel version <code>v{_html(g, installed)}</code>"
),
(
f"Latest version <code>v{_html(g, latest)}</code>"
if latest
else "Latest version <i>Not detected</i>"
),
(
f"Update source {_html(g, source or 'main')}"
),
(
"Installed main "
f"<code>{_html(g, current_revision or 'not tracked')}</code>"
),
(
"Latest main "
f"<code>{_html(g, latest_revision or 'not detected')}</code>"
),
(
f"Updater {_html(g, updater_text)}"
),
"",
"<b>Telegram</b>",
(
f"Integration {_html(g, telegram_text)}"
),
(
f"Bot token {_html(g, token_text)}"
),
(
f"Administrators <code>{len(admins)}</code>"
),
"",
"<b>Notifications</b>",
(
"Panel / Interface "
f"{enabled_text('app_down')} / "
f"{enabled_text('iface_down')}"
),
(
"Login / Security "
f"{enabled_text('login_fail')} / "
f"{enabled_text('suspicious_4xx')}"
),
"",
(
"<i>Configuration changes remain available "
"in the authenticated web panel.</i>"
),
]
keyboard = InlineKeyboardMarkup([
[
InlineKeyboardButton(
"Panel settings",
url=f"{g['PANEL'].rstrip('/')}/settings",
),
InlineKeyboardButton(
"GitHub",
url="https://github.com/Azumi67/WG_Panel",
),
],
[
InlineKeyboardButton(
"Refresh",
callback_data="home:settings",
),
InlineKeyboardButton(
"Home",
callback_data="home:main",
),
],
])
return (
"\n".join(lines),
keyboard,
)
async def handle_callback(g, update, context):
data=update.callback_query.data
edit=g["edit_send"]
if not (data.startswith("client:") or data=="home:settings"):
return False
if data.startswith("client:list:"):
page=int(data.rsplit(":",1)[-1]); text,kb=await asyncio.to_thread(render_list,g,page,""); await edit(update,text,kb); return True
if data=="client:search":
context.user_data["v58_client_search"]=True; await edit(update,"🔎 <b>Search clients</b>\n\nSend name, ID, phone, Telegram ID, or note. Send <code>-</code> to cancel.",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Clients",callback_data="client:list:1")]])); return True
if data=="client:new":
context.user_data["v58_client_create"]={"step":0,"data":{}}; key,prompt,default=CREATE_FIELDS[0]; await edit(update,f"➕ <b>New client · 1/{len(CREATE_FIELDS)}</b>\n\n{_html(g,prompt)}"+(f"\nDefault: <code>{_html(g,default)}</code>" if default else "")+"\n\nSend <code>-</code> for default.",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Cancel",callback_data="client:list:1")]])); return True
if data.startswith("client:open:"):
text,kb=await asyncio.to_thread(render_client,g,int(data.rsplit(":",1)[-1])); await edit(update,text,kb); return True
if data.startswith("client:edit:"):
text,kb=await asyncio.to_thread(render_edit,g,int(data.rsplit(":",1)[-1])); await edit(update,text,kb); return True
if data.startswith("client:field:"):
_,_,sid,key=data.split(":",3); context.user_data["v58_client_edit"]={"sid":int(sid),"key":key}; await edit(update,f"✏️ <b>Edit {_html(g,key.replace('_',' ').title())}</b>\n\nSend the new value. Use <code>-</code> to clear optional text.",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Cancel",callback_data=f"client:edit:{sid}")]])); return True
if data.startswith("client:configs:"):
_,_,sid,page=data.split(":",3); text,kb=await asyncio.to_thread(render_configs,g,int(sid),int(page)); await edit(update,text,kb); return True
if data.startswith("client:config:"):
_,_,sid,link=data.split(":",3); text,kb=await asyncio.to_thread(render_config,g,int(sid),int(link)); await edit(update,text,kb); return True
if data.startswith("client:add:"):
_,_,sid,page=data.split(":",3); text,kb=await asyncio.to_thread(render_catalog,g,int(sid),int(page)); await edit(update,text,kb); return True
if data.startswith("client:attach:"):
_,_,sid,pid=data.split(":",3); result=await asyncio.to_thread(_api,g,"POST",f"/api/subscriptions/{int(sid)}/inbounds",{"targets":[{"peer_id":int(pid)}]},45)
if not result.get("ok"): await edit(update,f"❌ <b>Attach failed</b>\n\n<code>{_html(g,_err(result))}</code>",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Back",callback_data=f"client:add:{sid}:1")]])); return True
text,kb=await asyncio.to_thread(render_configs,g,int(sid),1); await edit(update,"✅ Config attached.\n\n"+text,kb); return True
if data.startswith("client:detachq:"):
_,_,sid,link,delete=data.split(":",4); warn="The peer will also be deleted." if delete=="1" else "The peer remains in Peers."; kb=InlineKeyboardMarkup([[InlineKeyboardButton("✅ Confirm",callback_data=f"client:detach:{sid}:{link}:{delete}")],[InlineKeyboardButton("⬅️ Cancel",callback_data=f"client:config:{sid}:{link}")]]); await edit(update,f"⚠️ <b>Detach config?</b>\n\n{warn}",kb); return True
if data.startswith("client:detach:"):
_,_,sid,link,delete=data.split(":",4); result=await asyncio.to_thread(_api,g,"DELETE",f"/api/subscriptions/{int(sid)}/inbounds/{int(link)}?delete_peer={delete}",None,45)
if not result.get("ok"): await edit(update,f"❌ <b>Detach failed</b>\n\n<code>{_html(g,_err(result))}</code>",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Back",callback_data=f"client:configs:{sid}:1")]])); return True
text,kb=await asyncio.to_thread(render_configs,g,int(sid),1); await edit(update,"✅ Config detached.\n\n"+text,kb); return True
for action in ("enable","disable","reset_data","reset_timer"):
if data.startswith(f"client:{action}:confirm:"):
sid=int(data.rsplit(":",1)[-1]); warnings={"enable":"Enabling resets data and timer.","disable":"Disabling preserves data and timer.","reset_data":"Shared usage will be cleared.","reset_timer":"The shared timer will restart."}; kb=InlineKeyboardMarkup([[InlineKeyboardButton("✅ Confirm",callback_data=f"client:{action}:run:{sid}")],[InlineKeyboardButton("⬅️ Cancel",callback_data=f"client:open:{sid}")]]); await edit(update,f"⚠️ <b>Confirm action?</b>\n\n{warnings[action]}",kb); return True
if data.startswith(f"client:{action}:run:"):
sid=int(data.rsplit(":",1)[-1]); result=await asyncio.to_thread(_api,g,"POST",f"/api/subscriptions/{sid}/{action}",None,45)
if not result.get("ok") and not result.get("partial"): await edit(update,f"❌ <b>Action failed</b>\n\n<code>{_html(g,_err(result))}</code>",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Client",callback_data=f"client:open:{sid}")]])); return True
text,kb=await asyncio.to_thread(render_client,g,sid); await edit(update,"✅ Action completed.\n\n"+text,kb); return True
if data.startswith("client:delete:confirm:"):
sid=int(data.rsplit(":",1)[-1]); kb=InlineKeyboardMarkup([[InlineKeyboardButton("🗑 Delete client + peers",callback_data=f"client:delete:{sid}:1")],[InlineKeyboardButton("🔗 Delete client only",callback_data=f"client:delete:{sid}:0")],[InlineKeyboardButton("⬅️ Cancel",callback_data=f"client:open:{sid}")]]); await edit(update,"⚠️ <b>Delete client?</b>\n\nChoose whether attached peers should also be deleted.",kb); return True
if data.startswith("client:delete:"):
_,_,sid,peers=data.split(":",3); result=await asyncio.to_thread(_api,g,"DELETE",f"/api/subscriptions/{int(sid)}?delete_peers={peers}",None,45)
if not result.get("ok"): await edit(update,f"❌ <b>Delete failed</b>\n\n<code>{_html(g,_err(result))}</code>",InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Client",callback_data=f"client:open:{sid}")]])); return True
text,kb=await asyncio.to_thread(render_list,g,1,""); await edit(update,"✅ Client deleted.\n\n"+text,kb); return True
if data=="home:settings":
text,kb=await asyncio.to_thread(diagnostics,g); await edit(update,text,kb); return True
return False
async def handle_text(g, update, context):
text=(update.message.text or "").strip()
if context.user_data.get("v58_client_search"):
context.user_data.pop("v58_client_search",None); query="" if text=="-" else text; out,kb=await asyncio.to_thread(render_list,g,1,query); await g["send_text"](update,out,kb=kb); return True
edit_state=context.user_data.get("v58_client_edit")
if edit_state:
sid=int(edit_state["sid"]);key=edit_state["key"]
try: value=_value(key,text)
except Exception as exc: await g["send_text"](update,f"⚠️ Invalid value: {_html(g,exc)}",kb=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Edit",callback_data=f"client:edit:{sid}")]])); return True
result=await asyncio.to_thread(_api,g,"PUT",f"/api/subscriptions/{sid}",{key:value},35)
if not result.get("ok"): await g["send_text"](update,f"❌ Save failed.\n<code>{_html(g,_err(result))}</code>",kb=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Edit",callback_data=f"client:edit:{sid}")]])); return True
context.user_data.pop("v58_client_edit",None); out,kb=await asyncio.to_thread(render_edit,g,sid); await g["send_text"](update,"✅ Saved.\n\n"+out,kb=kb); return True
create=context.user_data.get("v58_client_create")
if create:
step=int(create.get("step") or 0);key,prompt,default=CREATE_FIELDS[step]
try: create["data"][key]=_value(key,default if text=="-" else text)
except Exception as exc: await g["send_text"](update,f"⚠️ Invalid value: {_html(g,exc)}",kb=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Cancel",callback_data="client:list:1")]])); return True
step+=1;create["step"]=step;context.user_data["v58_client_create"]=create
if step<len(CREATE_FIELDS):
_,prompt,default=CREATE_FIELDS[step];await g["send_text"](update,f"➕ <b>New client · {step+1}/{len(CREATE_FIELDS)}</b>\n\n{_html(g,prompt)}"+(f"\nDefault: <code>{_html(g,default)}</code>" if default else "")+"\n\nSend <code>-</code> for default.",kb=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Cancel",callback_data="client:list:1")]])); return True
context.user_data.pop("v58_client_create",None);result=await asyncio.to_thread(_api,g,"POST","/api/subscriptions",create["data"],45)
if not result.get("ok"): await g["send_text"](update,f"❌ <b>Create failed</b>\n\n<code>{_html(g,_err(result))}</code>",kb=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Clients",callback_data="client:list:1")]])); return True
sid=int((result.get("subscription") or {}).get("id") or 0);out,kb=await asyncio.to_thread(render_client,g,sid);await g["send_text"](update,"✅ <b>Client created.</b>\n\nUse <b>Configs</b> to attach existing peers.\n\n"+out,kb=kb);return True
return False