significant refactor/cleanup

This commit is contained in:
2025-02-11 20:01:07 -05:00
parent 2c368aaf1a
commit 88d870ce8f
12 changed files with 440 additions and 467 deletions

View File

@@ -1,21 +1,22 @@
#!/usr/bin/env python3.12
# pylint: disable=invalid-name
import logging
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from aiohttp import ClientSession, ClientTimeout
from .constructors import ValidXCRequest
# pylint: disable=invalid-name
class XC(FastAPI):
"""XC (CrossComm) Endpoints"""
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
def __init__(self, app: FastAPI, util, constants, glob_state) -> None: # pylint: disable=super-init-not-called
self.app = app
self.util = util
self.constants = constants
self.glob_state = glob_state
self.endpoints = {
self.endpoints: dict = {
"xc": self.xc_handler,
}
@@ -23,25 +24,18 @@ class XC(FastAPI):
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
include_in_schema=False)
# async def put_ws_handler(self, ws: WebSocket):
# await ws.accept()
# await self.audio_streamer.handle_client(ws)
async def xc_handler(self, data: ValidXCRequest, request: Request):
"""
/xc
Handle XC Commands
"""
async def xc_handler(self, data: ValidXCRequest, request: Request) -> dict:
"""Handle XC Commands"""
try:
key = data.key
bid = data.bid
cmd = data.cmd
cmd_data = data.data
key: str = data.key
bid: int = data.bid
cmd: str = data.cmd
cmd_data: dict = data.data
if not self.util.check_key(path=request.url.path, req_type=0, key=key):
raise HTTPException(status_code=403, detail="Unauthorized")
BID_ADDR_MAP = {
BID_ADDR_MAP: dict = {
0: '10.10.10.101:5991', # Patrick (a.k.a. Thomas a.k.a. Aces)
# TODO: add Havoc?
}
@@ -52,15 +46,15 @@ class XC(FastAPI):
'errorText': 'Invalid bot id'
}
bot_api_url = f'http://{BID_ADDR_MAP[bid]}/'
bot_api_url: str = f'http://{BID_ADDR_MAP[bid]}/'
async with ClientSession() as session:
async with await session.post(f"{bot_api_url}{cmd}", json=cmd_data, headers={
'Content-Type': 'application/json; charset=utf-8'
}, timeout=ClientTimeout(connect=5, sock_read=5)) as request:
response = await request.json()
response: dict = await request.json()
return {
'success': True,
'response': response
}
except:
pass
except Exception as e:
logging.debug("Error: %s", str(e))