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
@@ -7,9 +7,32 @@ import 'dart:io';
|
||||
import '../metrics/sample_book.dart';
|
||||
|
||||
const helperMissingNote =
|
||||
'本机 HTTP 探测使用和节点相同的 Go 引擎。请把 netquality-reflector 放在程序旁边,或设置环境变量 NQ_PROBE_HELPER。远程节点不受影响。';
|
||||
'本机 HTTP 探测需要 Go 程序 netquality-reflector,现在没有找到,所以探测没有开始。请把探测来源改成「远程节点」。桌面端也可以把 netquality-reflector 放在程序旁边,或设置环境变量 NQ_PROBE_HELPER。';
|
||||
|
||||
const helperStartFailedNote =
|
||||
'本机 HTTP 探测找到了 netquality-reflector,但没有启动成功,所以探测没有开始。请把探测来源改成「远程节点」。桌面端也可以检查这个程序能否运行,或改用环境变量 NQ_PROBE_HELPER。';
|
||||
|
||||
/// Thrown before any sample is recorded when the local HTTP engine cannot run.
|
||||
class LocalHttpHelperException implements Exception {
|
||||
const LocalHttpHelperException(this.message);
|
||||
|
||||
final String message;
|
||||
}
|
||||
|
||||
bool isLocalHttpHelperFailure(Outcome outcome) {
|
||||
return outcome.status == Outcome.error &&
|
||||
(outcome.note == helperMissingNote ||
|
||||
outcome.note == helperStartFailedNote);
|
||||
}
|
||||
|
||||
/// Tests set this to skip the search next to the executable.
|
||||
String? Function()? locateProbeHelperOverride;
|
||||
|
||||
String? locateProbeHelper() {
|
||||
final override = locateProbeHelperOverride;
|
||||
if (override != null) {
|
||||
return override();
|
||||
}
|
||||
final env = Platform.environment['NQ_PROBE_HELPER'];
|
||||
if (env != null && env.isNotEmpty && File(env).existsSync()) {
|
||||
return env;
|
||||
@@ -27,6 +50,23 @@ String? locateProbeHelper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Null when the local Go helper can be started. Otherwise a Chinese reason
|
||||
/// that already tells the user to switch the source to a remote node.
|
||||
Future<String?> localHttpBlockedReason({String? binary}) async {
|
||||
final path = binary ?? locateProbeHelper();
|
||||
if (path == null || path.isEmpty || !File(path).existsSync()) {
|
||||
return helperMissingNote;
|
||||
}
|
||||
try {
|
||||
final process = await Process.start(path, const ['-mode', 'helper']);
|
||||
process.kill();
|
||||
process.exitCode.ignore();
|
||||
return null;
|
||||
} catch (_) {
|
||||
return helperStartFailedNote;
|
||||
}
|
||||
}
|
||||
|
||||
Outcome outcomeFromHelper(String line) {
|
||||
try {
|
||||
final decoded = jsonDecode(line);
|
||||
@@ -107,14 +147,23 @@ class HttpHelperSession {
|
||||
return null;
|
||||
}
|
||||
final path = binary ?? locateProbeHelper();
|
||||
if (path == null) {
|
||||
if (path == null || path.isEmpty || !File(path).existsSync()) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: helperMissingNote,
|
||||
);
|
||||
}
|
||||
final process = await Process.start(path, const ['-mode', 'helper']);
|
||||
final Process process;
|
||||
try {
|
||||
process = await Process.start(path, const ['-mode', 'helper']);
|
||||
} catch (_) {
|
||||
return const Outcome(
|
||||
status: Outcome.error,
|
||||
kind: Outcome.query,
|
||||
note: helperStartFailedNote,
|
||||
);
|
||||
}
|
||||
_process = process;
|
||||
process.stdout
|
||||
.transform(utf8.decoder)
|
||||
|
||||
@@ -11,6 +11,7 @@ import '../models/probe_profile.dart';
|
||||
import '../models/session_config.dart';
|
||||
import '../ui/address_text.dart';
|
||||
import 'endpoint_picker.dart';
|
||||
import 'http_helper.dart';
|
||||
import 'probe_loop.dart';
|
||||
import 'remote_client.dart';
|
||||
|
||||
@@ -226,6 +227,13 @@ class ProbeController extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
if (!config.remote && config.profileId == 'http') {
|
||||
final helperProblem = await localHttpBlockedReason();
|
||||
if (helperProblem != null) {
|
||||
_rejectSetup(helperProblem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await stop();
|
||||
_nodeToken = nodeToken;
|
||||
_remoteMode = config.remote;
|
||||
@@ -237,6 +245,9 @@ class ProbeController extends ChangeNotifier {
|
||||
..start();
|
||||
final source = config.remote ? (config.node?.name ?? '远程节点') : '本机';
|
||||
final profile = ProbeProfile.byId(config.profileId);
|
||||
final httpTarget = config.httpTarget;
|
||||
final target = httpTarget?.url ?? config.host.trim();
|
||||
final port = httpTarget?.port ?? config.port;
|
||||
view = ProbeView(
|
||||
running: true,
|
||||
phase: '探测中',
|
||||
@@ -250,7 +261,7 @@ class ProbeController extends ChangeNotifier {
|
||||
sessionId: session,
|
||||
profileId: config.profileId,
|
||||
elapsed: Duration.zero,
|
||||
targetHost: '${config.host.trim()}:${config.port}',
|
||||
targetHost: httpTarget?.url ?? '$target:$port',
|
||||
profileLabel: profile.label,
|
||||
startedAt: _startedAt,
|
||||
);
|
||||
@@ -270,7 +281,9 @@ class ProbeController extends ChangeNotifier {
|
||||
baseUrl = link.url;
|
||||
token = nodeToken;
|
||||
}
|
||||
if (config.profileId == 'http' || !config.allowIPv4 || !config.allowIPv6) {
|
||||
if (config.profileId == 'http' ||
|
||||
!config.allowIPv4 ||
|
||||
!config.allowIPv6) {
|
||||
if (!selected.confidential) {
|
||||
final info = await fetchNodeInfo(baseUrl);
|
||||
final upgrade = remoteUpgradeMessage(
|
||||
@@ -290,8 +303,8 @@ class ProbeController extends ChangeNotifier {
|
||||
baseUrl: baseUrl,
|
||||
nodeToken: token,
|
||||
body: ProbeBody(
|
||||
target: config.host.trim(),
|
||||
port: config.port,
|
||||
target: target,
|
||||
port: port,
|
||||
profile: config.profileId,
|
||||
intervalMs: config.intervalMs,
|
||||
timeoutMs: config.timeoutMs,
|
||||
@@ -347,8 +360,8 @@ class ProbeController extends ChangeNotifier {
|
||||
_local = loop;
|
||||
await loop.run(
|
||||
request: ProbeRequest(
|
||||
host: config.host.trim(),
|
||||
port: config.port,
|
||||
host: target,
|
||||
port: port,
|
||||
profileId: config.profileId,
|
||||
interval: Duration(milliseconds: config.intervalMs),
|
||||
timeout: Duration(milliseconds: config.timeoutMs),
|
||||
@@ -373,6 +386,8 @@ class ProbeController extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
_finish(generation, '已停止');
|
||||
} on LocalHttpHelperException catch (error) {
|
||||
_rejectSetup(error.message, generation: generation);
|
||||
} on RemoteException catch (error) {
|
||||
_finish(generation, '出错', banner: error.message);
|
||||
} on Object {
|
||||
@@ -416,6 +431,27 @@ class ProbeController extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
void _rejectSetup(String message, {int? generation}) {
|
||||
if (generation != null && generation != _token) {
|
||||
return;
|
||||
}
|
||||
_clock.stop();
|
||||
_tick?.cancel();
|
||||
view = view.copyWith(
|
||||
running: false,
|
||||
phase: '无法开始',
|
||||
banner: message,
|
||||
note: '',
|
||||
detail: const {},
|
||||
summary: ProbeSummary.empty,
|
||||
samples: const [],
|
||||
elapsed: Duration.zero,
|
||||
clearReport: true,
|
||||
clearTrying: true,
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _finish(int generation, String phase, {String? banner}) {
|
||||
if (generation != _token || !view.running) {
|
||||
return;
|
||||
|
||||
@@ -119,6 +119,11 @@ class ProbeLoop {
|
||||
if (!countsAfterStop(outcome, cancelled: cancelled())) {
|
||||
break;
|
||||
}
|
||||
if (request.profileId == 'http' &&
|
||||
outcome != null &&
|
||||
isLocalHttpHelperFailure(outcome)) {
|
||||
throw LocalHttpHelperException(outcome.note);
|
||||
}
|
||||
var commit = <Outcome>[outcome!];
|
||||
if (_usesDatagram(request.profileId) && _picker != null) {
|
||||
_ledger ??= SelectionLedger(_picker!.candidates.length);
|
||||
|
||||
Reference in new issue
Block a user