From 7042d94213532191a0c72d34d7c85193184c079f Mon Sep 17 00:00:00 2001 From: Kyomotoi <kyomotoiowo@gmail.com> Date: Wed, 4 May 2022 16:07:18 +0800 Subject: =?UTF-8?q?=E2=9C=A8=20=E4=B8=BA=E5=89=8D=E7=AB=AF=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=89=93=E5=9F=BA=E7=A1=80,=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=8F=8AAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/plugins/status/driver/__init__.py | 41 ++++++++++++++++++++++++++++++++++ ATRI/plugins/status/driver/view.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 ATRI/plugins/status/driver/__init__.py create mode 100644 ATRI/plugins/status/driver/view.py (limited to 'ATRI/plugins/status/driver') diff --git a/ATRI/plugins/status/driver/__init__.py b/ATRI/plugins/status/driver/__init__.py new file mode 100644 index 0000000..199fb1e --- /dev/null +++ b/ATRI/plugins/status/driver/__init__.py @@ -0,0 +1,41 @@ +from nonebot import get_driver +from nonebot.drivers.fastapi import Driver + +from fastapi.middleware.cors import CORSMiddleware + +from .view import ( + handle_auther, + handle_base_uri, + handle_runtime_info, + handle_message_deal_info, +) + + +CONSOLE_API_URI = "/capi" # base point +CONSOLE_API_AUTH_URI = "/capi/auth" # 验证后台许可 +CONSOLE_API_RUNTIME_URI = "/capi/runtime" # 获取运行占用信息 +CONSOLE_API_MESSAGE_URI = "/capi/message" +# CONSOLE_API_AUTH_COOKIES_URI = "/capi/auth/cookies" # 验证cookies + + +def register_routes(driver: Driver): + app = driver.server_app + + origins = ["*"] + app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) + + app.get(CONSOLE_API_URI)(handle_base_uri) + app.get(CONSOLE_API_RUNTIME_URI)(handle_runtime_info) + app.get(CONSOLE_API_MESSAGE_URI)(handle_message_deal_info) + app.get(CONSOLE_API_AUTH_URI)(handle_auther) + + +def init(): + driver = get_driver() + register_routes(driver) # type: ignore diff --git a/ATRI/plugins/status/driver/view.py b/ATRI/plugins/status/driver/view.py new file mode 100644 index 0000000..5d8c56f --- /dev/null +++ b/ATRI/plugins/status/driver/view.py @@ -0,0 +1,39 @@ +from ..data_source import Status +from ..listener import get_message_deal_info + + +def handle_base_uri(): + return {"status": 204, "msg": "This path just for console load."} + + +def handle_runtime_info(token: str): + auth, data = auth_token(token) + plat, bot = Status().get_status(True) + if auth: + return {"status": 200, "data": {"platform": plat, "bot": bot}} + else: + return data + + +def handle_message_deal_info(token: str): + auth, data = auth_token(token) + if auth: + return {"status": 200, "data": get_message_deal_info()} + else: + return data + + +def handle_auther(token: str): + auth, data = auth_token(token) + return data if auth else data + + +def auth_token(token: str) -> tuple: + auth_data: dict = Status().get_auth_info() + if not auth_data.get("token", None): + return False, {"status": 500, "msg": "This bot is not create auth data yet."} + _token = auth_data["token"] + if token != _token: + return False, {"status": 403, "msg": "Token error, please check again."} + else: + return True, {"status": 200, "msg": "OK"} -- cgit v1.2.3