HTTP 探测改为填写网址,缺本机引擎时立刻失败
HTTP 模式不再单独填端口。完整网址沿用其中的协议、端口和路径;只写主机名或 IP 时按 http 和 80 端口。超时默认改为 5000 毫秒。本机找不到或启动不了 netquality-reflector 时直接说明原因,并建议改用远程节点,不再记成一次全部失败。 Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
1 parent
846e40c023
commit
918ec8f190
20 files changed
+807
-131
No files matched your search
@@ -0,0 +1,103 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/// An HTTP probe target. The separate port field is not used.
|
||||
class HttpTarget {
|
||||
const HttpTarget({
|
||||
required this.url,
|
||||
required this.host,
|
||||
required this.port,
|
||||
required this.scheme,
|
||||
});
|
||||
|
||||
/// Canonical absolute URL. Default ports are omitted.
|
||||
final String url;
|
||||
|
||||
/// Hostname or IP, without brackets.
|
||||
final String host;
|
||||
final int port;
|
||||
final String scheme;
|
||||
}
|
||||
|
||||
HttpTarget? parseHttpTarget(String raw) => readHttpTarget(raw).target;
|
||||
|
||||
String? httpTargetError(String raw) => readHttpTarget(raw).error;
|
||||
|
||||
({HttpTarget? target, String? error}) readHttpTarget(String raw) {
|
||||
final text = raw.trim();
|
||||
if (text.isEmpty) {
|
||||
return (target: null, error: '请填写目标地址');
|
||||
}
|
||||
if (text.contains('://')) {
|
||||
final uri = Uri.tryParse(text);
|
||||
if (uri == null) {
|
||||
return (target: null, error: _portOrUrlError(text));
|
||||
}
|
||||
if ((uri.scheme != 'http' && uri.scheme != 'https') || uri.host.isEmpty) {
|
||||
return (target: null, error: 'HTTP 目标需要是主机名或 http/https 网址');
|
||||
}
|
||||
return _fromUri(uri);
|
||||
}
|
||||
final cut = _splitCut(text);
|
||||
final hostport = cut < 0 ? text : text.substring(0, cut);
|
||||
final rest = cut < 0 ? '' : text.substring(cut);
|
||||
if (hostport.isEmpty) {
|
||||
return (target: null, error: 'HTTP 目标需要是主机名或 http/https 网址');
|
||||
}
|
||||
var bracketed = hostport;
|
||||
if (':'.allMatches(hostport).length >= 2 && !hostport.startsWith('[')) {
|
||||
bracketed = '[$hostport]';
|
||||
}
|
||||
final uri = Uri.tryParse('http://$bracketed$rest');
|
||||
if (uri == null || uri.host.isEmpty || uri.scheme != 'http') {
|
||||
return (target: null, error: _portOrUrlError(text));
|
||||
}
|
||||
return _fromUri(uri);
|
||||
}
|
||||
|
||||
({HttpTarget? target, String? error}) _fromUri(Uri uri) {
|
||||
final port = uri.hasPort ? uri.port : (uri.scheme == 'https' ? 443 : 80);
|
||||
if (port < 1 || port > 65535) {
|
||||
return (target: null, error: '端口需要在 1 到 65535 之间');
|
||||
}
|
||||
final built = Uri(
|
||||
scheme: uri.scheme,
|
||||
host: uri.host,
|
||||
port: port,
|
||||
path: uri.path.isEmpty ? '/' : uri.path,
|
||||
query: uri.hasQuery ? uri.query : null,
|
||||
fragment: uri.hasFragment ? uri.fragment : null,
|
||||
);
|
||||
return (
|
||||
target: HttpTarget(
|
||||
url: built.toString(),
|
||||
host: uri.host,
|
||||
port: port,
|
||||
scheme: uri.scheme,
|
||||
),
|
||||
error: null,
|
||||
);
|
||||
}
|
||||
|
||||
int _splitCut(String text) {
|
||||
final slash = text.indexOf('/');
|
||||
final query = text.indexOf('?');
|
||||
if (slash < 0) {
|
||||
return query;
|
||||
}
|
||||
if (query < 0) {
|
||||
return slash;
|
||||
}
|
||||
return slash < query ? slash : query;
|
||||
}
|
||||
|
||||
String _portOrUrlError(String text) {
|
||||
final match = RegExp(r':(\d+)(?=[/?#]|$)').firstMatch(text);
|
||||
if (match != null) {
|
||||
final port = int.tryParse(match.group(1)!);
|
||||
if (port == null || port < 1 || port > 65535) {
|
||||
return '端口需要在 1 到 65535 之间';
|
||||
}
|
||||
}
|
||||
return 'HTTP 目标需要是主机名或 http/https 网址';
|
||||
}
|
||||
@@ -23,20 +23,29 @@ String portHint(ProbeProfile profile) {
|
||||
return '合作反射 TCP 默认 19090';
|
||||
case 'nq-udp':
|
||||
return '合作反射 UDP 默认 19091';
|
||||
case 'http':
|
||||
return 'HTTP 默认 443(https)或 80(http)';
|
||||
default:
|
||||
return '${profile.label} 默认 ${profile.port}';
|
||||
}
|
||||
}
|
||||
|
||||
/// Default port for an HTTP target. A full http:// URL uses 80. Anything else uses 443.
|
||||
int httpDefaultPort(String target) {
|
||||
final text = target.trim().toLowerCase();
|
||||
if (text.startsWith('http://')) {
|
||||
return 80;
|
||||
const nonHttpTimeoutMs = 1000;
|
||||
const httpTimeoutMs = 5000;
|
||||
|
||||
int timeoutDefaultFor(String profileId) =>
|
||||
profileId == 'http' ? httpTimeoutMs : nonHttpTimeoutMs;
|
||||
|
||||
/// An empty timeout, or one that is still the previous profile's default,
|
||||
/// follows the newly selected profile. A value the user typed stays.
|
||||
String timeoutAfterProfileChange({
|
||||
required String current,
|
||||
required int previousDefault,
|
||||
required int nextDefault,
|
||||
}) {
|
||||
final trimmed = current.trim();
|
||||
if (trimmed.isEmpty || int.tryParse(trimmed) == previousDefault) {
|
||||
return '$nextDefault';
|
||||
}
|
||||
return 443;
|
||||
return current;
|
||||
}
|
||||
|
||||
const portHelperText =
|
||||
|
||||
@@ -68,8 +68,7 @@ class ProbeProfile {
|
||||
id: 'http',
|
||||
label: 'HTTP',
|
||||
port: 443,
|
||||
blurb:
|
||||
'按 HTTP 请求测量。默认优先 HTTP/2,服务器不支持时降到 HTTP/1.1。强制的版本不支持就失败,不会偷偷降级。4xx 和 5xx 会显示状态码,不算丢包。',
|
||||
blurb: '按 HTTP 请求测量。目标填完整网址;只填主机名或 IP 时按 http,端口 80。默认优先 HTTP/2,服务器不支持时降到 HTTP/1.1。强制的版本不支持就失败,不会偷偷降级。4xx 和 5xx 会显示状态码,不算丢包。',
|
||||
rateLabel: '失败率',
|
||||
);
|
||||
static const customUdp = ProbeProfile(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import '../protocol/probe_packet.dart';
|
||||
import 'http_target.dart';
|
||||
import 'ip_family.dart';
|
||||
import 'probe_profile.dart';
|
||||
|
||||
@@ -66,7 +67,9 @@ class RemoteNode {
|
||||
baseUrl: confidential
|
||||
? ''
|
||||
: (json['baseUrl'] is String ? json['baseUrl'] as String : ''),
|
||||
visibility: json['visibility'] is String ? json['visibility'] as String : '',
|
||||
visibility: json['visibility'] is String
|
||||
? json['visibility'] as String
|
||||
: '',
|
||||
fromCenter: json['fromCenter'] == true,
|
||||
confidential: confidential,
|
||||
centerLinkId: json['centerLinkId'] is String
|
||||
@@ -88,11 +91,7 @@ class CenterLink {
|
||||
final String url;
|
||||
final String account;
|
||||
|
||||
Map<String, Object?> toJson() => {
|
||||
'id': id,
|
||||
'url': url,
|
||||
'account': account,
|
||||
};
|
||||
Map<String, Object?> toJson() => {'id': id, 'url': url, 'account': account};
|
||||
|
||||
factory CenterLink.fromJson(Map<String, dynamic> json) {
|
||||
return CenterLink(
|
||||
@@ -185,6 +184,10 @@ class SessionConfig {
|
||||
|
||||
ProbeProfile get profile => ProbeProfile.byId(profileId);
|
||||
|
||||
/// Parsed HTTP target. Null for other profiles, or when the text is invalid.
|
||||
HttpTarget? get httpTarget =>
|
||||
profileId == 'http' ? parseHttpTarget(host) : null;
|
||||
|
||||
RemoteNode? get node {
|
||||
for (final item in nodes) {
|
||||
if (item.id == nodeId) {
|
||||
@@ -195,7 +198,15 @@ class SessionConfig {
|
||||
}
|
||||
|
||||
String? validate() {
|
||||
final family = familyRefusal(host, v4: allowIPv4, v6: allowIPv6);
|
||||
final http = profileId == 'http' ? readHttpTarget(host) : null;
|
||||
if (http != null && http.error != null) {
|
||||
return http.error;
|
||||
}
|
||||
final family = familyRefusal(
|
||||
http?.target?.host ?? host,
|
||||
v4: allowIPv4,
|
||||
v6: allowIPv6,
|
||||
);
|
||||
if (family != null) {
|
||||
return family;
|
||||
}
|
||||
@@ -209,7 +220,7 @@ class SessionConfig {
|
||||
return resolveFamily.replaceFirst('目标', '指定解析');
|
||||
}
|
||||
}
|
||||
if (host.trim().isEmpty) {
|
||||
if (profileId != 'http' && host.trim().isEmpty) {
|
||||
return '请填写目标地址';
|
||||
}
|
||||
if (profileId == 'http') {
|
||||
@@ -224,8 +235,7 @@ class SessionConfig {
|
||||
if (!versions.contains(httpVersion)) {
|
||||
return 'HTTP 版本需要是自动、1.1、2 或 3';
|
||||
}
|
||||
}
|
||||
if (port < 1 || port > 65535) {
|
||||
} else if (port < 1 || port > 65535) {
|
||||
return '端口需要在 1 到 65535 之间';
|
||||
}
|
||||
final minInterval = remote ? 200 : 100;
|
||||
@@ -333,11 +343,15 @@ class SessionConfig {
|
||||
dnsName: json['dnsName'] is String
|
||||
? json['dnsName'] as String
|
||||
: defaults.dnsName,
|
||||
payloadHex: json['payloadHex'] is String ? json['payloadHex'] as String : '',
|
||||
payloadHex: json['payloadHex'] is String
|
||||
? json['payloadHex'] as String
|
||||
: '',
|
||||
expectHex: json['expectHex'] is String ? json['expectHex'] as String : '',
|
||||
allowIPv4: json['allowIPv4'] is bool ? json['allowIPv4'] as bool : true,
|
||||
allowIPv6: json['allowIPv6'] is bool ? json['allowIPv6'] as bool : true,
|
||||
autoUpdate: json['autoUpdate'] is bool ? json['autoUpdate'] as bool : true,
|
||||
autoUpdate: json['autoUpdate'] is bool
|
||||
? json['autoUpdate'] as bool
|
||||
: true,
|
||||
httpVersion: json['httpVersion'] is String
|
||||
? json['httpVersion'] as String
|
||||
: 'auto',
|
||||
|
||||
@@ -34,11 +34,14 @@ class TargetEntry {
|
||||
}
|
||||
|
||||
String get label {
|
||||
final where = httpUrl.trim().isNotEmpty ? httpUrl.trim() : host.trim();
|
||||
final methodText = profileId == 'http' && method.trim().isNotEmpty
|
||||
? ' ${method.trim().toUpperCase()}'
|
||||
: '';
|
||||
return '$where:$port · $profileId$methodText';
|
||||
if (profileId == 'http') {
|
||||
final where = httpUrl.trim().isNotEmpty ? httpUrl.trim() : host.trim();
|
||||
final methodText = method.trim().isNotEmpty
|
||||
? ' ${method.trim().toUpperCase()}'
|
||||
: '';
|
||||
return '$where · HTTP$methodText';
|
||||
}
|
||||
return '${host.trim()}:$port · $profileId';
|
||||
}
|
||||
|
||||
Map<String, Object?> toJson() => {
|
||||
@@ -94,7 +97,8 @@ class TargetHistory {
|
||||
|
||||
TargetHistory clear() => empty;
|
||||
|
||||
String encode() => jsonEncode(entries.map((entry) => entry.toJson()).toList());
|
||||
String encode() =>
|
||||
jsonEncode(entries.map((entry) => entry.toJson()).toList());
|
||||
|
||||
factory TargetHistory.decode(String raw) {
|
||||
if (raw.trim().isEmpty) {
|
||||
|
||||
Reference in new issue
Block a user