AIMaster7 commited on
Commit
b07bcdd
·
verified ·
1 Parent(s): 5a86b8e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -11
main.py CHANGED
@@ -3,31 +3,30 @@ import httpx
3
 
4
  app = FastAPI()
5
 
6
- # Internal secret key (never exposed to users)
7
  REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
8
  BASE_URL = "https://fast.typegpt.net"
9
-
10
- # Public API key that users must send in the Authorization header
11
  PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
12
 
13
  @app.api_route("/{path:path}", methods=["GET", "POST"])
14
  async def proxy(request: Request, path: str, authorization: str = Header(None)):
15
- # Validate public access token
16
- if authorization != PUBLIC_AUTH_TOKEN:
 
 
 
 
 
 
17
  raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
18
 
19
- # Construct the target URL
20
  target_url = f"{BASE_URL}/{path}"
21
 
22
- # Copy headers from request and replace Authorization with real key
23
  headers = dict(request.headers)
24
  headers["Authorization"] = f"Bearer {REAL_API_KEY}"
25
- headers.pop("host", None) # remove 'host' header if present
26
 
27
- # Get the request body
28
  body = await request.body()
29
 
30
- # Send request to backend (TypeGPT)
31
  async with httpx.AsyncClient() as client:
32
  response = await client.request(
33
  method=request.method,
@@ -36,5 +35,4 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
36
  headers=headers,
37
  )
38
 
39
- # Return JSON response from TypeGPT
40
  return response.json()
 
3
 
4
  app = FastAPI()
5
 
 
6
  REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
7
  BASE_URL = "https://fast.typegpt.net"
 
 
8
  PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
9
 
10
  @app.api_route("/{path:path}", methods=["GET", "POST"])
11
  async def proxy(request: Request, path: str, authorization: str = Header(None)):
12
+ # Remove 'Bearer ' prefix if present
13
+ if not authorization or not authorization.startswith("Bearer "):
14
+ raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
15
+
16
+ token = authorization.replace("Bearer ", "").strip()
17
+
18
+ # Check if it's the correct public token
19
+ if token != PUBLIC_AUTH_TOKEN:
20
  raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
21
 
 
22
  target_url = f"{BASE_URL}/{path}"
23
 
 
24
  headers = dict(request.headers)
25
  headers["Authorization"] = f"Bearer {REAL_API_KEY}"
26
+ headers.pop("host", None)
27
 
 
28
  body = await request.body()
29
 
 
30
  async with httpx.AsyncClient() as client:
31
  response = await client.request(
32
  method=request.method,
 
35
  headers=headers,
36
  )
37
 
 
38
  return response.json()