feat: WASM web support

This commit is contained in:
xkeyC
2025-11-13 20:07:47 +08:00
parent 193d2c7496
commit 476c40f4cd
106 changed files with 2276 additions and 4299 deletions

View File

@@ -1,17 +1,27 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:starcitizen_doctor/common/conf/conf.dart';
import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
import 'package:starcitizen_doctor/common/rust/api/http_api.dart';
import 'package:starcitizen_doctor/common/rust/http_package.dart';
class RSHttp {
static late Dio _dio;
static Map<String, String> _defaultHeaders = {};
static Future<void> init() async {
await rust_http.setDefaultHeader(headers: {
_defaultHeaders = {
"User-Agent":
"SCToolBox/${ConstConf.appVersion} (${ConstConf.appVersionCode})${ConstConf.isMSE ? "" : " DEV"} RSHttp"
});
"SCToolBox/${ConstConf.appVersion} (${ConstConf.appVersionCode})${ConstConf.isMSE ? "" : " DEV"} RSHttp",
};
_dio = Dio(
BaseOptions(
headers: _defaultHeaders,
responseType: ResponseType.bytes,
validateStatus: (status) => true, // 接受所有状态码
),
);
}
static Future<RustHttpResponse> get(
@@ -20,14 +30,13 @@ class RSHttp {
String? withIpAddress,
bool? withCustomDns,
}) async {
final r = await rust_http.fetch(
method: MyMethod.gets,
return await _fetch(
method: 'GET',
url: url,
headers: headers,
withIpAddress: withIpAddress,
withCustomDns: withCustomDns,
);
return r;
}
static Future<String> getText(
@@ -36,10 +45,7 @@ class RSHttp {
String? withIpAddress,
bool? withCustomDns,
}) async {
final r = await get(url,
headers: headers,
withIpAddress: withIpAddress,
withCustomDns: withCustomDns);
final r = await get(url, headers: headers, withIpAddress: withIpAddress, withCustomDns: withCustomDns);
if (r.data == null) return "";
final str = utf8.decode(r.data!);
return str;
@@ -57,15 +63,14 @@ class RSHttp {
headers ??= {};
headers["Content-Type"] = contentType;
}
final r = await rust_http.fetch(
method: MyMethod.post,
return await _fetch(
method: 'POST',
url: url,
headers: headers,
inputData: data,
data: data,
withIpAddress: withIpAddress,
withCustomDns: withCustomDns,
);
return r;
}
static Future<RustHttpResponse> head(
@@ -74,21 +79,81 @@ class RSHttp {
String? withIpAddress,
bool? withCustomDns,
}) async {
final r = await rust_http.fetch(
method: MyMethod.head,
return await _fetch(
method: 'HEAD',
url: url,
headers: headers,
withIpAddress: withIpAddress,
withCustomDns: withCustomDns,
);
return r;
}
static Future<List<String>> dnsLookupTxt(String host) async {
return await rust_http.dnsLookupTxt(host: host);
// TXT 记录查询在 Web 平台上无法实现,返回空列表
return [];
}
static Future<List<String>> dnsLookupIps(String host) async {
return await rust_http.dnsLookupIps(host: host);
// Web 平台无法直接查询 DNS返回空列表
// 在 Web 平台上DNS 解析由浏览器自动处理
return [];
}
static Future<RustHttpResponse> _fetch({
required String method,
required String url,
Map<String, String>? headers,
Uint8List? data,
String? withIpAddress,
bool? withCustomDns,
}) async {
try {
final mergedHeaders = {..._defaultHeaders, ...?headers};
final response = await _dio.request(
url,
data: data,
options: Options(
method: method,
headers: mergedHeaders,
responseType: ResponseType.bytes,
validateStatus: (status) => true,
),
);
// 将 Dio Response 转换为 RustHttpResponse
return RustHttpResponse(
statusCode: response.statusCode ?? 0,
headers: _convertHeaders(response.headers.map),
url: response.realUri.toString(),
contentLength: response.headers.value('content-length') != null
? BigInt.from(int.parse(response.headers.value('content-length')!))
: null,
version: MyHttpVersion.httpUnknown,
remoteAddr: response.realUri.host,
data: response.data is Uint8List ? response.data : null,
);
} catch (e) {
// 发生错误时返回默认响应
return RustHttpResponse(
statusCode: 0,
headers: {},
url: url,
contentLength: null,
version: MyHttpVersion.httpUnknown,
remoteAddr: '',
data: null,
);
}
}
static Map<String, String> _convertHeaders(Map<String, List<String>> headers) {
final result = <String, String>{};
headers.forEach((key, value) {
if (value.isNotEmpty) {
result[key] = value.join(', ');
}
});
return result;
}
}