diff --git a/README.md b/README.md index 3a442f8..b3a980b 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ > - [x] ArkoseToken > - [x] GPT4.0 画图、工具 (测试中,可能有bug) > - [x] 使用 RefreshToken 代替 AccessToken -> - [x] 反向代理 UI (http://127.0.0.1:5005, 不支持登录) +> - [x] 反向代理 UI (http://127.0.0.1:5005, 不支持登录使用) > > TODO > @@ -42,7 +42,7 @@ git clone https://github.com/LanQian528/chat2api cd chat2api pip install -r requirements.txt -python chat2api.py +python app.py ``` ### Docker部署 @@ -139,13 +139,12 @@ curl --location 'http://127.0.0.1:5005/v1/chat/completions' \ ### 环境变量 ``` -AUTHORIZATION=your_first_token, your_second_token // 使用免登3.5的Bearer token,不设置则无需Bearer token -FREE35_BASE_URL=https://chat.openai.com/backend-anon // 免登3.5的网关地址 -CHATGPT_BASE_URL=https://chat.openai.com/backend-api // 非免登3.5、4.0的网关地址 -HISTORY_DISABLED=true // 是否保存聊天记录 -PROXY_URL=your_first_proxy, your_second_proxy // 代理url,多个代理用逗号分隔 -ARKOSE_TOKEN_URL=https://arkose.example.com/token // 获取Arkose token的地址,上文有提供说明 -RETRY_TIMES=3 // 重试次数 +AUTHORIZATION=your_first_token, your_second_token // 使用免登3.5的Bearer token,不设置则无需Bearer token +CHATGPT_BASE_URL=https://chat.openai.com // ChatGPT网关地址 +HISTORY_DISABLED=true // 是否保存聊天记录 +PROXY_URL=your_first_proxy, your_second_proxy // 代理url,多个代理用逗号分隔 +ARKOSE_TOKEN_URL=https://arkose.example.com/token // 获取Arkose token的地址,上文有提供说明 +RETRY_TIMES=3 // 重试次数 ``` ## License diff --git a/chatgpt/reverseProxy.py b/chatgpt/reverseProxy.py index e70d389..870cf46 100644 --- a/chatgpt/reverseProxy.py +++ b/chatgpt/reverseProxy.py @@ -5,7 +5,6 @@ from fastapi.responses import StreamingResponse, Response from utils.Client import Client from utils.config import chatgpt_base_url_list, proxy_url_list - headers_reject_list = [ "x-real-ip", "x-forwarded-for", @@ -57,9 +56,9 @@ headers_reject_list = [ "cf-visitor", ] + async def chatgpt_reverse_proxy(request: Request, path: str): try: - base_url = random.choice(chatgpt_base_url_list) origin_host = request.url.netloc if ":" in origin_host: petrol = "http" @@ -67,30 +66,30 @@ async def chatgpt_reverse_proxy(request: Request, path: str): petrol = "https" if path.startswith("v1/"): base_url = "https://ab.chatgpt.com" - if path.startswith("authorize") and "max_age" not in dict(request.query_params).keys(): - base_url = "https://auth.openai.com" - if path.startswith("authorize") or path.startswith("u/") or path.startswith("oauth/") or path.startswith("assets/"): - base_url = "https://auth0.openai.com" + else: + base_url = random.choice(chatgpt_base_url_list) 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"] and key.lower() not in headers_reject_list) + if (key.lower() not in ["host", "origin", "referer", "user-agent", + "authorization"] and key.lower() not in headers_reject_list) } 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" + "accept-Language": "en-US,en;q=0.9", + "host": base_url.replace("https://", "").replace("http://", ""), + "origin": base_url, + "referer": f"{base_url}/", + "sec-ch-ua": '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"', + "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/124.0.0.0 Safari/537.36 Edg/124.0.0.0" }) + if request.headers.get('Authorization'): headers['Authorization'] = request.headers['Authorization'] @@ -103,23 +102,20 @@ async def chatgpt_reverse_proxy(request: Request, path: str): 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", ""): + if r.status_code == 302: + return Response(status_code=302, headers={"Location": r.headers.get("Location")}) + elif 'stream' in r.headers.get("content-type", ""): return StreamingResponse(r.aiter_content(), media_type=r.headers.get("content-type", "")) else: content = ((await r.atext()).replace("chat.openai.com", origin_host) .replace("ab.chatgpt.com", origin_host) .replace("cdn.oaistatic.com", origin_host) - .replace("auth.openai.com", origin_host) - .replace("auth0.openai.com", origin_host) .replace("https", petrol)) response = Response(content=content, media_type=r.headers.get("content-type"), status_code=r.status_code) for key, value in r.cookies.items(): if key in cookies.keys(): continue - if key.startswith("__"): - response.set_cookie(key=key, value=value, secure=True, httponly=True, path='/') - else: - response.set_cookie(key=key, value=value, secure=True, httponly=True) + response.set_cookie(key=key, value=value) return response except Exception as e: