test_proxy_manager.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import sys
  3. import time
  4. import unittest
  5. from unittest.mock import MagicMock, patch
  6. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  7. if project_root not in sys.path:
  8. sys.path.insert(0, project_root)
  9. from toolkit.proxy_manager import ProxyManager
  10. from core_types import Proxy, BizLogicError
  11. from toolkit.cloud_api import CloudApi
  12. class TestProxyManager(unittest.TestCase):
  13. def setUp(self):
  14. self.pm = ProxyManager.Instance()
  15. # 清理可能存在的隔离状态
  16. self.pm._quarantine.clear()
  17. self.pm._sliding_history.clear()
  18. self.pm._reputation_cache.clear()
  19. # Mock 掉耗时的真实外部探测与云端 HTTP 调用
  20. self.patcher_probe = patch.object(self.pm, "_async_probe_egress_ip", return_value=None)
  21. self.patcher_probe.start()
  22. self.patcher_rep = patch.object(CloudApi.Instance(), "get_proxy_reputation_map", return_value={})
  23. self.patcher_rep.start()
  24. self.patcher_report = patch.object(CloudApi.Instance(), "report_proxy_metric", return_value=True)
  25. self.patcher_report.start()
  26. def tearDown(self):
  27. self.patcher_probe.stop()
  28. self.patcher_rep.stop()
  29. self.patcher_report.stop()
  30. def test_local_proxy_single(self):
  31. # 测试 local 池 (单端口)
  32. res = self.pm.get_next_proxy(["local:local"])
  33. self.assertIsInstance(res, dict)
  34. self.assertEqual(res["ip"], "127.0.0.1")
  35. self.assertEqual(res["port"], 7897)
  36. proxy_obj = Proxy(**res)
  37. self.assertEqual(proxy_obj.port, 7897)
  38. def test_local_proxy_multi_pool(self):
  39. # 测试 local_multi_pool
  40. res = self.pm.get_next_proxy(["local:local_multi_pool"])
  41. self.assertIsInstance(res, dict)
  42. self.assertEqual(res["ip"], "127.0.0.1")
  43. self.assertTrue(res["port"] >= 10000)
  44. proxy_obj = Proxy(**res)
  45. self.assertTrue(proxy_obj.port >= 10000)
  46. def test_invalid_prefix(self):
  47. # 测试未带前缀的非法格式,必须抛出 BizLogicError
  48. with self.assertRaises(BizLogicError) as ctx:
  49. self.pm.get_next_proxy(["proxy-cheap"])
  50. self.assertIn("Must start with 'local:' or 'cloud:'", str(ctx.exception))
  51. def test_nonexistent_local_pool(self):
  52. # 测试不存在的本地池
  53. with self.assertRaises(BizLogicError) as ctx:
  54. self.pm.get_next_proxy(["local:non_existent_pool_12345"])
  55. self.assertIn("No proxies found in local pools", str(ctx.exception))
  56. def test_reputation_quarantine_and_revival(self):
  57. """测试专属 WAF 阻断进入隔离名单,以及成功后自愈复活"""
  58. plugin = "vfs_plugin"
  59. proxy = self.pm.get_next_proxy(["local:local_multi_pool"], plugin_name=plugin)
  60. port = proxy["port"]
  61. egress_ip = proxy["egress_ip"]
  62. # 模拟一次 403202 cf-mitigated 专属阻断
  63. self.pm.mark_proxy_result(
  64. proxy=proxy,
  65. plugin_name=plugin,
  66. action="spawn",
  67. success=False,
  68. http_code=403,
  69. error_type="cf-mitigated (403202)"
  70. )
  71. # 验证该 IP 已进入隔离名单
  72. self.assertIn(egress_ip, self.pm._quarantine.get(plugin, {}))
  73. # 模拟下一次该 IP 执行成功,验证自愈复活
  74. self.pm.mark_proxy_result(
  75. proxy=proxy,
  76. plugin_name=plugin,
  77. action="spawn",
  78. success=True,
  79. http_code=200
  80. )
  81. self.assertNotIn(egress_ip, self.pm._quarantine.get(plugin, {}))
  82. def test_site_outage_freeze(self):
  83. """测试 Layer 1: 全站故障时冻结扣分与隔离"""
  84. plugin = "vfs_plugin"
  85. # 构造跨多个不同 IP 的连续系统级故障 (502 / 503)
  86. for i in range(6):
  87. fake_proxy = {"port": 16000 + i, "egress_ip": f"104.28.1.{i}", "name": f"node_{i}"}
  88. self.pm.mark_proxy_result(
  89. proxy=fake_proxy,
  90. plugin_name=plugin,
  91. action="spawn",
  92. success=False,
  93. http_code=502,
  94. error_type="Bad Gateway"
  95. )
  96. # 此时全站故障已触发
  97. self.assertTrue(self.pm._is_site_outage(plugin))
  98. # 在全站故障状态下,即使有节点发生 WAF 阻断,也不会被拉黑隔离
  99. new_proxy = {"port": 16099, "egress_ip": "104.28.99.99", "name": "node_99"}
  100. self.pm.mark_proxy_result(
  101. proxy=new_proxy,
  102. plugin_name=plugin,
  103. action="spawn",
  104. success=False,
  105. http_code=403,
  106. error_type="cf-mitigated (403202)"
  107. )
  108. self.assertNotIn("104.28.99.99", self.pm._quarantine.get(plugin, {}))
  109. def test_minimum_viable_pool_anti_starvation(self):
  110. """测试 Layer 3: 最低可用保底池防雪崩,过度拉黑时强制重置隔离名单"""
  111. plugin = "test_plugin"
  112. # 人为把隔离名单填满
  113. self.pm._quarantine[plugin] = {
  114. f"port_{16000 + i}": time.time() + 1800 for i in range(1000)
  115. }
  116. # 获取下一个代理,不应该抛出异常,应该触发防雪崩重置并成功返回代理
  117. proxy = self.pm.get_next_proxy(["local:local_multi_pool"], plugin_name=plugin)
  118. self.assertIsInstance(proxy, dict)
  119. self.assertTrue(proxy["port"] >= 10000)
  120. if __name__ == "__main__":
  121. unittest.main()