config.py 927 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from __future__ import annotations
  2. import json
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. from typing import Literal
  6. Strategy = Literal["broadcast", "top2", "backup"]
  7. @dataclass
  8. class RelayNode:
  9. name: str
  10. host: str
  11. port: int
  12. token: str
  13. weight: int = 100
  14. @dataclass
  15. class Config:
  16. relays: list[RelayNode]
  17. strategy: Strategy = "top2"
  18. redundancy: int = 2
  19. tcp_warmup_bytes: int = 262144
  20. probe_interval: float = 15.0
  21. @classmethod
  22. def load(cls, path: str) -> "Config":
  23. raw = json.loads(Path(path).read_text())
  24. relays = [RelayNode(**item) for item in raw["relays"]]
  25. return cls(
  26. relays=relays,
  27. strategy=raw.get("strategy", "top2"),
  28. redundancy=raw.get("redundancy", 2),
  29. tcp_warmup_bytes=raw.get("tcp_warmup_bytes", 262144),
  30. probe_interval=raw.get("probe_interval", 15.0),
  31. )