From 27f88ca49fc08f33b8d4014b6d9141301a302f7c Mon Sep 17 00:00:00 2001 From: lanqian528 <5499636+lanqian528@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:34:32 +0800 Subject: [PATCH 1/5] v1.6.10-beta1 fix bugs --- gateway/reverseProxy.py | 13 +++++-------- version.txt | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/gateway/reverseProxy.py b/gateway/reverseProxy.py index 48f6352..73cc8e1 100644 --- a/gateway/reverseProxy.py +++ b/gateway/reverseProxy.py @@ -208,14 +208,11 @@ async def chatgpt_reverse_proxy(request: Request, path: str): background = BackgroundTask(client.close) r = await client.request(request.method, f"{base_url}/{path}", params=params, headers=headers, cookies=request_cookies, data=data, stream=True, allow_redirects=False) - if r.status_code == 307: - if "bing" in path: - return Response(status_code=307, - headers={"Location": r.headers.get("Location").replace("chatgpt.com", origin_host) - .replace("https", petrol)}, background=background) - if r.status_code == 302: - return Response(status_code=302, - headers={"Location": r.headers.get("Location").replace("chatgpt.com", origin_host) + if r.status_code == 307 or r.status_code == 302 or r.status_code == 301: + return Response(status_code=307, + headers={"Location": r.headers.get("Location") + .replace("ab.chatgpt.com", origin_host) + .replace("chatgpt.com", origin_host) .replace("cdn.oaistatic.com", origin_host) .replace("https", petrol)}, background=background) elif 'stream' in r.headers.get("content-type", ""): diff --git a/version.txt b/version.txt index e184d84..1f04f2a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.6.9 \ No newline at end of file +1.6.10-beta1 \ No newline at end of file From e1aa87f2fa60e419cb1140a0d502f0996c615d86 Mon Sep 17 00:00:00 2001 From: lanqian528 <5499636+lanqian528@users.noreply.github.com> Date: Tue, 5 Nov 2024 16:21:48 +0800 Subject: [PATCH 2/5] v1.6.10-beta2 support gpts --- gateway/backend.py | 57 +- templates/chatgpt.html | 154 +- ...emix_context.json => chatgpt_context.json} | 2562 +++++--- templates/gpts_context.json | 5510 +++++++++++++++++ version.txt | 2 +- 5 files changed, 7295 insertions(+), 990 deletions(-) rename templates/{remix_context.json => chatgpt_context.json} (72%) create mode 100644 templates/gpts_context.json diff --git a/gateway/backend.py b/gateway/backend.py index cf94c72..a55251c 100644 --- a/gateway/backend.py +++ b/gateway/backend.py @@ -10,8 +10,24 @@ from app import app, templates from gateway.reverseProxy import chatgpt_reverse_proxy from utils.configs import enable_gateway -with open("templates/remix_context.json", "r", encoding="utf-8") as f: - remix_context = json.load(f) +with open("templates/chatgpt_context.json", "r", encoding="utf-8") as f: + chatgpt_context = json.load(f) + +with open("templates/gpts_context.json", "r", encoding="utf-8") as f: + gpts_context = json.load(f) + +banned_paths = [ + "backend-api/accounts/logout_all", + "backend-api/accounts/deactivate", + "backend-api/user_system_messages", + "backend-api/memories", + "backend-api/settings/clear_account_user_memory", + "backend-api/conversations/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" + "backend-api/accounts/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/invites", + "admin", +] +redirect_paths = ["auth/logout"] +chatgpt_paths = ["c/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"] def set_value_for_key(data, target_key, new_value): @@ -35,7 +51,7 @@ if enable_gateway: if not token: return await login_html(request) - user_remix_context = remix_context.copy() + user_remix_context = chatgpt_context.copy() set_value_for_key(user_remix_context, "user", {"id": "user-chatgpt"}) set_value_for_key(user_remix_context, "accessToken", token) @@ -71,6 +87,14 @@ if enable_gateway: async def get_gpts(): return {"kind": "store"} + @app.get("/g/g-{gizmo_id}") + async def get_gizmo_json(request: Request, gizmo_id: str): + params = request.query_params + if params.get("_data") == "routes/g.$gizmoId._index": + return Response(content=json.dumps(gpts_context, indent=4), media_type="application/json") + else: + return await chatgpt_html(request) + @app.get("/backend-api/gizmos/bootstrap") async def get_gizmos_bootstrap(): @@ -129,8 +153,7 @@ if enable_gateway: return patch_response else: data = await request.json() - if conversation_id in globals.seed_map[token][ - "conversations"] and conversation_id in globals.conversation_map: + if conversation_id in globals.seed_map[token]["conversations"] and conversation_id in globals.conversation_map: if not data.get("is_visible", True): globals.conversation_map.pop(conversation_id) globals.seed_map[token]["conversations"].remove(conversation_id) @@ -143,6 +166,16 @@ if enable_gateway: return patch_response + @app.post("/v1/initialize") + async def initialize(request: Request): + initialize_response = (await chatgpt_reverse_proxy(request, f"/v1/initialize")) + initialize_str = initialize_response.body.decode('utf-8') + initialize_json = json.loads(initialize_str) + set_value_for_key(initialize_json, "ip", "8.8.8.8") + set_value_for_key(initialize_json, "country", "US") + return Response(content=json.dumps(initialize_json, indent=4), media_type="application/json") + + @app.get("/backend-api/me") async def get_me(request: Request): me = { @@ -191,20 +224,6 @@ if enable_gateway: return me - banned_paths = [ - "backend-api/accounts/logout_all", - "backend-api/accounts/deactivate", - "backend-api/user_system_messages", - "backend-api/memories", - "backend-api/settings/clear_account_user_memory", - "backend-api/conversations/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" - "backend-api/accounts/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/invites", - "admin", - ] - redirect_paths = ["auth/logout"] - chatgpt_paths = ["c/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"] - - @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"]) async def reverse_proxy(request: Request, path: str): if re.match("/v1/rgstr", path): diff --git a/templates/chatgpt.html b/templates/chatgpt.html index 6447bca..92eed68 100644 --- a/templates/chatgpt.html +++ b/templates/chatgpt.html @@ -1,5 +1,5 @@ - +
@@ -18,55 +18,54 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -96,7 +95,7 @@ }()