Spaces:
Sleeping
Sleeping
| import re | |
| from fastapi import FastAPI, Request, HTTPException | |
| from fastapi.responses import JSONResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from slowapi import Limiter | |
| from slowapi.util import get_remote_address | |
| from slowapi.errors import RateLimitExceeded | |
| # Importamos los mΓ³dulos de tus endpoints originales | |
| from app.api.endpoints import session, attack | |
| # Inicializamos el limiter basado en IP | |
| limiter = Limiter(key_func=get_remote_address) | |
| app = FastAPI( | |
| title="DECI - Vertex Coders Core", | |
| version="0.1.0-sprint1", | |
| description="PoH Engine β Decentralized Cognitive Identity" | |
| ) | |
| # Acoplamos el limiter a la aplicaciΓ³n | |
| app.state.limiter = limiter | |
| # Manejador global de exceso de peticiones (429) | |
| async def custom_rate_limit_handler(request: Request, exc: RateLimitExceeded): | |
| return JSONResponse( | |
| status_code=429, | |
| content={ | |
| "detail": "Too many requests. Vertex Security rate-limit triggered.", | |
| "retry_after": exc.detail | |
| } | |
| ) | |
| # ββ CONFIGURACIΓN DE CORS βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ββ MIDDLEWARE DE DEFENSA POLΓGLOTA (Vertex Security Layer) ββββββββββββββββββ | |
| async def polyglot_defense_middleware(request: Request, call_next): | |
| suspicious_patterns = [r"ostende", r"secretum", r"0x[0-9a-fA-F]+", r"----"] | |
| if request.method == "POST" and "session" in request.url.path: | |
| body = await request.body() | |
| content = body.decode().lower() | |
| for pattern in suspicious_patterns: | |
| if re.search(pattern, content): | |
| print(f"π¨ [DEFENSE] Vertex Security bloqueΓ³ patrΓ³n: {pattern}") | |
| raise HTTPException(status_code=403, detail="Vertex Security: Pattern Blocked") | |
| async def receive(): | |
| return {"type": "http.request", "body": body} | |
| request._receive = receive | |
| return await call_next(request) | |
| # ββ REGISTRO DE ROUTERS LIMPIOS ββββββββββββββββββββββββββββββββββββββββββββββ | |
| app.include_router(session.router, prefix="/session", tags=["Session"]) | |
| app.include_router(attack.router, prefix="/attack", tags=["Attack"]) | |
| # ββ ENDPOINTS DE CONTROL ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def root(): | |
| return { | |
| "service": "DECI PoH Engine", | |
| "version": "0.1.0-sprint1", | |
| "status": "operational", | |
| "company": "Vertex Coders LLC" | |
| } | |
| async def health(): | |
| return { | |
| "api": "ok", | |
| "vault": "ok", | |
| "shadow_mode": True | |
| } |