protector.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """Background protector: periodically checks protector_list entries and blocks offending dst_addr via advanced_acl.add_ip
  2. """
  3. from __future__ import annotations
  4. import threading
  5. import time
  6. from typing import Optional
  7. import advanced_acl
  8. from auth_session import load_config
  9. import protector_list
  10. from analysis_connections import find_high_connections_for_src_ports
  11. from log_util import get_logger
  12. logger = get_logger("protector")
  13. class ProtectorRunner:
  14. def __init__(self):
  15. cfg = load_config()
  16. self.interval = int(cfg.get("scan_interval", 60))
  17. self._stop = threading.Event()
  18. self.last_run_time: Optional[float] = None
  19. # pause control: _pause_event is set when running, cleared when paused
  20. self._pause_event = threading.Event()
  21. self._pause_event.set()
  22. def run_once(self):
  23. # if paused, skip immediate execution (protect against external triggers)
  24. try:
  25. if not self._pause_event.is_set():
  26. logger.info("Protector is paused, skipping run_once")
  27. return
  28. except Exception:
  29. pass
  30. # record start time to help external coordinators avoid duplicate runs
  31. try:
  32. self.last_run_time = time.time()
  33. except Exception:
  34. pass
  35. items = protector_list.load_list()
  36. for entry in items:
  37. target = entry.get("target_ip")
  38. src_port = int(entry.get("src_port"))
  39. threshold = entry.get("threshold")
  40. try:
  41. res = find_high_connections_for_src_ports(target, src_port, threshold=threshold)
  42. except Exception as e:
  43. logger.exception(f"检查失败 {target}:{src_port} - {e}")
  44. continue
  45. matches = res.get("by_src_port", {}).get(int(src_port), [])
  46. if not matches:
  47. logger.info(f"{target}:{src_port} - 无异常连接")
  48. continue
  49. for m in matches:
  50. dst = m.get("dst_addr")
  51. # Call advanced_acl.add_ip to block
  52. try:
  53. # Do not pass custom comment here so add_ip will place the IP into Test_* groups
  54. # (comment was causing new rules to be named AutoBlock_..., not grouped).
  55. result = advanced_acl.add_ip(dst)
  56. # Support both legacy (resp, data) tuple and new dict result
  57. if isinstance(result, dict):
  58. added = result.get("added")
  59. rule = result.get("rule")
  60. row_id = result.get("row_id")
  61. msg = result.get("message")
  62. logger.info(f"已尝试阻断 {dst}, added={added}, rule={rule}, row_id={row_id}, msg={msg}")
  63. else:
  64. # assume (resp, data)
  65. resp, data = result
  66. logger.info(f"已尝试阻断 {dst}, 状态: {resp.status_code}, 返回: {data}")
  67. except Exception as e:
  68. logger.exception(f"阻断失败 {dst}: {e}")
  69. def start(self):
  70. self._stop.clear()
  71. # mark last_run_time when loop is started so external callers can compute
  72. # next run time predictably (will be updated when run_once actually runs)
  73. try:
  74. self.last_run_time = time.time()
  75. except Exception:
  76. pass
  77. def _loop():
  78. while not self._stop.is_set():
  79. # respect pause state
  80. if not self._pause_event.is_set():
  81. # paused: wait until unpaused or stopped
  82. # wake every 1s to check stop flag
  83. self._pause_event.wait(timeout=1)
  84. continue
  85. try:
  86. self.run_once()
  87. except Exception:
  88. logger.exception("运行一次保护检查失败")
  89. # reload interval in case config changed
  90. try:
  91. cfg = load_config()
  92. self.interval = int(cfg.get("scan_interval", self.interval))
  93. except Exception:
  94. pass
  95. # sleep in one-second steps so we can be responsive to pause/stop
  96. slept = 0
  97. while slept < self.interval and not self._stop.is_set():
  98. if not self._pause_event.is_set():
  99. break
  100. time.sleep(1)
  101. slept += 1
  102. t = threading.Thread(target=_loop, daemon=True)
  103. t.start()
  104. return t
  105. def stop(self):
  106. self._stop.set()
  107. def get_interval(self) -> int:
  108. """Return current scan interval in seconds."""
  109. try:
  110. cfg = load_config()
  111. return int(cfg.get("scan_interval", self.interval))
  112. except Exception:
  113. return self.interval
  114. def get_next_run_time(self) -> float | None:
  115. """Return the epoch timestamp of the next scheduled run, or None if unknown."""
  116. try:
  117. interval = self.get_interval()
  118. if self.last_run_time:
  119. return float(self.last_run_time + interval)
  120. # if never run, next run is interval seconds from now (if started)
  121. return float(time.time() + interval)
  122. except Exception:
  123. return None
  124. def pause(self):
  125. """Pause periodic execution."""
  126. try:
  127. self._pause_event.clear()
  128. logger.info("Protector 已暂停")
  129. except Exception:
  130. pass
  131. def resume(self):
  132. """Resume periodic execution."""
  133. try:
  134. self._pause_event.set()
  135. logger.info("Protector 已恢复")
  136. except Exception:
  137. pass
  138. def is_paused(self) -> bool:
  139. return not self._pause_event.is_set()
  140. def run_protector_blocking_once():
  141. pr = ProtectorRunner()
  142. pr.run_once()
  143. if __name__ == "__main__":
  144. r = ProtectorRunner()
  145. r.start()
  146. try:
  147. while True:
  148. time.sleep(1)
  149. except KeyboardInterrupt:
  150. r.stop()