解析代理API响应,支持JSON和文本格式,提取ip/port/user/pass
(self, text)
| 297 | self.init_account(config_str) |
| 298 | |
| 299 | def _parse_proxy_response(self, text): |
| 300 | """解析代理API响应,支持JSON和文本格式,提取ip/port/user/pass""" |
| 301 | text = text.strip() |
| 302 | |
| 303 | def extract(d): |
| 304 | if not d or not d.get('ip') or not d.get('port'): |
| 305 | return None |
| 306 | return { |
| 307 | 'ip': str(d['ip']), |
| 308 | 'port': int(d['port']), |
| 309 | 'user': str(d.get('account') or d.get('user') or ''), |
| 310 | 'pass': str(d.get('password') or d.get('pass') or '') |
| 311 | } |
| 312 | try: |
| 313 | json_start = text.find('{') |
| 314 | json_end = text.rfind('}') |
| 315 | if json_start != -1 and json_end != -1: |
| 316 | data = json.loads(text[json_start:json_end + 1]) |
| 317 | if data.get('ip') and data.get('port'): |
| 318 | return extract(data) |
| 319 | if data.get('data'): |
| 320 | inner = data['data'] |
| 321 | if isinstance(inner, dict) and inner.get('list') and isinstance(inner['list'], list) and len(inner['list']) > 0: |
| 322 | return extract(inner['list'][0]) |
| 323 | if isinstance(inner, list) and len(inner) > 0: |
| 324 | return extract(inner[0]) |
| 325 | if isinstance(inner, dict) and inner.get('ip'): |
| 326 | return extract(inner) |
| 327 | if data.get('result') and isinstance(data['result'], dict) and data['result'].get('ip'): |
| 328 | return extract(data['result']) |
| 329 | except: |
| 330 | pass |
| 331 | m = re.search(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[:\s\t]+(\d{1,5})', text) |
| 332 | if m: |
| 333 | return {'ip': m.group(1), 'port': int(m.group(2)), 'user': '', 'pass': ''} |
| 334 | return None |
| 335 | |
| 336 | def configure_proxy(self): |
| 337 | proxy_api = os.environ.get("UNICOM_PROXY_API") |