From 6e2f8ab527ea5aa45ea2378f22430d2d2075ea41 Mon Sep 17 00:00:00 2001 From: LanQian <5499636+LanQian528@users.noreply.github.com> Date: Mon, 22 Apr 2024 00:40:33 +0800 Subject: [PATCH] fix chatgpt_reverse_proxy --- app.py | 2 +- chatgpt/reverseProxy.py | 70 ++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/app.py b/app.py index 7d8c6cf..6b1c302 100644 --- a/app.py +++ b/app.py @@ -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__": diff --git a/chatgpt/reverseProxy.py b/chatgpt/reverseProxy.py index cd33219..3df20b5 100644 --- a/chatgpt/reverseProxy.py +++ b/chatgpt/reverseProxy.py @@ -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", ""):