Add netquality 1.1.2 client and node behavior.

Show the address actually probed, keep address-selection misses out of formal loss, and add session metrics, a stop summary, pinned chart points, and in-app metric and release-history screens. Client version is 1.1.2+5. Attribution remains 言灸 under AGPL-3.0-only.

Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
Cursor Agentandyanjiu0721 committed 2026-11-19 23:20:50 +00:00
1 parent 8ca2185c01
commit aeb08ec049
40 files changed
+3827 -545

No files matched your search

+340 -107
View File
@@ -1,22 +1,67 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import '../metrics/sample_book.dart';
import '../models/probe_profile.dart';
import '../models/session_config.dart';
import '../ui/address_text.dart';
import 'endpoint_picker.dart';
import 'probe_loop.dart';
import 'remote_client.dart';
class ChartSample {
const ChartSample({required this.sec, required this.rttMs, required this.ratePct});
const ChartSample({
required this.sec,
required this.rttMs,
required this.ratePct,
});
final double sec;
final double? rttMs;
final double? ratePct;
}
class SessionReport {
const SessionReport({
required this.startedAt,
required this.endedAt,
required this.elapsed,
required this.target,
required this.profileLabel,
required this.sourceLabel,
required this.profileId,
required this.summary,
required this.rateLabel,
required this.resolved,
required this.usedAddress,
required this.usedFamily,
required this.fallbackText,
required this.addressUnreported,
required this.usedIsFake,
});
final DateTime startedAt;
final DateTime endedAt;
final Duration elapsed;
final String target;
final String profileLabel;
final String sourceLabel;
final String profileId;
final ProbeSummary summary;
final String rateLabel;
final List<ResolvedAddress> resolved;
final String? usedAddress;
final String? usedFamily;
final String fallbackText;
final bool addressUnreported;
final bool usedIsFake;
}
class ProbeView {
const ProbeView({
required this.running,
@@ -28,8 +73,22 @@ class ProbeView {
required this.samples,
required this.rateLabel,
required this.sourceLabel,
this.targetAddress,
this.targetIsFake = false,
required this.sessionId,
this.profileId = '',
required this.elapsed,
this.targetHost = '',
this.profileLabel = '',
this.tryingAddress,
this.tryingFamily,
this.usedAddress,
this.usedFamily,
this.usedIsFake = false,
this.resolved = const [],
this.fallbackText = '',
this.addressUnreported = false,
this.report,
this.startedAt,
this.endedAt,
});
static const initial = ProbeView(
@@ -42,6 +101,8 @@ class ProbeView {
samples: [],
rateLabel: '探测无应答',
sourceLabel: '本机',
sessionId: 0,
elapsed: Duration.zero,
);
final bool running;
@@ -53,8 +114,22 @@ class ProbeView {
final List<ChartSample> samples;
final String rateLabel;
final String sourceLabel;
final String? targetAddress;
final bool targetIsFake;
final int sessionId;
final String profileId;
final Duration elapsed;
final String targetHost;
final String profileLabel;
final String? tryingAddress;
final String? tryingFamily;
final String? usedAddress;
final String? usedFamily;
final bool usedIsFake;
final List<ResolvedAddress> resolved;
final String fallbackText;
final bool addressUnreported;
final SessionReport? report;
final DateTime? startedAt;
final DateTime? endedAt;
ProbeView copyWith({
bool? running,
@@ -67,8 +142,24 @@ class ProbeView {
List<ChartSample>? samples,
String? rateLabel,
String? sourceLabel,
String? targetAddress,
bool? targetIsFake,
int? sessionId,
String? profileId,
Duration? elapsed,
String? targetHost,
String? profileLabel,
String? tryingAddress,
String? tryingFamily,
bool clearTrying = false,
String? usedAddress,
String? usedFamily,
bool? usedIsFake,
List<ResolvedAddress>? resolved,
String? fallbackText,
bool? addressUnreported,
SessionReport? report,
bool clearReport = false,
DateTime? startedAt,
DateTime? endedAt,
}) {
return ProbeView(
running: running ?? this.running,
@@ -80,8 +171,22 @@ class ProbeView {
samples: samples ?? this.samples,
rateLabel: rateLabel ?? this.rateLabel,
sourceLabel: sourceLabel ?? this.sourceLabel,
targetAddress: targetAddress ?? this.targetAddress,
targetIsFake: targetIsFake ?? this.targetIsFake,
sessionId: sessionId ?? this.sessionId,
profileId: profileId ?? this.profileId,
elapsed: elapsed ?? this.elapsed,
targetHost: targetHost ?? this.targetHost,
profileLabel: profileLabel ?? this.profileLabel,
tryingAddress: clearTrying ? null : tryingAddress ?? this.tryingAddress,
tryingFamily: clearTrying ? null : tryingFamily ?? this.tryingFamily,
usedAddress: usedAddress ?? this.usedAddress,
usedFamily: usedFamily ?? this.usedFamily,
usedIsFake: usedIsFake ?? this.usedIsFake,
resolved: resolved ?? this.resolved,
fallbackText: fallbackText ?? this.fallbackText,
addressUnreported: addressUnreported ?? this.addressUnreported,
report: clearReport ? null : report ?? this.report,
startedAt: startedAt ?? this.startedAt,
endedAt: endedAt ?? this.endedAt,
);
}
}
@@ -93,113 +198,154 @@ class ProbeController extends ChangeNotifier {
RemoteProbe? _remote;
String _nodeToken = '';
var _token = 0;
var _session = 0;
var _busy = false;
var _remoteMode = false;
DateTime? _startedAt;
Timer? _tick;
Future<void> start(SessionConfig config, {required String nodeToken, required String echoToken}) async {
final problem = config.validate();
if (problem != null) {
view = view.copyWith(banner: problem, phase: '无法开始');
notifyListeners();
Future<void> start(
SessionConfig config, {
required String nodeToken,
required String echoToken,
}) async {
if (_busy) {
return;
}
final echoProblem = _echoProblem(config.profileId, echoToken);
if (echoProblem != null) {
view = view.copyWith(banner: echoProblem, phase: '无法开始');
notifyListeners();
return;
}
await stop();
_nodeToken = nodeToken;
final generation = ++_token;
_clock
..reset()
..start();
final source = config.remote ? (config.node?.name ?? '远程节点') : '本机';
view = ProbeView(
running: true,
phase: '探测中',
banner: null,
note: config.remote ? '正在让节点向目标发包' : '正在从本机向目标发包',
detail: const {},
summary: ProbeSummary.empty,
samples: const [],
rateLabel: rateLabelFor(config.profileId),
sourceLabel: source,
);
notifyListeners();
bool cancelled() => generation != _token;
_busy = true;
try {
if (config.remote) {
final remote = RemoteProbe();
_remote = remote;
await remote.run(
baseUrl: config.node!.baseUrl,
nodeToken: nodeToken,
body: ProbeBody(
target: config.host.trim(),
port: config.port,
profile: config.profileId,
intervalMs: config.intervalMs,
timeoutMs: config.timeoutMs,
durationSec: config.durationSec,
packetSize: config.packetSize,
echoToken: echoToken,
dnsName: config.dnsName.trim(),
payloadHex: config.payloadHex.trim(),
expectHex: config.expectHex.trim(),
),
onSample: (sample) => _accept(generation, sample),
cancelled: cancelled,
);
} else {
final loop = ProbeLoop(
onEndpoint: (address) {
if (generation != _token) {
return;
}
view = view.copyWith(
targetAddress: address.address,
targetIsFake: isClashFakeIp(address),
);
notifyListeners();
},
);
_local = loop;
await loop.run(
request: ProbeRequest(
host: config.host.trim(),
port: config.port,
profileId: config.profileId,
interval: Duration(milliseconds: config.intervalMs),
timeout: Duration(milliseconds: config.timeoutMs),
packetSize: config.packetSize,
echoToken: echoToken,
dnsName: config.dnsName.trim(),
payloadHex: config.payloadHex.trim(),
expectHex: config.expectHex.trim(),
),
onSample: (sample) => _accept(generation, sample),
cancelled: cancelled,
);
}
if (generation == _token) {
view = view.copyWith(running: false, phase: '已停止');
final problem = config.validate();
if (problem != null) {
view = view.copyWith(banner: problem, phase: '无法开始');
notifyListeners();
return;
}
} on RemoteException catch (error) {
if (generation == _token) {
view = view.copyWith(running: false, phase: '出错', banner: error.message);
final echoProblem = _echoProblem(config.profileId, echoToken);
if (echoProblem != null) {
view = view.copyWith(banner: echoProblem, phase: '无法开始');
notifyListeners();
return;
}
} on Object {
if (generation == _token) {
view = view.copyWith(running: false, phase: '出错', banner: '探测中断');
notifyListeners();
await stop();
_nodeToken = nodeToken;
_remoteMode = config.remote;
final generation = ++_token;
final session = ++_session;
_startedAt = DateTime.now();
_clock
..reset()
..start();
final source = config.remote ? (config.node?.name ?? '远程节点') : '本机';
final profile = ProbeProfile.byId(config.profileId);
view = ProbeView(
running: true,
phase: '探测中',
banner: null,
note: config.remote ? '正在让节点向目标发包' : '正在从本机向目标发包',
detail: const {},
summary: ProbeSummary.empty,
samples: const [],
rateLabel: rateLabelFor(config.profileId),
sourceLabel: source,
sessionId: session,
profileId: config.profileId,
elapsed: Duration.zero,
targetHost: '${config.host.trim()}:${config.port}',
profileLabel: profile.label,
startedAt: _startedAt,
);
notifyListeners();
_armTick(generation);
bool cancelled() => generation != _token;
try {
if (config.remote) {
final remote = RemoteProbe();
_remote = remote;
await remote.run(
baseUrl: config.node!.baseUrl,
nodeToken: nodeToken,
body: ProbeBody(
target: config.host.trim(),
port: config.port,
profile: config.profileId,
intervalMs: config.intervalMs,
timeoutMs: config.timeoutMs,
durationSec: config.durationSec,
packetSize: config.packetSize,
echoToken: echoToken,
dnsName: config.dnsName.trim(),
payloadHex: config.payloadHex.trim(),
expectHex: config.expectHex.trim(),
),
onSample: (sample) => _accept(generation, sample),
onResolved: (resolved) {
if (generation != _token) {
return;
}
view = view.copyWith(resolved: resolved);
notifyListeners();
},
cancelled: cancelled,
);
} else {
final loop = ProbeLoop(
onEndpoint: (address) {
if (generation != _token) {
return;
}
view = view.copyWith(
tryingAddress: address.address,
tryingFamily: addressFamily(address),
);
notifyListeners();
},
onResolved: (addresses) {
if (generation != _token) {
return;
}
view = view.copyWith(
resolved: [
for (final address in addresses)
ResolvedAddress(
address: address.address,
family: addressFamily(address),
),
],
);
notifyListeners();
},
);
_local = loop;
await loop.run(
request: ProbeRequest(
host: config.host.trim(),
port: config.port,
profileId: config.profileId,
interval: Duration(milliseconds: config.intervalMs),
timeout: Duration(milliseconds: config.timeoutMs),
packetSize: config.packetSize,
echoToken: echoToken,
dnsName: config.dnsName.trim(),
payloadHex: config.payloadHex.trim(),
expectHex: config.expectHex.trim(),
),
onSample: (sample) => _accept(generation, sample),
cancelled: cancelled,
);
}
_finish(generation, '已停止');
} on RemoteException catch (error) {
_finish(generation, '出错', banner: error.message);
} on Object {
_finish(generation, '出错', banner: '探测中断');
} finally {
if (generation == _token) {
_local = null;
_remote = null;
}
}
} finally {
if (generation == _token) {
_local = null;
_remote = null;
}
_busy = false;
}
}
@@ -217,11 +363,73 @@ class ProbeController extends ChangeNotifier {
_nodeToken = '';
}
if (view.running) {
view = view.copyWith(running: false, phase: '已停止');
_clock.stop();
_tick?.cancel();
final ended = DateTime.now();
view = view.copyWith(
running: false,
phase: '已停止',
elapsed: _clock.elapsed,
endedAt: ended,
report: _snapshot(ended),
);
notifyListeners();
}
}
void _finish(int generation, String phase, {String? banner}) {
if (generation != _token || !view.running) {
return;
}
_clock.stop();
_tick?.cancel();
final ended = DateTime.now();
view = view.copyWith(
running: false,
phase: phase,
banner: banner,
elapsed: _clock.elapsed,
endedAt: ended,
report: _snapshot(ended),
);
notifyListeners();
}
SessionReport? _snapshot(DateTime ended) {
final started = _startedAt;
if (started == null) {
return null;
}
return SessionReport(
startedAt: started,
endedAt: ended,
elapsed: _clock.elapsed,
target: view.targetHost,
profileLabel: view.profileLabel,
sourceLabel: view.sourceLabel,
profileId: view.profileId,
summary: view.summary,
rateLabel: view.rateLabel,
resolved: view.resolved,
usedAddress: view.usedAddress,
usedFamily: view.usedFamily,
fallbackText: view.fallbackText,
addressUnreported: view.addressUnreported,
usedIsFake: view.usedIsFake,
);
}
void _armTick(int generation) {
_tick?.cancel();
_tick = Timer.periodic(const Duration(seconds: 1), (_) {
if (generation != _token || !view.running) {
return;
}
view = view.copyWith(elapsed: _clock.elapsed);
notifyListeners();
});
}
void _accept(int generation, ProbeSample sample) {
if (generation != _token) {
return;
@@ -246,6 +454,16 @@ class ProbeController extends ChangeNotifier {
ratePct: rate,
),
);
final reported = sample.detail['address'];
final hasAddress = reported != null && reported.isNotEmpty;
final fallback = sample.detail['fallback'];
final parsed = hasAddress ? InternetAddress.tryParse(reported) : null;
final family = hasAddress
? ((sample.detail['family'] != null &&
sample.detail['family']!.isNotEmpty)
? sample.detail['family']
: familyOfAddress(reported))
: null;
view = view.copyWith(
phase: phaseFor(sample.status),
note: sample.note,
@@ -253,9 +471,24 @@ class ProbeController extends ChangeNotifier {
summary: sample.summary,
samples: samples,
clearBanner: true,
elapsed: _clock.elapsed,
usedAddress: hasAddress ? reported : view.usedAddress,
usedFamily: hasAddress ? family : view.usedFamily,
usedIsFake: hasAddress
? parsed != null && isClashFakeIp(parsed)
: view.usedIsFake,
clearTrying: hasAddress || (fallback != null && fallback.isNotEmpty),
fallbackText: (fallback != null && fallback.isNotEmpty) ? fallback : null,
addressUnreported: _remoteMode && !hasAddress,
);
notifyListeners();
}
@override
void dispose() {
_tick?.cancel();
super.dispose();
}
}
String? _echoProblem(String profileId, String echoToken) {