jdeip/README.md

75 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## JD EIP 代理轮换服务 (FastAPI)
基于京东 EIP API 的代理 IP 自动轮换后端。支持:
- 鉴权获取 `X-Token`
- 查询城市与设备
- 设置网关链路,自动切换到未使用过的 IP按天去重
- 使用 Redis 存储每日已使用 IP 与状态
### 运行
1. 创建并编辑 `.env`(参考 `.env.example`
2. 安装依赖:
```bash
pip install -r requirements.txt
```
3. 启动:
```bash
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则返回未变更原因。
- 请求示例:
```bash
curl -X POST 'http://localhost:8000/proxy/rotate' \
-H 'Content-Type: application/json' \
-d '{"id": 1}'
```
- 响应示例(变更成功):
```json
{
"changed": true,
"ip": "1.2.3.4",
"edge": "edge-id-xxx",
"status": { "...": "gateway_status_payload" }
}
```
- 响应示例(无可用 IP
```json
{
"changed": false,
"reason": "没有可用且今天未使用的 IP"
}
```
### 备注
- EIP 详细接口见 `API.md`