2025-02-11 20:01:07 -05:00
|
|
|
import logging
|
2025-02-14 16:07:24 -05:00
|
|
|
from typing import Optional
|
2025-01-11 20:59:10 -05:00
|
|
|
from fastapi import FastAPI, Request, HTTPException
|
2025-02-15 21:09:33 -05:00
|
|
|
from fastapi.responses import JSONResponse
|
2024-08-19 11:42:23 -04:00
|
|
|
from aiohttp import ClientSession, ClientTimeout
|
2025-02-11 11:19:52 -05:00
|
|
|
from .constructors import ValidXCRequest
|
2024-11-29 15:33:12 -05:00
|
|
|
|
2024-08-19 11:42:23 -04:00
|
|
|
class XC(FastAPI):
|
|
|
|
"""XC (CrossComm) Endpoints"""
|
2025-02-16 08:50:53 -05:00
|
|
|
def __init__(self, app: FastAPI, util, constants) -> None:
|
2025-02-15 21:09:33 -05:00
|
|
|
self.app: FastAPI = app
|
2024-08-19 11:42:23 -04:00
|
|
|
self.util = util
|
|
|
|
self.constants = constants
|
2024-11-29 15:33:12 -05:00
|
|
|
|
2025-02-11 20:01:07 -05:00
|
|
|
self.endpoints: dict = {
|
2024-08-19 11:42:23 -04:00
|
|
|
"xc": self.xc_handler,
|
|
|
|
}
|
2024-11-29 15:33:12 -05:00
|
|
|
|
2024-08-19 11:42:23 -04:00
|
|
|
for endpoint, handler in self.endpoints.items():
|
2025-01-29 15:48:47 -05:00
|
|
|
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
|
|
|
|
include_in_schema=False)
|
2024-11-29 15:33:12 -05:00
|
|
|
|
2025-02-15 21:09:33 -05:00
|
|
|
async def xc_handler(self, data: ValidXCRequest,
|
|
|
|
request: Request) -> JSONResponse:
|
2025-02-11 20:01:07 -05:00
|
|
|
"""Handle XC Commands"""
|
2024-08-19 11:42:23 -04:00
|
|
|
|
2025-02-05 20:23:06 -05:00
|
|
|
try:
|
2025-02-11 20:01:07 -05:00
|
|
|
key: str = data.key
|
2025-02-15 21:09:33 -05:00
|
|
|
bid: int = int(data.bid)
|
2025-02-11 20:01:07 -05:00
|
|
|
cmd: str = data.cmd
|
2025-02-14 16:07:24 -05:00
|
|
|
cmd_data: Optional[dict] = data.data
|
2025-02-05 20:25:28 -05:00
|
|
|
if not self.util.check_key(path=request.url.path, req_type=0, key=key):
|
2025-02-05 20:23:06 -05:00
|
|
|
raise HTTPException(status_code=403, detail="Unauthorized")
|
|
|
|
|
2025-02-11 20:01:07 -05:00
|
|
|
BID_ADDR_MAP: dict = {
|
2025-02-26 21:03:10 -05:00
|
|
|
# TODO: add Havoc?
|
2025-02-05 20:23:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if not bid in BID_ADDR_MAP:
|
2025-02-15 21:09:33 -05:00
|
|
|
return JSONResponse(status_code=500, content={
|
2025-02-05 20:23:06 -05:00
|
|
|
'err': True,
|
|
|
|
'errorText': 'Invalid bot id'
|
2025-02-15 21:09:33 -05:00
|
|
|
})
|
2025-02-05 20:23:06 -05:00
|
|
|
|
2025-02-11 20:01:07 -05:00
|
|
|
bot_api_url: str = f'http://{BID_ADDR_MAP[bid]}/'
|
2025-02-05 20:23:06 -05:00
|
|
|
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'
|
2025-02-14 16:07:24 -05:00
|
|
|
}, timeout=ClientTimeout(connect=5, sock_read=5)) as aiohttp_request:
|
|
|
|
response: dict = await aiohttp_request.json()
|
2025-02-15 21:09:33 -05:00
|
|
|
return JSONResponse(content={
|
2025-02-05 20:23:06 -05:00
|
|
|
'success': True,
|
|
|
|
'response': response
|
2025-02-15 21:09:33 -05:00
|
|
|
})
|
2025-02-11 20:01:07 -05:00
|
|
|
except Exception as e:
|
2025-02-14 16:07:24 -05:00
|
|
|
logging.debug("Error: %s", str(e))
|
2025-02-15 21:09:33 -05:00
|
|
|
return JSONResponse(status_code=500, content={
|
2025-02-14 16:07:24 -05:00
|
|
|
'err': True,
|
|
|
|
'errorText': 'General error.',
|
2025-02-15 21:09:33 -05:00
|
|
|
})
|