TypeGPTProxy / main.py
AIMaster7's picture
Update main.py
b07bcdd verified
raw
history blame
1.27 kB
from fastapi import FastAPI, Request, Header, HTTPException
import httpx
app = FastAPI()
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
BASE_URL = "https://fast.typegpt.net"
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
@app.api_route("/{path:path}", methods=["GET", "POST"])
async def proxy(request: Request, path: str, authorization: str = Header(None)):
# Remove 'Bearer ' prefix if present
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
token = authorization.replace("Bearer ", "").strip()
# Check if it's the correct public token
if token != PUBLIC_AUTH_TOKEN:
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
target_url = f"{BASE_URL}/{path}"
headers = dict(request.headers)
headers["Authorization"] = f"Bearer {REAL_API_KEY}"
headers.pop("host", None)
body = await request.body()
async with httpx.AsyncClient() as client:
response = await client.request(
method=request.method,
url=target_url,
content=body,
headers=headers,
)
return response.json()