from fastapi import FastAPI, Request, Header, HTTPException from fastapi.responses import JSONResponse, Response import httpx import socket app = FastAPI() # Real secret API key (used internally, never exposed) REAL_API_KEY = "sk-94NDKhKQkhKoYnY65mg4NAIFK5BqNiCtxo8u3PsDpb0IucZt" BASE_URL = "https://fast.typegpt.net" # Public key that users must use in Authorization header PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL" # Show server IP at startup @app.on_event("startup") async def startup_event(): try: hostname = socket.gethostname() server_ip = socket.gethostbyname(hostname) print("===== Server Started =====") print(f"๐Ÿ“ก Server IP: {server_ip}") except Exception as e: print(f"โš ๏ธ Could not determine server IP: {e}") @app.api_route("/{path:path}", methods=["GET", "POST"]) async def proxy(request: Request, path: str, authorization: str = Header(None)): # Validate Authorization token if not authorization or not authorization.startswith("Bearer "): raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.") token = authorization.replace("Bearer ", "").strip() if token != PUBLIC_AUTH_TOKEN: raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.") # Rebuild the target URL target_url = f"{BASE_URL}/{path}" # Use clean headers to avoid Cloudflare issues headers = { "Authorization": f"Bearer {REAL_API_KEY}", "Content-Type": request.headers.get("content-type", "application/json"), "Accept": "application/json", "User-Agent": "FastAPI-Proxy" } # Read the body body = await request.body() # Log for debugging print(f"๐Ÿ”„ Forwarding to: {target_url}") print(f"๐Ÿ“ฆ Request Body (first 200 chars): {body[:200]}") async with httpx.AsyncClient() as client: try: response = await client.request( method=request.method, url=target_url, headers=headers, content=body, timeout=60 ) except httpx.RequestError as e: raise HTTPException(status_code=502, detail=f"Failed to reach backend: {e}") print(f"โ†ฉ๏ธ TypeGPT Status: {response.status_code}") print(f"๐Ÿงพ Response Snippet: {response.text[:200]}") # Return JSON if possible, else fallback try: return JSONResponse(content=response.json(), status_code=response.status_code) except Exception: return Response( content=response.content, status_code=response.status_code, media_type=response.headers.get("content-type", "text/plain") )