Leadaxe:sing-box-lx 实现无需 root 的链路劣化测试出站 impair

Leadaxe 在 sing-box-lx #86 中提出并说明已由 PR #87 实现测试出站 impair,可在 direct 或自身 detour 上双向模拟限速、延迟、抖动、丢包、停顿和定时断网,使用 seed 重复场景。按该提案,with_lx_impair 纳入 LX_TAGS 和 AAR,发布构建包含但未配置时不起作用,自定义构建缺少标签会拒绝该类型。它是诊断工具,不应放入生产配置;接收方向在真实网络之后劣化,不能改变远端看到的真实 RTT。

作者原文

Problem

Balancing (urltest round_robin / failover, least_test penalties, the pool self-heal), chains and transports have to be tested over a bad link: narrow, uneven, lossy, with outages. The real road to test servers is usually faster and smoother than the link these mechanisms are meant for. netem needs root and a kernel module, so it is not available on a phone and not always on a router, and a scenario (rate swings, bursty loss, an outage at second 55) is hard to repeat by hand.

Proposal: a test outbound impair

An outbound that dials like direct, or through its own detour, and degrades every connection made through it, in both directions, from one end, without privileges:

  • a bottleneck rate with a queue, one per instance and direction, shared by all its connections;
  • delay, jitter, reorder;
  • loss with loss_burst (Gilbert model): datagrams are dropped, streams stall as on a TCP retransmission (stream_stall), no data lost or reordered;
  • rate swings vary, random stalls of the whole link stall;
  • a timed schedule of link changes and outages (outage, reset), counted from process start or first use (start);
  • profiles dsl, lte-walk, wifi-lossy; seed for reproducible runs; per-direction up / down.

Main placement: under a proxy node ("detour": "<impair tag>" on the proxy), so the road to the proxy server is degraded and the proxy's encryption and framing ride on top, as on a real link. It logs a WARN on start and per-direction counters on close.

{"type": "vless", "tag": "via-se", "server": "se.example.net", "server_port": 443, "detour": "slow-se"},
{"type": "impair", "tag": "slow-se", "rate": "10mbit", "vary": {"min": "3mbit", "max": "12mbit", "every": "2s"}, "loss": "0.5%", "seed": 1}

Isolation

  • Build tag with_lx_impair, in LX_TAGS and in the AAR (decided 2026-11-25): release binaries have it, about 150 KB, and it does nothing unless a config declares type: impair. A custom build without the tag rejects type: impair with a clear error.
  • New files only, plus two upstream touches inside // lx:begin impair: 3 lines in include/registry.go and the AAR tag in cmd/internal/build_libbox/main.go. No new dependencies.
  • Tracked as FEATURE 020 (IMPAIR) / task 164 (IMPAIR_OUTBOUND).

Limits

  • A test tool: it ships in release builds for diagnostics, but has no place in a production config.
  • The receive side is degraded after the real network: the real link still carried the traffic, and the far end sees the real RTT.

Implemented in PR #87 (branch lx-spec164-impair).