bluewinliang commited on
Commit
d2e445a
·
verified ·
1 Parent(s): 6157b43

Upload proxy_handler.py

Browse files
Files changed (1) hide show
  1. proxy_handler.py +109 -10
proxy_handler.py CHANGED
@@ -6,6 +6,10 @@ from typing import AsyncGenerator, Dict, Any, Tuple, List
6
  import httpx
7
  from fastapi import HTTPException
8
  from fastapi.responses import StreamingResponse
 
 
 
 
9
 
10
  from config import settings
11
  from cookie_manager import cookie_manager
@@ -21,11 +25,43 @@ class ProxyHandler:
21
  limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),
22
  http2=True,
23
  )
 
 
24
 
25
  async def aclose(self):
26
  if not self.client.is_closed:
27
  await self.client.aclose()
28
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def _clean_thinking_content(self, text: str) -> str:
30
  """
31
  Aggressively cleans raw thinking content strings based on observed patterns
@@ -84,19 +120,82 @@ class ProxyHandler:
84
  else: out.append({"role": getattr(m, "role", "user"), "content": getattr(m, "content", str(m))})
85
  return out
86
 
87
- async def _prep_upstream(self, req: ChatCompletionRequest) -> Tuple[Dict[str, Any], Dict[str, str], str]:
88
- """Prepares the request body, headers, and cookie for the upstream API."""
 
89
  ck = await cookie_manager.get_next_cookie()
90
  if not ck: raise HTTPException(503, "No available cookies")
 
91
  model = settings.UPSTREAM_MODEL if req.model == settings.MODEL_NAME else req.model
92
- body = { "stream": True, "model": model, "messages": self._serialize_msgs(req.messages), "background_tasks": {"title_generation": True, "tags_generation": True}, "chat_id": str(uuid.uuid4()), "features": {"image_generation": False, "code_interpreter": False, "web_search": False, "auto_web_search": False, "enable_thinking": True,}, "id": str(uuid.uuid4()), "mcp_servers": ["deep-web-search"], "model_item": {"id": model, "name": "GLM-4.6", "owned_by": "openai"}, "params": {}, "tool_servers": [], "variables": {"{{USER_NAME}}": "User", "{{USER_LOCATION}}": "Unknown", "{{CURRENT_DATETIME}}": time.strftime("%Y-%m-%d %H:%M:%S"),},}
93
- headers = { "Content-Type": "application/json", "Authorization": f"Bearer {ck}", "User-Agent": ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"), "Accept": "application/json, text/event-stream", "Accept-Language": "zh-CN", "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"', "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": '"macOS"', "x-fe-version": "prod-fe-1.0.79","X-Signature": "ZTpUeXBlRXJyb3I6IENhbm5vdCByZWFkIHByb3BlcnRpZXMgb2YgdW5kZWZpbmVkIChyZWFkaW5nICdjaGlsZE5vZGVzJyk=", "Origin": "https://chat.z.ai", "Referer": "https://chat.z.ai/",}
94
- return body, headers, ck
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  async def stream_proxy_response(self, req: ChatCompletionRequest) -> AsyncGenerator[str, None]:
97
  ck = None
98
  try:
99
- body, headers, ck = await self._prep_upstream(req)
100
  comp_id = f"chatcmpl-{uuid.uuid4().hex[:29]}"
101
  think_open = False
102
  yielded_think_buffer = ""
@@ -127,7 +226,7 @@ class ProxyHandler:
127
  if cleaned_text:
128
  yield f"data: {json.dumps({'id': comp_id, 'object': 'chat.completion.chunk', 'created': int(time.time()), 'model': req.model, 'choices': [{'index': 0, 'delta': {'content': cleaned_text}, 'finish_reason': None}]})}\n\n"
129
 
130
- async with self.client.stream("POST", settings.UPSTREAM_URL, json=body, headers=headers) as resp:
131
  if resp.status_code != 200:
132
  await cookie_manager.mark_cookie_failed(ck); err_body = await resp.aread()
133
  err_msg = f"Error: {resp.status_code} - {err_body.decode(errors='ignore')}"
@@ -186,10 +285,10 @@ class ProxyHandler:
186
  async def non_stream_proxy_response(self, req: ChatCompletionRequest) -> ChatCompletionResponse:
187
  ck = None
188
  try:
189
- body, headers, ck = await self._prep_upstream(req)
190
  last_thinking_content = ""
191
  raw_answer_parts = []
192
- async with self.client.stream("POST", settings.UPSTREAM_URL, json=body, headers=headers) as resp:
193
  if resp.status_code != 200:
194
  await cookie_manager.mark_cookie_failed(ck); error_detail = await resp.text()
195
  raise HTTPException(resp.status_code, f"Upstream error: {error_detail}")
 
6
  import httpx
7
  from fastapi import HTTPException
8
  from fastapi.responses import StreamingResponse
9
+ import hashlib
10
+ import hmac
11
+ import urllib.parse
12
+ from datetime import datetime, timezone
13
 
14
  from config import settings
15
  from cookie_manager import cookie_manager
 
25
  limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),
26
  http2=True,
27
  )
28
+ # 从JavaScript代码中获取的固定密钥
29
+ self.primary_secret = "junjie".encode('utf-8')
30
 
31
  async def aclose(self):
32
  if not self.client.is_closed:
33
  await self.client.aclose()
34
+
35
+ # --- 新增方法:获取毫秒级时间戳 ---
36
+ def _get_timestamp_millis(self) -> int:
37
+ return int(time.time() * 1000)
38
+
39
+ # --- 新增方法:解析JWT以获取用户ID ---
40
+ def _parse_jwt_token(self, token: str) -> Dict[str, str]:
41
+ try:
42
+ parts = token.split('.')
43
+ if len(parts) != 3:
44
+ return {"userId": ""}
45
+ payload = json.loads(base64.urlsafe_b64decode(parts[1] + '==').decode('utf-8'))
46
+ return {"userId": payload.get("sub", "")}
47
+ except Exception:
48
+ return {"userId": ""}
49
+
50
+ # --- 新增方法:生成签名 ---
51
+ def _generate_signature(self, token: str, payload_str: str, mt: str) -> Tuple[str, int]:
52
+ timestamp_ms = self._get_timestamp_millis()
53
+ minute_bucket = str(timestamp_ms // 60000)
54
+
55
+ level1_data = f"{token}|{minute_bucket}".encode('utf-8')
56
+ mac1 = hmac.new(self.primary_secret, level1_data, hashlib.sha256)
57
+ derived_key_hex = mac1.hexdigest()
58
+
59
+ level2_data = f"{payload_str}|{mt}|{timestamp_ms}".encode('utf-8')
60
+ mac2 = hmac.new(derived_key_hex.encode('utf-8'), level2_data, hashlib.sha256)
61
+ signature = mac2.hexdigest()
62
+
63
+ return signature, timestamp_ms
64
+
65
  def _clean_thinking_content(self, text: str) -> str:
66
  """
67
  Aggressively cleans raw thinking content strings based on observed patterns
 
120
  else: out.append({"role": getattr(m, "role", "user"), "content": getattr(m, "content", str(m))})
121
  return out
122
 
123
+ # --- 重构 _prep_upstream 方法以加入签名逻辑 ---
124
+ async def _prep_upstream(self, req: ChatCompletionRequest) -> Tuple[Dict[str, Any], Dict[str, str], str, str]:
125
+ """Prepares the request body, headers, cookie, and URL for the upstream API."""
126
  ck = await cookie_manager.get_next_cookie()
127
  if not ck: raise HTTPException(503, "No available cookies")
128
+
129
  model = settings.UPSTREAM_MODEL if req.model == settings.MODEL_NAME else req.model
130
+ chat_id = str(uuid.uuid4())
131
+ request_id = str(uuid.uuid4())
132
+ user_info = self._parse_jwt_token(ck)
133
+ user_id = user_info.get("userId", "")
134
+
135
+ body = { "stream": True, "model": model, "messages": self._serialize_msgs(req.messages), "background_tasks": {"title_generation": True, "tags_generation": True}, "chat_id": chat_id, "features": {"image_generation": False, "code_interpreter": False, "web_search": False, "auto_web_search": False, "enable_thinking": True,}, "id": request_id, "mcp_servers": ["deep-web-search"], "model_item": {"id": model, "name": "GLM-4.6", "owned_by": "openai"}, "params": {}, "tool_servers": [], "variables": {"{{USER_NAME}}": "User", "{{USER_LOCATION}}": "Unknown", "{{CURRENT_DATETIME}}": time.strftime("%Y-%m-%d %H:%M:%S"),},}
136
+
137
+ # 构造用于签名的负载
138
+ timestamp = self._get_timestamp_millis()
139
+ now = datetime.now(timezone.utc)
140
+
141
+ payload_data = {
142
+ 'timestamp': str(timestamp),
143
+ 'requestId': request_id,
144
+ 'user_id': user_id,
145
+ 'token': ck,
146
+ 'user_agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
147
+ 'current_url': f"https://chat.z.ai/c/{chat_id}",
148
+ 'pathname': f"/c/{chat_id}",
149
+ 'timezone': 'UTC', # 简化时区处理
150
+ 'timezone_offset': '0',
151
+ 'local_time': now.isoformat(),
152
+ 'utc_time': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
153
+ 'version': '0.0.1',
154
+ 'platform': 'web',
155
+ 'language': 'zh-CN',
156
+ 'languages': 'zh-CN,en',
157
+ 'cookie_enabled': 'true',
158
+ 'screen_width': '2560',
159
+ 'screen_height': '1440',
160
+ 'screen_resolution': '2560x1440',
161
+ 'viewport_height': '1328',
162
+ 'viewport_width': '1342',
163
+ 'viewport_size': '1342x1328',
164
+ 'color_depth': '24',
165
+ 'pixel_ratio': '2',
166
+ 'search': '',
167
+ 'hash': '',
168
+ 'host': 'chat.z.ai',
169
+ 'hostname': 'chat.z.ai',
170
+ 'protocol': 'https:',
171
+ 'referrer': '',
172
+ 'title': 'Chat with Z.ai - Free AI Chatbot powered by GLM-4.5',
173
+ 'is_mobile': 'false',
174
+ 'is_touch': 'false',
175
+ 'max_touch_points': '0',
176
+ 'browser_name': 'Chrome',
177
+ 'os_name': 'Mac OS'
178
+ }
179
+
180
+ keys = sorted(payload_data.keys())
181
+ sorted_payload = ",".join([f"{k},{payload_data[k]}" for k in keys])
182
+ url_params = urllib.parse.urlencode(payload_data)
183
+
184
+ # 获取最后一条消息作为 mt
185
+ last_message = req.messages[-1].content if req.messages else ""
186
+
187
+ signature, sig_timestamp = self._generate_signature(ck, sorted_payload, last_message)
188
+
189
+ final_url = f"{settings.UPSTREAM_URL}?{url_params}&signature_timestamp={sig_timestamp}"
190
+
191
+ headers = { "Content-Type": "application/json", "Authorization": f"Bearer {ck}", "User-Agent": ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"), "Accept": "application/json, text/event-stream", "Accept-Language": "zh-CN", "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"', "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": '"macOS"', "x-fe-version": "prod-fe-1.0.79", "X-Signature": signature, "Origin": "https://chat.z.ai", "Referer": "https://chat.z.ai/",}
192
+
193
+ return body, headers, ck, final_url
194
 
195
  async def stream_proxy_response(self, req: ChatCompletionRequest) -> AsyncGenerator[str, None]:
196
  ck = None
197
  try:
198
+ body, headers, ck, url = await self._prep_upstream(req)
199
  comp_id = f"chatcmpl-{uuid.uuid4().hex[:29]}"
200
  think_open = False
201
  yielded_think_buffer = ""
 
226
  if cleaned_text:
227
  yield f"data: {json.dumps({'id': comp_id, 'object': 'chat.completion.chunk', 'created': int(time.time()), 'model': req.model, 'choices': [{'index': 0, 'delta': {'content': cleaned_text}, 'finish_reason': None}]})}\n\n"
228
 
229
+ async with self.client.stream("POST", url, json=body, headers=headers) as resp:
230
  if resp.status_code != 200:
231
  await cookie_manager.mark_cookie_failed(ck); err_body = await resp.aread()
232
  err_msg = f"Error: {resp.status_code} - {err_body.decode(errors='ignore')}"
 
285
  async def non_stream_proxy_response(self, req: ChatCompletionRequest) -> ChatCompletionResponse:
286
  ck = None
287
  try:
288
+ body, headers, ck, url = await self._prep_upstream(req)
289
  last_thinking_content = ""
290
  raw_answer_parts = []
291
+ async with self.client.stream("POST", url, json=body, headers=headers) as resp:
292
  if resp.status_code != 200:
293
  await cookie_manager.mark_cookie_failed(ck); error_detail = await resp.text()
294
  raise HTTPException(resp.status_code, f"Upstream error: {error_detail}")