网络质量探测 1.1.3:自更新、HTTP/3 与中心节点
版本名 1.1.3,构建号 6。应用标识和签名未改。包含自动更新、覆盖安装、目标历史、IPv4/IPv6 开关、HTTP(含 HTTP/3)以及中心服务端一键添加节点。
This commit is contained in:
1 parent
943259774f
commit
8cdc0f436f
66 files changed
+5489
-78
No files matched your search
@@ -0,0 +1,165 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../center/catalog.dart';
|
||||
import '../center/center_client.dart';
|
||||
import '../models/session_config.dart';
|
||||
|
||||
class AddedCenter {
|
||||
const AddedCenter({
|
||||
required this.link,
|
||||
required this.token,
|
||||
required this.catalog,
|
||||
});
|
||||
|
||||
final CenterLink link;
|
||||
final String token;
|
||||
final Catalog catalog;
|
||||
}
|
||||
|
||||
Future<AddedCenter?> showCenterAddDialog(BuildContext context) {
|
||||
return showDialog<AddedCenter>(
|
||||
context: context,
|
||||
builder: (context) => const _CenterAddDialog(),
|
||||
);
|
||||
}
|
||||
|
||||
class _CenterAddDialog extends StatefulWidget {
|
||||
const _CenterAddDialog();
|
||||
|
||||
@override
|
||||
State<_CenterAddDialog> createState() => _CenterAddDialogState();
|
||||
}
|
||||
|
||||
class _CenterAddDialogState extends State<_CenterAddDialog> {
|
||||
final _url = TextEditingController();
|
||||
final _account = TextEditingController();
|
||||
final _token = TextEditingController();
|
||||
final _password = TextEditingController();
|
||||
var _busy = false;
|
||||
String? _hint;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_url.dispose();
|
||||
_account.dispose();
|
||||
_token.dispose();
|
||||
_password.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_hint = null;
|
||||
});
|
||||
try {
|
||||
var token = _token.text.trim();
|
||||
if (token.isEmpty) {
|
||||
if (_password.text.isEmpty) {
|
||||
setState(() => _hint = '请填写口令。如果中心要求密码,也可以只填账号和密码。');
|
||||
return;
|
||||
}
|
||||
token = await loginCenter(
|
||||
baseUrl: _url.text,
|
||||
account: _account.text,
|
||||
password: _password.text,
|
||||
);
|
||||
}
|
||||
final catalog = await fetchCatalog(baseUrl: _url.text, token: token);
|
||||
final stored = normalizeNodeUrl(_url.text);
|
||||
if (stored == null) {
|
||||
setState(() => _hint = '中心地址需要是 http 或 https,且不能把口令写在地址里');
|
||||
return;
|
||||
}
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop(
|
||||
AddedCenter(
|
||||
link: CenterLink(
|
||||
id: DateTime.now().microsecondsSinceEpoch.toString(),
|
||||
url: stored,
|
||||
account: _account.text.trim(),
|
||||
),
|
||||
token: token,
|
||||
catalog: catalog,
|
||||
),
|
||||
);
|
||||
} on CenterClientException catch (error) {
|
||||
setState(() => _hint = error.message);
|
||||
} on Object {
|
||||
setState(() => _hint = '没有完成添加');
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('一键添加服务器'),
|
||||
content: SizedBox(
|
||||
width: 420,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text(
|
||||
'点一次确认,会把这个口令能用的服务器全部加进来。显示名由中心决定,这里不能改。保密服务器只保存名称,地址留在中心。',
|
||||
style: TextStyle(fontSize: 13, height: 1.4),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _url,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '中心地址',
|
||||
hintText: 'https://中心:8443',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _account,
|
||||
decoration: const InputDecoration(labelText: '账号'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _token,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '口令',
|
||||
hintText: '已有口令时填写',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _password,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '密码',
|
||||
hintText: '中心要求密码、手里还没有口令时填写',
|
||||
),
|
||||
),
|
||||
if (_hint != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(_hint!, style: const TextStyle(color: Color(0xFF9A3412))),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _busy ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _busy ? null : _submit,
|
||||
child: Text(_busy ? '正在获取' : '获取并添加'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
+488
-16
@@ -1,22 +1,29 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../center/catalog.dart';
|
||||
import '../center/center_client.dart';
|
||||
import '../models/node_edit.dart';
|
||||
import '../models/port_field.dart';
|
||||
import '../models/probe_profile.dart';
|
||||
import '../models/session_config.dart';
|
||||
import '../models/target_history.dart';
|
||||
import '../probe/endpoint_picker.dart';
|
||||
import '../probe/probe_controller.dart';
|
||||
import '../probe/remote_client.dart';
|
||||
import '../storage/app_store.dart';
|
||||
import '../storage/token_vault.dart';
|
||||
import '../update/apply_update.dart';
|
||||
import 'address_text.dart';
|
||||
import 'center_dialog.dart';
|
||||
import 'format.dart';
|
||||
import 'history_page.dart';
|
||||
import 'metric_guide_page.dart';
|
||||
import 'metrics_panel.dart';
|
||||
import 'quality_charts.dart';
|
||||
import 'settings_page.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
const HomePage({super.key});
|
||||
@@ -39,22 +46,40 @@ class _HomePageState extends State<HomePage> {
|
||||
final _dns = TextEditingController(text: 'example.com');
|
||||
final _payload = TextEditingController();
|
||||
final _expect = TextEditingController();
|
||||
final _httpResolve = TextEditingController();
|
||||
final _httpBody = TextEditingController();
|
||||
final _httpReferer = TextEditingController();
|
||||
final _httpUserAgent = TextEditingController();
|
||||
final _httpCookies = TextEditingController();
|
||||
final _httpRedirects = TextEditingController(text: '5');
|
||||
var _httpVersion = 'auto';
|
||||
var _httpMethod = 'GET';
|
||||
var _allow4 = true;
|
||||
var _allow6 = true;
|
||||
var _autoUpdate = true;
|
||||
var _centers = <CenterLink>[];
|
||||
var _history = TargetHistory.empty;
|
||||
var _centerHint = '';
|
||||
var _remote = false;
|
||||
var _profileId = ProbeProfile.nqTcp.id;
|
||||
var _nodeId = '';
|
||||
var _nodes = <RemoteNode>[];
|
||||
var _ready = false;
|
||||
var _idSeq = 0;
|
||||
var _formEpoch = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller.addListener(_rebuild);
|
||||
_host.addListener(_onHostChanged);
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
_vault = await openTokenVault();
|
||||
final loaded = await _store.load();
|
||||
final history = await _store.loadHistory();
|
||||
final echo = await _vault.read(echoTokenKey);
|
||||
if (!mounted) {
|
||||
return;
|
||||
@@ -74,12 +99,42 @@ class _HomePageState extends State<HomePage> {
|
||||
_dns.text = config.dnsName;
|
||||
_payload.text = config.payloadHex;
|
||||
_expect.text = config.expectHex;
|
||||
_allow4 = config.allowIPv4;
|
||||
_allow6 = config.allowIPv6;
|
||||
_autoUpdate = config.autoUpdate;
|
||||
_httpVersion = config.httpVersion;
|
||||
_httpMethod = config.httpMethod;
|
||||
_httpResolve.text = config.httpResolve;
|
||||
_httpBody.text = config.httpBody;
|
||||
_httpReferer.text = config.httpReferer;
|
||||
_httpUserAgent.text = config.httpUserAgent;
|
||||
_httpCookies.text = config.httpCookies;
|
||||
_httpRedirects.text = '${config.httpMaxRedirects}';
|
||||
_centers = config.centers;
|
||||
_history = history;
|
||||
_echo.text = (echo == null || echo.isEmpty) ? loaded.legacyEcho : echo;
|
||||
_ready = true;
|
||||
});
|
||||
if (loaded.legacyEcho.isNotEmpty && (echo == null || echo.isEmpty)) {
|
||||
await _vault.write(echoTokenKey, loaded.legacyEcho);
|
||||
}
|
||||
if (config.autoUpdate && Platform.environment['FLUTTER_TEST'] != 'true') {
|
||||
unawaited(_launchUpdate());
|
||||
}
|
||||
}
|
||||
|
||||
void _onHostChanged() {
|
||||
if (_profileId != 'http') {
|
||||
return;
|
||||
}
|
||||
final current = int.tryParse(_port.text.trim());
|
||||
if (_port.text.trim().isNotEmpty && current != 80 && current != 443) {
|
||||
return;
|
||||
}
|
||||
final next = '${httpDefaultPort(_host.text)}';
|
||||
if (_port.text != next) {
|
||||
_port.text = next;
|
||||
}
|
||||
}
|
||||
|
||||
void _rebuild() {
|
||||
@@ -93,6 +148,7 @@ class _HomePageState extends State<HomePage> {
|
||||
unawaited(_controller.stop());
|
||||
unawaited(_persist());
|
||||
_controller.removeListener(_rebuild);
|
||||
_host.removeListener(_onHostChanged);
|
||||
_controller.dispose();
|
||||
for (final field in [
|
||||
_host,
|
||||
@@ -105,6 +161,12 @@ class _HomePageState extends State<HomePage> {
|
||||
_dns,
|
||||
_payload,
|
||||
_expect,
|
||||
_httpResolve,
|
||||
_httpBody,
|
||||
_httpReferer,
|
||||
_httpUserAgent,
|
||||
_httpCookies,
|
||||
_httpRedirects,
|
||||
]) {
|
||||
field.dispose();
|
||||
}
|
||||
@@ -126,6 +188,18 @@ class _HomePageState extends State<HomePage> {
|
||||
dnsName: _dns.text.trim(),
|
||||
payloadHex: _payload.text.trim(),
|
||||
expectHex: _expect.text.trim(),
|
||||
allowIPv4: _allow4,
|
||||
allowIPv6: _allow6,
|
||||
autoUpdate: _autoUpdate,
|
||||
httpVersion: _httpVersion,
|
||||
httpResolve: _httpResolve.text.trim(),
|
||||
httpMethod: _httpMethod,
|
||||
httpBody: _httpBody.text,
|
||||
httpReferer: _httpReferer.text.trim(),
|
||||
httpUserAgent: _httpUserAgent.text.trim(),
|
||||
httpCookies: _httpCookies.text,
|
||||
httpMaxRedirects: int.tryParse(_httpRedirects.text.trim()) ?? 5,
|
||||
centers: _centers,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -137,9 +211,28 @@ class _HomePageState extends State<HomePage> {
|
||||
Future<void> _start() async {
|
||||
await _persist();
|
||||
final config = _config();
|
||||
final nodeToken = config.node == null
|
||||
if (config.validate() == null) {
|
||||
_history = _history.add(
|
||||
TargetEntry(
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
profileId: config.profileId,
|
||||
httpUrl: config.profileId == 'http' ? config.host : '',
|
||||
method: config.profileId == 'http' ? config.httpMethod : '',
|
||||
savedAtMs: DateTime.now().millisecondsSinceEpoch,
|
||||
),
|
||||
);
|
||||
await _store.saveHistory(_history);
|
||||
}
|
||||
final node = config.node;
|
||||
final nodeToken = node == null
|
||||
? ''
|
||||
: (await _vault.read(nodeTokenKey(config.node!.id)) ?? '');
|
||||
: (await _vault.read(
|
||||
node.confidential
|
||||
? centerTokenKey(node.centerLinkId)
|
||||
: nodeTokenKey(node.id),
|
||||
) ??
|
||||
'');
|
||||
await _controller.start(
|
||||
config,
|
||||
nodeToken: nodeToken,
|
||||
@@ -153,11 +246,17 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
final previous = ProbeProfile.byId(_profileId);
|
||||
final next = ProbeProfile.byId(id);
|
||||
final currentPort = int.tryParse(_port.text.trim());
|
||||
var previousDefault = previous.port;
|
||||
if (_profileId == 'http' && (currentPort == 80 || currentPort == 443)) {
|
||||
previousDefault = currentPort!;
|
||||
}
|
||||
final nextDefault = id == 'http' ? httpDefaultPort(_host.text) : next.port;
|
||||
setState(() {
|
||||
_port.text = portAfterProfileChange(
|
||||
current: _port.text,
|
||||
previousDefault: previous.port,
|
||||
nextDefault: next.port,
|
||||
previousDefault: previousDefault,
|
||||
nextDefault: nextDefault,
|
||||
);
|
||||
_profileId = id;
|
||||
});
|
||||
@@ -176,6 +275,10 @@ class _HomePageState extends State<HomePage> {
|
||||
PopupMenuButton<String>(
|
||||
tooltip: '说明',
|
||||
onSelected: (value) {
|
||||
if (value == 'settings') {
|
||||
_openSettings();
|
||||
return;
|
||||
}
|
||||
final page = value == 'history'
|
||||
? const HistoryPage()
|
||||
: const MetricGuidePage();
|
||||
@@ -183,6 +286,7 @@ class _HomePageState extends State<HomePage> {
|
||||
.push(MaterialPageRoute<void>(builder: (_) => page));
|
||||
},
|
||||
itemBuilder: (context) => const [
|
||||
PopupMenuItem(value: 'settings', child: Text('设置')),
|
||||
PopupMenuItem(value: 'metrics', child: Text('指标说明')),
|
||||
PopupMenuItem(value: 'history', child: Text('更新历史')),
|
||||
],
|
||||
@@ -210,7 +314,20 @@ class _HomePageState extends State<HomePage> {
|
||||
children: [
|
||||
_pair(
|
||||
wide,
|
||||
_field(_host, '目标地址', '域名或 IP'),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: _field(_host, '目标地址', '域名、IP,或 http(s) 地址'),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('target-history'),
|
||||
tooltip: '最近目标',
|
||||
onPressed: view.running ? null : _showHistory,
|
||||
icon: const Icon(Icons.history),
|
||||
),
|
||||
],
|
||||
),
|
||||
_field(
|
||||
_port,
|
||||
'端口',
|
||||
@@ -221,7 +338,9 @@ class _HomePageState extends State<HomePage> {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
key: const Key('profile'),
|
||||
key: _formEpoch == 0
|
||||
? const Key('profile')
|
||||
: Key('profile-$_formEpoch'),
|
||||
initialValue: _profileId,
|
||||
decoration: const InputDecoration(labelText: '探测类型'),
|
||||
items: [
|
||||
@@ -238,6 +357,7 @@ class _HomePageState extends State<HomePage> {
|
||||
profile.blurb,
|
||||
style: const TextStyle(color: muted, fontSize: 13),
|
||||
),
|
||||
if (profile.id == 'http') ..._httpFields(view.running),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -400,11 +520,30 @@ class _HomePageState extends State<HomePage> {
|
||||
? null
|
||||
: (value) => setState(() => _nodeId = value ?? ''),
|
||||
),
|
||||
OutlinedButton(
|
||||
onPressed: view.running ? null : _manageNodes,
|
||||
child: const Text('管理节点'),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
OutlinedButton(
|
||||
onPressed: view.running ? null : _manageNodes,
|
||||
child: const Text('管理节点'),
|
||||
),
|
||||
OutlinedButton(
|
||||
onPressed: view.running ? null : _addCenter,
|
||||
child: const Text('一键添加服务器'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_centers.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
for (final link in _centers) _centerTile(link, view.running),
|
||||
],
|
||||
if (_centerHint.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(_centerHint, style: const TextStyle(fontSize: 13)),
|
||||
),
|
||||
if (selected != null && nodeUrlLooksPublic(selected.baseUrl))
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(top: 8),
|
||||
@@ -418,6 +557,323 @@ class _HomePageState extends State<HomePage> {
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _httpFields(bool running) {
|
||||
return [
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
key: ValueKey('http-version-$_httpVersion-$_formEpoch'),
|
||||
initialValue: _httpVersion,
|
||||
decoration: const InputDecoration(labelText: '协议'),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'auto', child: Text('自动(HTTP/2,可降到 1.1)')),
|
||||
DropdownMenuItem(value: '1.1', child: Text('仅 HTTP/1.1')),
|
||||
DropdownMenuItem(value: '2', child: Text('仅 HTTP/2')),
|
||||
DropdownMenuItem(value: '3', child: Text('仅 HTTP/3')),
|
||||
],
|
||||
onChanged: running
|
||||
? null
|
||||
: (value) => setState(() => _httpVersion = value ?? 'auto'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_field(
|
||||
_httpResolve,
|
||||
'指定解析',
|
||||
'可选。Host 和 SNI 仍用上面的地址,连接改走这里的 IP 或主机名',
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
key: ValueKey('http-method-$_httpMethod-$_formEpoch'),
|
||||
initialValue: _httpMethod,
|
||||
decoration: const InputDecoration(labelText: '方法'),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'GET', child: Text('GET')),
|
||||
DropdownMenuItem(value: 'POST', child: Text('POST')),
|
||||
],
|
||||
onChanged: running
|
||||
? null
|
||||
: (value) => setState(() => _httpMethod = value ?? 'GET'),
|
||||
),
|
||||
if (_httpMethod == 'POST') ...[
|
||||
const SizedBox(height: 8),
|
||||
_field(_httpBody, '请求体', '可选'),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
_field(_httpReferer, 'Referer', '可选'),
|
||||
const SizedBox(height: 8),
|
||||
_field(_httpUserAgent, 'User-Agent', '可选'),
|
||||
const SizedBox(height: 8),
|
||||
_field(_httpCookies, 'Cookies', '可选', secret: true),
|
||||
const SizedBox(height: 8),
|
||||
_field(_httpRedirects, '最大跳转次数', '默认 5,0 到 20'),
|
||||
];
|
||||
}
|
||||
|
||||
String _httpSummary(Map<String, String> detail) {
|
||||
final parts = [
|
||||
if (detail['httpVersion'] != null) 'HTTP/${detail['httpVersion']}',
|
||||
if (detail['statusCode'] != null) '状态 ${detail['statusCode']}',
|
||||
if (detail['finalUrl'] != null) detail['finalUrl']!,
|
||||
];
|
||||
final times = [
|
||||
if (detail['dnsMs'] != null) 'DNS ${detail['dnsMs']} ms',
|
||||
if (detail['connectMs'] != null) '连接 ${detail['connectMs']} ms',
|
||||
if (detail['tlsMs'] != null) 'TLS ${detail['tlsMs']} ms',
|
||||
if (detail['ttfbMs'] != null) 'TTFB ${detail['ttfbMs']} ms',
|
||||
if (detail['totalMs'] != null) '总计 ${detail['totalMs']} ms',
|
||||
];
|
||||
final chain = detail['redirects'];
|
||||
return [
|
||||
parts.join(' · '),
|
||||
if (times.isNotEmpty) times.join(','),
|
||||
if (chain != null && chain.isNotEmpty) '跳转 $chain',
|
||||
].where((part) => part.isNotEmpty).join('\n');
|
||||
}
|
||||
|
||||
Future<void> _openSettings() async {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => SettingsPage(
|
||||
allowIPv4: _allow4,
|
||||
allowIPv6: _allow6,
|
||||
autoUpdate: _autoUpdate,
|
||||
onAllowIPv4: (value) {
|
||||
setState(() => _allow4 = value);
|
||||
unawaited(_persist());
|
||||
},
|
||||
onAllowIPv6: (value) {
|
||||
setState(() => _allow6 = value);
|
||||
unawaited(_persist());
|
||||
},
|
||||
onAutoUpdate: (value) {
|
||||
setState(() => _autoUpdate = value);
|
||||
unawaited(_persist());
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _launchUpdate() async {
|
||||
final result = await checkForUpdate();
|
||||
final offer = result.offer;
|
||||
if (offer == null || !mounted || !_autoUpdate) {
|
||||
return;
|
||||
}
|
||||
await installOffer(offer);
|
||||
}
|
||||
|
||||
Future<void> _showHistory() async {
|
||||
if (_history.entries.isEmpty) {
|
||||
setState(() => _centerHint = '');
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('最近目标'),
|
||||
content: const Text('还没有保存过目标。点「开始探测」之后,这里会记下地址、端口和类型。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('最近目标'),
|
||||
content: SizedBox(
|
||||
width: 420,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
for (var i = 0; i < _history.entries.length; i++)
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(_history.entries[i].label),
|
||||
onTap: () {
|
||||
_applyHistory(_history.entries[i]);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
trailing: IconButton(
|
||||
tooltip: '删除',
|
||||
onPressed: () async {
|
||||
_history = _history.removeAt(i);
|
||||
await _store.saveHistory(_history);
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
await _showHistory();
|
||||
},
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
_history = _history.clear();
|
||||
await _store.saveHistory(_history);
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
},
|
||||
child: const Text('全部清除'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _applyHistory(TargetEntry entry) {
|
||||
setState(() {
|
||||
_formEpoch += 1;
|
||||
_profileId = entry.profileId;
|
||||
_host.text = entry.httpUrl.isNotEmpty ? entry.httpUrl : entry.host;
|
||||
_port.text = '${entry.port}';
|
||||
if (entry.profileId == 'http' && entry.method.isNotEmpty) {
|
||||
_httpMethod = entry.method;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String _newId() {
|
||||
_idSeq += 1;
|
||||
return '${DateTime.now().microsecondsSinceEpoch}-$_idSeq';
|
||||
}
|
||||
|
||||
Future<void> _addCenter() async {
|
||||
final added = await showCenterAddDialog(context);
|
||||
if (added == null) {
|
||||
return;
|
||||
}
|
||||
var link = added.link;
|
||||
for (final existing in _centers) {
|
||||
if (existing.url == added.link.url && existing.account == added.link.account) {
|
||||
link = existing;
|
||||
break;
|
||||
}
|
||||
}
|
||||
await _vault.write(centerTokenKey(link.id), added.token);
|
||||
await _applyCatalog(link, added.catalog, '已添加 ${added.catalog.servers.length} 台服务器');
|
||||
}
|
||||
|
||||
Future<void> _refreshCenter(CenterLink link) async {
|
||||
try {
|
||||
final token = await _vault.read(centerTokenKey(link.id)) ?? '';
|
||||
final catalog = await fetchCatalog(baseUrl: link.url, token: token);
|
||||
await _applyCatalog(link, catalog, '已按中心重新同步');
|
||||
} on CenterClientException catch (error) {
|
||||
setState(() => _centerHint = error.message);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _removeCenter(CenterLink link) async {
|
||||
final gone = [
|
||||
for (final node in _nodes)
|
||||
if (node.centerLinkId == link.id) node.id,
|
||||
];
|
||||
for (final id in gone) {
|
||||
await _vault.delete(nodeTokenKey(id));
|
||||
}
|
||||
await _vault.delete(centerTokenKey(link.id));
|
||||
setState(() {
|
||||
_centers = [for (final item in _centers) if (item.id != link.id) item];
|
||||
_nodes = [
|
||||
for (final node in _nodes)
|
||||
if (node.centerLinkId != link.id) node,
|
||||
];
|
||||
if (!_nodes.any((node) => node.id == _nodeId)) {
|
||||
_nodeId = '';
|
||||
}
|
||||
_centerHint = '已移除这个中心口令及其服务器';
|
||||
});
|
||||
await _persist();
|
||||
}
|
||||
|
||||
Future<void> _applyCatalog(CenterLink link, Catalog catalog, String hint) async {
|
||||
final before = {
|
||||
for (final node in _nodes)
|
||||
if (node.centerLinkId == link.id) node.id,
|
||||
};
|
||||
final merged = mergeCenterCatalog(
|
||||
existing: _nodes,
|
||||
link: link,
|
||||
catalog: catalog,
|
||||
newId: _newId,
|
||||
);
|
||||
final after = {
|
||||
for (final node in merged.nodes)
|
||||
if (node.centerLinkId == link.id) node.id,
|
||||
};
|
||||
for (final id in before.difference(after)) {
|
||||
await _vault.delete(nodeTokenKey(id));
|
||||
}
|
||||
for (final node in merged.nodes) {
|
||||
if (node.centerLinkId == link.id && node.confidential) {
|
||||
await _vault.delete(nodeTokenKey(node.id));
|
||||
}
|
||||
}
|
||||
for (final entry in merged.credentials.entries) {
|
||||
await _vault.write(nodeTokenKey(entry.key), entry.value);
|
||||
}
|
||||
setState(() {
|
||||
if (!_centers.any((item) => item.id == link.id)) {
|
||||
_centers = [..._centers, link];
|
||||
}
|
||||
_nodes = merged.nodes;
|
||||
_centerHint = hint;
|
||||
});
|
||||
await _persist();
|
||||
}
|
||||
|
||||
Widget _centerTile(CenterLink link, bool running) {
|
||||
final servers = [
|
||||
for (final node in _nodes)
|
||||
if (node.centerLinkId == link.id) node,
|
||||
];
|
||||
final title = link.account.isEmpty ? link.url : '${link.account} · ${link.url}';
|
||||
return ExpansionTile(
|
||||
tilePadding: EdgeInsets.zero,
|
||||
title: Text(title, maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
subtitle: Text('${servers.length} 台服务器'),
|
||||
children: [
|
||||
for (final node in servers)
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(node.name),
|
||||
subtitle: Text(
|
||||
node.confidential ? '地址由中心代为连接,本机不保存。' : node.baseUrl,
|
||||
),
|
||||
),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: running ? null : () => _refreshCenter(link),
|
||||
child: const Text('刷新'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: running ? null : () => _removeCenter(link),
|
||||
child: const Text('移除'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _nodeLabel(RemoteNode node) {
|
||||
final vis = visibilityLabel(node.visibility);
|
||||
return '${node.name} · $vis';
|
||||
@@ -493,6 +949,11 @@ class _HomePageState extends State<HomePage> {
|
||||
const SizedBox(height: 8),
|
||||
Text(view.note, style: const TextStyle(color: ink)),
|
||||
],
|
||||
if (view.detail['httpVersion'] != null ||
|
||||
view.detail['statusCode'] != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(_httpSummary(view.detail), style: const TextStyle(color: ink)),
|
||||
],
|
||||
if (motd != null || version != null || players != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
@@ -622,6 +1083,10 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
}
|
||||
|
||||
void _beginEdit(RemoteNode node) {
|
||||
if (node.fromCenter) {
|
||||
setState(() => _hint = '中心分配的名称和地址不能在这里修改。需要变更时,回到中心刷新。');
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_editingId = node.id;
|
||||
_name.text = node.name;
|
||||
@@ -642,6 +1107,10 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
}
|
||||
|
||||
Future<void> _check(RemoteNode node) async {
|
||||
if (node.confidential) {
|
||||
setState(() => _hint = '这台服务器由中心代为连接,本机没有地址,不能直接检查。');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final info = await fetchNodeInfo(node.baseUrl);
|
||||
final next = [
|
||||
@@ -695,18 +1164,21 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
'${node.name} · ${visibilityLabel(node.visibility)}',
|
||||
),
|
||||
subtitle: Text(
|
||||
node.baseUrl,
|
||||
maxLines: 1,
|
||||
node.confidential
|
||||
? '地址由中心代为连接,本机不保存。'
|
||||
: node.baseUrl,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: Wrap(
|
||||
spacing: 4,
|
||||
children: [
|
||||
TextButton(
|
||||
key: Key('edit-node-${node.id}'),
|
||||
onPressed: () => _beginEdit(node),
|
||||
child: const Text('编辑'),
|
||||
),
|
||||
if (!node.fromCenter)
|
||||
TextButton(
|
||||
key: Key('edit-node-${node.id}'),
|
||||
onPressed: () => _beginEdit(node),
|
||||
child: const Text('编辑'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => _check(node),
|
||||
child: const Text('检查'),
|
||||
|
||||
@@ -57,6 +57,14 @@ class MetricGuidePage extends StatelessWidget {
|
||||
title: '实际地址和 IPv6 回退',
|
||||
body: '「实际地址」是这次真正连通或真正发包的 IP,并注明 IPv4 或 IPv6。「解析到」只是域名解析的结果,不会被说成已经在用的地址。\n\n正在尝试、还没有正式结果时,会标明正在尝试,而不是写成已经发往。远程节点如果没有在结果里带上地址(旧版本就是这样),界面写「节点没有报告实际使用的地址」,不会在手机上另做一次解析来冒充。\n\n地址落在 198.18.0.0/15 时,会提示这是 VPN 假地址,只有这个 VPN 开着时才能用。',
|
||||
),
|
||||
_Section(
|
||||
title: 'IPv4 和 IPv6',
|
||||
body: '设置里有两个开关,默认都开着。解析出多个地址以后,只向打开的地址族发包。只开一个,就只用那一个。两个都关掉时不会开始,并说明原因。目标本身就是被关掉的那种地址时,也不会开始。\n\n远程任务会带上同样的选择,节点不会改用另一个地址族。结果里的实际地址和地址族,仍然是这一次真正用到的那个。',
|
||||
),
|
||||
_Section(
|
||||
title: 'HTTP',
|
||||
body: 'HTTP 探测记下 DNS、连接、TLS、首字节和总时间,以及状态码、最终地址和实际用到的 HTTP 版本(1.1、2 或 3)。曲线上的延迟是总时间。已经收到的响应算成功,4xx 和 5xx 只显示状态码,不叫丢包,也不算进失败率。连不上或强制的版本不被支持,才算失败。\n\n默认先试 HTTP/2,不行再降到 1.1。选了「仅 HTTP/1.1」「仅 HTTP/2」或「仅 HTTP/3」时,服务器不支持就失败,不会悄悄换一种。指定解析只改连接走的地址,Host 和 SNI 仍用目标网址,用来看 CDN。它不是把 1.1.1.1 当成 DNS 服务器。\n\nHTTP/3 的连接和 TLS 是同一次 QUIC 握手,这两项时间相同。本机探测和节点用的是同一个 Go 引擎。桌面端需要把 netquality-reflector 放在程序旁边,或设置 NQ_PROBE_HELPER。Android 上如果系统不允许从应用目录启动这个程序,本机 HTTP 会说明缺引擎;远程节点不受影响。',
|
||||
),
|
||||
_Section(
|
||||
title: '停止以后',
|
||||
body: '点停止会保留曲线,并给出开始、结束、用时、目标、类型、来源、实际地址和同一套最终指标。正在进行、因为停止而被取消的请求不计入丢包。重新开始会换一次新的会话,旧曲线不会混进来。\n\n点曲线上的一点可以钉住该时刻的延迟和比率,松手后仍然留着。再点另一点会换。新会话或这个点滚出窗口后,选择会被清掉,不会跳到别的点上。',
|
||||
|
||||
@@ -69,6 +69,12 @@ MetricLine _timeoutLine(ProbeSummary summary, String profileId) {
|
||||
case 'tcp-connect':
|
||||
case 'minecraft-java':
|
||||
return const MetricLine('超时', '不适用', detail: '这种探测把连不上记为失败,不单独叫探测超时。');
|
||||
case 'http':
|
||||
return const MetricLine(
|
||||
'超时',
|
||||
'不适用',
|
||||
detail: 'HTTP 把连不上记为失败。已经收到的 4xx 和 5xx 是状态码,不是丢包,也不算进超时。',
|
||||
);
|
||||
default:
|
||||
return MetricLine(
|
||||
'超时',
|
||||
@@ -97,6 +103,12 @@ MetricLine _rateLine(ProbeSummary summary, String profileId) {
|
||||
formatPct(summary.failurePct),
|
||||
detail: '未成功的正式尝试 ÷ 已完成的正式尝试。这不是物理丢包。',
|
||||
);
|
||||
case 'http':
|
||||
return MetricLine(
|
||||
'失败率',
|
||||
formatPct(summary.failurePct),
|
||||
detail: '连不上或协议不对才算失败。收到 HTTP 响应就算成功,4xx 和 5xx 只显示状态码,不叫丢包。曲线上的延迟是这一次的总时间。',
|
||||
);
|
||||
default:
|
||||
return MetricLine(
|
||||
'无应答(未知)',
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../update/apply_update.dart';
|
||||
import '../update/update_check.dart';
|
||||
import '../version.dart';
|
||||
|
||||
class SettingsPage extends StatefulWidget {
|
||||
const SettingsPage({
|
||||
super.key,
|
||||
required this.allowIPv4,
|
||||
required this.allowIPv6,
|
||||
required this.autoUpdate,
|
||||
required this.onAllowIPv4,
|
||||
required this.onAllowIPv6,
|
||||
required this.onAutoUpdate,
|
||||
});
|
||||
|
||||
final bool allowIPv4;
|
||||
final bool allowIPv6;
|
||||
final bool autoUpdate;
|
||||
final ValueChanged<bool> onAllowIPv4;
|
||||
final ValueChanged<bool> onAllowIPv6;
|
||||
final ValueChanged<bool> onAutoUpdate;
|
||||
|
||||
@override
|
||||
State<SettingsPage> createState() => _SettingsPageState();
|
||||
}
|
||||
|
||||
class _SettingsPageState extends State<SettingsPage> {
|
||||
late bool _allow4;
|
||||
late bool _allow6;
|
||||
late bool _auto;
|
||||
var _checking = false;
|
||||
String _message = '';
|
||||
UpdateOffer? _offer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_allow4 = widget.allowIPv4;
|
||||
_allow6 = widget.allowIPv6;
|
||||
_auto = widget.autoUpdate;
|
||||
}
|
||||
|
||||
Future<void> _check() async {
|
||||
setState(() {
|
||||
_checking = true;
|
||||
_message = '';
|
||||
_offer = null;
|
||||
});
|
||||
final result = await checkForUpdate();
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_checking = false;
|
||||
_message = result.message;
|
||||
_offer = result.offer;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _install() async {
|
||||
final offer = _offer;
|
||||
if (offer == null) {
|
||||
return;
|
||||
}
|
||||
setState(() => _checking = true);
|
||||
final message = await installOffer(offer);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_checking = false;
|
||||
_message = message;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final wide = MediaQuery.sizeOf(context).width >= 720;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('设置'),
|
||||
backgroundColor: const Color(0xFFF3F1EC),
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Align(
|
||||
alignment: Alignment.topCenter,
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 760),
|
||||
child: ListView(
|
||||
padding: EdgeInsets.fromLTRB(wide ? 24 : 12, 8, wide ? 24 : 12, 28),
|
||||
children: [
|
||||
SwitchListTile(
|
||||
title: const Text('允许 IPv4'),
|
||||
subtitle: const Text('关掉以后,解析结果里的 IPv4 不会用来发包。直接填写 IPv4 地址也会被拒绝。'),
|
||||
value: _allow4,
|
||||
onChanged: (value) {
|
||||
setState(() => _allow4 = value);
|
||||
widget.onAllowIPv4(value);
|
||||
},
|
||||
),
|
||||
SwitchListTile(
|
||||
title: const Text('允许 IPv6'),
|
||||
subtitle: const Text('关掉以后,解析结果里的 IPv6 不会用来发包。直接填写 IPv6 地址也会被拒绝。'),
|
||||
value: _allow6,
|
||||
onChanged: (value) {
|
||||
setState(() => _allow6 = value);
|
||||
widget.onAllowIPv6(value);
|
||||
},
|
||||
),
|
||||
const Padding(
|
||||
padding: EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Text(
|
||||
'两个都关掉时不能开始探测。远程节点会收到同样的选择。1.1.2 的节点还不会遵守这个开关,遇到这种情况会提示升级。',
|
||||
style: TextStyle(color: Color(0xFF5C656B), fontSize: 13, height: 1.4),
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
SwitchListTile(
|
||||
title: const Text('自动检查更新'),
|
||||
subtitle: const Text('默认打开。关闭后,启动时不检查,也不会自动下载。'),
|
||||
value: _auto,
|
||||
onChanged: (value) {
|
||||
setState(() => _auto = value);
|
||||
widget.onAutoUpdate(value);
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Text(
|
||||
'当前版本 $appVersion(构建 $appBuild)。1.1.2 没有自更新,从 1.1.2 升到 1.1.3 仍要手动安装。从这一版起才会自己检查。先试 GitHub,连不上再试言灸的 Git。\n\n要在没有新版本时试这个功能,可以把环境变量 NQ_UPDATE_GITHUB_API 指到一个同样格式的测试地址,NQ_UPDATE_GITEA_API 是备用。',
|
||||
style: const TextStyle(color: Color(0xFF5C656B), fontSize: 13, height: 1.4),
|
||||
),
|
||||
),
|
||||
if (Platform.isAndroid)
|
||||
const Padding(
|
||||
padding: EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Text(
|
||||
'在 Android 上安装时,系统会弹出安装确认。这一步不能跳过。',
|
||||
style: TextStyle(color: Color(0xFF9A3412), fontSize: 13, height: 1.4),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: _checking ? null : _check,
|
||||
child: Text(_checking ? '正在检查' : '检查更新'),
|
||||
),
|
||||
if (_offer != null)
|
||||
FilledButton(
|
||||
onPressed: _checking ? null : _install,
|
||||
child: const Text('下载并安装'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_message.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
|
||||
child: Text(_message),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user