Add proxy speed measurement, connection logs and standalone Android packaging

This commit is contained in:
shen
2026-10-13 23:08:57 -07:00
parent f94dab0555
commit 72566b9a8a
22 changed files with 551 additions and 91 deletions
+101 -66
View File
@@ -12,6 +12,7 @@ import 'proxy_panel.dart';
import 'configuration_pages.dart';
import 'detail_pages.dart';
import 'runtime_diagnostics.dart';
import 'proxy_speed_test.dart';
class LabScreen extends StatefulWidget {
const LabScreen({super.key, this.initializeNative = true});
@@ -70,6 +71,12 @@ class _LabScreenState extends State<LabScreen> {
Map<String, dynamic> status = {'state': 'Stopped', 'peer_count': 0};
Map<String, dynamic>? result;
final logs = <String>[];
final proxySpeedTest = ProxySpeedTest();
bool speedTesting = false;
ProxySpeedResult? speedResult;
String speedError = '';
double speedProgressMbps = 0;
int speedBytes = 0;
final diagnosticRedactor = DiagnosticRedactor();
String lastProxyState = '';
int proxyEventSequence = 0;
@@ -502,6 +509,9 @@ class _LabScreenState extends State<LabScreen> {
}
Future<void> startConfig(String config) async {
proxySpeedTest.cancel();
speedResult = null;
speedError = '';
proxyEventSequence = 0;
if (singBoxEnabled) proxyProfile.validateConnection();
// Validation selects mode from effective flags, avoiding ad-hoc TOML parsing.
@@ -608,9 +618,9 @@ class _LabScreenState extends State<LabScreen> {
if (mounted) {
setState(() {
legacyMigrationPending = info?['migration_pending'] == true;
if (info?['path'] is String) {
if (info?['path'] is String) {
localProfilePath = info!['path'] as String;
}
}
});
}
if (legacyMigrationPending) log('旧配置需要迁移,请在设置中点击“迁移旧配置”');
@@ -664,6 +674,7 @@ class _LabScreenState extends State<LabScreen> {
}
Future<void> stopActive(MeshPortClient c) async {
proxySpeedTest.cancel();
try {
if (c == applicationClient && proxySession != null) {
await proxySession!.stop();
@@ -811,6 +822,7 @@ class _LabScreenState extends State<LabScreen> {
@override
void dispose() {
cancel = true;
proxySpeedTest.cancel();
timer?.cancel();
subscriptionTimer?.cancel();
unawaited(
@@ -906,6 +918,91 @@ class _LabScreenState extends State<LabScreen> {
},
);
Future<void> measureProxySpeed() async {
if (speedTesting || busy || !running) return;
setState(() {
speedTesting = true;
speedResult = null;
speedError = '';
speedBytes = 0;
speedProgressMbps = 0;
});
log('代理测速 · 开始 · $runningProxyMode · $runningProxyLabel');
var lastUpdate = Duration.zero;
try {
final measured = await proxySpeedTest.run(
port: proxySession?.port ?? 10818,
onProgress: (bytes, elapsed) {
if (!mounted ||
elapsed - lastUpdate < const Duration(milliseconds: 250)) {
return;
}
lastUpdate = elapsed;
setState(() {
speedBytes = bytes;
speedProgressMbps =
bytes * 8 / elapsed.inMicroseconds.clamp(1, 1 << 62);
});
},
);
if (!mounted) return;
setState(() {
speedResult = measured;
speedBytes = measured.bytes;
});
log(
'代理测速 · ${measured.mbps.toStringAsFixed(1)} Mbps · '
'${formatBytes(measured.bytes)} · 首字节 ${measured.firstByte.inMilliseconds} ms',
);
} catch (e) {
if (mounted) {
setState(() => speedError = diagnosticRedactor.sanitize('$e'));
log('代理测速 · $e');
}
} finally {
if (mounted) setState(() => speedTesting = false);
}
}
Widget speedPanel() => panel('代理测速', [
Text(
speedTesting
? '${speedProgressMbps.toStringAsFixed(1)} Mbps · 已下载 ${formatBytes(speedBytes)}'
: speedResult == null
? '尚未测速'
: '${speedResult!.mbps.toStringAsFixed(1)} Mbps · '
'${(speedResult!.mbps / 8).toStringAsFixed(2)} MB/s',
),
if (speedResult != null)
Text(
'首字节 ${speedResult!.firstByte.inMilliseconds} ms · '
'下载 ${formatBytes(speedResult!.bytes)}',
),
if (speedTesting) const LinearProgressIndicator(),
if (speedError.isNotEmpty) Text(speedError),
const Text(
'通过当前代理入口下载 Cloudflare 测试资源,遵循当前分流规则。最多 32 MB / 15 秒,会占用带宽;结果不代表视频站点速度。',
),
if (!running) const Text('连接代理后可测速'),
Wrap(
spacing: 12,
children: [
FilledButton.tonalIcon(
onPressed: !running || busy || speedTesting
? null
: measureProxySpeed,
icon: const Icon(Icons.speed),
label: const Text('开始测速'),
),
if (speedTesting)
TextButton(
onPressed: proxySpeedTest.cancel,
child: const Text('取消测速'),
),
],
),
]);
Widget overview() {
if (proxyProfile.feature != 'proxy') return networkOverview();
final selected = proxyProfile.nodes
@@ -956,6 +1053,7 @@ class _LabScreenState extends State<LabScreen> {
],
),
]),
speedPanel(),
if (logs.isNotEmpty)
panel('最近事件', logs.take(3).map((line) => Text(line)).toList()),
],
@@ -1005,15 +1103,7 @@ class _LabScreenState extends State<LabScreen> {
SelectableText('SOCKS5:${proxySession?.endpoint ?? '—'}'),
const Text('TCP / UDP · 自动公告分流。子网变化时重建引擎,已有代理连接需重连。'),
]),
metricGroup([
metric(
'最近测试耗时',
result == null
? '—'
: '${(result!['elapsed_ms'] as num).toStringAsFixed(1)} ms',
),
metric('远端节点', '${status['peer_count'] ?? 0}'),
]),
if (proxyProfile.feature == 'combined') speedPanel(),
panel('网络流量', [
metricGroup([
metric(
@@ -1065,20 +1155,6 @@ class _LabScreenState extends State<LabScreen> {
label: const Text('复制网络诊断'),
),
]),
panel('最近测试趋势', [
const Text('显示连接或回显耗时,并非持续服务器 RTT'),
const SizedBox(height: 12),
SizedBox(height: 140, child: CustomPaint(painter: _Trend(latencies))),
]),
panel('快速测试', [
Text('目标 ${target.text}'),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: () => openDetail('网络诊断', tests),
icon: const Icon(Icons.play_arrow),
label: const Text('打开连接测试'),
),
]),
panel(
'最近事件',
logs
@@ -1689,44 +1765,3 @@ class _LabScreenState extends State<LabScreen> {
);
}
}
class _Trend extends CustomPainter {
final List<double> samples;
_Trend(this.samples);
@override
void paint(Canvas canvas, Size size) {
final grid = Paint()
..color = const Color(0xffe9e6e0)
..strokeWidth = 1;
for (var i = 0; i < 5; i++) {
final y = size.height * i / 4;
canvas.drawLine(Offset(0, y), Offset(size.width, y), grid);
}
if (samples.length < 2) {
return;
}
final max = samples.reduce((a, b) => a > b ? a : b) * 1.2 + 1;
final path = Path();
for (var i = 0; i < samples.length; i++) {
final p = Offset(
size.width * i / (samples.length - 1),
size.height * (1 - samples[i] / max),
);
if (i == 0) {
path.moveTo(p.dx, p.dy);
} else {
path.lineTo(p.dx, p.dy);
}
}
canvas.drawPath(
path,
Paint()
..color = const Color(0xff087d8e)
..style = PaintingStyle.stroke
..strokeWidth = 2,
);
}
@override
bool shouldRepaint(_Trend oldDelegate) => true;
}