fix(client): 看门狗+延迟统一到数据面 IP TCP 探测 + 消费服务端 down 信号

修复:数据面挂了客户端无感。原看门狗探 generate_204,会被分流判 direct 走本地
出网假阳性,永不触发。改为三路融合判活,针对「实际所连节点」(_connectedNode,
非会随 ping 漂移的 effectiveNode):
① 客户端→数据口 TCP 握手(节点 host:443,TUN 内必 direct→直奔真实节点,不被分流糊弄);
② 延迟同源:①的 RTT 回填连接页延迟(ConnectionState.livePingMs),挂了掉成 —;
③ 服务端权威:刷 /v1/nodes,所连节点被判 down(dp_healthy=0/agent掉线/运维下线)即处理。
看门狗即时首测 + 周期 15s;连续 3 次握不上 / 服务端 down → 智能切节点 / 手动告警断开。
连接页延迟改读 livePingMs(删除内核 urltest 取数)。补信号③测试。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-29 21:27:49 +08:00
parent 23bf3077c8
commit 554708a092
3 changed files with 111 additions and 55 deletions
+39 -2
View File
@@ -62,6 +62,17 @@ class _StubNodes extends NodesNotifier {
Future<void> refresh() async {}
}
// 当前最优节点 HK 被服务端判 down(status≠up),JP 仍可用 —— 用于验证信号③。
class _StubNodesHKDown extends NodesNotifier {
@override
Future<List<Node>> build() async => const [
Node(code: 'HK', nameZh: '香港', nameEn: 'Hong Kong', ping: 20, uuid: 'hk-uuid', host: 'hk', port: 443, status: 'down'),
Node(code: 'JP', nameZh: '东京', nameEn: 'Tokyo', ping: 35, uuid: 'jp-uuid', host: 'jp', port: 443),
];
@override
Future<void> refresh() async {}
}
void main() {
const t = StringsZh();
@@ -72,7 +83,8 @@ void main() {
connectApiFactoryProvider.overrideWithValue((_) => _FakeConnectApi()),
connectionProvider.overrideWith((ref) => ConnectionController(
ref, ref.watch(vpnBridgeProvider),
prober: () async => healthy,
// 数据面 TCP 探测:健康→返回 RTT 30ms,不健康→0(不可达)。
prober: (host, port) async => healthy ? 30 : 0,
)),
]);
@@ -108,7 +120,7 @@ void main() {
addTearDown(bridge.dispose);
addTearDown(c.dispose);
await tester.pumpWidget(UncontrolledProviderScope(container: c, child: const SizedBox()));
c.read(selectedNodeCodeProvider.notifier).state = 'HK'; // 手动选定 HK
c.read(selectedNodeCodeProvider.notifier).select('HK'); // 手动选定 HK
await driveOnAndProbe(tester, c, bridge);
expect(c.read(selectedNodeCodeProvider), 'HK', reason: '手动模式不应自动换节点');
final st = c.read(connectionProvider);
@@ -129,4 +141,29 @@ void main() {
bridge.emit(VpnStatus.off); // 收尾:停看门狗/计时器,避免 pending timer
await tester.pump();
});
testWidgets('信号③:服务端判当前节点 down → 即使 TCP 探测正常也切换', (tester) async {
final bridge = _FakeBridge();
final c = ProviderContainer(overrides: [
vpnBridgeProvider.overrideWithValue(bridge),
nodesProvider.overrideWith(_StubNodesHKDown.new),
connectApiFactoryProvider.overrideWithValue((_) => _FakeConnectApi()),
connectionProvider.overrideWith((ref) => ConnectionController(
ref, ref.watch(vpnBridgeProvider),
prober: (host, port) async => 30, // TCP 健康,证明触发来自服务端 down 信号而非探测
)),
]);
addTearDown(bridge.dispose);
addTearDown(c.dispose);
await tester.pumpWidget(UncontrolledProviderScope(container: c, child: const SizedBox()));
// 智能模式:当前最优 HK 被服务端判 down → 应切到 JP(唯一可用)。
// 不复用 driveOnAndProbe:服务端 down 在「即时首测」就触发切换,相位已离开 on。
c.read(nodesProvider);
await tester.pump();
c.read(connectionProvider);
bridge.emit(VpnStatus.on);
await tester.pump(); // 即时首测:serverDown=true → 立即 _onNodeUnhealthy
await tester.pump();
expect(c.read(selectedNodeCodeProvider), 'JP', reason: '服务端判当前节点 down,智能模式应切到其他可用节点');
});
}