mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-06-28 07:54:44 +08:00
feat: InternalDNS switch
This commit is contained in:
@ -5,4 +5,5 @@ class ConstConf {
|
||||
static const gameChannels = ["LIVE", "PTU", "EPTU", "TECH-PREVIEW", "HOTFIX"];
|
||||
static const isMSE =
|
||||
String.fromEnvironment("MSE", defaultValue: "false") == "true";
|
||||
static const dohAddress = "https://223.6.6.6/resolve";
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
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/utils/log.dart';
|
||||
@ -38,16 +40,14 @@ class URLConf {
|
||||
|
||||
static Future<bool> checkHost() async {
|
||||
// 使用 DNS 获取可用列表
|
||||
final gitApiList =
|
||||
_genFinalList(await RSHttp.dnsLookupTxt("git.dns.scbox.org"));
|
||||
final gitApiList = _genFinalList(await dnsLookupTxt("git.dns.scbox.org"));
|
||||
dPrint("DNS gitApiList ==== $gitApiList");
|
||||
final fasterGit = await getFasterUrl(gitApiList);
|
||||
dPrint("gitApiList.Faster ==== $fasterGit");
|
||||
if (fasterGit != null) {
|
||||
gitApiHome = fasterGit;
|
||||
}
|
||||
final rssApiList =
|
||||
_genFinalList(await RSHttp.dnsLookupTxt("rss.dns.scbox.org"));
|
||||
final rssApiList = _genFinalList(await dnsLookupTxt("rss.dns.scbox.org"));
|
||||
final fasterRss = await getFasterUrl(rssApiList);
|
||||
dPrint("DNS rssApiList ==== $rssApiList");
|
||||
dPrint("rssApiList.Faster ==== $fasterRss");
|
||||
@ -58,6 +58,15 @@ class URLConf {
|
||||
return isUrlCheckPass;
|
||||
}
|
||||
|
||||
static Future<List<String>> dnsLookupTxt(String host) async {
|
||||
if (await Api.isUseInternalDNS()) {
|
||||
dPrint("[URLConf] use internal DNS LookupTxt $host");
|
||||
return RSHttp.dnsLookupTxt(host);
|
||||
}
|
||||
dPrint("[URLConf] use DOH LookupTxt $host");
|
||||
return (await DohClient.resolveTXT(host)) ?? [];
|
||||
}
|
||||
|
||||
static Future<String?> getFasterUrl(List<String> urls) async {
|
||||
String firstUrl = "";
|
||||
int callLen = 0;
|
||||
|
46
lib/common/io/doh_client.dart
Normal file
46
lib/common/io/doh_client.dart
Normal file
@ -0,0 +1,46 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:starcitizen_doctor/common/conf/const_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/io/rs_http.dart';
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
import 'package:starcitizen_doctor/data/doh_client_response_data.dart';
|
||||
|
||||
class DohClient {
|
||||
static Future<DohClientResponseData?> resolve(
|
||||
String domain, String type) async {
|
||||
try {
|
||||
final r = await RSHttp.getText(
|
||||
"${ConstConf.dohAddress}?name=$domain&type=$type");
|
||||
final data = DohClientResponseData.fromJson(json.decode(r));
|
||||
return data;
|
||||
} catch (e) {
|
||||
dPrint("DohClient.resolve error: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<String>?> resolveIP(String domain, String type) async {
|
||||
final data = await resolve(domain, type);
|
||||
if (data == null) return [];
|
||||
return data.answer?.map((e) => _removeDataPadding(e.data)).toList();
|
||||
}
|
||||
|
||||
static Future<List<String>?> resolveTXT(String domain) async {
|
||||
final data = await resolve(domain, "TXT");
|
||||
if (data == null) return [];
|
||||
return data.answer?.map((e) => _removeDataPadding(e.data)).toList();
|
||||
}
|
||||
|
||||
static String _removeDataPadding(String? data) {
|
||||
// data demo: {"data":"\"https://git.scbox.xkeyc.cn,https://gitapi.scbox.org\""}
|
||||
if (data == null) return "";
|
||||
data = data.trim();
|
||||
if (data.startsWith("\"")){
|
||||
data = data.substring(1);
|
||||
}
|
||||
if (data.endsWith("\"")){
|
||||
data = data.substring(0, data.length - 1);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user