Refactor /rotate endpoint to accept city list and update README. Enhance rotate function to handle multiple cities and add new endpoint to retrieve available cities.

This commit is contained in:
wangqifan 2025-10-21 19:30:11 +08:00
parent 9c6d4033bb
commit 102ac41025
3 changed files with 27 additions and 20 deletions

View File

@ -48,7 +48,7 @@ app/
```bash ```bash
curl -X POST 'http://localhost:8000/proxy/rotate' \ curl -X POST 'http://localhost:8000/proxy/rotate' \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"cityhash": "310000", "num": 20}' -d '{"id": 1}'
``` ```
- 响应示例(变更成功): - 响应示例(变更成功):

View File

@ -82,7 +82,7 @@ def apply_gateway_route(edge_id: Optional[str], ip: str, geo: str, client_id:str
"table": 1, # 路由表ID "table": 1, # 路由表ID
"enable": True, # 启用规则 "enable": True, # 启用规则
"edge": [edge_id] if edge_id else [], # 边缘设备列表 "edge": [edge_id] if edge_id else [], # 边缘设备列表
"network": [client_infos[client_id]], # 网络配置(当前为空) "network": [client_infos[str(client_id)]], # 网络配置(当前为空)
"cityhash": geo or "", # 城市哈希值 "cityhash": geo or "", # 城市哈希值
} }
config = {"id": 1, "rules": [rule]} # 配置ID和规则列表 config = {"id": 1, "rules": [rule]} # 配置ID和规则列表
@ -91,7 +91,7 @@ def apply_gateway_route(edge_id: Optional[str], ip: str, geo: str, client_id:str
def rotate(client_id) -> Dict[str, Any]: def rotate(client_id,cities) -> Dict[str, Any]:
""" """
执行IP轮换操作 执行IP轮换操作
@ -113,10 +113,10 @@ def rotate(client_id) -> Dict[str, Any]:
ValueError: 当cityhash为空时 ValueError: 当cityhash为空时
""" """
# 确定地理位置参数 # 确定地理位置参数
geo = get_random_cityhash() geo = get_random_cityhash(cities)
# 确定获取设备数量 # 确定获取设备数量
n = 1 n = 1000
# 获取设备列表 # 获取设备列表
devices = eip.list_devices(geo=geo, offset=0, num=n) devices = eip.list_devices(geo=geo, offset=0, num=n)
@ -139,6 +139,9 @@ def rotate(client_id) -> Dict[str, Any]:
return {"changed": True, "ip": ip, "edge": edge_id, "status": status, "geo": geo} return {"changed": True, "ip": ip, "edge": edge_id, "status": status, "geo": geo}
def citie_list():
return {"data":list(city_dict.keys())}
def status() -> Dict[str, Any]: def status() -> Dict[str, Any]:
""" """
获取当前轮换服务状态 获取当前轮换服务状态
@ -168,7 +171,7 @@ def status() -> Dict[str, Any]:
return {"current": cur, "used_today": used_count, "gateway": gw} return {"current": cur, "used_today": used_count, "gateway": gw}
def get_random_city() -> Dict[str, str]: def get_random_city(city_list) -> Dict[str, str]:
""" """
随机获取一个城市编码 随机获取一个城市编码
@ -183,12 +186,13 @@ def get_random_city() -> Dict[str, str]:
# 收集所有城市信息 # 收集所有城市信息
all_cities = [] all_cities = []
for province, cities in city_dict.items(): for province, cities in city_dict.items():
for city, cityhash in cities.items(): if province in city_list:
all_cities.append({ for city, cityhash in cities.items():
"province": province, all_cities.append({
"city": city, "province": province,
"cityhash": cityhash "city": city,
}) "cityhash": cityhash
})
# 随机选择一个城市 # 随机选择一个城市
if not all_cities: if not all_cities:
@ -198,14 +202,14 @@ def get_random_city() -> Dict[str, str]:
return selected_city return selected_city
def get_random_cityhash() -> str: def get_random_cityhash(cities) -> str:
""" """
随机获取一个城市编码简化版本 随机获取一个城市编码简化版本
Returns: Returns:
str: 随机城市编码 str: 随机城市编码
""" """
city_info = get_random_city() city_info = get_random_city(cities)
return city_info["cityhash"] return city_info["cityhash"]

View File

@ -3,15 +3,15 @@ from typing import Optional
from fastapi import APIRouter, Body, Query, Form,Request from fastapi import APIRouter, Body, Query, Form,Request
from pydantic import BaseModel from pydantic import BaseModel
from ..rotation_service import rotate as rotate_impl, status as status_impl from ..rotation_service import rotate as rotate_impl, status as status_impl ,citie_list as cities_impl
router = APIRouter() router = APIRouter()
class RotateRequest(BaseModel): class RotateRequest(BaseModel):
cityhash: Optional[str] = None id: Optional[int] = None
num: Optional[int] = None citys: Optional[str] = None
@router.post("/rotate") @router.post("/rotate")
@ -20,9 +20,10 @@ def rotate(
): ):
# 优先级Query > Form > JSON # 优先级Query > Form > JSON
client_id = req.id client_id = req.id
cities = str(req.citys).split(',')
# effective_cityhash = req.cityhash # effective_cityhash = req.cityhash
# effective_num = req.num # effective_num = req.num
result = rotate_impl(client_id=client_id) result = rotate_impl(client_id=client_id,cities =cities)
return result return result
@ -30,4 +31,6 @@ def rotate(
def get_status(): def get_status():
return status_impl() return status_impl()
@router.get("/cities")
def get_cities():
return cities_impl()