|
|
@@ -0,0 +1,459 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+import pytest
|
|
|
+import pytest_asyncio
|
|
|
+import time
|
|
|
+from datetime import datetime, timedelta
|
|
|
+from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
|
+from sqlalchemy import select
|
|
|
+from fastapi.testclient import TestClient
|
|
|
+
|
|
|
+from app.core.database import Base, get_db
|
|
|
+from app.core.auth import get_current_user, require_min_role, RoleLevel
|
|
|
+from app.models.proxy_reputation import ProxyReputation, ProxyTelemetryLog
|
|
|
+from app.services.proxy_reputation_service import ProxyReputationService
|
|
|
+from app.schemas.proxy_reputation import ProxyTelemetryReportRequest
|
|
|
+from app.main import app
|
|
|
+
|
|
|
+# 使用 SQLite 内存数据库进行测试
|
|
|
+TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
|
|
+
|
|
|
+test_engine = create_async_engine(TEST_DATABASE_URL, echo=False)
|
|
|
+TestSessionLocal = async_sessionmaker(
|
|
|
+ bind=test_engine,
|
|
|
+ class_=AsyncSession,
|
|
|
+ autoflush=False,
|
|
|
+ expire_on_commit=False,
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+@pytest_asyncio.fixture(scope="function")
|
|
|
+async def async_db():
|
|
|
+ async with test_engine.begin() as conn:
|
|
|
+ await conn.run_sync(Base.metadata.create_all)
|
|
|
+
|
|
|
+ async with TestSessionLocal() as session:
|
|
|
+ yield session
|
|
|
+
|
|
|
+ async with test_engine.begin() as conn:
|
|
|
+ await conn.run_sync(Base.metadata.drop_all)
|
|
|
+
|
|
|
+
|
|
|
+class DummyUser:
|
|
|
+ id = 1
|
|
|
+ role = "user"
|
|
|
+
|
|
|
+
|
|
|
+@pytest.fixture
|
|
|
+def test_client():
|
|
|
+ async def override_get_db():
|
|
|
+ async with TestSessionLocal() as session:
|
|
|
+ yield session
|
|
|
+
|
|
|
+ app.dependency_overrides[get_db] = override_get_db
|
|
|
+ app.dependency_overrides[get_current_user] = lambda: DummyUser()
|
|
|
+ app.dependency_overrides[require_min_role(RoleLevel.user)] = lambda: DummyUser()
|
|
|
+
|
|
|
+ with TestClient(app) as client:
|
|
|
+ yield client
|
|
|
+
|
|
|
+ app.dependency_overrides.clear()
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_new_ip_telemetry_initialization(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试新 IP 首次上报初始化:
|
|
|
+ 初始分 60 分,状态为 probation,流水表与信誉表正常生成
|
|
|
+ """
|
|
|
+ payload = ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name="vfs_plugin",
|
|
|
+ egress_ip="104.28.198.42",
|
|
|
+ local_port=16001,
|
|
|
+ node_name="HK_01",
|
|
|
+ action="spawn",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ error_type=None,
|
|
|
+ latency_ms=350,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ )
|
|
|
+
|
|
|
+ recorded = await ProxyReputationService.record_telemetry(async_db, payload)
|
|
|
+ assert recorded is True
|
|
|
+
|
|
|
+ # 验证信誉表数据
|
|
|
+ res = await async_db.execute(
|
|
|
+ select(ProxyReputation).where(ProxyReputation.egress_ip == "104.28.198.42")
|
|
|
+ )
|
|
|
+ rep = res.scalar_one_or_none()
|
|
|
+ assert rep is not None
|
|
|
+ assert rep.plugin_name == "vfs_plugin"
|
|
|
+ # 初始 60 分 + 成功 1 次 (5分) = 65分
|
|
|
+ assert rep.score == 65
|
|
|
+ assert rep.state == "probation"
|
|
|
+ assert rep.total_calls == 1
|
|
|
+ assert rep.success_calls == 1
|
|
|
+ assert rep.consecutive_successes == 1
|
|
|
+ assert rep.consecutive_fails == 0
|
|
|
+
|
|
|
+ # 验证流水日志
|
|
|
+ logs = (await async_db.execute(select(ProxyTelemetryLog))).scalars().all()
|
|
|
+ assert len(logs) == 1
|
|
|
+ assert logs[0].egress_ip == "104.28.198.42"
|
|
|
+ assert logs[0].http_code == 200
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_success_score_increment_and_promotion_to_trusted(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试加分与晋升规则:
|
|
|
+ 一次成功调用 +5 分;连续成功 3 次且分数 >= 75 晋升为 trusted;分数上限 100
|
|
|
+ """
|
|
|
+ ip = "104.28.198.50"
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+
|
|
|
+ # 第 1 次成功:初始 60 + 5 = 65,consecutive = 1
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ # 第 2 次成功:65 + 5 = 70,consecutive = 2
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ assert rep.score == 70
|
|
|
+ assert rep.state == "probation"
|
|
|
+ assert rep.consecutive_successes == 2
|
|
|
+
|
|
|
+ # 第 3 次成功:70 + 5 = 75,consecutive = 3 -> 满足 >= 75 且 consecutive >= 3,晋升为 trusted
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ await async_db.refresh(rep)
|
|
|
+ assert rep.score == 75
|
|
|
+ assert rep.state == "trusted"
|
|
|
+ assert rep.consecutive_successes == 3
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_exempt_system_errors(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试目标系统严重故障豁免:
|
|
|
+ 若 http_code 为 500, 502, 503, 504 或 Cloudflare 520~526,绝对不扣减 IP 分数
|
|
|
+ """
|
|
|
+ ip = "104.28.198.60"
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+
|
|
|
+ # 初始化 IP(初始 60 分)
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ assert rep.score == 65
|
|
|
+
|
|
|
+ # 上报 502 Bad Gateway 失败
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=False,
|
|
|
+ http_code=502,
|
|
|
+ error_type="Bad Gateway",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ await async_db.refresh(rep)
|
|
|
+ # 分数依然维持 65 分,不扣分
|
|
|
+ assert rep.score == 65
|
|
|
+ assert rep.consecutive_fails == 0
|
|
|
+
|
|
|
+ # 上报 Cloudflare 521 Web server is down
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=False,
|
|
|
+ http_code=521,
|
|
|
+ error_type="Web server is down",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ await async_db.refresh(rep)
|
|
|
+ assert rep.score == 65
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_waf_blocked_penalty_and_blocked_state(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试命中 WAF 阻断扣除 40 分并降级拉黑:
|
|
|
+ 命中 403202 / cf-mitigated / WAF Blocked 直接扣 40 分,分数 < 40 降级为 blocked 并设置 30分钟 TTL
|
|
|
+ """
|
|
|
+ ip = "104.28.198.70"
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+
|
|
|
+ # 新 IP 初始 60 分,遭遇 WAF 阻断
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="spawn",
|
|
|
+ success=False,
|
|
|
+ http_code=403,
|
|
|
+ error_type="cf-mitigated (403202)",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ # 60 - 40 = 20 分
|
|
|
+ assert rep.score == 20
|
|
|
+ assert rep.state == "blocked"
|
|
|
+ assert rep.unblock_time is not None
|
|
|
+ # unblock_time 应约为当前时间后 30 分钟
|
|
|
+ assert rep.unblock_time > datetime.utcnow() + timedelta(minutes=25)
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_general_error_penalty_and_trusted_downgrade(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试普通错误扣除 2 分以及 trusted 降级为 probation
|
|
|
+ """
|
|
|
+ ip = "104.28.198.80"
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+
|
|
|
+ # 先将其提升至 trusted (75分)
|
|
|
+ for _ in range(3):
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=True,
|
|
|
+ http_code=200,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ assert rep.score == 75
|
|
|
+ assert rep.state == "trusted"
|
|
|
+
|
|
|
+ # 发生普通业务超时错误,扣除 2 分 -> 73 分,降级回 probation
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="query",
|
|
|
+ success=False,
|
|
|
+ http_code=408,
|
|
|
+ error_type="Network Timeout",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ await async_db.refresh(rep)
|
|
|
+ assert rep.score == 73
|
|
|
+ assert rep.state == "probation"
|
|
|
+ assert rep.consecutive_fails == 1
|
|
|
+ assert rep.consecutive_successes == 0
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_half_open_revival_after_ttl(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试黑名单 30 分钟 TTL 自动半开复活 (Half-Open Revival):
|
|
|
+ 进入 blocked 的 IP 在 30 分钟后自动重置为 probation(恢复至 50 分)
|
|
|
+ """
|
|
|
+ ip = "104.28.198.90"
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+
|
|
|
+ # 触发拉黑
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_01",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=ip,
|
|
|
+ action="spawn",
|
|
|
+ success=False,
|
|
|
+ http_code=403,
|
|
|
+ error_type="cf-mitigated",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ assert rep.state == "blocked"
|
|
|
+
|
|
|
+ # 模拟时间流逝:将 unblock_time 修改为 5 分钟前(已过 TTL)
|
|
|
+ rep.unblock_time = datetime.utcnow() - timedelta(minutes=5)
|
|
|
+ await async_db.commit()
|
|
|
+
|
|
|
+ # 触发查询字典接口
|
|
|
+ map_resp = await ProxyReputationService.get_reputation_map(async_db, plugin)
|
|
|
+ assert ip in map_resp.reputation_map
|
|
|
+ item = map_resp.reputation_map[ip]
|
|
|
+ # 已半开复活
|
|
|
+ assert item.state == "probation"
|
|
|
+ assert item.score == 50
|
|
|
+
|
|
|
+ # 验证数据库中也已更新
|
|
|
+ await async_db.refresh(rep)
|
|
|
+ assert rep.state == "probation"
|
|
|
+ assert rep.score == 50
|
|
|
+ assert rep.unblock_time is None
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_global_outage_breaker(async_db: AsyncSession):
|
|
|
+ """
|
|
|
+ 测试全局目标故障保护 (Global Outage Breaker):
|
|
|
+ 全网 3 分钟内失败率 > 85% 时,冻结打分与拉黑
|
|
|
+ """
|
|
|
+ plugin = "vfs_plugin"
|
|
|
+ # 模拟在最近 1 分钟内,其他节点产生了大面积失败(10 次全部失败,失败率 100%)
|
|
|
+ now = datetime.utcnow()
|
|
|
+ for i in range(10):
|
|
|
+ log = ProxyTelemetryLog(
|
|
|
+ client_id=f"Client_{i}",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=f"192.168.1.{i}",
|
|
|
+ action="query",
|
|
|
+ success=False,
|
|
|
+ http_code=403,
|
|
|
+ error_type="cf-mitigated",
|
|
|
+ latency_ms=1000,
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ created_at=now,
|
|
|
+ )
|
|
|
+ async_db.add(log)
|
|
|
+ await async_db.commit()
|
|
|
+
|
|
|
+ # 此时有一个正常 IP (score=60) 上报失败
|
|
|
+ test_ip = "104.28.198.100"
|
|
|
+ await ProxyReputationService.record_telemetry(
|
|
|
+ async_db,
|
|
|
+ ProxyTelemetryReportRequest(
|
|
|
+ client_id="Client_X",
|
|
|
+ plugin_name=plugin,
|
|
|
+ egress_ip=test_ip,
|
|
|
+ action="query",
|
|
|
+ success=False,
|
|
|
+ http_code=403,
|
|
|
+ error_type="cf-mitigated",
|
|
|
+ timestamp=int(time.time()),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+
|
|
|
+ res = await async_db.execute(select(ProxyReputation).where(ProxyReputation.egress_ip == test_ip))
|
|
|
+ rep = res.scalar_one()
|
|
|
+ # 因为处于全网故障保护期,分数冻结,不扣 40 分,不拉黑
|
|
|
+ assert rep.score == 60
|
|
|
+ assert rep.state == "probation"
|
|
|
+
|
|
|
+
|
|
|
+def test_http_api_endpoints(test_client):
|
|
|
+ """
|
|
|
+ 测试 HTTP 接口:
|
|
|
+ 1. POST /api/proxy/telemetry
|
|
|
+ 2. GET /api/proxy/reputation/{plugin_name}
|
|
|
+ """
|
|
|
+ import asyncio
|
|
|
+ async def setup_db():
|
|
|
+ async with test_engine.begin() as conn:
|
|
|
+ await conn.run_sync(Base.metadata.create_all)
|
|
|
+ asyncio.run(setup_db())
|
|
|
+
|
|
|
+ # 1. 代理指标埋点上报 (POST /api/proxy/telemetry)
|
|
|
+ payload = {
|
|
|
+ "client_id": "Jerry_Win11_PC",
|
|
|
+ "plugin_name": "vfs_plugin",
|
|
|
+ "local_port": 16001,
|
|
|
+ "node_name": "🇭🇰 香港 01丨1x HK",
|
|
|
+ "egress_ip": "104.28.198.42",
|
|
|
+ "action": "spawn",
|
|
|
+ "success": True,
|
|
|
+ "http_code": 200,
|
|
|
+ "error_type": None,
|
|
|
+ "latency_ms": 1150,
|
|
|
+ "timestamp": 1758985800,
|
|
|
+ }
|
|
|
+
|
|
|
+ resp = test_client.post(
|
|
|
+ "/api/proxy/telemetry",
|
|
|
+ json=payload,
|
|
|
+ headers={"Authorization": "Bearer tok_test_token"},
|
|
|
+ )
|
|
|
+ assert resp.status_code == 200
|
|
|
+ data = resp.json()
|
|
|
+ assert data["code"] == 0
|
|
|
+ assert data["message"] == "success"
|
|
|
+ assert data["data"]["recorded"] is True
|
|
|
+
|
|
|
+ # 2. 插件代理信誉字典查询 (GET /api/proxy/reputation/vfs_plugin)
|
|
|
+ resp2 = test_client.get(
|
|
|
+ "/api/proxy/reputation/vfs_plugin",
|
|
|
+ headers={"Authorization": "Bearer tok_test_token"},
|
|
|
+ )
|
|
|
+ assert resp2.status_code == 200
|
|
|
+ data2 = resp2.json()
|
|
|
+ assert data2["code"] == 0
|
|
|
+ assert data2["data"]["plugin_name"] == "vfs_plugin"
|
|
|
+ assert "version" in data2["data"]
|
|
|
+ assert "104.28.198.42" in data2["data"]["reputation_map"]
|
|
|
+
|
|
|
+ ip_info = data2["data"]["reputation_map"]["104.28.198.42"]
|
|
|
+ assert ip_info["score"] == 65
|
|
|
+ assert ip_info["state"] == "probation"
|
|
|
+ assert ip_info["success_rate"] == 1.0
|
|
|
+ assert ip_info["last_seen"] == 1758985800
|