From f165cf3e2e5c78c9194e9611b4a67d9ab2b8ed70 Mon Sep 17 00:00:00 2001 From: CKylinMC Date: Thu, 11 Sep 2025 18:35:33 +0800 Subject: [PATCH 1/2] feat: add napcat token verify --- main.py | 11 ++++++++++- src/config/official_configs.py | 3 +++ template/template_config.toml | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 424860d..66525af 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import asyncio import sys import json +import http import websockets as Server from src.logger import logger from src.recv_handler.message_handler import message_handler @@ -49,10 +50,18 @@ async def main(): message_send_instance.maibot_router = router _ = await asyncio.gather(napcat_server(), mmc_start_com(), message_process(), check_timeout_response()) +def check_napcat_server_token(conn, request): + token = global_config.napcat_server.token + if not token or token.strip() == "": + return None + auth_header = request.headers.get("Authorization") + if auth_header != f"Bearer {token}": + return Server.Response(http.HTTPStatus.UNAUTHORIZED, "", headers=Server.Headers([("Content-Type", "text/plain")]), body=b"Unauthorized\n") + return None async def napcat_server(): logger.info("正在启动adapter...") - async with Server.serve(message_recv, global_config.napcat_server.host, global_config.napcat_server.port, max_size=2**26) as server: + async with Server.serve(message_recv, global_config.napcat_server.host, global_config.napcat_server.port, max_size=2**26, process_request=check_napcat_server_token) as server: logger.info( f"Adapter已启动,监听地址: ws://{global_config.napcat_server.host}:{global_config.napcat_server.port}" ) diff --git a/src/config/official_configs.py b/src/config/official_configs.py index d652f35..98b4552 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -28,6 +28,9 @@ class NapcatServerConfig(ConfigBase): port: int = 8095 """Napcat服务端的端口号""" + token: str = "" + """Napcat服务端的访问令牌,若无则留空""" + heartbeat_interval: int = 30 """Napcat心跳间隔时间,单位为秒""" diff --git a/template/template_config.toml b/template/template_config.toml index 06ac657..84a7a12 100644 --- a/template/template_config.toml +++ b/template/template_config.toml @@ -8,6 +8,7 @@ nickname = "" [napcat_server] # Napcat连接的ws服务设置 host = "localhost" # Napcat设定的主机地址 port = 8095 # Napcat设定的端口 +token = "" # Napcat设定的访问令牌,若无则留空 heartbeat_interval = 30 # 与Napcat设置的心跳相同(按秒计) [maibot_server] # 连接麦麦的ws服务设置 From 8ac7d023620f61bfcd390e31d184c4fffc1cafe5 Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer <53261159+UnCLAS-Prommer@users.noreply.github.com> Date: Sat, 13 Sep 2025 13:42:44 +0800 Subject: [PATCH 2/2] Update main.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 66525af..6f824a6 100644 --- a/main.py +++ b/main.py @@ -56,7 +56,11 @@ def check_napcat_server_token(conn, request): return None auth_header = request.headers.get("Authorization") if auth_header != f"Bearer {token}": - return Server.Response(http.HTTPStatus.UNAUTHORIZED, "", headers=Server.Headers([("Content-Type", "text/plain")]), body=b"Unauthorized\n") + return Server.Response( + status=http.HTTPStatus.UNAUTHORIZED, + headers=Server.Headers([("Content-Type", "text/plain")]), + body=b"Unauthorized\n" + ) return None async def napcat_server():