发布网络质量探测 1.1.2:新增客户端源文件(一)

署名:言灸
This commit is contained in:
YanJiu committed 2026-11-20 18:58:20 +08:00
1 parent 8ca2185c01
commit bc39956b6c
4 files changed
+285

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
/// Versions that exist as this release or as a real Git tag / GitHub Release.
/// Unpublished earlier behavior mentioned inside the 1.1.1 notes is not given
/// its own entry.
class ReleaseEntry {
const ReleaseEntry({
required this.version,
required this.title,
required this.source,
required this.points,
this.published,
});
final String version;
final String title;
final String source;
final String? published;
final List<String> points;
}
const releaseHistory = <ReleaseEntry>[
ReleaseEntry(
version: '1.1.2',
title: '网络质量探测 1.1.2',
source: '仓库 docs/release-notes/1.1.2.md。这一版的 Git 标签要等发布时才有,这里写的是即将发布的内容。',
points: [
'可以修改已有远程节点的名称、控制地址和口令。节点编号和当前选择保持不变。口令留空表示不改,界面不显示已经保存的口令。',
'端口框标明当前类型的默认端口。合作反射 TCP 是 19090,UDP 是 19091。只有端口仍是上一种类型的默认值,或端口是空的,切换类型才会自动更换。',
'本机和远程节点都显示实际连通或实际发包的地址,以及 IPv4 / IPv6。旧节点如果没有上报地址,会写明未报告,不会把域名解析的第一个地址当成已经使用的地址。',
'选址阶段失败的候选(例如 IPv6 没有应答后改用 IPv4)不再计入正式丢包,并单独说明回退。选定地址之后的失败仍然计入。全部候选都失败时显示失败,不会显示 0% 成功。',
'结果给出已发送、已收到、超时、比率、当前/最小/平均/中位/P95/最大延迟、抖动和用时。某种探测没有测量的量会标成不适用或 —。',
'手动停止后保留曲线,并给出开始、结束、用时、目标、来源、实际地址和最终指标。停止时还没完成的请求不计入丢包。',
'点曲线可以钉住该时刻的数值,松手后仍然保留;再点另一点会更换。新的一次探测,或该点滚出窗口后,不会沿用过期选择。',
'新增「指标说明」。',
],
),
ReleaseEntry(
version: '1.1.1',
title: '网络质量探测 1.1.1',
published: '2026-11-09',
source: 'Git 标签 v1.1.1,以及 GitHub Release「网络质量探测 1.1.1」(发布于 2026-11-09,之后补过桌面和节点压缩包)。',
points: [
'本机 UDP 会把解析出的地址逐个试过,停在第一个真正有应答的地址上。',
'还没有地址应答时,一次超时就换下一个。已经有应答之后,要连续 3 次没有应答才换。',
'结果里显示当时实际在用的地址。地址落在 198.18.0.0/15 时,会提示这是 VPN 假地址。',
'发布物包含 Android APK(arm64-v8a 与 armeabi-v7a,versionName 1.1.1,versionCode 分别为 2004 和 1004)、Linux 与 Windows 桌面客户端,以及 Linux amd64、Linux arm64、Windows amd64 的检测节点包。',
'标签 v1.1.1 没有因为后来补上的桌面和节点压缩包而移动。',
],
),
];
const releaseHistoryNote =
'仓库里目前公开的版本标签只有 v1.1.1。1.1.1 的发布说明提到过更早的发包行为,但没有单独的标签或 Release,所以这里不写成另一个版本。';
+65
View File
@@ -0,0 +1,65 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
/// Latency over successful round-trips only.
///
/// * current: the latest successful sample, stored separately as `rttMs`
/// * min / max: smallest and largest successful sample
/// * mean: arithmetic mean
/// * median: the middle sample after sorting; when the count is even, the
/// mean of the two middle samples
/// * p95: nearest-rank. Rank is `ceil(0.95 * n)` computed as
/// `(95 * n + 99) ~/ 100` so binary floating point cannot move the rank.
/// Rank is 1-based and then clamped to the sample count.
class LatencyStats {
const LatencyStats({
required this.min,
required this.mean,
required this.median,
required this.p95,
required this.max,
});
final double min;
final double mean;
final double median;
final double p95;
final double max;
static LatencyStats? from(List<double> samples) {
if (samples.isEmpty) {
return null;
}
final sorted = [...samples]..sort();
var sum = 0.0;
for (final value in sorted) {
sum += value;
}
final count = sorted.length;
final median = count.isOdd
? sorted[count ~/ 2]
: (sorted[count ~/ 2 - 1] + sorted[count ~/ 2]) / 2;
final rank = nearestRank95(count);
return LatencyStats(
min: sorted.first,
mean: sum / count,
median: median,
p95: sorted[rank - 1],
max: sorted.last,
);
}
}
int nearestRank95(int count) {
if (count <= 1) {
return 1;
}
final rank = (95 * count + 99) ~/ 100;
if (rank < 1) {
return 1;
}
if (rank > count) {
return count;
}
return rank;
}
+120
View File
@@ -0,0 +1,120 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import 'sample_book.dart';
/// A candidate "responded" when the address itself answered: success, a
/// protocol mismatch, or an explicit refusal. Silence, timeout and a
/// connect error are not a response, so another address may be tried.
bool selectionResponded(Outcome outcome) {
switch (outcome.status) {
case Outcome.ok:
case Outcome.protocol:
case Outcome.refused:
return true;
default:
return false;
}
}
class SelectionDecision {
const SelectionDecision({required this.commit, this.skipped = 0});
final List<Outcome> commit;
final int skipped;
}
/// Separates address selection from measured probes.
///
/// Before any candidate has responded, failures are held. A later response
/// drops those held failures: they are reported as a fallback diagnostic and
/// are not added to sent, timeout or loss. If every candidate fails in the
/// round, the held failures are committed, so the session cannot show a
/// false 0% success. After a response, further failures are committed
/// immediately. [endMeasurement] is called when the picker leaves the
/// address that was being measured; the failure that caused the leave has
/// already been committed.
class SelectionLedger {
SelectionLedger(this.candidateCount);
final int candidateCount;
bool measuring = false;
final List<Outcome> _held = [];
SelectionDecision take(Outcome outcome, {required bool responded}) {
if (candidateCount <= 1) {
if (responded) {
measuring = true;
}
return SelectionDecision(commit: [outcome]);
}
if (measuring) {
return SelectionDecision(commit: [outcome]);
}
if (responded) {
final skipped = _held.length;
final next = skipped == 0
? outcome
: _attach(outcome, _fallbackText(_held, outcome), skipped);
_held.clear();
measuring = true;
return SelectionDecision(commit: [next], skipped: skipped);
}
_held.add(outcome);
if (_held.length >= candidateCount) {
final commit = List<Outcome>.of(_held);
_held.clear();
return SelectionDecision(commit: commit);
}
return const SelectionDecision(commit: []);
}
void endMeasurement() {
measuring = false;
}
}
Outcome _attach(Outcome outcome, String fallback, int skipped) {
return Outcome(
status: outcome.status,
kind: outcome.kind,
rttMs: outcome.rttMs,
note: outcome.note,
reconnect: outcome.reconnect,
selectionSkipped: outcome.selectionSkipped + skipped,
detail: {...outcome.detail, 'fallback': fallback},
);
}
String _fallbackText(List<Outcome> held, Outcome success) {
final failed = held
.map((outcome) {
final address = outcome.detail['address'];
final family = outcome.detail['family'];
final where = address == null || address.isEmpty
? '未知地址'
: (family == null || family.isEmpty)
? address
: '$address($family)';
final why = outcome.note.isEmpty ? '没有成功' : outcome.note;
return '$where:$why';
})
.join(';');
final used = success.detail['address'] ?? '';
final usedFamily = success.detail['family'] ?? '';
final usedText = used.isEmpty
? '另一个地址'
: usedFamily.isEmpty
? used
: '$used($usedFamily)';
return '候选未成功:$failed。已改用 $usedText。这些失败没有计入正式丢包或失败率。';
}
/// Stop discards the attempt that was still in flight. A finished attempt
/// whose note says it was stopped is not a measured loss either.
bool countsAfterStop(Outcome? outcome, {required bool cancelled}) {
if (cancelled || outcome == null) {
return false;
}
return outcome.note != '已停止';
}
+45
View File
@@ -0,0 +1,45 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import '../storage/token_vault.dart';
import 'session_config.dart';
/// Blank means "keep the token already stored". A non-blank value replaces it.
/// The saved token is never returned here.
String? replacementNodeToken(String entered) {
final trimmed = entered.trim();
if (trimmed.isEmpty) {
return null;
}
return trimmed;
}
/// Writes a replacement token. Returns false when the existing secret is kept.
Future<bool> saveEditedNodeToken(
TokenVault vault,
String id,
String entered,
) async {
final next = replacementNodeToken(entered);
if (next == null) {
return false;
}
await vault.write(nodeTokenKey(id), next);
return true;
}
/// Keeps [RemoteNode.id]. Changing the control URL clears a cached visibility
/// check because it may now be a different server. The token is not part of
/// this object.
RemoteNode applyNodeEdit(
RemoteNode node, {
required String name,
required String baseUrl,
}) {
final urlChanged = node.baseUrl != baseUrl;
return node.copyWith(
name: name,
baseUrl: baseUrl,
visibility: urlChanged ? '' : node.visibility,
);
}