type: 收敛测试 schemas 与协议层类型
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
3. 在 _PLATFORM_CREDENTIAL_FIELDS 中定义凭据字段
|
||||
"""
|
||||
|
||||
from typing import Optional, Type
|
||||
from typing import Any, Optional, Type, cast
|
||||
|
||||
from .base import BaseWhitelistAdapter, get_exit_ip_via_proxy
|
||||
from .xiequ import XiequAdapter
|
||||
@@ -22,19 +22,30 @@ _PLATFORM_REGISTRY: dict[str, Type[BaseWhitelistAdapter]] = {
|
||||
_PLATFORM_CREDENTIAL_FIELDS: dict[str, list[dict]] = {
|
||||
"xiequ": [
|
||||
{"key": "uid", "label": "UID", "placeholder": "如: 99769"},
|
||||
{"key": "ukey", "label": "UKEY", "placeholder": "如: C99371082B965B70F46DCAA87A04618B"},
|
||||
{
|
||||
"key": "ukey",
|
||||
"label": "UKEY",
|
||||
"placeholder": "如: C99371082B965B70F46DCAA87A04618B",
|
||||
},
|
||||
],
|
||||
"xkdaili": [
|
||||
{"key": "apikey", "label": "API Key", "placeholder": "如: XK86872CC0C85A415461"},
|
||||
{"key": "wl_sign", "label": "白名单签名(Sign)", "placeholder": "白名单接口专用,如: 8953b3082173aa4188e7403e81249027"},
|
||||
{
|
||||
"key": "apikey",
|
||||
"label": "API Key",
|
||||
"placeholder": "如: XK86872CC0C85A415461",
|
||||
},
|
||||
{
|
||||
"key": "wl_sign",
|
||||
"label": "白名单签名(Sign)",
|
||||
"placeholder": "白名单接口专用,如: 8953b3082173aa4188e7403e81249027",
|
||||
},
|
||||
{"key": "flag", "label": "Flag", "placeholder": "套餐标识,如: 8"},
|
||||
],
|
||||
}
|
||||
|
||||
# ── 平台显示名 ──
|
||||
_PLATFORM_LABELS: dict[str, str] = {
|
||||
name: cls(dict()).platform_label
|
||||
for name, cls in _PLATFORM_REGISTRY.items()
|
||||
name: cast(Any, cls)({}).platform_label for name, cls in _PLATFORM_REGISTRY.items()
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +54,7 @@ def create_adapter(platform: str, credentials: dict) -> Optional[BaseWhitelistAd
|
||||
cls = _PLATFORM_REGISTRY.get(platform)
|
||||
if not cls:
|
||||
return None
|
||||
return cls(credentials)
|
||||
return cast(Any, cls)(credentials)
|
||||
|
||||
|
||||
def get_platform_names() -> list[str]:
|
||||
|
||||
@@ -110,20 +110,26 @@ class BaseWhitelistAdapter(ABC):
|
||||
return True, msg
|
||||
|
||||
# 当前IP已存在但备注不同,不处理
|
||||
if any(r.get('ip') == current_ip for r in records):
|
||||
logger.info(f"白名单IP {current_ip} 已存在(备注不同),无需重复添加")
|
||||
if any(r.get("ip") == current_ip for r in records):
|
||||
logger.info(
|
||||
f"白名单IP {current_ip} 已存在(备注不同),无需重复添加"
|
||||
)
|
||||
return True, f"白名单IP已存在: {current_ip}"
|
||||
|
||||
# 超过上限,删除最老的
|
||||
if len(memo_ips) >= keep_recent:
|
||||
to_delete = memo_ips[:len(memo_ips) - keep_recent + 1]
|
||||
to_delete = memo_ips[: len(memo_ips) - keep_recent + 1]
|
||||
for old_ip in to_delete:
|
||||
if not old_ip:
|
||||
continue
|
||||
logger.info(f"白名单同备注IP超限,删除旧的: {old_ip}")
|
||||
self.delete_ip(old_ip)
|
||||
time.sleep(1)
|
||||
|
||||
# 添加新IP
|
||||
logger.info(f"白名单添加新出口IP: {current_ip} (当前 {len(memo_ips)} 个同备注)")
|
||||
logger.info(
|
||||
f"白名单添加新出口IP: {current_ip} (当前 {len(memo_ips)} 个同备注)"
|
||||
)
|
||||
ok, resp = self.add_ip(current_ip)
|
||||
if ok:
|
||||
msg = f"白名单IP已添加: {current_ip}"
|
||||
@@ -152,10 +158,12 @@ def _get_local_exit_ip() -> Optional[str]:
|
||||
]
|
||||
for url in targets:
|
||||
try:
|
||||
response = requests.get(url, timeout=8, headers={"User-Agent": "Mozilla/5.0"})
|
||||
response = requests.get(
|
||||
url, timeout=8, headers={"User-Agent": "Mozilla/5.0"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
text = response.text.strip()
|
||||
match = re.search(r'(\d{1,3}(?:\.\d{1,3}){3})', text)
|
||||
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
except Exception:
|
||||
@@ -174,7 +182,9 @@ def get_exit_ip_via_proxy(proxy: str) -> Optional[str]:
|
||||
for url in targets:
|
||||
try:
|
||||
response = requests.get(
|
||||
url, proxies=proxies, timeout=6,
|
||||
url,
|
||||
proxies=proxies,
|
||||
timeout=6,
|
||||
headers={"User-Agent": "Mozilla/5.0"},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
Reference in New Issue
Block a user