diff --git a/model.py b/model.py index 30f4962..0178c13 100644 --- a/model.py +++ b/model.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Optional, List, Union, Literal from lib_not_dr.types import Options +from pydantic import BaseModel from socketio import AsyncClient @@ -50,11 +51,15 @@ class Message(Options): } -@dataclass -class SioConfig: - HOST: str - KEY: str - SELF_ID: str +class SioConfig(BaseModel): + host: str + key: str + self_id: str + admin: List[int] + validate_http: str + client_id: str + client_secret: str + @dataclass diff --git a/server.py b/server.py index 5b409da..9bfb228 100644 --- a/server.py +++ b/server.py @@ -18,7 +18,7 @@ app = Sanic('GiteaPush', ctx=Ctx) def get_config() -> SioConfig: with open('config.toml', 'rb') as f: config = tomllib.load(f) - return SioConfig(config['host'], config['private_key'], config['self_id']) + return SioConfig(**config) @app.before_server_start @@ -28,7 +28,7 @@ async def setup_before_start(_app): start_sio_listener() - await _app.ctx.sio.connect(_app.ctx.sio_config.HOST) + await _app.ctx.sio.connect(_app.ctx.sio_config.host) @app.post('/receive') @@ -85,7 +85,7 @@ def start_sio_listener(): logger.info( f"{Colors.BLUE}versions: {Colors.PURPLE}{versions} {Colors.BLUE}with type {type(salt)}|{salt=}{Colors.END}") # 准备数据 - sign = SigningKey(bytes.fromhex(app.ctx.sio_config.KEY)) + sign = SigningKey(bytes.fromhex(app.ctx.sio_config.key)) signature = sign.sign(bytes.fromhex(salt)) # 发送数据 @@ -154,13 +154,8 @@ def start_sio_listener(): logger.debug(sio_log_format('add_message:', data)) sender_id = data['message']['senderId'] - is_self = (sender_id == app.ctx.sio_config.SELF_ID) - sender_name = data['message']['username'] - content = data['message']['content'] - room_id = data['roomId'] - - if not is_self: + if sender_id != app.ctx.sio_config.self_id: msg = command_convert(data) if msg is not None: await app.ctx.sio.emit('sendMessage', msg.to_json())