TF-2271 Add DNService for dns lookup to catch SRV Record to get jmap url

(cherry picked from commit 49110aa8db8b4e0707127ae39d5b2db2ecd6e339)
This commit is contained in:
dab246
2023-11-10 10:46:15 +07:00
committed by Dat Vu
parent 390d7cc0ed
commit b71328a0e4
4 changed files with 91 additions and 0 deletions
@@ -0,0 +1,65 @@
import 'package:core/utils/app_logger.dart';
import 'package:dns_client/dns_client.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.dart';
class DNSService {
static const String _jmapServiceName = '_jmap._tcp';
Future<String> _dnsLookupToUrlFromSRVType({required String hostName}) async {
try {
final url = await _dnsLookupToUrlByGoogle(hostName: hostName);
if (url.isEmpty) {
return await _dnsLookupToUrlByCloudflare(hostName: hostName);
}
return url;
} catch (e) {
return await _dnsLookupToUrlByCloudflare(hostName: hostName);
}
}
Future<String> _dnsLookupToUrlByGoogle({required String hostName}) async {
final dns = DnsOverHttps.google();
final listData = await dns.lookupDataByRRType(hostName, RRType.SRVType);
if (listData.isEmpty) {
throw NotFoundDataResourceRecordException();
}
return _parsingUrlFromDataResourceRecord(listData.first);
}
Future<String> _dnsLookupToUrlByCloudflare({required String hostName}) async {
final dns = DnsOverHttps.cloudflare();
final listData = await dns.lookupDataByRRType(hostName, RRType.SRVType);
if (listData.isEmpty) {
throw NotFoundDataResourceRecordException();
}
return _parsingUrlFromDataResourceRecord(listData.first);
}
String _parsingUrlFromDataResourceRecord(String data) {
if (data.isEmpty) {
throw NotFoundDataResourceRecordException();
}
final listFieldData = data.split(' ');
if (listFieldData.isEmpty) {
throw NotFoundUrlException();
}
final url = _removeDotAtEndOfString(listFieldData.last);
log('DNSService::_parsingUrlFromDataResourceRecord:url: $url');
return url;
}
Future<String> getJmapUrl(String emailAddress) async {
final domainName = emailAddress.split('@')[1];
final jmapHostName = '$_jmapServiceName.$domainName';
log('DNSHandler::getJmapUrl:jmapHostName: $jmapHostName');
return await _dnsLookupToUrlFromSRVType(hostName: jmapHostName);
}
String _removeDotAtEndOfString(String value) {
if (value.lastIndexOf('.') == value.length - 1) {
return value.substring(0, value.length - 1);
}
return value;
}
}
@@ -0,0 +1,4 @@
class NotFoundDataResourceRecordException implements Exception {}
class NotFoundUrlException implements Exception {}
+17
View File
@@ -360,6 +360,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.0.0"
dns_client:
dependency: "direct main"
description:
path: "."
ref: twake-supported
resolved-ref: "0ba4d48f287d408c70612dfc8bacc5ba11cd421b"
url: "https://github.com/dab246/dns_client.git"
source: git
version: "0.2.1"
dotted_border:
dependency: "direct main"
description:
@@ -861,6 +870,14 @@ packages:
relative: true
source: path
version: "1.0.0+1"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
sha256: "1be037f901137bf2b9a0c309e9bf2694d6ec77687645211218191ade4f41a4b8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
frontend_server_client:
dependency: transitive
description:
+5
View File
@@ -86,6 +86,11 @@ dependencies:
url: https://github.com/dab246/super_tag_editor.git
ref: master
dns_client:
git:
url: https://github.com/dab246/dns_client.git
ref: twake-supported
### Dependencies from pub.dev ###
cupertino_icons: 1.0.5