|
27 | 27 | #### 1. 根 CA 证书生成 (`CertStore._create_authority`) |
28 | 28 |
|
29 | 29 | ```python |
30 | | -key = rsa.generate_private_key(public_exponent=65537, key_size=2048) |
31 | | -cert = ( |
32 | | - x509.CertificateBuilder() |
33 | | - .add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True) |
34 | | - .add_extension(x509.KeyUsage(key_cert_sign=True, crl_sign=True, ...), critical=True) |
35 | | - .sign(key, hashes.SHA256()) |
36 | | -) |
| 30 | +name = x509.Name([ |
| 31 | + x509.NameAttribute(NameOID.COUNTRY_NAME, self._profile['country']), # 随机国家 (JP/TW/DE/NL/SG) |
| 32 | + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, self._profile['state']), |
| 33 | + x509.NameAttribute(NameOID.LOCALITY_NAME, self._profile['city']), |
| 34 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, self._profile['org']), |
| 35 | + x509.NameAttribute(NameOID.COMMON_NAME, self._profile['cn']) |
| 36 | +]) |
| 37 | +# ... 证书签发逻辑 ... |
37 | 38 | ``` |
38 | 39 |
|
39 | 40 | 生成的 `ca.crt` 需要被系统/浏览器信任,才能使后续的域名证书被接受。 |
@@ -105,14 +106,17 @@ elif 'GenerateContent' in path: |
105 | 106 |
|
106 | 107 | ### 响应解码流程 (`ResponseHandler.handle_response`) |
107 | 108 |
|
| 109 | +```python |
108 | 110 | ```python |
109 | 111 | async def handle_response(self, data, host, path, headers): |
| 112 | + # 使用 memoryview 优化内存复制 |
110 | 113 | decoded, completed = self._unchunk(bytes(data)) |
111 | 114 | decoded = self._inflate(decoded) |
112 | 115 | result = self._extract_content(decoded) |
113 | 116 | result['done'] = completed |
114 | 117 | return result |
115 | 118 | ``` |
| 119 | +``` |
116 | 120 |
|
117 | 121 | #### 1. Chunked 解码 (`_unchunk`) |
118 | 122 |
|
@@ -171,17 +175,24 @@ elif len(payload) > 2: |
171 | 175 |
|
172 | 176 | 通过分析包含 Function Call 的响应,发现参数使用递归嵌套数组表示: |
173 | 177 |
|
| 178 | +```python |
174 | 179 | ```python |
175 | 180 | def _parse_tool_args(self, args): |
| 181 | + extractors = { |
| 182 | + 1: lambda v: None, |
| 183 | + 2: lambda v: v[1], |
| 184 | + 3: lambda v: v[2], |
| 185 | + 4: lambda v: v[3] == 1, |
| 186 | + 5: lambda v: self._parse_tool_args(v[4]), |
| 187 | + } |
| 188 | + |
176 | 189 | for param in params: |
177 | | - name = param[0] |
178 | | - value = param[1] |
179 | | - |
180 | | - if len(value) == 1: result[name] = None |
181 | | - elif len(value) == 2: result[name] = value[1] |
182 | | - elif len(value) == 3: result[name] = value[2] |
183 | | - elif len(value) == 4: result[name] = value[3] == 1 |
184 | | - elif len(value) == 5: result[name] = self._parse_tool_args(value[4]) |
| 190 | + name, value = param[0], param[1] |
| 191 | + if isinstance(value, list): |
| 192 | + extractor = extractors.get(len(value)) |
| 193 | + if extractor: |
| 194 | + result[name] = extractor(value) |
| 195 | +``` |
185 | 196 | ``` |
186 | 197 |
|
187 | 198 | value 数组长度与数据类型的映射关系: |
|
0 commit comments