mirror of
https://github.com/lanqian528/chat2api.git
synced 2026-06-13 21:02:46 +08:00
fix chatgpt_reverse_proxy
This commit is contained in:
parent
e1df9998ad
commit
6e2f8ab527
2
app.py
2
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__":
|
||||
|
||||
@ -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", ""):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user