mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
still bugs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from nonebot import get_driver, on_command
|
||||
@@ -11,25 +12,49 @@ from nonebot.log import logger
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
import socketio
|
||||
import functools
|
||||
|
||||
from .api import test, get_global_conf, auth
|
||||
from .api import test, get_global_conf, auth, get_subs_info, get_target_name
|
||||
from .token_manager import token_manager as tm
|
||||
from .jwt import load_jwt
|
||||
from ..plugin_config import plugin_config
|
||||
|
||||
URL_BASE = '/hk_reporter/'
|
||||
GLOBAL_CONF_URL = f'{URL_BASE}api/global_conf'
|
||||
AUTH_URL = f'{URL_BASE}api/auth'
|
||||
SUBSCRIBE_URL = f'{URL_BASE}api/subs'
|
||||
GET_TARGET_NAME_URL = f'{URL_BASE}api/target_name'
|
||||
TEST_URL = f'{URL_BASE}test'
|
||||
|
||||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
|
||||
socket_app = socketio.ASGIApp(sio, socketio_path="socket")
|
||||
|
||||
|
||||
def register_router_fastapi(driver: Driver, socketio):
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from fastapi.param_functions import Depends
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
oath_scheme = OAuth2PasswordBearer(tokenUrl='token')
|
||||
|
||||
async def get_jwt_obj(token: str = Depends(oath_scheme)):
|
||||
obj = load_jwt(token)
|
||||
if not obj:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
||||
return obj
|
||||
|
||||
app = driver.server_app
|
||||
static_path = str((Path(__file__).parent / "dist").resolve())
|
||||
app.get(TEST_URL)(test)
|
||||
app.get(GLOBAL_CONF_URL)(get_global_conf)
|
||||
app.get(AUTH_URL)(auth)
|
||||
|
||||
@app.get(SUBSCRIBE_URL)
|
||||
async def subs(jwt_obj: dict = Depends(get_jwt_obj)):
|
||||
return await get_subs_info(jwt_obj)
|
||||
@app.get(GET_TARGET_NAME_URL)
|
||||
async def _get_target_name(platformName: str, target: str, jwt_obj: dict = Depends(get_jwt_obj)):
|
||||
return await get_target_name(platformName, target, jwt_obj)
|
||||
app.mount(URL_BASE, StaticFiles(directory=static_path, html=True), name="hk_reporter")
|
||||
|
||||
def init():
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from ..platform import platform_manager
|
||||
from ..platform import platform_manager, check_sub_target
|
||||
from .token_manager import token_manager
|
||||
from .jwt import pack_jwt
|
||||
from ..config import Config
|
||||
@@ -9,15 +9,15 @@ async def test():
|
||||
return {"status": 200, "text": "test"}
|
||||
|
||||
async def get_global_conf():
|
||||
res = []
|
||||
res = {}
|
||||
for platform_name, platform in platform_manager.items():
|
||||
res.append({
|
||||
res[platform_name] = {
|
||||
'platformName': platform_name,
|
||||
'categories': platform.categories,
|
||||
'enabledTag': platform.enable_tag,
|
||||
'name': platform.name,
|
||||
'hasTarget': getattr(platform, 'has_target')
|
||||
})
|
||||
}
|
||||
return { 'platformConf': res }
|
||||
|
||||
|
||||
@@ -52,5 +52,14 @@ async def get_subs_info(jwt_obj: dict):
|
||||
for group in groups:
|
||||
group_id = group['id']
|
||||
config = Config()
|
||||
|
||||
subs = list(map(lambda sub: {'targetType': sub['target_type'], 'target': sub['target'], 'targetName': sub['target_name'], 'cats': sub['cats'], 'tags': sub['tags']},
|
||||
subs = list(map(lambda sub: {
|
||||
'targetType': sub['target_type'], 'target': sub['target'], 'targetName': sub['target_name'], 'cats': sub['cats'], 'tags': sub['tags']
|
||||
}, config.list_subscribe(group_id, 'group')))
|
||||
res[group_id] = {
|
||||
'name': group['name'],
|
||||
'subscribes': subs
|
||||
}
|
||||
return res
|
||||
|
||||
async def get_target_name(platform_name: str, target: str, jwt_obj: dict):
|
||||
return {'targetName': await check_sub_target(platform_name, target)}
|
||||
|
||||
@@ -14,7 +14,7 @@ def pack_jwt(obj: dict) -> str:
|
||||
|
||||
def load_jwt(token: str) -> Optional[dict]:
|
||||
try:
|
||||
return jwt.decode(token, _key, algorithm='HS256')
|
||||
return jwt.decode(token, _key, algorithms=['HS256'])
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user