fix chatgpt_reverse_proxy

This commit is contained in:
LanQian 2024-04-22 00:40:33 +08:00
parent e1df9998ad
commit 6e2f8ab527
2 changed files with 36 additions and 36 deletions

2
app.py
View File

@ -46,7 +46,7 @@ async def send_conversation(request: Request, token=Depends(verify_token)):
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
async def reverse_proxy(request: Request, path: str):
return chatgpt_reverse_proxy(request, path)
return await chatgpt_reverse_proxy(request, path)
if __name__ == "__main__":

View File

@ -8,42 +8,42 @@ from utils.config import chatgpt_base_url_list, proxy_url_list
async def chatgpt_reverse_proxy(request: Request, path: str):
base_url = random.choice(chatgpt_base_url_list)
if ":" in request.url.netloc:
origin_url = "http://" + request.url.netloc
else:
origin_url = "https://" + request.url.netloc
if "v1" in path:
base_url = "https://ab.chatgpt.com"
params = dict(request.query_params)
headers = {key: value for key, value in request.headers.items() if
key.lower() not in ["host", "origin", "referer", "user-agent", "authorization"]}
cookies = dict(request.cookies)
headers.update({
"Accept-Language": "en-US,en;q=0.9",
"Host": f"{base_url.split('//')[1]}",
"Origin": f"{base_url}",
"Referer": f"{base_url}/{path}",
"Sec-Ch-Ua": '"Chromium";v="123", "Not(A:Brand";v="24", "Microsoft Edge";v="123"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
})
if request.headers.get('Authorization'):
headers['Authorization'] = request.headers['Authorization']
if headers.get("Content-Type") == "application/json":
data = await request.json()
else:
data = await request.body()
client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
try:
base_url = random.choice(chatgpt_base_url_list)
if ":" in request.url.netloc:
origin_url = "http://" + request.url.netloc
else:
origin_url = "https://" + request.url.netloc
if "v1" in path:
base_url = "https://ab.chatgpt.com"
params = dict(request.query_params)
headers = {key: value for key, value in request.headers.items() if
key.lower() not in ["host", "origin", "referer", "user-agent", "authorization"]}
cookies = dict(request.cookies)
headers.update({
"Accept-Language": "en-US,en;q=0.9",
"Host": f"{base_url.split('//')[1]}",
"Origin": f"{base_url}",
"Referer": f"{base_url}/{path}",
"Sec-Ch-Ua": '"Chromium";v="123", "Not(A:Brand";v="24", "Microsoft Edge";v="123"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
})
if request.headers.get('Authorization'):
headers['Authorization'] = request.headers['Authorization']
if headers.get("Content-Type") == "application/json":
data = await request.json()
else:
data = await request.body()
client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
r = await client.request(request.method, f"{base_url}/{path}", params=params, headers=headers, cookies=cookies,
data=data, stream=True)
if 'stream' in r.headers.get("content-type", ""):