/rotate endpoint to accept city list and update README. Enhance rotate function to handle multiple cities and add new endpoint to retrieve available cities.
JD EIP 代理轮换服务 (FastAPI)
基于京东 EIP API 的代理 IP 自动轮换后端。支持:
- 鉴权获取
X-Token - 查询城市与设备
- 设置网关链路,自动切换到未使用过的 IP(按天去重)
- 使用 Redis 存储每日已使用 IP 与状态
运行
- 创建并编辑
.env(参考.env.example) - 安装依赖:
pip install -r requirements.txt
- 启动:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
目录结构
app/
config.py # 配置与环境变量
eip_client.py # 与 JD EIP API 交互
redis_store.py # Redis 存取封装
rotation_service.py # 轮换逻辑
routers/
proxy.py # /proxy API 路由
main.py # FastAPI 入口
API 概览
- GET
/health健康检查 - POST
/proxy/rotate执行轮换(可传 cityhash/num) - GET
/proxy/status当前状态
POST /proxy/rotate
-
请求体(JSON):
cityhash(可选,string):城市哈希;若不传,将使用环境变量EIP_DEFAULT_CITYHASH。num(可选,int):拉取设备数量上限;若不传,使用环境变量EIP_DEFAULT_NUM,再不设则默认 10。
-
行为说明:
- 根据
cityhash调用设备列表接口(数量为num),从中选择「今天未使用过」的第一个 IP。 - 将选中设备的
edge配置到网关规则中,并把该 IP 记录为当天已使用。 - 若没有可用且今天未使用的 IP,则返回未变更原因。
- 根据
-
请求示例:
curl -X POST 'http://localhost:8000/proxy/rotate' \
-H 'Content-Type: application/json' \
-d '{"id": 1}'
- 响应示例(变更成功):
{
"changed": true,
"ip": "1.2.3.4",
"edge": "edge-id-xxx",
"status": { "...": "gateway_status_payload" }
}
- 响应示例(无可用 IP):
{
"changed": false,
"reason": "没有可用且今天未使用的 IP"
}
备注
- EIP 详细接口见
API.md
Description
Languages
Python
77%
HTML
23%