Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import asyncio
|
| 3 |
import aiohttp
|
| 4 |
-
import
|
| 5 |
from fastapi import FastAPI, Request, Response
|
| 6 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 7 |
|
| 8 |
TARGET_BASE_URL = "https://api.deepinfra.com/v1/openai"
|
| 9 |
-
REAL_API_KEY = os.environ["REAL_API_KEY"]
|
| 10 |
-
FAKE_KEY = os.environ.get("FAKE_KEY", "123456")
|
| 11 |
|
| 12 |
@asynccontextmanager
|
| 13 |
async def lifespan(app: FastAPI):
|
|
@@ -26,7 +27,7 @@ async def proxy_endpoint(path: str = "", request: Request = None):
|
|
| 26 |
"Access-Control-Allow-Headers": "Authorization, Content-Type"
|
| 27 |
})
|
| 28 |
|
| 29 |
-
#
|
| 30 |
auth_header = request.headers.get("authorization", "")
|
| 31 |
if auth_header.lower() != f"bearer {FAKE_KEY}":
|
| 32 |
return JSONResponse(status_code=401, content={"error": "Invalid fake key"})
|
|
@@ -90,4 +91,4 @@ async def proxy_endpoint(path: str = "", request: Request = None):
|
|
| 90 |
"content-type": resp.headers.get("content-type", "application/json"),
|
| 91 |
"access-control-allow-origin": "*"
|
| 92 |
}
|
| 93 |
-
)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import asyncio
|
| 4 |
import aiohttp
|
| 5 |
+
from contextlib import asynccontextmanager
|
| 6 |
from fastapi import FastAPI, Request, Response
|
| 7 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 8 |
|
| 9 |
TARGET_BASE_URL = "https://api.deepinfra.com/v1/openai"
|
| 10 |
+
REAL_API_KEY = os.environ["REAL_API_KEY"]
|
| 11 |
+
FAKE_KEY = os.environ.get("FAKE_KEY", "123456")
|
| 12 |
|
| 13 |
@asynccontextmanager
|
| 14 |
async def lifespan(app: FastAPI):
|
|
|
|
| 27 |
"Access-Control-Allow-Headers": "Authorization, Content-Type"
|
| 28 |
})
|
| 29 |
|
| 30 |
+
# 验证本地传来的假 key
|
| 31 |
auth_header = request.headers.get("authorization", "")
|
| 32 |
if auth_header.lower() != f"bearer {FAKE_KEY}":
|
| 33 |
return JSONResponse(status_code=401, content={"error": "Invalid fake key"})
|
|
|
|
| 91 |
"content-type": resp.headers.get("content-type", "application/json"),
|
| 92 |
"access-control-allow-origin": "*"
|
| 93 |
}
|
| 94 |
+
)
|