app/lib/common/io/doh_client.dart

47 lines
1.5 KiB
Dart
Raw Normal View History

2024-11-03 16:42:39 +08:00
import 'dart:convert';
2024-12-20 19:17:30 +08:00
import 'package:starcitizen_doctor/common/conf/conf.dart';
2024-11-03 16:42:39 +08:00
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();
2024-12-14 13:48:24 +08:00
if (data.startsWith("\"")) {
2024-11-03 16:42:39 +08:00
data = data.substring(1);
}
2024-12-14 13:48:24 +08:00
if (data.endsWith("\"")) {
2024-11-03 16:42:39 +08:00
data = data.substring(0, data.length - 1);
}
return data;
}
}