Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat: let the traffic ceiling cover direct-routed metered sources

Peach local maintenancea9dc7f055f41
diff --git a/scripts/traffic_watch.py b/scripts/traffic_watch.py
index 9b016f7b5..29a3d6f1a 100644
--- a/scripts/traffic_watch.py
+++ b/scripts/traffic_watch.py
@@ -25,6 +25,11 @@ def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Peach 代理流量停机哨兵")
parser.add_argument("--limit", type=float, default=50.0)
parser.add_argument("--warn", type=float)
+ parser.add_argument(
+ "--count-direct",
+ action="store_true",
+ help="把直连下载也计入停机预算;计费来源直连时不加这个开关等于没有闸门",
+ )
parser.add_argument("--interval", type=int, default=2)
parser.add_argument("--once", action="store_true")
parser.add_argument("--pipe", default=DEFAULT_PIPE)
@@ -93,6 +98,27 @@ def is_direct(connection: dict) -> bool:
return len(chains) == 1 and chains[0] == "DIRECT"
+def accumulate(
+ previous: dict, current: dict, count_direct: bool
+) -> tuple[float, float, dict[str, float]]:
+ """把两次快照的差算成 (计入预算, 未计入, 各 host 计入)。
+
+ 连接会被回收,看不到的连接不能倒扣,所以只累加非负增量——这是下界不是精确值。
+ 计费来源如果走直连,`count_direct=False` 会让闸门永远不触发;口径要跟着来源走。
+ """
+ counted = uncounted = 0.0
+ hosts: dict[str, float] = {}
+ for connection_id, (downloaded, direct, host) in current.items():
+ old = previous.get(connection_id, (0, direct, host))[0]
+ delta = max(downloaded - old, 0)
+ if direct and not count_direct:
+ uncounted += delta
+ else:
+ counted += delta
+ hosts[host] = hosts.get(host, 0) + delta
+ return counted, uncounted, hosts
+
+
def snapshot(pipe: str, secret: str) -> tuple[int, dict]:
payload = clash(pipe, secret)
per_connection = {}
@@ -152,11 +178,14 @@ def run(args: argparse.Namespace, log) -> int:
print("连不上 Clash 管道:", exc)
return 1
- proxy_bytes = 0.0
- direct_bytes = 0.0
- proxy_hosts: dict[str, float] = {}
+ counted_bytes = 0.0
+ uncounted_bytes = 0.0
+ counted_hosts: dict[str, float] = {}
log(f"哨兵启动。mihomo 累计(含直连){total_start/1024**3:,.1f} GB,活跃连接 {len(previous)}")
- log("计量口径:只累计非 DIRECT 连接下载增量;这是轮询下界")
+ if args.count_direct:
+ log("计量口径:代理 + 直连下载增量都计入;这是轮询下界")
+ else:
+ log("计量口径:只累计非 DIRECT 连接下载增量;这是轮询下界")
log(f"阈值 {args.limit:.0f} GB,预警 {warn_gb:.0f} GB,每 {args.interval}s 检查")
if args.once:
@@ -176,25 +205,25 @@ def run(args: argparse.Namespace, log) -> int:
except Exception as exc:
log(f"读取失败({exc}),跳过本轮")
continue
- for connection_id, (downloaded, direct, host) in current.items():
- old = previous.get(connection_id, (0, direct, host))[0]
- delta = max(downloaded - old, 0)
- if direct:
- direct_bytes += delta
- else:
- proxy_bytes += delta
- proxy_hosts[host] = proxy_hosts.get(host, 0) + delta
+ counted_delta, uncounted_delta, host_delta = accumulate(
+ previous, current, args.count_direct
+ )
+ counted_bytes += counted_delta
+ uncounted_bytes += uncounted_delta
+ for host, value in host_delta.items():
+ counted_hosts[host] = counted_hosts.get(host, 0) + value
previous = current
- used_gb = proxy_bytes / 1024**3
+ used_gb = counted_bytes / 1024**3
if used_gb >= args.limit:
log(f"增量 {used_gb:.1f} GB 超过阈值 {args.limit:.0f} GB——停止所属任务")
stopped = stop_owned_jobs(log)
- top = sorted(proxy_hosts.items(), key=lambda item: -item[1])[:5]
+ top = sorted(counted_hosts.items(), key=lambda item: -item[1])[:5]
+ scope = "代理+直连" if args.count_direct else "代理"
lines = [
f"流量警报 {time.strftime('%Y-%m-%d %H:%M:%S')}",
- f"代理下载增量 {used_gb:.1f} GB(阈值 {args.limit:.0f} GB)",
- f"同期直连 {direct_bytes/1024**3:.1f} GB(不计入)",
- "", "代理目标 Top:",
+ f"{scope}下载增量 {used_gb:.1f} GB(阈值 {args.limit:.0f} GB)",
+ f"同期未计入 {uncounted_bytes/1024**3:.1f} GB",
+ "", "计入目标 Top:",
*[f" {value/1024**3:>8.3f} GB {host}" for host, value in top],
"", "已停止的 Peach 任务:",
*([f" {item}" for item in stopped] or [" (无匹配任务)"]),
@@ -207,7 +236,9 @@ def run(args: argparse.Namespace, log) -> int:
if used_gb >= warn_gb and not warned:
warned = True
log(f"增量已达 {used_gb:.1f} GB,接近停机线")
- log(f"代理 {used_gb:>7.2f}/{args.limit:.0f} GB(直连 {direct_bytes/1024**3:>7.2f} GB 不计)连接 {len(current)}")
+ scope = "代理+直连" if args.count_direct else "代理"
+ log(f"{scope} {used_gb:>7.2f}/{args.limit:.0f} GB"
+ f"(未计入 {uncounted_bytes/1024**3:>7.2f} GB)连接 {len(current)}")
def main(argv: list[str] | None = None) -> int:
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index a2fec22db..f18a58f7f 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -86,6 +86,24 @@ class OperationalScriptTests(unittest.TestCase):
self.assertFalse(self.traffic_watch.is_direct({"chains": ["Proxy", "Relay"]}))
self.assertEqual(self.creator_boards.safe_name("A/B:C"), "A_B_C")
+ def test_traffic_ceiling_can_cover_direct_sources(self):
+ """115 实测走 DIRECT。计费来源若也直连,只算代理等于没有闸门。"""
+ accumulate = self.traffic_watch.accumulate
+ previous = {"a": (100, True, "cdn.example"), "b": (100, False, "proxy.example")}
+ current = {"a": (700, True, "cdn.example"), "b": (400, False, "proxy.example")}
+
+ counted, uncounted, hosts = accumulate(previous, current, False)
+ self.assertEqual((counted, uncounted), (300, 600))
+ self.assertEqual(hosts, {"proxy.example": 300})
+
+ counted, uncounted, hosts = accumulate(previous, current, True)
+ self.assertEqual((counted, uncounted), (900, 0))
+ self.assertEqual(hosts, {"cdn.example": 600, "proxy.example": 300})
+
+ # 连接被回收后重新编号会让计数倒退;倒退不能反向抵扣已用预算。
+ self.assertEqual(accumulate({"a": (900, True, "h")}, {"a": (5, True, "h")}, True)[0], 0)
+ self.assertFalse(self.traffic_watch.build_parser().parse_args([]).count_direct)
+
def test_probe_never_records_an_unknown_duration_as_zero(self):
"""0 会同时躲过 probe 的 `duration IS NULL` 和抽帧的 `duration>2`,永久卡住。"""
module = self.probe