mirror of
https://github.com/mikumifa/biliTickerBuy.git
synced 2026-11-18 06:23:16 -08:00
feat: add proxy api compatibility profiles
This commit is contained in:
1 parent
aed5b4ad7f
commit
24445fdfb7
7 files changed
+2706
-1380
No files matched your search
@@ -0,0 +1,147 @@
|
||||
# Proxy API Provider Demos
|
||||
|
||||
All examples are sanitized. Credential-like query values are intentionally left blank.
|
||||
|
||||
## Youdaili
|
||||
|
||||
Demo URL shape, with credential values omitted:
|
||||
|
||||
```text
|
||||
http://api.youdaili.com/v1/proxy/get?count=5&format=json&protocol=http
|
||||
```
|
||||
|
||||
Representative JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"msg": "OK",
|
||||
"data": {
|
||||
"count": 1,
|
||||
"proxy_list": [
|
||||
{ "ip": "8.8.8.8", "port": 12234 }
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Kuaidaili
|
||||
|
||||
Demo URL shape, with credential and signature values omitted:
|
||||
|
||||
```text
|
||||
https://dps.kdlapi.com/api/getdps/?num=5&format=json&sep=1
|
||||
```
|
||||
|
||||
Text output often uses one proxy per line:
|
||||
|
||||
```text
|
||||
192.0.2.10:15818
|
||||
192.0.2.11:15819
|
||||
```
|
||||
|
||||
## Zhima HTTP
|
||||
|
||||
Demo URL shape:
|
||||
|
||||
```text
|
||||
https://webapi.http.zhimacangku.com/getip?num=5&type=2&pro=&city=0&yys=0&port=1&ts=1&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1®ions=
|
||||
```
|
||||
|
||||
Representative JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"success": true,
|
||||
"data": [
|
||||
{ "ip": "192.0.2.20", "port": 15820 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Xiaoxiang
|
||||
|
||||
Demo URL shape:
|
||||
|
||||
```text
|
||||
https://api.xiaoxiangdaili.com/ip/get?cnt=5&wt=json
|
||||
```
|
||||
|
||||
Representative JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"success": true,
|
||||
"data": [
|
||||
{ "ip": "192.0.2.30", "port": 15830 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Xiequ
|
||||
|
||||
Demo URL shape, with account values omitted:
|
||||
|
||||
```text
|
||||
http://api.xiequ.cn/VAD/GetIp.aspx?act=get&num=5&time=30&plat=0&re=0&type=2&so=1&ow=1&spl=1&addr=&db=1
|
||||
```
|
||||
|
||||
Text output can be plain `ip:port` lines.
|
||||
|
||||
## Juliang
|
||||
|
||||
Demo URL shape:
|
||||
|
||||
```text
|
||||
https://v2.api.juliangip.com/dynamic/getips?filter=1&num=5&pt=1&result_type=json
|
||||
```
|
||||
|
||||
Representative JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"proxy_list": [
|
||||
"192.0.2.40:15840"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Pinyi
|
||||
|
||||
Demo URL shape:
|
||||
|
||||
```text
|
||||
http://tiqu.pyhttp.taolop.com/getflowip?count=5&neek=NEEK&type=1&yys=0&port=1&sb=&mr=1&sep=1
|
||||
```
|
||||
|
||||
Text output can be plain `ip:port` lines.
|
||||
|
||||
## Zdaye
|
||||
|
||||
Demo URL shape:
|
||||
|
||||
```text
|
||||
https://www.zdaye.com/dayProxy/ip/PORT/?count=5&returnType=2
|
||||
```
|
||||
|
||||
Representative JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": "10001",
|
||||
"msg": "获取成功",
|
||||
"data": {
|
||||
"proxy_list": [
|
||||
{ "ip": "192.0.2.50", "port": 15850 }
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Response formats vary by package; generic JSON and text parsing are used as fallback.
|
||||
@@ -0,0 +1,45 @@
|
||||
# 代理 API 兼容说明
|
||||
|
||||
代理 API 模块支持直接粘贴代理服务商后台生成的提取 API/demo 链接,并把不同服务商的返回统一转换成 `ProxyManager` 使用的代理地址格式。
|
||||
|
||||
## 兼容模型
|
||||
|
||||
1. **自动识别**:根据域名、路径和参数名识别常见服务商。
|
||||
2. **服务商 Profile**:只在已知规则下补齐为空或缺失的数量、返回格式、协议参数。
|
||||
3. **通用兜底解析**:从 JSON、数组、嵌套对象、XML、纯文本、CSV 样式文本中提取代理。
|
||||
|
||||
模块不会再把所有 URL 强行改写成 `count/format/protocol`。很多服务商后台生成的链接带签名或特殊参数,随意改写可能导致链接失效。
|
||||
|
||||
## 初始 Profile
|
||||
|
||||
| Profile | 常见域名 | 数量参数 | 格式参数 | 说明 |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| `youdaili` | `youdaili.com` | `count` | `format` | 保留已有有代理 JSON 结构支持。 |
|
||||
| `kuaidaili` | `kdlapi.com`, `kuaidaili.com` | `num` | `format` | 支持文本和 JSON 返回。 |
|
||||
| `zhima` | `zhimacangku.com`, `zhimahttp.com` | `num` | `type` | 常见 demo 中 `type=2` 表示 JSON。 |
|
||||
| `xiaoxiang` | `xiaoxiangdaili.com` | `cnt` | `wt` | 常见 demo 中 `wt=json` 表示 JSON。 |
|
||||
| `xiequ` | `xiequ.cn` | `num` | `type` | 后台生成链接包含较多服务商开关,默认保留。 |
|
||||
| `juliang` | `juliangip.com` | `num` | `result_type` | 常见返回把代理放在 `data.proxy_list`。 |
|
||||
| `qingguo` | `qg.net`, `qingguo` 相关域名 | `num`, `count` | 通用兜底 | 强兼容常见青果代理链接。 |
|
||||
| `yiniuyun` | `16yun.cn`, `yiniuyun` 相关域名 | `num`, `count` | 通用兜底 | 强兼容常见亿牛云代理链接。 |
|
||||
| `pinyi` | `pyhttp`, `pinyi` 相关域名 | `count`, `num` | `type` | 文本返回较常见。 |
|
||||
| `taiyang` | `taiyang` 相关域名 | `num`, `count` | 通用兜底 | 走历史/通用兜底解析。 |
|
||||
| `mogu` | `mogu`, `mogumiao` 相关域名 | `num`, `count` | 通用兜底 | 走历史/通用兜底解析。 |
|
||||
| `xundaili` | `xundaili` 相关域名 | `num`, `count` | 通用兜底 | 走历史/通用兜底解析。 |
|
||||
| `zdaye` | `zdaye.com` | `count`, `num` | `returnType` | 将服务商成功码 `10001` 视为成功,再走通用提取。 |
|
||||
| `generic` | 未识别 | 只补已有且为空的兼容参数 | 只补已有且为空的兼容参数 | 不强行添加服务商参数。 |
|
||||
|
||||
## 标准输出
|
||||
|
||||
解析器返回以下格式:
|
||||
|
||||
```text
|
||||
http://192.0.2.10:15818
|
||||
socks5://192.0.2.11:1080
|
||||
```
|
||||
|
||||
非法条目会被忽略。重复代理会去重,并保留第一次出现的顺序。
|
||||
|
||||
`fetch_proxy_api()` 仍返回兼容旧调用方的 `proxies: list[str]`,同时在 `proxy_records` 中保留解析到的结构化代理元数据,例如 `ttl`、`expire_time`、`deadline` 等有效期字段。
|
||||
|
||||
设置页获取代理 API 后会展示脱敏后的 API URL,常见凭据参数如 `key`、`token`、`signature` 会显示为 `***`。
|
||||
@@ -0,0 +1,100 @@
|
||||
# Proxy API Compatibility Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Build a three-layer proxy API compatibility system with automatic provider recognition, provider profiles, and generic fallback parsing.
|
||||
|
||||
**Architecture:** Keep `fetch_proxy_api` as the stable public entry point. Split request preparation, profile detection, success detection, and response extraction into focused helpers inside `util/proxy/ProxyApiProvider.py` unless the file becomes too large, then extract profile constants to a sibling module.
|
||||
|
||||
**Tech Stack:** Python 3.11, `requests`, `pytest`, existing `ProxyManager` normalized proxy strings.
|
||||
|
||||
## Global Constraints
|
||||
|
||||
- Preserve existing callers in `tab/config.py` and `task/buy.py`.
|
||||
- Do not force all provider URLs into `count/format/protocol`.
|
||||
- Keep sanitized demos only; never commit real tokens, app keys, or secrets.
|
||||
- Every behavior change must have a focused pytest case.
|
||||
|
||||
---
|
||||
|
||||
### Task 1: Compatibility Fixtures And Docs
|
||||
|
||||
**Files:**
|
||||
- Create: `demos/proxy-api/provider-demos.md`
|
||||
- Create: `docs/proxy-api-compatibility.md`
|
||||
- Modify: `tests/test_proxy_api_provider.py`
|
||||
|
||||
**Interfaces:**
|
||||
- Consumes: existing `build_proxy_api_url`, `parse_proxy_api_response`, `fetch_proxy_api`.
|
||||
- Produces: failing tests for `detect_proxy_api_profile`, `build_proxy_api_request`, `parse_proxy_api_response_text`, and widened `fetch_proxy_api`.
|
||||
|
||||
- [x] **Step 1: Add sanitized provider demo notes**
|
||||
|
||||
- [x] **Step 2: Add failing tests for new request and parsing behavior**
|
||||
|
||||
- [x] **Step 3: Run focused tests and verify failures are from missing new behavior**
|
||||
|
||||
### Task 2: Profile Detection And Conservative Request Builder
|
||||
|
||||
**Files:**
|
||||
- Modify: `util/proxy/ProxyApiProvider.py`
|
||||
- Test: `tests/test_proxy_api_provider.py`
|
||||
|
||||
**Interfaces:**
|
||||
- Produces:
|
||||
- `detect_proxy_api_profile(api_url: str) -> ProxyApiProfile`
|
||||
- `build_proxy_api_request(api_url: str, *, count: int, protocol: str) -> ProxyApiRequest`
|
||||
- `build_proxy_api_url(api_url: str, *, count: int, protocol: str) -> str`
|
||||
|
||||
- [ ] **Step 1: Implement dataclasses and profile table**
|
||||
|
||||
- [ ] **Step 2: Preserve existing `build_proxy_api_url` as a compatibility wrapper**
|
||||
|
||||
- [ ] **Step 3: Run request-builder tests**
|
||||
|
||||
### Task 3: Generic JSON And Text Parser
|
||||
|
||||
**Files:**
|
||||
- Modify: `util/proxy/ProxyApiProvider.py`
|
||||
- Test: `tests/test_proxy_api_provider.py`
|
||||
|
||||
**Interfaces:**
|
||||
- Produces:
|
||||
- `parse_proxy_api_response(payload: Any, *, protocol: str, profile: ProxyApiProfile | str | None = None) -> list[str]`
|
||||
- `parse_proxy_api_response_text(text: str, *, protocol: str, profile: ProxyApiProfile | str | None = None) -> list[str]`
|
||||
|
||||
- [ ] **Step 1: Add text response support**
|
||||
|
||||
- [ ] **Step 2: Broaden JSON traversal and provider failure detection**
|
||||
|
||||
- [ ] **Step 3: Run parser tests**
|
||||
|
||||
### Task 4: Fetch Integration And UI Copy
|
||||
|
||||
**Files:**
|
||||
- Modify: `util/proxy/ProxyApiProvider.py`
|
||||
- Modify: `tab/config.py`
|
||||
- Test: `tests/test_proxy_api_provider.py`
|
||||
|
||||
**Interfaces:**
|
||||
- Consumes: `build_proxy_api_request`, `parse_proxy_api_response_text`.
|
||||
- Produces: `fetch_proxy_api` that supports JSON and text responses while returning existing `ProxyApiResult`.
|
||||
|
||||
- [ ] **Step 1: Update `fetch_proxy_api` to parse response text**
|
||||
|
||||
- [ ] **Step 2: Update proxy API UI help copy to describe demo URL support**
|
||||
|
||||
- [ ] **Step 3: Run integration-facing tests**
|
||||
|
||||
### Task 5: Regression Verification
|
||||
|
||||
**Files:**
|
||||
- Existing tests only unless failures reveal a bug.
|
||||
|
||||
- [ ] **Step 1: Run focused proxy tests**
|
||||
|
||||
- [ ] **Step 2: Run local fanout proxy strategy tests**
|
||||
|
||||
- [ ] **Step 3: Run full pytest suite**
|
||||
|
||||
- [ ] **Step 4: Inspect git diff for accidental unrelated changes**
|
||||
@@ -0,0 +1,54 @@
|
||||
# Proxy API Compatibility Design
|
||||
|
||||
## Goal
|
||||
|
||||
Refactor proxy API fetching into an automatic recognition, provider profile, and generic fallback parser pipeline. Users should be able to paste a provider's generated extraction API demo URL and get a normalized proxy list without hand-editing provider-specific parameters.
|
||||
|
||||
## Architecture
|
||||
|
||||
The public entry point remains `fetch_proxy_api(api_url, count, protocol, timeout=15)` so UI and buy-task code do not need broad changes. Internally it will resolve a `ProxyApiProfile`, build a conservative request URL, fetch text, and parse the response through provider-aware success checks plus a generic extractor.
|
||||
|
||||
The request builder will preserve the user's URL by default. A profile may fill blank or missing count, format, or protocol parameters only when that behavior is known for that provider. Unknown providers use `generic`, which never forces `count/format/protocol` onto the URL unless an existing compatible key is blank.
|
||||
|
||||
The parser accepts JSON objects, JSON arrays, and plain text. It extracts proxies from common fields and free-form text, normalizes them to `scheme://[user[:pass]@]host:port`, de-duplicates while preserving order, and raises clear Chinese errors when the provider reports failure or no proxy can be parsed.
|
||||
|
||||
## Provider Profiles
|
||||
|
||||
Initial first-class profiles:
|
||||
|
||||
- `youdaili`
|
||||
- `kuaidaili`
|
||||
- `zhima`
|
||||
- `xiaoxiang`
|
||||
- `xiequ`
|
||||
- `juliang`
|
||||
- `pinyi`
|
||||
- `zdaye`
|
||||
- `generic`
|
||||
|
||||
Profiles recognize URL domains and known parameter names. Profiles should stay small: host/path matching, request parameter aliases, success-code hints, and optional protocol mapping.
|
||||
|
||||
## Data Flow
|
||||
|
||||
1. UI or buy task calls `fetch_proxy_api`.
|
||||
2. `detect_proxy_api_profile(api_url)` chooses a profile.
|
||||
3. `build_proxy_api_request(api_url, count, protocol)` returns a URL and profile metadata.
|
||||
4. `requests.get` fetches the provider response as text.
|
||||
5. The parser tries JSON first, then text.
|
||||
6. Parsed proxies are passed unchanged to `ProxyManager.replace_proxy_list` or displayed in the UI.
|
||||
|
||||
## Error Handling
|
||||
|
||||
- Empty API URL raises `ProxyApiError("请先填写代理 API 地址")`.
|
||||
- HTTP status errors continue to use `requests` exceptions.
|
||||
- Provider failure payloads surface `msg`, `message`, `reason`, `ERRORMSG`, or similar fields.
|
||||
- Successful responses without parseable proxies raise `ProxyApiError("代理 API 返回成功,但没有解析到代理 IP 和端口")`.
|
||||
- Invalid proxy entries are ignored rather than aborting the whole response.
|
||||
|
||||
## Testing
|
||||
|
||||
Tests cover request-building conservatism, profile detection, JSON extraction, text extraction, provider failure detection, auth formats, IPv6, de-duplication, and the unchanged public `fetch_proxy_api` interface.
|
||||
|
||||
## Documentation Artifacts
|
||||
|
||||
Provider API demos and sanitized response samples live under `demos/proxy-api/`. Compatibility notes live in `docs/proxy-api-compatibility.md`.
|
||||
+1054
-1051
File diff suppressed because it is too large.
Load diff
+352
-102
@@ -1,143 +1,393 @@
|
||||
import pytest
|
||||
|
||||
import pytest
|
||||
|
||||
from util.proxy.ProxyManager import ProxyManager
|
||||
from util.proxy.ProxyApiProvider import (
|
||||
ProxyApiError,
|
||||
build_proxy_api_request,
|
||||
build_proxy_api_url,
|
||||
detect_proxy_api_profile,
|
||||
fetch_proxy_api,
|
||||
mask_proxy_api_url,
|
||||
parse_proxy_api_response,
|
||||
parse_proxy_api_response_text,
|
||||
)
|
||||
|
||||
|
||||
def _proxy_url(scheme: str, username: str, password: str, host: str, port: int) -> str:
|
||||
return f"{scheme}://" + f"{username}:{password}@" + f"{host}:{port}"
|
||||
|
||||
|
||||
def test_build_proxy_api_url_overrides_required_params():
|
||||
|
||||
|
||||
def _proxy_url(scheme: str, username: str, password: str, host: str, port: int) -> str:
|
||||
return f"{scheme}://" + f"{username}:{password}@" + f"{host}:{port}"
|
||||
|
||||
|
||||
def test_build_proxy_api_url_preserves_existing_provider_demo_params():
|
||||
original_url = (
|
||||
"https://dps.kdlapi.com/api/getdps/?client_id=demo"
|
||||
"&signed_value=a%2fb%2b%3d&num=7&format=text&sep=1"
|
||||
)
|
||||
url = build_proxy_api_url(
|
||||
original_url,
|
||||
count=3,
|
||||
protocol="socks5",
|
||||
)
|
||||
|
||||
assert url == original_url
|
||||
|
||||
|
||||
def test_build_proxy_api_url_fills_blank_known_params_only():
|
||||
url = build_proxy_api_url(
|
||||
"http://api.example.com/get?app_key=abc&count=&format=text&protocol=http",
|
||||
"http://api.youdaili.com/v1/proxy/get?client_id=&count=&format=&protocol=",
|
||||
count=3,
|
||||
protocol="socks5",
|
||||
)
|
||||
|
||||
assert url == (
|
||||
"http://api.example.com/get?app_key=abc&count=3&format=json&protocol=socks5"
|
||||
"http://api.youdaili.com/v1/proxy/get?client_id=&count=3&format=json&protocol=socks5"
|
||||
)
|
||||
|
||||
|
||||
def test_detect_proxy_api_profile_by_common_provider_hosts():
|
||||
samples = {
|
||||
"http://api.youdaili.com/v1/proxy/get?count=1": "youdaili",
|
||||
"https://dps.kdlapi.com/api/getdps/?num=1": "kuaidaili",
|
||||
"https://webapi.http.zhimacangku.com/getip?num=1&type=2": "zhima",
|
||||
"https://api.xiaoxiangdaili.com/ip/get?cnt=1&wt=json": "xiaoxiang",
|
||||
"http://api.xiequ.cn/VAD/GetIp.aspx?num=1": "xiequ",
|
||||
"https://v2.api.juliangip.com/dynamic/getips?num=1": "juliang",
|
||||
"https://share.proxy.qg.net/get?num=1": "qingguo",
|
||||
"https://proxy.16yun.cn/api?num=1": "yiniuyun",
|
||||
"http://tiqu.pyhttp.taolop.com/getflowip?count=1": "pinyi",
|
||||
"https://taiyang.example.com/api?num=1": "taiyang",
|
||||
"https://mogumiao.example.com/proxy?num=1": "mogu",
|
||||
"https://xundaili.example.com/api?num=1": "xundaili",
|
||||
"https://www.zdaye.com/dayProxy/ip/123/demo?count=1": "zdaye",
|
||||
"https://proxy.example.com/api?qty=1": "generic",
|
||||
}
|
||||
|
||||
for url, expected_profile in samples.items():
|
||||
assert detect_proxy_api_profile(url).name == expected_profile
|
||||
|
||||
|
||||
def test_build_proxy_api_request_adds_profile_specific_missing_params():
|
||||
request = build_proxy_api_request(
|
||||
"https://api.xiaoxiangdaili.com/ip/get",
|
||||
count=6,
|
||||
protocol="http",
|
||||
)
|
||||
|
||||
|
||||
def test_parse_youdaili_success_response_as_http_proxy():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"msg": "OK",
|
||||
"data": {
|
||||
"count": 1,
|
||||
"proxy_list": [
|
||||
{
|
||||
"ip": "8.8.8.8",
|
||||
"port": 12234,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
"http://8.8.8.8:12234"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_youdaili_success_response_as_socks_proxy():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"msg": "OK",
|
||||
"data": {
|
||||
"proxy_list": [
|
||||
{
|
||||
"ip": "8.8.8.8",
|
||||
"port": 12234,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="socks5") == [
|
||||
"socks5://8.8.8.8:12234"
|
||||
]
|
||||
|
||||
|
||||
assert request.profile.name == "xiaoxiang"
|
||||
assert request.url == (
|
||||
"https://api.xiaoxiangdaili.com/ip/get?cnt=6&wt=json"
|
||||
)
|
||||
|
||||
|
||||
def test_build_proxy_api_request_does_not_add_params_for_unknown_provider():
|
||||
request = build_proxy_api_request(
|
||||
"https://proxy.example.com/api?client_id=demo",
|
||||
count=6,
|
||||
protocol="socks5",
|
||||
)
|
||||
|
||||
assert request.profile.name == "generic"
|
||||
assert request.url == "https://proxy.example.com/api?client_id=demo"
|
||||
|
||||
|
||||
def test_parse_youdaili_success_response_as_http_proxy():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"msg": "OK",
|
||||
"data": {
|
||||
"count": 1,
|
||||
"proxy_list": [
|
||||
{
|
||||
"ip": "8.8.8.8",
|
||||
"port": 12234,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
"http://8.8.8.8:12234"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_youdaili_success_response_as_socks_proxy():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"msg": "OK",
|
||||
"data": {
|
||||
"proxy_list": [
|
||||
{
|
||||
"ip": "8.8.8.8",
|
||||
"port": 12234,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="socks5") == [
|
||||
"socks5://8.8.8.8:12234"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_from_standard_url():
|
||||
proxy = _proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.10", 15674)
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [proxy],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [proxy]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_from_host_port_user_pass():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"proxies": [
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [proxy],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [proxy]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_from_host_port_user_pass():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"proxies": [
|
||||
"192.0.2.20:15115:proxy_user:proxy_pass",
|
||||
],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
_proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.20", 15115)
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_from_object_fields():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [
|
||||
{
|
||||
"host": "192.0.2.30",
|
||||
"port": 15115,
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_from_object_fields():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [
|
||||
{
|
||||
"host": "192.0.2.30",
|
||||
"port": 15115,
|
||||
"Authkey": "proxy_user",
|
||||
"Authpwd": "proxy_pass",
|
||||
"protocol": "http",
|
||||
}
|
||||
],
|
||||
"protocol": "http",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
_proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.30", 15115)
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_merges_auth_fields_with_proxy_field():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [
|
||||
{
|
||||
"proxy": "192.0.2.40:15115",
|
||||
"Username": "proxy_user",
|
||||
"Password": "proxy_pass",
|
||||
"protocol": "http",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
_proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.40", 15115)
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_for_socks5_url():
|
||||
proxy = _proxy_url("socks5", "user", "pass", "127.0.0.1", 1080)
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [proxy],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="socks5") == [proxy]
|
||||
|
||||
|
||||
def test_parse_proxy_api_response_text_extracts_plain_lines():
|
||||
payload = """
|
||||
192.0.2.50:15115
|
||||
192.0.2.51:15116
|
||||
192.0.2.50:15115
|
||||
"""
|
||||
|
||||
assert parse_proxy_api_response_text(payload, protocol="http") == [
|
||||
"http://192.0.2.50:15115",
|
||||
"http://192.0.2.51:15116",
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_response_text_extracts_csv_like_rows():
|
||||
payload = "ip,port\n192.0.2.54,15119\n"
|
||||
|
||||
assert parse_proxy_api_response_text(payload, protocol="http") == [
|
||||
"http://192.0.2.54:15119"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_response_walks_nested_provider_payloads():
|
||||
payload = {
|
||||
"ERRORCODE": "0",
|
||||
"RESULT": {
|
||||
"rows": [
|
||||
{
|
||||
"IP": "192.0.2.60",
|
||||
"PORT": "15120",
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
_proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.30", 15115)
|
||||
"http://192.0.2.60:15120"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_response_accepts_json_list_root():
|
||||
payload = [
|
||||
{"ip": "192.0.2.61", "port": 15121},
|
||||
"192.0.2.62:15122",
|
||||
]
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="socks5") == [
|
||||
"socks5://192.0.2.61:15121",
|
||||
"socks5://192.0.2.62:15122",
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_merges_auth_fields_with_proxy_field():
|
||||
def test_parse_proxy_api_response_extracts_more_proxy_fields_and_ttl_metadata():
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [
|
||||
{
|
||||
"proxy": "192.0.2.40:15115",
|
||||
"Username": "proxy_user",
|
||||
"Password": "proxy_pass",
|
||||
"protocol": "http",
|
||||
"server": "192.0.2.64:15124",
|
||||
"proxy_ip": "198.51.100.64",
|
||||
"deadline": "2026-07-07 12:30:00",
|
||||
"ttl": 120,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http") == [
|
||||
_proxy_url("http", "proxy_user", "proxy_pass", "192.0.2.40", 15115)
|
||||
"http://192.0.2.64:15124"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_proxy_api_keeps_auth_for_socks5_url():
|
||||
proxy = _proxy_url("socks5", "user", "pass", "127.0.0.1", 1080)
|
||||
payload = {
|
||||
"code": 0,
|
||||
"data": [proxy],
|
||||
}
|
||||
def test_parse_proxy_api_response_text_accepts_xml_payload():
|
||||
payload = """
|
||||
<root>
|
||||
<code>0</code>
|
||||
<data>
|
||||
<item>
|
||||
<ip>192.0.2.65</ip>
|
||||
<port>15125</port>
|
||||
<expire_time>2026-07-07 12:30:00</expire_time>
|
||||
</item>
|
||||
</data>
|
||||
</root>
|
||||
"""
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="socks5") == [proxy]
|
||||
assert parse_proxy_api_response_text(payload, protocol="socks5") == [
|
||||
"socks5://192.0.2.65:15125"
|
||||
]
|
||||
|
||||
|
||||
def test_parse_youdaili_failure_response_raises():
|
||||
payload = {
|
||||
"code": 104,
|
||||
"msg": "未检索到满足要求的代理IP,请调整筛选条件后再试,或联系客服处理!",
|
||||
"data": None,
|
||||
}
|
||||
def test_parse_proxy_api_response_detects_provider_failure_codes():
|
||||
payload = {"ERRORCODE": "10055", "ERRORMSG": "提取数量不足"}
|
||||
|
||||
with pytest.raises(ProxyApiError, match="提取数量不足"):
|
||||
parse_proxy_api_response(payload, protocol="http")
|
||||
|
||||
|
||||
def test_parse_zdaye_success_code_10001_as_success():
|
||||
payload = {
|
||||
"code": "10001",
|
||||
"msg": "获取成功",
|
||||
"data": {
|
||||
"proxy_list": [
|
||||
{"ip": "192.0.2.63", "port": 15123},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
assert parse_proxy_api_response(payload, protocol="http", profile="zdaye") == [
|
||||
"http://192.0.2.63:15123"
|
||||
]
|
||||
|
||||
|
||||
def test_fetch_proxy_api_parses_text_response(monkeypatch):
|
||||
class Response:
|
||||
text = "192.0.2.70:15130\n192.0.2.71:15131"
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
captured = {}
|
||||
|
||||
def fake_request(method, url, headers, data, timeout):
|
||||
captured["method"] = method
|
||||
captured["url"] = url
|
||||
captured["timeout"] = timeout
|
||||
return Response()
|
||||
|
||||
monkeypatch.setattr("util.proxy.ProxyApiProvider.requests.request", fake_request)
|
||||
|
||||
result = fetch_proxy_api(
|
||||
"https://dps.kdlapi.com/api/getdps/?num=2&format=text",
|
||||
count=9,
|
||||
protocol="http",
|
||||
timeout=3,
|
||||
)
|
||||
|
||||
assert captured == {
|
||||
"method": "GET",
|
||||
"url": "https://dps.kdlapi.com/api/getdps/?num=2&format=text",
|
||||
"timeout": 3,
|
||||
}
|
||||
assert result.proxies == [
|
||||
"http://192.0.2.70:15130",
|
||||
"http://192.0.2.71:15131",
|
||||
]
|
||||
assert result.response == {"raw": Response.text}
|
||||
|
||||
|
||||
def test_fetch_proxy_api_result_can_replace_proxy_manager_pool(monkeypatch):
|
||||
class Response:
|
||||
text = "192.0.2.80:15140\n192.0.2.80:15140\n"
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"util.proxy.ProxyApiProvider.requests.request",
|
||||
lambda method, url, headers, data, timeout: Response(),
|
||||
)
|
||||
|
||||
manager = ProxyManager("none")
|
||||
result = fetch_proxy_api(
|
||||
"http://api.youdaili.com/v1/proxy/get?count=1&format=text",
|
||||
count=1,
|
||||
protocol="http",
|
||||
)
|
||||
manager.replace_proxy_list(",".join(result.proxies))
|
||||
|
||||
with pytest.raises(ProxyApiError):
|
||||
parse_proxy_api_response(payload, protocol="http")
|
||||
assert manager.proxy_list == ["http://192.0.2.80:15140"]
|
||||
assert result.proxy_records == [
|
||||
{
|
||||
"proxy": "http://192.0.2.80:15140",
|
||||
"host": "192.0.2.80",
|
||||
"port": "15140",
|
||||
"scheme": "http",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_mask_proxy_api_url_redacts_sensitive_query_values():
|
||||
assert mask_proxy_api_url(
|
||||
"https://example.com/get?key=&token=&num=2&signature="
|
||||
) == "https://example.com/get?key=***&token=***&num=2&signature=***"
|
||||
|
||||
|
||||
def test_parse_youdaili_failure_response_raises():
|
||||
payload = {
|
||||
"code": 104,
|
||||
"msg": "未检索到满足要求的代理IP,请调整筛选条件后再试,或联系客服处理!",
|
||||
"data": None,
|
||||
}
|
||||
|
||||
with pytest.raises(ProxyApiError):
|
||||
parse_proxy_api_response(payload, protocol="http")
|
||||
+954
-227
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user