网络质量探测 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,152 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../metrics/sample_book.dart';
|
||||
|
||||
const helperMissingNote =
|
||||
'本机 HTTP 探测使用和节点相同的 Go 引擎。请把 netquality-reflector 放在程序旁边,或设置环境变量 NQ_PROBE_HELPER。远程节点不受影响。';
|
||||
|
||||
String? locateProbeHelper() {
|
||||
final env = Platform.environment['NQ_PROBE_HELPER'];
|
||||
if (env != null && env.isNotEmpty && File(env).existsSync()) {
|
||||
return env;
|
||||
}
|
||||
final dir = File(Platform.resolvedExecutable).parent;
|
||||
final names = Platform.isWindows
|
||||
? const ['netquality-reflector.exe', 'netquality-helper.exe']
|
||||
: const ['netquality-reflector', 'netquality-helper'];
|
||||
for (final name in names) {
|
||||
final file = File('${dir.path}${Platform.pathSeparator}$name');
|
||||
if (file.existsSync()) {
|
||||
return file.path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Outcome outcomeFromHelper(String line) {
|
||||
try {
|
||||
final decoded = jsonDecode(line);
|
||||
if (decoded is! Map) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: '探测引擎返回的内容无效',
|
||||
);
|
||||
}
|
||||
final detail = <String, String>{};
|
||||
final rawDetail = decoded['detail'];
|
||||
if (rawDetail is Map) {
|
||||
rawDetail.forEach((key, value) {
|
||||
if (key is String && value != null) {
|
||||
detail[key] = '$value';
|
||||
}
|
||||
});
|
||||
}
|
||||
return Outcome(
|
||||
status: decoded['status'] is String
|
||||
? decoded['status'] as String
|
||||
: Outcome.error,
|
||||
kind: decoded['kind'] is String
|
||||
? decoded['kind'] as String
|
||||
: Outcome.query,
|
||||
rttMs: decoded['rttMs'] is num
|
||||
? (decoded['rttMs'] as num).toDouble()
|
||||
: null,
|
||||
note: decoded['note'] is String ? decoded['note'] as String : '',
|
||||
detail: detail,
|
||||
);
|
||||
} on FormatException {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: '探测引擎返回的内容无效',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One long-running helper process. Each line is one HTTP sample.
|
||||
class HttpHelperSession {
|
||||
HttpHelperSession({this.binary});
|
||||
|
||||
final String? binary;
|
||||
Process? _process;
|
||||
final _lines = <String>[];
|
||||
var _closed = false;
|
||||
|
||||
Future<Outcome> probe(Map<String, Object?> request) async {
|
||||
final started = await _ensure();
|
||||
if (started != null) {
|
||||
return started;
|
||||
}
|
||||
final process = _process;
|
||||
if (process == null) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: helperMissingNote,
|
||||
);
|
||||
}
|
||||
process.stdin.writeln(jsonEncode(request));
|
||||
final line = await _nextLine();
|
||||
if (line == null) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: '探测引擎没有返回结果',
|
||||
);
|
||||
}
|
||||
return outcomeFromHelper(line);
|
||||
}
|
||||
|
||||
Future<Outcome?> _ensure() async {
|
||||
if (_process != null) {
|
||||
return null;
|
||||
}
|
||||
final path = binary ?? locateProbeHelper();
|
||||
if (path == null) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: helperMissingNote,
|
||||
);
|
||||
}
|
||||
final process = await Process.start(path, const ['-mode', 'helper']);
|
||||
_process = process;
|
||||
process.stdout
|
||||
.transform(utf8.decoder)
|
||||
.transform(const LineSplitter())
|
||||
.listen((line) {
|
||||
if (line.trim().isNotEmpty) {
|
||||
_lines.add(line);
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String?> _nextLine() async {
|
||||
final deadline = DateTime.now().add(const Duration(seconds: 20));
|
||||
while (DateTime.now().isBefore(deadline)) {
|
||||
if (_lines.isNotEmpty) {
|
||||
return _lines.removeAt(0);
|
||||
}
|
||||
if (_closed) {
|
||||
return null;
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
_closed = true;
|
||||
final process = _process;
|
||||
_process = null;
|
||||
if (process != null) {
|
||||
process.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -259,11 +259,36 @@ class ProbeController extends ChangeNotifier {
|
||||
bool cancelled() => generation != _token;
|
||||
try {
|
||||
if (config.remote) {
|
||||
final selected = config.node!;
|
||||
var baseUrl = selected.baseUrl;
|
||||
var token = nodeToken;
|
||||
if (selected.confidential) {
|
||||
final link = config.centerLink(selected.centerLinkId);
|
||||
if (link == null) {
|
||||
throw RemoteException('找不到签发这台服务器的中心');
|
||||
}
|
||||
baseUrl = link.url;
|
||||
token = nodeToken;
|
||||
}
|
||||
if (config.profileId == 'http' || !config.allowIPv4 || !config.allowIPv6) {
|
||||
if (!selected.confidential) {
|
||||
final info = await fetchNodeInfo(baseUrl);
|
||||
final upgrade = remoteUpgradeMessage(
|
||||
info,
|
||||
profileId: config.profileId,
|
||||
allow4: config.allowIPv4,
|
||||
allow6: config.allowIPv6,
|
||||
);
|
||||
if (upgrade != null) {
|
||||
throw RemoteException(upgrade);
|
||||
}
|
||||
}
|
||||
}
|
||||
final remote = RemoteProbe();
|
||||
_remote = remote;
|
||||
await remote.run(
|
||||
baseUrl: config.node!.baseUrl,
|
||||
nodeToken: nodeToken,
|
||||
baseUrl: baseUrl,
|
||||
nodeToken: token,
|
||||
body: ProbeBody(
|
||||
target: config.host.trim(),
|
||||
port: config.port,
|
||||
@@ -276,7 +301,11 @@ class ProbeController extends ChangeNotifier {
|
||||
dnsName: config.dnsName.trim(),
|
||||
payloadHex: config.payloadHex.trim(),
|
||||
expectHex: config.expectHex.trim(),
|
||||
allowIPv4: config.allowIPv4,
|
||||
allowIPv6: config.allowIPv6,
|
||||
http: config.profileId == 'http' ? _httpJson(config) : null,
|
||||
),
|
||||
jobsPath: _jobsPath(config),
|
||||
onSample: (sample) => _accept(generation, sample),
|
||||
onResolved: (resolved) {
|
||||
if (generation != _token) {
|
||||
@@ -328,6 +357,16 @@ class ProbeController extends ChangeNotifier {
|
||||
dnsName: config.dnsName.trim(),
|
||||
payloadHex: config.payloadHex.trim(),
|
||||
expectHex: config.expectHex.trim(),
|
||||
allowIPv4: config.allowIPv4,
|
||||
allowIPv6: config.allowIPv6,
|
||||
httpVersion: config.httpVersion,
|
||||
httpResolve: config.httpResolve.trim(),
|
||||
httpMethod: config.httpMethod,
|
||||
httpBody: config.httpBody,
|
||||
httpReferer: config.httpReferer,
|
||||
httpUserAgent: config.httpUserAgent,
|
||||
httpCookies: config.httpCookies,
|
||||
httpMaxRedirects: config.httpMaxRedirects,
|
||||
),
|
||||
onSample: (sample) => _accept(generation, sample),
|
||||
cancelled: cancelled,
|
||||
@@ -491,6 +530,25 @@ class ProbeController extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object?> _httpJson(SessionConfig config) => {
|
||||
'version': config.httpVersion,
|
||||
'resolve': config.httpResolve.trim(),
|
||||
'method': config.httpMethod,
|
||||
'body': config.httpBody,
|
||||
'referer': config.httpReferer,
|
||||
'userAgent': config.httpUserAgent,
|
||||
'cookies': config.httpCookies,
|
||||
'maxRedirects': config.httpMaxRedirects,
|
||||
};
|
||||
|
||||
String _jobsPath(SessionConfig config) {
|
||||
final node = config.node;
|
||||
if (node != null && node.confidential && node.serverId.isNotEmpty) {
|
||||
return '/v1/center/servers/${node.serverId}/jobs';
|
||||
}
|
||||
return '/v1/jobs';
|
||||
}
|
||||
|
||||
String? _echoProblem(String profileId, String echoToken) {
|
||||
if (profileId != 'nq-tcp' && profileId != 'nq-udp') {
|
||||
return null;
|
||||
|
||||
@@ -4,10 +4,12 @@ import 'dart:typed_data';
|
||||
|
||||
import '../metrics/sample_book.dart';
|
||||
import '../metrics/selection_ledger.dart';
|
||||
import '../models/ip_family.dart';
|
||||
import '../protocol/probe_packet.dart';
|
||||
import '../ui/address_text.dart';
|
||||
import 'endpoint_picker.dart';
|
||||
import 'game_codec.dart';
|
||||
import 'http_helper.dart';
|
||||
|
||||
class ProbeRequest {
|
||||
const ProbeRequest({
|
||||
@@ -21,6 +23,16 @@ class ProbeRequest {
|
||||
required this.dnsName,
|
||||
required this.payloadHex,
|
||||
required this.expectHex,
|
||||
this.allowIPv4 = true,
|
||||
this.allowIPv6 = true,
|
||||
this.httpVersion = 'auto',
|
||||
this.httpResolve = '',
|
||||
this.httpMethod = 'GET',
|
||||
this.httpBody = '',
|
||||
this.httpReferer = '',
|
||||
this.httpUserAgent = '',
|
||||
this.httpCookies = '',
|
||||
this.httpMaxRedirects = 5,
|
||||
});
|
||||
|
||||
final String host;
|
||||
@@ -33,12 +45,22 @@ class ProbeRequest {
|
||||
final String dnsName;
|
||||
final String payloadHex;
|
||||
final String expectHex;
|
||||
final bool allowIPv4;
|
||||
final bool allowIPv6;
|
||||
final String httpVersion;
|
||||
final String httpResolve;
|
||||
final String httpMethod;
|
||||
final String httpBody;
|
||||
final String httpReferer;
|
||||
final String httpUserAgent;
|
||||
final String httpCookies;
|
||||
final int httpMaxRedirects;
|
||||
}
|
||||
|
||||
/// Sends one probe at a time from this device. A cancelled attempt is not
|
||||
/// recorded, so stopping cannot paint a fake loss sample.
|
||||
class ProbeLoop {
|
||||
ProbeLoop({this.lookup, this.onEndpoint, this.onResolved});
|
||||
ProbeLoop({this.lookup, this.onEndpoint, this.onResolved, this.helperPath});
|
||||
|
||||
/// Replaces [lookupAll] in tests.
|
||||
final Future<List<InternetAddress>> Function(String host)? lookup;
|
||||
@@ -49,6 +71,10 @@ class ProbeLoop {
|
||||
/// Fires once with the lookup result. This is not the address in use.
|
||||
final void Function(List<InternetAddress> addresses)? onResolved;
|
||||
|
||||
/// Overrides the Go HTTP engine binary. Null looks beside the app and at
|
||||
/// NQ_PROBE_HELPER.
|
||||
final String? helperPath;
|
||||
|
||||
_TcpPipe? _tcp;
|
||||
_UdpPipe? _udp;
|
||||
EndpointPicker? _picker;
|
||||
@@ -59,6 +85,9 @@ class ProbeLoop {
|
||||
int _seq = 0;
|
||||
int _dnsId = 1;
|
||||
final int _guid = DateTime.now().microsecondsSinceEpoch;
|
||||
bool _allow4 = true;
|
||||
bool _allow6 = true;
|
||||
HttpHelperSession? _http;
|
||||
|
||||
Future<void> run({
|
||||
required ProbeRequest request,
|
||||
@@ -66,6 +95,23 @@ class ProbeLoop {
|
||||
required bool Function() cancelled,
|
||||
}) async {
|
||||
final book = SampleBook(request.profileId);
|
||||
_allow4 = request.allowIPv4;
|
||||
_allow6 = request.allowIPv6;
|
||||
final refusal = familyRefusal(
|
||||
request.host,
|
||||
v4: request.allowIPv4,
|
||||
v6: request.allowIPv6,
|
||||
);
|
||||
if (refusal != null) {
|
||||
final outcome = Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: refusal,
|
||||
);
|
||||
book.add(outcome);
|
||||
onSample(book.sample(1, outcome));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
while (!cancelled()) {
|
||||
final started = DateTime.now();
|
||||
@@ -108,6 +154,11 @@ class ProbeLoop {
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
final http = _http;
|
||||
_http = null;
|
||||
if (http != null) {
|
||||
await http.close();
|
||||
}
|
||||
final tcp = _tcp;
|
||||
_tcp = null;
|
||||
if (tcp != null) {
|
||||
@@ -165,6 +216,8 @@ class ProbeLoop {
|
||||
return _java(request, cancelled);
|
||||
case 'minecraft-bedrock':
|
||||
return _bedrock(request, cancelled);
|
||||
case 'http':
|
||||
return _httpProbe(request);
|
||||
default:
|
||||
return Future.value(
|
||||
const Outcome(
|
||||
@@ -656,9 +709,35 @@ class ProbeLoop {
|
||||
return cached;
|
||||
}
|
||||
final found = await (lookup ?? lookupAll)(host);
|
||||
_resolved = found;
|
||||
onResolved?.call(found);
|
||||
return found;
|
||||
final kept = filterAddresses(found, v4: _allow4, v6: _allow6);
|
||||
if (kept.isEmpty) {
|
||||
throw const SocketException('解析到的地址都不在已打开的地址族里');
|
||||
}
|
||||
_resolved = kept;
|
||||
onResolved?.call(kept);
|
||||
return kept;
|
||||
}
|
||||
|
||||
Future<Outcome> _httpProbe(ProbeRequest request) async {
|
||||
final session = _http ??= HttpHelperSession(binary: helperPath);
|
||||
return session.probe({
|
||||
'profile': 'http',
|
||||
'target': request.host,
|
||||
'port': request.port,
|
||||
'timeoutMs': request.timeout.inMilliseconds,
|
||||
'allowIPv4': request.allowIPv4,
|
||||
'allowIPv6': request.allowIPv6,
|
||||
'http': {
|
||||
'version': request.httpVersion,
|
||||
'resolve': request.httpResolve,
|
||||
'method': request.httpMethod,
|
||||
'body': request.httpBody,
|
||||
'referer': request.httpReferer,
|
||||
'userAgent': request.httpUserAgent,
|
||||
'cookies': request.httpCookies,
|
||||
'maxRedirects': request.httpMaxRedirects,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Future<_TcpOpen> _openTcp(
|
||||
@@ -809,6 +888,13 @@ Outcome _stampAddress(Outcome outcome, InternetAddress peer) {
|
||||
}
|
||||
|
||||
Outcome classifySocket(SocketException error, {bool reconnect = false}) {
|
||||
if (error.message.contains('地址族') || error.message.contains('已关闭')) {
|
||||
return Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: error.message,
|
||||
);
|
||||
}
|
||||
final text = '${error.message} ${error.osError?.message ?? ''}'.toLowerCase();
|
||||
final code = error.osError?.errorCode;
|
||||
if (text.contains('timed out') || text.contains('timeout')) {
|
||||
|
||||
@@ -10,6 +10,7 @@ class RemoteProbe {
|
||||
HttpClient? _client;
|
||||
String? _jobId;
|
||||
Uri? _base;
|
||||
String _jobsPath = '/v1/jobs';
|
||||
|
||||
Future<void> run({
|
||||
required String baseUrl,
|
||||
@@ -18,19 +19,21 @@ class RemoteProbe {
|
||||
required void Function(ProbeSample sample) onSample,
|
||||
required bool Function() cancelled,
|
||||
void Function(List<ResolvedAddress> resolved)? onResolved,
|
||||
String jobsPath = '/v1/jobs',
|
||||
}) async {
|
||||
final normalized = normalizeNodeUrl(baseUrl);
|
||||
if (normalized == null) {
|
||||
throw RemoteException('节点地址需要是 http 或 https,且不能把口令写在地址里');
|
||||
}
|
||||
_base = Uri.parse(normalized);
|
||||
_jobsPath = jobsPath.startsWith('/') ? jobsPath : '/$jobsPath';
|
||||
final client = HttpClient();
|
||||
_client = client;
|
||||
try {
|
||||
final created = await _send(
|
||||
client,
|
||||
'POST',
|
||||
_base!.resolve('/v1/jobs'),
|
||||
_base!.resolve(_jobsPath),
|
||||
nodeToken,
|
||||
utf8.encode(jsonEncode(body.toJson())),
|
||||
);
|
||||
@@ -92,7 +95,7 @@ class RemoteProbe {
|
||||
await _send(
|
||||
client,
|
||||
'DELETE',
|
||||
base.resolve('/v1/jobs/$id'),
|
||||
base.resolve('$_jobsPath/$id'),
|
||||
nodeToken,
|
||||
const [],
|
||||
);
|
||||
@@ -111,7 +114,7 @@ class RemoteProbe {
|
||||
bool Function() cancelled,
|
||||
) async {
|
||||
final request = await client.getUrl(
|
||||
_base!.resolve('/v1/jobs/$_jobId/stream'),
|
||||
_base!.resolve('$_jobsPath/$_jobId/stream'),
|
||||
);
|
||||
request.followRedirects = false;
|
||||
_auth(request, nodeToken);
|
||||
@@ -154,6 +157,9 @@ class ProbeBody {
|
||||
required this.dnsName,
|
||||
required this.payloadHex,
|
||||
required this.expectHex,
|
||||
this.allowIPv4 = true,
|
||||
this.allowIPv6 = true,
|
||||
this.http,
|
||||
});
|
||||
|
||||
final String target;
|
||||
@@ -167,6 +173,9 @@ class ProbeBody {
|
||||
final String dnsName;
|
||||
final String payloadHex;
|
||||
final String expectHex;
|
||||
final bool allowIPv4;
|
||||
final bool allowIPv6;
|
||||
final Map<String, Object?>? http;
|
||||
|
||||
Map<String, Object?> toJson() => {
|
||||
'target': target,
|
||||
@@ -180,6 +189,9 @@ class ProbeBody {
|
||||
'dnsName': dnsName,
|
||||
'payloadHex': payloadHex,
|
||||
'expectHex': expectHex,
|
||||
'allowIPv4': allowIPv4,
|
||||
'allowIPv6': allowIPv6,
|
||||
if (http != null) 'http': http,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -192,10 +204,32 @@ class RemoteException implements Exception {
|
||||
}
|
||||
|
||||
class NodeInfo {
|
||||
const NodeInfo({required this.visibility, required this.authRequired});
|
||||
const NodeInfo({
|
||||
required this.visibility,
|
||||
required this.authRequired,
|
||||
this.profiles = const [],
|
||||
this.ipFamily = false,
|
||||
});
|
||||
|
||||
final String visibility;
|
||||
final bool authRequired;
|
||||
final List<String> profiles;
|
||||
final bool ipFamily;
|
||||
}
|
||||
|
||||
String? remoteUpgradeMessage(
|
||||
NodeInfo info, {
|
||||
required String profileId,
|
||||
required bool allow4,
|
||||
required bool allow6,
|
||||
}) {
|
||||
if (profileId == 'http' && !info.profiles.contains('http')) {
|
||||
return '这个节点还不支持 HTTP 探测,需要升级到 1.1.3。';
|
||||
}
|
||||
if ((!allow4 || !allow6) && !info.ipFamily) {
|
||||
return '这个节点还不会遵守 IPv4 / IPv6 开关,需要升级到 1.1.3。';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<NodeInfo> fetchNodeInfo(String baseUrl) async {
|
||||
@@ -218,11 +252,22 @@ Future<NodeInfo> fetchNodeInfo(String baseUrl) async {
|
||||
if (json is! Map) {
|
||||
throw RemoteException('节点信息无效');
|
||||
}
|
||||
final profiles = <String>[];
|
||||
final rawProfiles = json['profiles'];
|
||||
if (rawProfiles is List) {
|
||||
for (final item in rawProfiles) {
|
||||
if (item is String) {
|
||||
profiles.add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
return NodeInfo(
|
||||
visibility: json['visibility'] is String
|
||||
? json['visibility'] as String
|
||||
: '',
|
||||
authRequired: json['authRequired'] == true,
|
||||
profiles: profiles,
|
||||
ipFamily: json['ipFamily'] == true,
|
||||
);
|
||||
} on RemoteException {
|
||||
rethrow;
|
||||
|
||||
Reference in new issue
Block a user