Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -5,23 +5,23 @@ import socket
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
#
|
9 |
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
10 |
BASE_URL = "https://fast.typegpt.net"
|
11 |
|
12 |
-
# Public
|
13 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
14 |
|
15 |
-
#
|
16 |
@app.on_event("startup")
|
17 |
async def startup_event():
|
18 |
try:
|
19 |
hostname = socket.gethostname()
|
20 |
server_ip = socket.gethostbyname(hostname)
|
21 |
-
print(
|
22 |
print(f"📡 Server IP: {server_ip}")
|
23 |
except Exception as e:
|
24 |
-
print(f"⚠️ Could not
|
25 |
|
26 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
27 |
async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
@@ -30,40 +30,43 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
|
30 |
raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
|
31 |
|
32 |
token = authorization.replace("Bearer ", "").strip()
|
33 |
-
|
34 |
if token != PUBLIC_AUTH_TOKEN:
|
35 |
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
|
36 |
|
37 |
-
#
|
38 |
target_url = f"{BASE_URL}/{path}"
|
39 |
|
40 |
-
#
|
41 |
-
headers =
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
-
# Read
|
46 |
body = await request.body()
|
47 |
|
48 |
-
#
|
|
|
|
|
|
|
49 |
async with httpx.AsyncClient() as client:
|
50 |
try:
|
51 |
response = await client.request(
|
52 |
method=request.method,
|
53 |
url=target_url,
|
54 |
-
content=body,
|
55 |
headers=headers,
|
|
|
56 |
timeout=60
|
57 |
)
|
58 |
except httpx.RequestError as e:
|
59 |
-
raise HTTPException(status_code=502, detail=f"
|
60 |
|
61 |
-
# Debug log
|
62 |
-
print(f"🔄 Forwarded to: {target_url}")
|
63 |
print(f"↩️ TypeGPT Status: {response.status_code}")
|
64 |
-
print(f"🧾
|
65 |
|
66 |
-
#
|
67 |
try:
|
68 |
return JSONResponse(content=response.json(), status_code=response.status_code)
|
69 |
except Exception:
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Real secret API key (used internally, never exposed)
|
9 |
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
10 |
BASE_URL = "https://fast.typegpt.net"
|
11 |
|
12 |
+
# Public key that users must use in Authorization header
|
13 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
14 |
|
15 |
+
# Show server IP at startup
|
16 |
@app.on_event("startup")
|
17 |
async def startup_event():
|
18 |
try:
|
19 |
hostname = socket.gethostname()
|
20 |
server_ip = socket.gethostbyname(hostname)
|
21 |
+
print("===== Server Started =====")
|
22 |
print(f"📡 Server IP: {server_ip}")
|
23 |
except Exception as e:
|
24 |
+
print(f"⚠️ Could not determine server IP: {e}")
|
25 |
|
26 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
27 |
async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
|
|
30 |
raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
|
31 |
|
32 |
token = authorization.replace("Bearer ", "").strip()
|
|
|
33 |
if token != PUBLIC_AUTH_TOKEN:
|
34 |
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
|
35 |
|
36 |
+
# Rebuild the target URL
|
37 |
target_url = f"{BASE_URL}/{path}"
|
38 |
|
39 |
+
# Use clean headers to avoid Cloudflare issues
|
40 |
+
headers = {
|
41 |
+
"Authorization": f"Bearer {REAL_API_KEY}",
|
42 |
+
"Content-Type": request.headers.get("content-type", "application/json"),
|
43 |
+
"Accept": "application/json",
|
44 |
+
"User-Agent": "FastAPI-Proxy"
|
45 |
+
}
|
46 |
|
47 |
+
# Read the body
|
48 |
body = await request.body()
|
49 |
|
50 |
+
# Log for debugging
|
51 |
+
print(f"🔄 Forwarding to: {target_url}")
|
52 |
+
print(f"📦 Request Body (first 200 chars): {body[:200]}")
|
53 |
+
|
54 |
async with httpx.AsyncClient() as client:
|
55 |
try:
|
56 |
response = await client.request(
|
57 |
method=request.method,
|
58 |
url=target_url,
|
|
|
59 |
headers=headers,
|
60 |
+
content=body,
|
61 |
timeout=60
|
62 |
)
|
63 |
except httpx.RequestError as e:
|
64 |
+
raise HTTPException(status_code=502, detail=f"Failed to reach backend: {e}")
|
65 |
|
|
|
|
|
66 |
print(f"↩️ TypeGPT Status: {response.status_code}")
|
67 |
+
print(f"🧾 Response Snippet: {response.text[:200]}")
|
68 |
|
69 |
+
# Return JSON if possible, else fallback
|
70 |
try:
|
71 |
return JSONResponse(content=response.json(), status_code=response.status_code)
|
72 |
except Exception:
|