File size: 1,359 Bytes
08a3216
 
 
 
 
5a86b8e
 
08a3216
 
5a86b8e
 
08a3216
 
5a86b8e
 
 
 
08a3216
5a86b8e
08a3216
 
5a86b8e
08a3216
5a86b8e
 
08a3216
5a86b8e
08a3216
 
5a86b8e
08a3216
 
5a86b8e
 
08a3216
5a86b8e
08a3216
 
5a86b8e
08a3216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, Request, Header, HTTPException
import httpx

app = FastAPI()

# Internal secret key (never exposed to users)
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
BASE_URL = "https://fast.typegpt.net"

# Public API key that users must send in the Authorization header
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"

@app.api_route("/{path:path}", methods=["GET", "POST"])
async def proxy(request: Request, path: str, authorization: str = Header(None)):
    # Validate public access token
    if authorization != PUBLIC_AUTH_TOKEN:
        raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")

    # Construct the target URL
    target_url = f"{BASE_URL}/{path}"

    # Copy headers from request and replace Authorization with real key
    headers = dict(request.headers)
    headers["Authorization"] = f"Bearer {REAL_API_KEY}"
    headers.pop("host", None)  # remove 'host' header if present

    # Get the request body
    body = await request.body()

    # Send request to backend (TypeGPT)
    async with httpx.AsyncClient() as client:
        response = await client.request(
            method=request.method,
            url=target_url,
            content=body,
            headers=headers,
        )

    # Return JSON response from TypeGPT
    return response.json()