nonebot-plugin-shiro-web-console 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nonebot-plugin-shiro-web-console might be problematic. Click here for more details.

@@ -4,6 +4,9 @@ import httpx
4
4
  import os
5
5
  import random
6
6
  import time
7
+ import secrets
8
+ import hashlib
9
+ import re
7
10
  from datetime import datetime, timedelta
8
11
  from urllib.parse import quote, unquote
9
12
  from typing import Dict, List, Set, Optional, Any
@@ -13,14 +16,15 @@ from collections import deque
13
16
  from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, Response, Depends, HTTPException
14
17
  from fastapi.staticfiles import StaticFiles
15
18
  from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
16
- from nonebot import get_app, get_bot, get_driver, logger, on_message, on_command, require
17
- require("nonebot_plugin_localstore")
19
+ from nonebot import get_app, get_bot, get_bots, get_driver, logger, on_message, on_command, require
18
20
  import nonebot_plugin_localstore
19
21
  from .config import Config, config
20
22
  from nonebot.permission import SUPERUSER
21
23
  from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GroupMessageEvent, PrivateMessageEvent, MessageSegment
22
24
  from nonebot.plugin import PluginMetadata
23
25
 
26
+ START_TIME = time.time()
27
+
24
28
  __plugin_meta__ = PluginMetadata(
25
29
  name="Shiro Web Console",
26
30
  description="通过浏览器查看日志、管理机器人并发送消息",
@@ -31,20 +35,43 @@ __plugin_meta__ = PluginMetadata(
31
35
  supported_adapters={"~onebot.v11"},
32
36
  extra={
33
37
  "author": "luojisama",
34
- "version": "0.1.6",
38
+ "version": "0.1.7",
35
39
  "pypi_test": "nonebot-plugin-shiro-web-console",
36
40
  },
37
41
  )
38
42
 
43
+ # WebSocket 连接池
44
+ active_connections: Set[WebSocket] = set()
45
+
46
+ async def broadcast_message(data: dict):
47
+ if not active_connections:
48
+ return
49
+
50
+ dead_connections = set()
51
+ for ws in active_connections:
52
+ try:
53
+ await ws.send_json(data)
54
+ except Exception:
55
+ dead_connections.add(ws)
56
+
57
+ for ws in dead_connections:
58
+ active_connections.remove(ws)
59
+
39
60
  # 日志缓冲区,保留最近 200 条日志
40
61
  log_buffer = deque(maxlen=200)
41
62
 
42
- def log_sink(message):
43
- log_buffer.append({
63
+ async def log_sink(message):
64
+ log_entry = {
44
65
  "time": datetime.now().strftime("%H:%M:%S"),
45
66
  "level": message.record["level"].name,
46
67
  "message": message.record["message"],
47
68
  "module": message.record["module"]
69
+ }
70
+ log_buffer.append(log_entry)
71
+ # 推送日志
72
+ await broadcast_message({
73
+ "type": "new_log",
74
+ "data": log_entry
48
75
  })
49
76
 
50
77
  # 注册 loguru sink
@@ -63,20 +90,30 @@ class AuthManager:
63
90
  self.password_file = self.data_dir / "password.json"
64
91
 
65
92
  # 初始加载密码
66
- self.admin_password = self._load_password()
93
+ self.admin_password_hash = self._load_password_hash()
67
94
 
68
- def _load_password(self) -> str:
95
+ def _load_password_hash(self) -> str:
96
+ pwd = "admin123"
69
97
  if self.password_file.exists():
70
98
  try:
71
99
  data = json.loads(self.password_file.read_text(encoding="utf-8"))
72
- return data.get("password", "admin123")
100
+ if "password_hash" in data:
101
+ return data["password_hash"]
102
+ pwd = data.get("password", "admin123")
73
103
  except:
74
104
  pass
75
- return config.web_console_password
105
+ else:
106
+ pwd = config.web_console_password
107
+
108
+ # 迁移或初始化:将明文转换为哈希
109
+ return hashlib.sha256(pwd.encode()).hexdigest()
76
110
 
77
111
  def save_password(self, new_password: str):
78
- self.admin_password = new_password
79
- self.password_file.write_text(json.dumps({"password": new_password}), encoding="utf-8")
112
+ pwd_hash = hashlib.sha256(new_password.encode()).hexdigest()
113
+ self.admin_password_hash = pwd_hash
114
+ self.password_file.write_text(json.dumps({"password_hash": pwd_hash}), encoding="utf-8")
115
+ # 修改密码后使旧 token 失效
116
+ self.token = None
80
117
 
81
118
  def generate_code(self) -> str:
82
119
  self.code = "".join([str(random.randint(0, 9)) for _ in range(6)])
@@ -96,13 +133,14 @@ class AuthManager:
96
133
  return False
97
134
 
98
135
  def verify_password(self, password: str) -> bool:
99
- if password == self.admin_password:
136
+ input_hash = hashlib.sha256(password.encode()).hexdigest()
137
+ if input_hash == self.admin_password_hash:
100
138
  self.generate_token()
101
139
  return True
102
140
  return False
103
141
 
104
142
  def generate_token(self):
105
- self.token = "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for _ in range(32)])
143
+ self.token = secrets.token_hex(16)
106
144
  self.token_expire = datetime.now() + timedelta(days=7)
107
145
 
108
146
  def verify_token(self, token: str) -> bool:
@@ -126,32 +164,36 @@ async def check_auth(request: Request):
126
164
  return True
127
165
 
128
166
  try:
129
- app: Optional[FastAPI] = get_app()
167
+ _app = get_app()
130
168
  except (ValueError, AssertionError):
131
- app = None
169
+ _app = None
132
170
 
133
- if app is None:
134
- app = FastAPI()
135
- logger.warning("FastAPI app not found, created a new one. This might happen during plugin test.")
171
+ # 只有在驱动器支持 FastAPI 时才挂载
172
+ if isinstance(_app, FastAPI):
173
+ app = _app
174
+ else:
175
+ app = None
176
+ logger.warning("驱动器不支持 FastAPI,Web 控制台路由将无法访问。")
136
177
 
137
- static_path = Path(__file__).parent / "static"
138
- index_html = static_path / "index.html"
178
+ if app:
179
+ static_path = Path(__file__).parent / "static"
180
+ index_html = static_path / "index.html"
139
181
 
140
- # 挂载静态文件
141
- if static_path.exists():
142
- app.mount("/web_console/static", StaticFiles(directory=str(static_path)), name="web_console_static")
182
+ # 挂载静态文件
183
+ if static_path.exists():
184
+ app.mount("/web_console/static", StaticFiles(directory=str(static_path)), name="web_console_static")
143
185
 
144
- # Web 控制台入口路由
145
- @app.get("/web_console", response_class=HTMLResponse)
146
- async def serve_console():
147
- if not index_html.exists():
148
- return HTMLResponse("<h1>index.html not found</h1>", status_code=404)
149
- return HTMLResponse(content=index_html.read_text(encoding="utf-8"), status_code=200)
186
+ # Web 控制台入口路由
187
+ @app.get("/web_console", response_class=HTMLResponse)
188
+ async def serve_console():
189
+ if not index_html.exists():
190
+ return HTMLResponse("<h1>index.html not found</h1>", status_code=404)
191
+ return HTMLResponse(content=index_html.read_text(encoding="utf-8"), status_code=200)
150
192
 
151
- # 兼容 /web_console/ 路径
152
- @app.get("/web_console/", response_class=HTMLResponse)
153
- async def serve_console_slash():
154
- return await serve_console()
193
+ # 兼容 /web_console/ 路径
194
+ @app.get("/web_console/", response_class=HTMLResponse)
195
+ async def serve_console_slash():
196
+ return await serve_console()
155
197
 
156
198
  # 消息缓存 {chat_id: [messages]}
157
199
  message_cache: Dict[str, List[dict]] = {}
@@ -160,7 +202,8 @@ image_cache: Dict[str, dict] = {}
160
202
  CACHE_SIZE = 100
161
203
 
162
204
  # WebSocket 连接池
163
- active_connections: Set[WebSocket] = set()
205
+ # active_connections defined at top
206
+
164
207
 
165
208
  # 基础人设
166
209
  def get_chat_id(event: MessageEvent) -> str:
@@ -179,7 +222,7 @@ async def handle_password_cmd(bot: Bot, event: MessageEvent):
179
222
  await password_cmd.finish("请在命令后输入新密码,例如:web密码 mynewpassword")
180
223
 
181
224
  auth_manager.save_password(new_password)
182
- await password_cmd.finish(f"Web控制台密码已修改为: {new_password}\n请妥善保存。")
225
+ await password_cmd.finish(f"Web控制台密码已修改。\n请妥善保存。")
183
226
 
184
227
  @login_cmd.handle()
185
228
  async def handle_login_cmd(bot: Bot, event: MessageEvent):
@@ -366,481 +409,543 @@ async def handle_all_messages(bot: Bot, event: MessageEvent):
366
409
  "data": msg_data
367
410
  })
368
411
 
369
- async def broadcast_message(data: dict):
370
- if not active_connections:
371
- return
372
-
373
- dead_connections = set()
374
- for ws in active_connections:
375
- try:
376
- await ws.send_json(data)
377
- except Exception:
378
- dead_connections.add(ws)
379
-
380
- for ws in dead_connections:
381
- active_connections.remove(ws)
382
412
 
383
- # 认证 API
384
- @app.post("/web_console/api/send_code")
385
- async def send_code():
386
- if not superusers:
387
- return {"error": "未设置 SUPERUSERS 管理员列表"}
388
-
389
- code = auth_manager.generate_code()
390
- bot = get_bot()
391
-
392
- success_count = 0
393
- for user_id in superusers:
394
- try:
395
- await bot.send_private_msg(user_id=int(user_id), message=f"【Web控制台】您的登录验证码为:{code},5分钟内有效。")
396
- success_count += 1
397
- except Exception as e:
398
- logger.error(f"发送验证码给管理员 {user_id} 失败: {e}")
399
-
400
- if success_count > 0:
401
- return {"msg": "验证码已发送至管理员 QQ"}
402
- return {"error": "验证码发送失败,请检查机器人是否在线或管理员账号是否正确"}
403
-
404
- @app.post("/web_console/api/login")
405
- async def login(data: dict):
406
- code = data.get("code")
407
- password = data.get("password")
408
-
409
- if code:
410
- if auth_manager.verify_code(code):
411
- return {"token": auth_manager.token}
412
- return {"error": "验证码错误或已过期", "code": 401}
413
- elif password:
414
- if auth_manager.verify_password(password):
415
- return {"token": auth_manager.token}
416
- return {"error": "密码错误", "code": 401}
413
+ if app:
414
+ # 认证 API
415
+ @app.post("/web_console/api/send_code")
416
+ async def send_code():
417
+ if not superusers:
418
+ return {"error": "未设置 SUPERUSERS 管理员列表"}
417
419
 
418
- return {"error": "请输入验证码或密码", "code": 400}
419
-
420
- @app.get("/web_console/api/status", dependencies=[Depends(check_auth)])
421
- async def get_system_status():
422
- from nonebot import get_bots
423
- import psutil
424
- import platform
425
- import time
426
- import datetime
427
-
428
- # 系统性能
429
- cpu_percent = psutil.cpu_percent()
430
- memory = psutil.virtual_memory()
431
- disk = psutil.disk_usage('/')
432
-
433
- # 网络流量
434
- net_io = psutil.net_io_counters()
435
-
436
- # 运行时间
437
- boot_time = psutil.boot_time()
438
- uptime = time.time() - boot_time
439
- uptime_str = str(datetime.timedelta(seconds=int(uptime)))
440
-
441
- # 机器人信息
442
- bots_info = []
443
- for bot_id, bot in get_bots().items():
444
- try:
445
- profile = await bot.get_login_info()
446
- bots_info.append({
447
- "id": bot_id,
448
- "nickname": profile.get("nickname", "未知"),
449
- "avatar": f"https://q.qlogo.cn/headimg_dl?dst_uin={bot_id}&spec=640",
450
- "status": "在线"
451
- })
452
- except:
453
- bots_info.append({
454
- "id": bot_id,
455
- "nickname": "机器人",
456
- "avatar": f"https://q.qlogo.cn/headimg_dl?dst_uin={bot_id}&spec=640",
457
- "status": "离线"
458
- })
459
-
460
- return {
461
- "system": {
462
- "os": platform.system(),
463
- "cpu": f"{cpu_percent}%",
464
- "memory": f"{memory.percent}%",
465
- "memory_used": f"{round(memory.used / 1024 / 1024 / 1024, 2)} GB",
466
- "memory_total": f"{round(memory.total / 1024 / 1024 / 1024, 2)} GB",
467
- "disk": f"{disk.percent}%",
468
- "disk_used": f"{round(disk.used / 1024 / 1024 / 1024, 2)} GB",
469
- "disk_total": f"{round(disk.total / 1024 / 1024 / 1024, 2)} GB",
470
- "net_sent": f"{round(net_io.bytes_sent / 1024 / 1024, 2)} MB",
471
- "net_recv": f"{round(net_io.bytes_recv / 1024 / 1024, 2)} MB",
472
- "uptime": uptime_str,
473
- "python": platform.python_version()
474
- },
475
- "bots": bots_info
476
- }
477
-
478
- @app.get("/web_console/api/logs", dependencies=[Depends(check_auth)])
479
- async def get_logs():
480
- return list(log_buffer)
481
-
482
- @app.get("/web_console/api/plugins", dependencies=[Depends(check_auth)])
483
- async def get_plugins():
484
- from nonebot import get_loaded_plugins
485
- import os
486
- plugins = []
487
- for p in get_loaded_plugins():
488
- metadata = p.metadata
420
+ code = auth_manager.generate_code()
421
+
422
+ # 兼容多 Bot 场景
423
+ from nonebot import get_bots
424
+ bots = get_bots()
425
+ if not bots:
426
+ return {"error": "未连接任何 Bot"}
427
+ bot = list(bots.values())[0]
489
428
 
490
- # 识别插件来源
491
- plugin_type = "local"
492
- module_name = p.module_name
429
+ success_count = 0
430
+ for user_id in superusers:
431
+ try:
432
+ await bot.send_private_msg(user_id=int(user_id), message=f"【Web控制台】您的登录验证码为:{code},5分钟内有效。")
433
+ success_count += 1
434
+ except Exception as e:
435
+ logger.error(f"发送验证码给管理员 {user_id} 失败: {e}")
436
+
437
+ if success_count > 0:
438
+ return {"msg": "验证码已发送至管理员 QQ"}
439
+ return {"error": "验证码发送失败,请检查机器人是否在线或管理员账号是否正确"}
440
+
441
+ @app.post("/web_console/api/login")
442
+ async def login(data: dict):
443
+ code = data.get("code")
444
+ password = data.get("password")
493
445
 
494
- if module_name.startswith("nonebot.plugins"):
495
- plugin_type = "builtin"
496
- elif metadata and metadata.homepage and ("github.com/nonebot" in metadata.homepage or "nonebot.dev" in metadata.homepage):
497
- plugin_type = "official"
498
- elif module_name.startswith("nonebot_plugin_"):
499
- plugin_type = "store"
446
+ if code:
447
+ if auth_manager.verify_code(code):
448
+ return {"token": auth_manager.token}
449
+ return {"error": "验证码错误或已过期", "code": 401}
450
+ elif password:
451
+ if auth_manager.verify_password(password):
452
+ return {"token": auth_manager.token}
453
+ return {"error": "密码错误", "code": 401}
500
454
 
501
- plugins.append({
502
- "id": p.name,
503
- "name": metadata.name if metadata else p.name,
504
- "description": metadata.description if metadata else "暂无描述",
505
- "version": metadata.extra.get("version", "1.0.0") if metadata and metadata.extra else "1.0.0",
506
- "type": plugin_type,
507
- "module": module_name,
508
- "homepage": metadata.homepage if metadata else None
509
- })
510
- return plugins
511
-
512
- @app.post("/web_console/api/system/action", dependencies=[Depends(check_auth)])
513
- async def system_action(request: Request):
514
- data = await request.json()
515
- action = data.get("action")
516
-
517
- if action not in ["reboot", "shutdown"]:
518
- return {"error": "无效操作"}
455
+ return {"error": "请输入验证码或密码", "code": 400}
456
+
457
+ @app.get("/web_console/api/status", dependencies=[Depends(check_auth)])
458
+ async def get_system_status():
459
+ from nonebot import get_bots
460
+ import psutil
461
+ import platform
462
+ import time
463
+ import datetime
519
464
 
520
- import os
521
- import sys
522
- import subprocess
523
- import asyncio
524
-
525
- logger.warning(f"收到系统指令: {action}")
526
-
527
- if action == "shutdown":
528
- # 延迟执行关闭,确保响应能发出去
529
- loop = asyncio.get_event_loop()
530
- loop.call_later(1.0, lambda: os._exit(0))
531
- return {"msg": "Bot 正在关闭..."}
465
+ # 系统性能
466
+ cpu_percent = psutil.cpu_percent()
467
+ memory = psutil.virtual_memory()
468
+ disk = psutil.disk_usage('.')
532
469
 
533
- elif action == "reboot":
534
- # 获取项目根目录
535
- root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
536
- bot_py = os.path.join(root_dir, "bot.py")
470
+ # 网络流量
471
+ net_io = psutil.net_io_counters()
537
472
 
538
- if os.path.exists(bot_py):
539
- cmd = [sys.executable, bot_py]
540
- else:
541
- cmd = [sys.executable] + sys.argv
542
-
543
- def do_reboot():
473
+ # 运行时间
474
+ uptime = time.time() - START_TIME
475
+ uptime_str = str(datetime.timedelta(seconds=int(uptime)))
476
+
477
+ # 机器人信息
478
+ bots_info = []
479
+ for bot_id, bot in get_bots().items():
544
480
  try:
545
- if sys.platform == "win32":
546
- subprocess.Popen(cmd, cwd=root_dir)
547
- os._exit(0)
548
- else:
549
- os.chdir(root_dir)
550
- os.execv(sys.executable, cmd)
551
- except Exception as e:
552
- logger.error(f"重启执行失败: {e}")
553
- os._exit(1)
481
+ profile = await bot.get_login_info()
482
+ bots_info.append({
483
+ "id": bot_id,
484
+ "nickname": profile.get("nickname", "未知"),
485
+ "avatar": f"https://q.qlogo.cn/headimg_dl?dst_uin={bot_id}&spec=640",
486
+ "status": "在线"
487
+ })
488
+ except:
489
+ bots_info.append({
490
+ "id": bot_id,
491
+ "nickname": "机器人",
492
+ "avatar": f"https://q.qlogo.cn/headimg_dl?dst_uin={bot_id}&spec=640",
493
+ "status": "离线"
494
+ })
495
+
496
+ return {
497
+ "system": {
498
+ "os": platform.system(),
499
+ "cpu": f"{cpu_percent}%",
500
+ "memory": f"{memory.percent}%",
501
+ "memory_used": f"{round(memory.used / 1024 / 1024 / 1024, 2)} GB",
502
+ "memory_total": f"{round(memory.total / 1024 / 1024 / 1024, 2)} GB",
503
+ "disk": f"{disk.percent}%",
504
+ "disk_used": f"{round(disk.used / 1024 / 1024 / 1024, 2)} GB",
505
+ "disk_total": f"{round(disk.total / 1024 / 1024 / 1024, 2)} GB",
506
+ "net_sent": f"{round(net_io.bytes_sent / 1024 / 1024, 2)} MB",
507
+ "net_recv": f"{round(net_io.bytes_recv / 1024 / 1024, 2)} MB",
508
+ "uptime": uptime_str,
509
+ "python": platform.python_version()
510
+ },
511
+ "bots": bots_info
512
+ }
554
513
 
555
- # 延迟执行重启
556
- loop = asyncio.get_event_loop()
557
- loop.call_later(1.0, do_reboot)
558
- return {"msg": "Bot 正在重启..."}
514
+ @app.get("/web_console/api/logs", dependencies=[Depends(check_auth)])
515
+ async def get_logs():
516
+ return list(log_buffer)
517
+
518
+ @app.get("/web_console/api/plugins", dependencies=[Depends(check_auth)])
519
+ async def get_plugins():
520
+ from nonebot import get_loaded_plugins
521
+ import os
522
+ plugins = []
523
+ for p in get_loaded_plugins():
524
+ metadata = p.metadata
525
+
526
+ # 识别插件来源
527
+ plugin_type = "local"
528
+ module_name = p.module_name
529
+
530
+ if module_name.startswith("nonebot.plugins"):
531
+ plugin_type = "builtin"
532
+ elif metadata and metadata.homepage and ("github.com/nonebot" in metadata.homepage or "nonebot.dev" in metadata.homepage):
533
+ plugin_type = "official"
534
+ elif module_name.startswith("nonebot_plugin_"):
535
+ plugin_type = "store"
536
+
537
+ plugins.append({
538
+ "id": p.name,
539
+ "name": metadata.name if metadata else p.name,
540
+ "description": metadata.description if metadata else "暂无描述",
541
+ "version": metadata.extra.get("version", "1.0.0") if metadata and metadata.extra else "1.0.0",
542
+ "type": plugin_type,
543
+ "module": module_name,
544
+ "homepage": metadata.homepage if metadata else None
545
+ })
546
+ return plugins
559
547
 
560
- @app.get("/web_console/api/plugins/{plugin_id}/config", dependencies=[Depends(check_auth)])
561
- async def get_plugin_config(plugin_id: str):
562
- from nonebot import get_loaded_plugins, get_driver
563
-
564
- # 查找插件
565
- target_plugin = None
566
- for p in get_loaded_plugins():
567
- if p.name == plugin_id:
568
- target_plugin = p
569
- break
548
+ @app.post("/web_console/api/system/action", dependencies=[Depends(check_auth)])
549
+ async def system_action(request: Request):
550
+ data = await request.json()
551
+ action = data.get("action")
552
+ confirm = data.get("confirm")
553
+
554
+ if action not in ["reboot", "shutdown"]:
555
+ return {"error": "无效操作"}
556
+
557
+ if not confirm:
558
+ return {"error": "请确认操作", "need_confirm": True}
570
559
 
571
- if not target_plugin:
572
- raise HTTPException(status_code=404, detail="Plugin not found")
560
+ import os
561
+ import sys
562
+ import subprocess
563
+ import asyncio
573
564
 
574
- # 获取配置元数据 (NoneBot 插件通常通过 metadata.config 导出 Config 类)
575
- config_schema = {}
576
- current_config = {}
577
-
578
- if target_plugin.metadata and target_plugin.metadata.config:
579
- try:
580
- config_class = target_plugin.metadata.config
581
- if hasattr(config_class, "schema"):
582
- schema = config_class.schema()
583
- config_schema = schema.get("properties", {})
584
- # 注入当前值
585
- driver_config = get_driver().config
586
- for key in config_schema:
587
- current_config[key] = getattr(driver_config, key, None)
588
- except Exception as e:
589
- logger.error(f"解析插件 {plugin_id} 配置失败: {e}")
565
+ logger.warning(f"收到系统指令: {action}")
566
+
567
+ if action == "shutdown":
568
+ # 延迟执行关闭,确保响应能发出去
569
+ loop = asyncio.get_event_loop()
570
+ loop.call_later(1.0, lambda: os._exit(0))
571
+ return {"msg": "Bot 正在关闭..."}
590
572
 
591
- return {"config": current_config, "schema": config_schema}
592
-
593
- # --- 插件商店相关 API ---
594
-
595
- STORE_URL = "https://registry.nonebot.dev/plugins.json"
596
- store_cache = {"data": [], "time": 0}
597
-
598
- @app.get("/web_console/api/store", dependencies=[Depends(check_auth)])
599
- async def get_store():
600
- # 缓存 1 小时
601
- if not store_cache["data"] or time.time() - store_cache["time"] > 3600:
602
- try:
603
- async with httpx.AsyncClient(follow_redirects=True, verify=False) as client:
604
- resp = await client.get(STORE_URL, timeout=15.0)
605
- if resp.status_code == 200:
606
- store_cache["data"] = resp.json()
607
- store_cache["time"] = time.time()
608
- else:
609
- logger.error(f"获取 NoneBot 商店数据失败: HTTP {resp.status_code}")
610
- except Exception as e:
611
- logger.error(f"获取 NoneBot 商店数据失败: {e}")
612
- # 如果之前有缓存,即使失败也返回旧缓存,避免页面空白
613
- if store_cache["data"]:
614
- return store_cache["data"]
615
- return {"error": "无法连接到 NoneBot 商店,请检查服务器网络或稍后再试"}
573
+ elif action == "reboot":
574
+ # 获取项目根目录 (通常是当前工作目录)
575
+ root_dir = Path.cwd()
576
+ bot_py = root_dir / "bot.py"
616
577
 
617
- return store_cache["data"]
618
-
619
- @app.post("/web_console/api/store/action", dependencies=[Depends(check_auth)])
620
- async def store_action(request: Request):
621
- data = await request.json()
622
- action = data.get("action") # install, update, uninstall
623
- plugin_name = data.get("plugin")
624
-
625
- if not action or not plugin_name:
626
- return {"error": "参数错误"}
578
+ if bot_py.exists():
579
+ cmd = [sys.executable, str(bot_py)]
580
+ else:
581
+ cmd = [sys.executable] + sys.argv
582
+
583
+ def do_reboot():
584
+ try:
585
+ if sys.platform == "win32":
586
+ subprocess.Popen(cmd, cwd=str(root_dir))
587
+ os._exit(0)
588
+ else:
589
+ os.chdir(root_dir)
590
+ os.execv(sys.executable, cmd)
591
+ except Exception as e:
592
+ logger.error(f"重启执行失败: {e}")
593
+ os._exit(1)
594
+
595
+ # 延迟执行重启
596
+ loop = asyncio.get_event_loop()
597
+ loop.call_later(1.0, do_reboot)
598
+ return {"msg": "Bot 正在重启..."}
599
+
600
+ @app.get("/web_console/api/plugins/{plugin_id}/config", dependencies=[Depends(check_auth)])
601
+ async def get_plugin_config(plugin_id: str):
602
+ from nonebot import get_loaded_plugins, get_driver
627
603
 
628
- # 执行命令
629
- import asyncio
630
- import sys
631
-
632
- # 构建命令
633
- cmd = []
634
- # 尝试定位 nb 命令
635
- import shutil
636
- nb_path = shutil.which("nb")
637
-
638
- if not nb_path:
639
- # 如果系统 PATH 中找不到,再尝试在 Python 脚本目录下找
640
- script_dir = os.path.dirname(sys.executable)
641
- possible_nb = os.path.join(script_dir, "nb.exe" if sys.platform == "win32" else "nb")
642
- if os.path.exists(possible_nb):
643
- nb_path = possible_nb
644
- else:
645
- nb_path = "nb" # 最后的保底,尝试直接运行 nb
604
+ # 查找插件
605
+ target_plugin = None
606
+ for p in get_loaded_plugins():
607
+ if p.name == plugin_id:
608
+ target_plugin = p
609
+ break
610
+
611
+ if not target_plugin:
612
+ raise HTTPException(status_code=404, detail="Plugin not found")
613
+
614
+ # 获取配置元数据 (NoneBot 插件通常通过 metadata.config 导出 Config 类)
615
+ config_schema = {}
616
+ current_config = {}
617
+
618
+ if target_plugin.metadata and target_plugin.metadata.config:
619
+ try:
620
+ config_class = target_plugin.metadata.config
621
+ if hasattr(config_class, "schema"):
622
+ schema = config_class.schema()
623
+ config_schema = schema.get("properties", {})
624
+ # 注入当前值
625
+ driver_config = get_driver().config
626
+ for key in config_schema:
627
+ current_config[key] = getattr(driver_config, key, None)
628
+ except Exception as e:
629
+ logger.error(f"解析插件 {plugin_id} 配置失败: {e}")
630
+
631
+ return {"config": current_config, "schema": config_schema}
646
632
 
647
- # 获取项目根目录 (bot.py 所在目录)
648
- root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
633
+ # --- 插件商店相关 API ---
649
634
 
650
- if action == "install":
651
- cmd = [nb_path, "plugin", "install", plugin_name]
652
- elif action == "update":
653
- cmd = [nb_path, "plugin", "update", plugin_name]
654
- elif action == "uninstall":
655
- cmd = [nb_path, "plugin", "uninstall", "-y", plugin_name]
656
- else:
657
- return {"error": "无效操作"}
635
+ STORE_URL = "https://registry.nonebot.dev/plugins.json"
636
+ store_cache = {"data": [], "time": 0}
637
+
638
+ @app.get("/web_console/api/store", dependencies=[Depends(check_auth)])
639
+ async def get_store():
640
+ # 缓存 1 小时
641
+ if not store_cache["data"] or time.time() - store_cache["time"] > 3600:
642
+ try:
643
+ async with httpx.AsyncClient(follow_redirects=True, verify=False) as client:
644
+ resp = await client.get(STORE_URL, timeout=15.0)
645
+ if resp.status_code == 200:
646
+ store_cache["data"] = resp.json()
647
+ store_cache["time"] = time.time()
648
+ else:
649
+ logger.error(f"获取 NoneBot 商店数据失败: HTTP {resp.status_code}")
650
+ except Exception as e:
651
+ logger.error(f"获取 NoneBot 商店数据失败: {e}")
652
+ # 如果之前有缓存,即使失败也返回旧缓存,避免页面空白
653
+ if store_cache["data"]:
654
+ return store_cache["data"]
655
+ return {"error": "无法连接到 NoneBot 商店,请检查服务器网络或稍后再试"}
656
+
657
+ return store_cache["data"]
658
+
659
+ @app.post("/web_console/api/store/action", dependencies=[Depends(check_auth)])
660
+ async def store_action(request: Request):
661
+ data = await request.json()
662
+ action = data.get("action") # install, update, uninstall
663
+ plugin_name = data.get("plugin")
658
664
 
659
- logger.info(f"开始执行插件操作: {' '.join(cmd)} (工作目录: {root_dir})")
660
-
661
- try:
662
- process = await asyncio.create_subprocess_exec(
663
- *cmd,
664
- stdout=asyncio.subprocess.PIPE,
665
- stderr=asyncio.subprocess.PIPE,
666
- cwd=root_dir
667
- )
665
+ if not action or not plugin_name:
666
+ return {"error": "参数错误"}
667
+
668
+ if not re.match(r'^[a-zA-Z0-9_-]+$', plugin_name):
669
+ return {"error": "非法插件名称"}
670
+
671
+ # 执行命令
672
+ import asyncio
673
+ import sys
668
674
 
669
- stdout_bytes, stderr_bytes = await process.communicate()
675
+ # 构建命令
676
+ cmd = []
677
+ # 尝试定位 nb 命令
678
+ import shutil
679
+ nb_path = shutil.which("nb")
670
680
 
671
- def safe_decode(data: bytes) -> str:
672
- if not data:
673
- return ""
674
- for encoding in ["utf-8", "gbk", "cp936"]:
675
- try:
676
- return data.decode(encoding).strip()
677
- except UnicodeDecodeError:
678
- continue
679
- return data.decode("utf-8", errors="replace").strip()
681
+ if not nb_path:
682
+ # 如果系统 PATH 中找不到,再尝试在 Python 脚本目录下找
683
+ script_dir = os.path.dirname(sys.executable)
684
+ possible_nb = os.path.join(script_dir, "nb.exe" if sys.platform == "win32" else "nb")
685
+ if os.path.exists(possible_nb):
686
+ nb_path = possible_nb
687
+ else:
688
+ nb_path = "nb" # 最后的保底,尝试直接运行 nb
680
689
 
681
- stdout = safe_decode(stdout_bytes)
682
- stderr = safe_decode(stderr_bytes)
683
-
684
- if process.returncode == 0:
685
- msg = f"插件 {plugin_name} {action} 成功"
686
- logger.info(msg)
687
- return {"msg": msg, "output": stdout}
690
+ # 获取项目根目录 (通常是当前工作目录)
691
+ root_dir = Path.cwd()
692
+
693
+ if action == "install":
694
+ cmd = [nb_path, "plugin", "install", plugin_name]
695
+ elif action == "update":
696
+ cmd = [nb_path, "plugin", "update", plugin_name]
697
+ elif action == "uninstall":
698
+ cmd = [nb_path, "plugin", "uninstall", plugin_name]
688
699
  else:
689
- error_msg = stderr or stdout
690
- logger.error(f"插件操作失败: {error_msg}")
691
- return {"error": error_msg}
692
-
693
- except Exception as e:
694
- logger.error(f"执行插件命令时发生异常: {e}")
695
- return {"error": str(e)}
700
+ return {"error": "无效操作"}
696
701
 
697
- except Exception as e:
698
- logger.error(f"执行命令异常: {e}")
699
- return {"error": str(e)}
700
-
701
- @app.post("/web_console/api/plugins/{plugin_id}/config", dependencies=[Depends(check_auth)])
702
- async def update_plugin_config(plugin_id: str, new_config: dict):
703
- # 这里仅做模拟保存逻辑,实际应用中通常需要修改 .env 文件或数据库
704
- # 由于直接修改运行中的 config 风险较大,此处返回成功,并提示需要重启
705
- logger.info(f"收到插件 {plugin_id} 的新配置: {new_config}")
706
- return {"success": True}
707
-
708
- # API 路由
709
- @app.get("/web_console/api/chats", dependencies=[Depends(check_auth)])
710
- async def get_chats():
711
- try:
712
- bot = get_bot()
713
- if not isinstance(bot, Bot):
714
- return {"error": "Only OneBot v11 is supported"}
702
+ logger.info(f"开始执行插件操作: {' '.join(cmd)} (工作目录: {root_dir})")
715
703
 
716
- groups = await bot.get_group_list()
717
- friends = await bot.get_friend_list()
704
+ try:
705
+ process = await asyncio.create_subprocess_exec(
706
+ *cmd,
707
+ stdout=asyncio.subprocess.PIPE,
708
+ stderr=asyncio.subprocess.PIPE,
709
+ cwd=str(root_dir)
710
+ )
711
+
712
+ stdout_bytes, stderr_bytes = await process.communicate()
713
+
714
+ def safe_decode(data: bytes) -> str:
715
+ if not data:
716
+ return ""
717
+ for encoding in ["utf-8", "gbk", "cp936"]:
718
+ try:
719
+ return data.decode(encoding).strip()
720
+ except UnicodeDecodeError:
721
+ continue
722
+ return data.decode("utf-8", errors="replace").strip()
723
+
724
+ stdout = safe_decode(stdout_bytes)
725
+ stderr = safe_decode(stderr_bytes)
726
+
727
+ if process.returncode == 0:
728
+ msg = f"插件 {plugin_name} {action} 成功"
729
+ logger.info(msg)
730
+ return {"msg": msg, "output": stdout}
731
+ else:
732
+ error_msg = stderr or stdout
733
+ logger.error(f"插件操作失败: {error_msg}")
734
+ return {"error": error_msg}
735
+
736
+ except Exception as e:
737
+ logger.error(f"执行插件命令时发生异常: {e}")
738
+ return {"error": str(e)}
739
+
740
+ @app.post("/web_console/api/plugins/{plugin_id}/config", dependencies=[Depends(check_auth)])
741
+ async def update_plugin_config(plugin_id: str, new_config: dict):
742
+ # 尝试更新 .env 文件
743
+ env_path = Path.cwd() / ".env"
744
+ # 简单查找逻辑
745
+ if not env_path.exists():
746
+ for name in [".env.prod", ".env.dev"]:
747
+ p = Path.cwd() / name
748
+ if p.exists():
749
+ env_path = p
750
+ break
718
751
 
719
- return {
720
- "groups": [
721
- {
722
- "id": f"group_{g['group_id']}",
723
- "name": g['group_name'],
724
- "avatar": f"https://p.qlogo.cn/gh/{g['group_id']}/{g['group_id']}/640"
725
- } for g in groups
726
- ],
727
- "private": [
728
- {
729
- "id": f"private_{f['user_id']}",
730
- "name": f['nickname'] or f['remark'] or str(f['user_id']),
731
- "avatar": f"https://q1.qlogo.cn/g?b=qq&nk={f['user_id']}&s=640"
732
- } for f in friends
733
- ]
734
- }
735
- except Exception as e:
736
- return {"error": str(e)}
752
+ try:
753
+ if env_path.exists():
754
+ content = env_path.read_text(encoding="utf-8")
755
+ lines = content.splitlines()
756
+ new_lines = []
757
+ keys_updated = set()
758
+
759
+ for line in lines:
760
+ line_strip = line.strip()
761
+ if not line_strip or line_strip.startswith("#"):
762
+ new_lines.append(line)
763
+ continue
764
+
765
+ if "=" in line:
766
+ key = line.split("=", 1)[0].strip()
767
+ if key in new_config:
768
+ val = new_config[key]
769
+ if isinstance(val, bool):
770
+ val_str = str(val).lower()
771
+ else:
772
+ val_str = str(val)
773
+ new_lines.append(f"{key}={val_str}")
774
+ keys_updated.add(key)
775
+ else:
776
+ new_lines.append(line)
777
+ else:
778
+ new_lines.append(line)
779
+
780
+ # 追加新配置
781
+ for key, val in new_config.items():
782
+ if key not in keys_updated:
783
+ if isinstance(val, bool):
784
+ val_str = str(val).lower()
785
+ else:
786
+ val_str = str(val)
787
+ new_lines.append(f"{key}={val_str}")
788
+
789
+ env_path.write_text("\n".join(new_lines), encoding="utf-8")
790
+ logger.info(f"已更新配置文件 {env_path}")
791
+ else:
792
+ logger.warning("未找到 .env 文件,无法持久化配置")
793
+
794
+ except Exception as e:
795
+ logger.error(f"保存配置失败: {e}")
796
+ return {"error": str(e)}
737
797
 
738
- @app.get("/web_console/api/history/{chat_id}", dependencies=[Depends(check_auth)])
739
- async def get_history(chat_id: str):
740
- return message_cache.get(chat_id, [])
798
+ logger.info(f"收到插件 {plugin_id} 的新配置: {new_config}")
799
+ return {"success": True, "msg": "配置已保存至 .env (需重启生效)"}
741
800
 
742
- @app.get("/web_console/proxy/image", dependencies=[Depends(check_auth)])
743
- async def proxy_image(url: str):
744
- url = unquote(url)
745
-
746
- # 处理 file:// 协议头 (Linux 下常见)
747
- if url.startswith("file://"):
748
- url = url.replace("file:///", "/").replace("file://", "")
749
- # Windows 下剥离开头的斜杠,例如 /C:/Users -> C:/Users
750
- if os.name == "nt" and url.startswith("/") and ":" in url:
751
- url = url.lstrip("/")
801
+ # API 路由
802
+ @app.get("/web_console/api/chats", dependencies=[Depends(check_auth)])
803
+ async def get_chats():
804
+ try:
805
+ from nonebot import get_bots
806
+ bots = get_bots()
807
+ if not bots:
808
+ return {"error": "No bot connected"}
809
+ bot = list(bots.values())[0]
810
+
811
+ if not isinstance(bot, Bot):
812
+ return {"error": "Only OneBot v11 is supported"}
813
+
814
+ groups = await bot.get_group_list()
815
+ friends = await bot.get_friend_list()
752
816
 
753
- if url.startswith("http"):
754
- # 尝试从缓存获取
755
- if url in image_cache:
756
- return Response(content=image_cache[url]["content"], media_type=image_cache[url]["type"])
817
+ return {
818
+ "groups": [
819
+ {
820
+ "id": f"group_{g['group_id']}",
821
+ "name": g['group_name'],
822
+ "avatar": f"https://p.qlogo.cn/gh/{g['group_id']}/{g['group_id']}/640"
823
+ } for g in groups
824
+ ],
825
+ "private": [
826
+ {
827
+ "id": f"private_{f['user_id']}",
828
+ "name": f['nickname'] or f['remark'] or str(f['user_id']),
829
+ "avatar": f"https://q1.qlogo.cn/g?b=qq&nk={f['user_id']}&s=640"
830
+ } for f in friends
831
+ ]
832
+ }
833
+ except Exception as e:
834
+ return {"error": str(e)}
835
+
836
+ @app.get("/web_console/api/history/{chat_id}", dependencies=[Depends(check_auth)])
837
+ async def get_history(chat_id: str):
838
+ return message_cache.get(chat_id, [])
839
+
840
+ @app.get("/web_console/proxy/image", dependencies=[Depends(check_auth)])
841
+ async def proxy_image(url: str):
842
+ url = unquote(url)
757
843
 
844
+ # 处理 file:// 协议头 (Linux 下常见)
845
+ if url.startswith("file://"):
846
+ url = url.replace("file:///", "/").replace("file://", "")
847
+ # 在 Windows 下剥离开头的斜杠,例如 /C:/Users -> C:/Users
848
+ if os.name == "nt" and url.startswith("/") and ":" in url:
849
+ url = url.lstrip("/")
850
+
851
+ if url.startswith("http"):
852
+ # 尝试从缓存获取
853
+ if url in image_cache:
854
+ return Response(content=image_cache[url]["content"], media_type=image_cache[url]["type"])
855
+
856
+ try:
857
+ async with httpx.AsyncClient() as client:
858
+ resp = await client.get(url, timeout=10.0, follow_redirects=True)
859
+ if resp.status_code == 200:
860
+ content = resp.content
861
+ media_type = resp.headers.get("content-type", "image/jpeg")
862
+ # 写入缓存
863
+ if len(image_cache) >= CACHE_SIZE:
864
+ image_cache.pop(next(iter(image_cache)))
865
+ image_cache[url] = {"content": content, "type": media_type}
866
+ return Response(content=content, media_type=media_type)
867
+ except Exception as e:
868
+ logger.error(f"代理图片下载失败: {e}")
869
+
870
+ # 尝试作为本地路径处理
758
871
  try:
759
- async with httpx.AsyncClient() as client:
760
- resp = await client.get(url, timeout=10.0, follow_redirects=True)
761
- if resp.status_code == 200:
762
- content = resp.content
763
- media_type = resp.headers.get("content-type", "image/jpeg")
764
- # 写入缓存
765
- if len(image_cache) >= CACHE_SIZE:
766
- image_cache.pop(next(iter(image_cache)))
767
- image_cache[url] = {"content": content, "type": media_type}
768
- return Response(content=content, media_type=media_type)
872
+ path = Path(url).resolve()
873
+ # 安全检查:只允许访问当前工作目录下的文件
874
+ if not str(path).startswith(str(Path.cwd())):
875
+ return Response(status_code=403)
876
+
877
+ if path.exists() and path.is_file():
878
+ return FileResponse(str(path))
769
879
  except Exception as e:
770
- logger.error(f"代理图片下载失败: {e}")
880
+ logger.error(f"本地图片读取失败: {e}")
771
881
 
772
- # 尝试作为本地路径处理
773
- try:
774
- path = Path(url)
775
- if path.exists() and path.is_file():
776
- return FileResponse(str(path))
777
- except Exception as e:
778
- logger.error(f"本地图片读取失败: {e}")
779
-
780
- return Response(status_code=404)
882
+ return Response(status_code=404)
781
883
 
782
- @app.post("/web_console/api/send", dependencies=[Depends(check_auth)])
783
- async def send_message(data: dict):
784
- try:
785
- bot = get_bot()
786
- chat_id = data.get("chat_id")
787
- content = data.get("content")
788
-
789
- if not chat_id or not content:
790
- return {"error": "Invalid data"}
791
-
792
- if chat_id.startswith("group_"):
793
- group_id = int(chat_id.replace("group_", ""))
794
- await bot.send_group_msg(group_id=group_id, message=content)
795
- else:
796
- user_id = int(chat_id.replace("private_", ""))
797
- await bot.send_private_msg(user_id=user_id, message=content)
884
+ @app.post("/web_console/api/send", dependencies=[Depends(check_auth)])
885
+ async def send_message(data: dict):
886
+ try:
887
+ from nonebot import get_bots
888
+ bots = get_bots()
889
+ if not bots:
890
+ return {"error": "No bot connected"}
891
+ bot = list(bots.values())[0]
892
+
893
+ chat_id = data.get("chat_id")
894
+ content = data.get("content")
798
895
 
799
- # 发送成功后手动添加一条自己的消息到缓存并推送
800
- my_msg = {
801
- "id": 0,
802
- "time": int(time.time()),
803
- "type": "group" if chat_id.startswith("group_") else "private",
804
- "sender_id": bot.self_id,
805
- "sender_name": "我",
806
- "sender_avatar": f"https://q1.qlogo.cn/g?b=qq&nk={bot.self_id}&s=640",
807
- "elements": [{"type": "text", "data": content}],
808
- "content": content,
809
- "is_self": True
810
- }
811
-
812
- if chat_id not in message_cache:
813
- message_cache[chat_id] = []
814
- message_cache[chat_id].append(my_msg)
815
-
816
- await broadcast_message({
817
- "type": "new_message",
818
- "chat_id": chat_id,
819
- "data": my_msg
820
- })
821
-
822
- return {"status": "ok"}
823
- except Exception as e:
824
- return {"error": str(e)}
825
-
826
- # WebSocket 端点
827
- @app.websocket("/web_console/ws")
828
- async def websocket_endpoint(websocket: WebSocket):
829
- await websocket.accept()
830
- active_connections.add(websocket)
831
- try:
832
- while True:
833
- # 保持连接,接收心跳或其他
834
- await websocket.receive_text()
835
- except WebSocketDisconnect:
836
- active_connections.remove(websocket)
837
- except Exception:
838
- if websocket in active_connections:
896
+ if not chat_id or not content:
897
+ return {"error": "Invalid data"}
898
+
899
+ if chat_id.startswith("group_"):
900
+ group_id = int(chat_id.replace("group_", ""))
901
+ await bot.send_group_msg(group_id=group_id, message=content)
902
+ else:
903
+ user_id = int(chat_id.replace("private_", ""))
904
+ await bot.send_private_msg(user_id=user_id, message=content)
905
+
906
+ # 发送成功后手动添加一条自己的消息到缓存并推送
907
+ my_msg = {
908
+ "id": 0,
909
+ "time": int(time.time()),
910
+ "type": "group" if chat_id.startswith("group_") else "private",
911
+ "sender_id": bot.self_id,
912
+ "sender_name": "我",
913
+ "sender_avatar": f"https://q1.qlogo.cn/g?b=qq&nk={bot.self_id}&s=640",
914
+ "elements": [{"type": "text", "data": content}],
915
+ "content": content,
916
+ "is_self": True
917
+ }
918
+
919
+ if chat_id not in message_cache:
920
+ message_cache[chat_id] = []
921
+ message_cache[chat_id].append(my_msg)
922
+
923
+ await broadcast_message({
924
+ "type": "new_message",
925
+ "chat_id": chat_id,
926
+ "data": my_msg
927
+ })
928
+
929
+ return {"status": "ok"}
930
+ except Exception as e:
931
+ return {"error": str(e)}
932
+
933
+ # WebSocket 端点
934
+ @app.websocket("/web_console/ws")
935
+ async def websocket_endpoint(websocket: WebSocket):
936
+ token = websocket.query_params.get("token")
937
+ if not token or not auth_manager.verify_token(token):
938
+ await websocket.close(code=1008)
939
+ return
940
+
941
+ await websocket.accept()
942
+ active_connections.add(websocket)
943
+ try:
944
+ while True:
945
+ # 保持连接,接收心跳或其他
946
+ await websocket.receive_text()
947
+ except WebSocketDisconnect:
839
948
  active_connections.remove(websocket)
840
-
841
- # 挂载静态文件
842
- app.mount("/web_console/static", StaticFiles(directory=str(static_path)), name="web_console_static")
843
-
844
- @app.get("/web_console")
845
- async def index():
846
- return FileResponse(static_path / "index.html")
949
+ except Exception:
950
+ if websocket in active_connections:
951
+ active_connections.remove(websocket)