mirror of
https://github.com/StarCitizenToolBox/app.git
synced 2026-02-06 15:10:20 +00:00
feat: use rust impl checkHost
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'package:starcitizen_doctor/api/api.dart';
|
||||
import 'package:starcitizen_doctor/common/io/doh_client.dart';
|
||||
import 'package:starcitizen_doctor/common/io/rs_http.dart';
|
||||
import 'package:starcitizen_doctor/common/rust/http_package.dart';
|
||||
import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
|
||||
class URLConf {
|
||||
@@ -43,13 +42,19 @@ class URLConf {
|
||||
// 使用 DNS 获取可用列表
|
||||
final gitApiList = _genFinalList(await dnsLookupTxt("git.dns.scbox.org"));
|
||||
dPrint("DNS gitApiList ==== $gitApiList");
|
||||
final fasterGit = await getFasterUrl(gitApiList, "git");
|
||||
final fasterGit = await rust_http.getFasterUrl(
|
||||
urls: gitApiList,
|
||||
pathSuffix: "/SCToolBox/Api/raw/branch/main/sc_doctor/version.json",
|
||||
);
|
||||
dPrint("gitApiList.Faster ==== $fasterGit");
|
||||
if (fasterGit != null) {
|
||||
gitApiHome = fasterGit;
|
||||
}
|
||||
final newsApiList = _genFinalList(await dnsLookupTxt("news.dns.scbox.org"));
|
||||
final fasterNews = await getFasterUrl(newsApiList, "news");
|
||||
final fasterNews = await rust_http.getFasterUrl(
|
||||
urls: newsApiList,
|
||||
pathSuffix: "/api/latest",
|
||||
);
|
||||
dPrint("DNS newsApiList ==== $newsApiList");
|
||||
dPrint("newsApiList.Faster ==== $fasterNews");
|
||||
if (fasterNews != null) {
|
||||
@@ -62,53 +67,12 @@ class URLConf {
|
||||
static Future<List<String>> dnsLookupTxt(String host) async {
|
||||
if (await Api.isUseInternalDNS()) {
|
||||
dPrint("[URLConf] use internal DNS LookupTxt $host");
|
||||
return RSHttp.dnsLookupTxt(host);
|
||||
return rust_http.dnsLookupTxt(host: host);
|
||||
}
|
||||
dPrint("[URLConf] use DOH LookupTxt $host");
|
||||
return (await DohClient.resolveTXT(host)) ?? [];
|
||||
}
|
||||
|
||||
static Future<String?> getFasterUrl(List<String> urls, String mode) async {
|
||||
String firstUrl = "";
|
||||
int callLen = 0;
|
||||
|
||||
void onCall(RustHttpResponse? response, String url) {
|
||||
callLen++;
|
||||
if (response != null && response.statusCode == 200 && firstUrl.isEmpty) {
|
||||
firstUrl = url;
|
||||
}
|
||||
}
|
||||
|
||||
for (var url in urls) {
|
||||
var reqUrl = url;
|
||||
switch (mode) {
|
||||
case "git":
|
||||
reqUrl = "$url/SCToolBox/Api/raw/branch/main/sc_doctor/version.json";
|
||||
break;
|
||||
case "news":
|
||||
reqUrl = "$url/api/latest";
|
||||
break;
|
||||
}
|
||||
RSHttp.head(reqUrl).then(
|
||||
(resp) => onCall(resp, url),
|
||||
onError: (err) {
|
||||
callLen++;
|
||||
dPrint("RSHttp.head error $err");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
await Future.delayed(const Duration(milliseconds: 16));
|
||||
if (firstUrl.isNotEmpty) {
|
||||
return firstUrl;
|
||||
}
|
||||
if (callLen == urls.length && firstUrl.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<String> _genFinalList(List<String> sList) {
|
||||
List<String> list = [];
|
||||
for (var ll in sList) {
|
||||
|
||||
@@ -34,4 +34,19 @@ Future<List<String>> dnsLookupTxt({required String host}) =>
|
||||
Future<List<String>> dnsLookupIps({required String host}) =>
|
||||
RustLib.instance.api.crateApiHttpApiDnsLookupIps(host: host);
|
||||
|
||||
/// Get the fastest URL from a list of URLs by testing them concurrently.
|
||||
/// Returns the first URL that responds successfully, canceling other requests.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `urls` - List of base URLs to test
|
||||
/// * `path_suffix` - Optional path suffix to append to each URL (e.g., "/api/version")
|
||||
/// If None, tests the base URL directly
|
||||
Future<String?> getFasterUrl({
|
||||
required List<String> urls,
|
||||
String? pathSuffix,
|
||||
}) => RustLib.instance.api.crateApiHttpApiGetFasterUrl(
|
||||
urls: urls,
|
||||
pathSuffix: pathSuffix,
|
||||
);
|
||||
|
||||
enum MyMethod { options, gets, post, put, delete, head, trace, connect, patch }
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
import '../frb_generated.dart';
|
||||
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
|
||||
|
||||
// These functions are ignored because they are not marked as `pub`: `get_process_path`
|
||||
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `clone`, `fmt`
|
||||
|
||||
Future<void> sendNotify({
|
||||
|
||||
@@ -69,7 +69,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
|
||||
String get codegenVersion => '2.11.1';
|
||||
|
||||
@override
|
||||
int get rustContentHash => 1227557070;
|
||||
int get rustContentHash => -518970253;
|
||||
|
||||
static const kDefaultExternalLibraryLoaderConfig =
|
||||
ExternalLibraryLoaderConfig(
|
||||
@@ -95,6 +95,11 @@ abstract class RustLibApi extends BaseApi {
|
||||
bool? withCustomDns,
|
||||
});
|
||||
|
||||
Future<String?> crateApiHttpApiGetFasterUrl({
|
||||
required List<String> urls,
|
||||
String? pathSuffix,
|
||||
});
|
||||
|
||||
Future<List<ProcessInfo>> crateApiWin32ApiGetProcessListByName({
|
||||
required String processName,
|
||||
});
|
||||
@@ -288,6 +293,39 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
|
||||
],
|
||||
);
|
||||
|
||||
@override
|
||||
Future<String?> crateApiHttpApiGetFasterUrl({
|
||||
required List<String> urls,
|
||||
String? pathSuffix,
|
||||
}) {
|
||||
return handler.executeNormal(
|
||||
NormalTask(
|
||||
callFfi: (port_) {
|
||||
var arg0 = cst_encode_list_String(urls);
|
||||
var arg1 = cst_encode_opt_String(pathSuffix);
|
||||
return wire.wire__crate__api__http_api__get_faster_url(
|
||||
port_,
|
||||
arg0,
|
||||
arg1,
|
||||
);
|
||||
},
|
||||
codec: DcoCodec(
|
||||
decodeSuccessData: dco_decode_opt_String,
|
||||
decodeErrorData: dco_decode_AnyhowException,
|
||||
),
|
||||
constMeta: kCrateApiHttpApiGetFasterUrlConstMeta,
|
||||
argValues: [urls, pathSuffix],
|
||||
apiImpl: this,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
TaskConstMeta get kCrateApiHttpApiGetFasterUrlConstMeta =>
|
||||
const TaskConstMeta(
|
||||
debugName: "get_faster_url",
|
||||
argNames: ["urls", "pathSuffix"],
|
||||
);
|
||||
|
||||
@override
|
||||
Future<List<ProcessInfo>> crateApiWin32ApiGetProcessListByName({
|
||||
required String processName,
|
||||
|
||||
@@ -762,6 +762,38 @@ class RustLibWire implements BaseWire {
|
||||
)
|
||||
>();
|
||||
|
||||
void wire__crate__api__http_api__get_faster_url(
|
||||
int port_,
|
||||
ffi.Pointer<wire_cst_list_String> urls,
|
||||
ffi.Pointer<wire_cst_list_prim_u_8_strict> path_suffix,
|
||||
) {
|
||||
return _wire__crate__api__http_api__get_faster_url(
|
||||
port_,
|
||||
urls,
|
||||
path_suffix,
|
||||
);
|
||||
}
|
||||
|
||||
late final _wire__crate__api__http_api__get_faster_urlPtr =
|
||||
_lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Void Function(
|
||||
ffi.Int64,
|
||||
ffi.Pointer<wire_cst_list_String>,
|
||||
ffi.Pointer<wire_cst_list_prim_u_8_strict>,
|
||||
)
|
||||
>
|
||||
>('frbgen_starcitizen_doctor_wire__crate__api__http_api__get_faster_url');
|
||||
late final _wire__crate__api__http_api__get_faster_url =
|
||||
_wire__crate__api__http_api__get_faster_urlPtr
|
||||
.asFunction<
|
||||
void Function(
|
||||
int,
|
||||
ffi.Pointer<wire_cst_list_String>,
|
||||
ffi.Pointer<wire_cst_list_prim_u_8_strict>,
|
||||
)
|
||||
>();
|
||||
|
||||
void wire__crate__api__win32_api__get_process_list_by_name(
|
||||
int port_,
|
||||
ffi.Pointer<wire_cst_list_prim_u_8_strict> process_name,
|
||||
@@ -1312,6 +1344,13 @@ final class wire_cst_list_record_string_string extends ffi.Struct {
|
||||
external int len;
|
||||
}
|
||||
|
||||
final class wire_cst_list_String extends ffi.Struct {
|
||||
external ffi.Pointer<ffi.Pointer<wire_cst_list_prim_u_8_strict>> ptr;
|
||||
|
||||
@ffi.Int32()
|
||||
external int len;
|
||||
}
|
||||
|
||||
final class wire_cst_rsi_launcher_asar_data extends ffi.Struct {
|
||||
external ffi.Pointer<wire_cst_list_prim_u_8_strict> asar_path;
|
||||
|
||||
@@ -1327,13 +1366,6 @@ final class wire_cst_list_prim_u_8_loose extends ffi.Struct {
|
||||
external int len;
|
||||
}
|
||||
|
||||
final class wire_cst_list_String extends ffi.Struct {
|
||||
external ffi.Pointer<ffi.Pointer<wire_cst_list_prim_u_8_strict>> ptr;
|
||||
|
||||
@ffi.Int32()
|
||||
external int len;
|
||||
}
|
||||
|
||||
final class wire_cst_process_info extends ffi.Struct {
|
||||
@ffi.Uint32()
|
||||
external int pid;
|
||||
|
||||
Reference in New Issue
Block a user