from fastapi import FastAPI from fastapi.requests import Request from fastapi.responses import Response, JSONResponse from fastapi.exceptions import HTTPException class WebExtension: app: FastAPI def __init__(self, core): self.core = core self.app = core.app self.dbService = core.dbService @self.app.get(f"{self.core.context}/ping") async def pong(request:Request): return {"pong": True} @self.app.get(f"{self.core.context}/status") async def status(request:Request): return { "status": self.core.meshState, "last_packet_catch": self.core.last_packet_catch } @self.app.get(f"{self.core.context}/auth/code") async def sendCodeToNode(request:Request, num: int): #в сообщение одноразовый код и юзер агент кто слал, так-же чтобы не спапили по фингерпринту один код в минуту if num: node = await self.dbService.oneNode(num) if node: code = self.core.authManager.request_auth(num) await self.core.device.sendMsgToDM(f"Auth code: {code}", num) return {"status": f"Code sended to {node['long_name']}"} raise HTTPException(status_code=400) @self.app.get(f"{self.core.context}/auth/code/check") async def acceptCodeFromNode(request:Request, code: int): #если такой код есть то авторизуем пользователя, иначе шлем н***й if code: try: num = self.core.authManager.accept_code(code) respond = self.core.authManager.setAuth(JSONResponse({"status":True}), num) return respond except: return {"status": False} return {"status": False} @self.app.get(f"{self.core.context}/auth/logout") async def clearSession(request:Request): return self.core.authManager.setAuth(JSONResponse({"status":True}), 0, True)