feat: verify the token

pull/57/head
CKylinMC 2025-09-10 18:10:19 +08:00
parent bc13a26127
commit 074272c3b3
1 changed files with 9 additions and 1 deletions

10
main.py
View File

@ -1,6 +1,7 @@
import asyncio import asyncio
import sys import sys
import json import json
import http
import websockets as Server import websockets as Server
from src.logger import logger from src.logger import logger
from src.recv_handler.message_handler import message_handler from src.recv_handler.message_handler import message_handler
@ -49,10 +50,17 @@ async def main():
message_send_instance.maibot_router = router message_send_instance.maibot_router = router
_ = await asyncio.gather(napcat_server(), mmc_start_com(), message_process(), check_timeout_response()) _ = await asyncio.gather(napcat_server(), mmc_start_com(), message_process(), check_timeout_response())
def check_napcat_server_token(path, request_headers):
token = global_config.napcat_server.token
if not token or token.strip() == "":
return
auth_header = request_headers.get("Authorization")
if auth_header != f"Bearer {token}":
return http.HTTPStatus.UNAUTHORIZED, [], b"Unauthorized\n"
async def napcat_server(): async def napcat_server():
logger.info("正在启动adapter...") 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_headers=check_napcat_server_token) as server:
logger.info( logger.info(
f"Adapter已启动监听地址: ws://{global_config.napcat_server.host}:{global_config.napcat_server.port}" f"Adapter已启动监听地址: ws://{global_config.napcat_server.host}:{global_config.napcat_server.port}"
) )