-
Notifications
You must be signed in to change notification settings - Fork 104
TF-3976 Support DNS SRV resolvers without Cloudflare/Google dependency
#4129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dab246
wants to merge
3
commits into
master
Choose a base branch
from
enhancement/tf-3976-dns-srv-resolvers-without-cloudfare-google
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
lib/features/login/data/datasource_impl/login_datasource_impl.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
lib/features/login/data/network/dns_lookup/dns_lookup_manager.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:core/utils/app_logger.dart'; | ||
| import 'package:core/utils/build_utils.dart'; | ||
| import 'package:super_dns_client/super_dns_client.dart'; | ||
| import 'package:tmail_ui_user/features/login/data/network/dns_lookup/dns_lookup_priority.dart'; | ||
|
|
||
| /// Handles DNS SRV lookups for JMAP service discovery. | ||
| /// | ||
| /// The manager attempts lookups in order of priority: | ||
| /// **System → Public UDP → Public DoH → Cloud (Google/Cloudflare)**. | ||
| class DnsLookupManager { | ||
| static const String _jmapServicePrefix = '_jmap._tcp'; | ||
| static const Duration _defaultTimeout = Duration(seconds: 3); | ||
|
|
||
| /// Builds the JMAP SRV hostname from [emailAddress]. | ||
| /// | ||
| /// Example: | ||
| /// ``` | ||
| /// input : user@example.com | ||
| /// output: _jmap._tcp.example.com | ||
| /// ``` | ||
| String buildJmapHostName(String emailAddress) { | ||
| final parts = emailAddress.split('@'); | ||
| if (parts.length != 2 || parts[1].isEmpty) { | ||
| throw ArgumentError('Invalid email address: $emailAddress'); | ||
| } | ||
| return '$_jmapServicePrefix.${parts[1]}'; | ||
| } | ||
|
|
||
| /// Creates the appropriate [DnsClient] for the given [priority]. | ||
| DnsClient createClient(DnsLookupPriority priority) { | ||
| const debug = BuildUtils.isDebugMode; | ||
| switch (priority) { | ||
| case DnsLookupPriority.system: | ||
| return SystemUdpSrvClient(debugMode: debug, timeout: _defaultTimeout); | ||
| case DnsLookupPriority.publicUdp: | ||
| return PublicUdpSrvClient(debugMode: debug, timeout: _defaultTimeout); | ||
| case DnsLookupPriority.publicDoh: | ||
| return DnsOverHttpsBinaryClient(debugMode: debug, timeout: _defaultTimeout); | ||
| case DnsLookupPriority.cloud: | ||
| return DnsOverHttps.empty(debugMode: debug, timeout: _defaultTimeout); | ||
| } | ||
| } | ||
|
|
||
| /// Attempts SRV resolution for the JMAP hostname derived from [emailAddress]. | ||
| /// | ||
| /// Each lookup attempt will timeout after [_defaultTimeout] seconds. | ||
| /// Returns the first successfully resolved hostname, or an empty string if all fail. | ||
| Future<String> lookupJmapUrl(String emailAddress) async { | ||
| final jmapHostName = buildJmapHostName(emailAddress); | ||
| log('$runtimeType::lookupJmapUrl → Resolving SRV for: $jmapHostName'); | ||
|
|
||
| final priorities = List.of(DnsLookupPriority.values) | ||
| ..sort((a, b) => a.priority.compareTo(b.priority)); | ||
|
|
||
| for (final priority in priorities) { | ||
| final client = createClient(priority); | ||
| log('$runtimeType::lookupJmapUrl → 🔍 Trying ${priority.label} (timeout: ${client.timeout.inSeconds}s)...'); | ||
|
|
||
| try { | ||
| final records = client is DnsOverHttps | ||
| ? await client.lookupSrvMulti(jmapHostName) | ||
| : await client.lookupSrv(jmapHostName); | ||
|
|
||
| final target = records.firstOrNull?.target ?? ''; | ||
| if (target.isNotEmpty) { | ||
| log('$runtimeType::lookupJmapUrl → ✅ Success via ${priority.label}: $target'); | ||
| return target; | ||
| } | ||
| log('$runtimeType::lookupJmapUrl → ⚠️ No records via ${priority.label}, continuing...'); | ||
| } on TimeoutException catch (_) { | ||
| logError( | ||
| '$runtimeType::lookupJmapUrl → ⏱️ ${priority.label} lookup timed out'); | ||
| } catch (error, stack) { | ||
| logError( | ||
| '$runtimeType::lookupJmapUrl → ❌ ${priority.label} lookup failed: $error, $stack'); | ||
| } | ||
| } | ||
|
|
||
| log('$runtimeType::lookupJmapUrl → 🚨 All DNS lookups failed for $jmapHostName'); | ||
| throw Exception('DNS lookup failed for $jmapHostName'); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
lib/features/login/data/network/dns_lookup/dns_lookup_priority.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /// Represents the priority order and description of different DNS lookup modes. | ||
| /// | ||
| /// Priority level increases with fallback order: | ||
| /// 1 → System default | ||
| /// 2 → Public resolvers (UDP/TCP or DOH) | ||
| /// 3 → Cloud resolvers (Google/Cloudflare) | ||
| enum DnsLookupPriority { | ||
| /// Uses the device's system-configured DNS (e.g., from ISP or OS settings). | ||
| system(1, 'System Default'), | ||
|
|
||
| /// Uses open DNS resolvers accessible via UDP/TCP (e.g., Quad9, OpenDNS). | ||
| publicUdp(2, 'Public DNS (UDP/TCP)'), | ||
|
|
||
| /// Uses DNS-over-HTTPS (DoH) resolvers for secure name resolution. | ||
| publicDoh(2, 'Public DNS (DoH)'), | ||
hoangdat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Uses Google or Cloudflare DNS resolvers. | ||
| cloud(3, 'Cloud DNS (Google/Cloudflare)'); | ||
|
|
||
| /// The lookup priority (lower means higher priority). | ||
| final int priority; | ||
|
|
||
| /// A human-readable description for UI or logging. | ||
| final String label; | ||
|
|
||
| const DnsLookupPriority(this.priority, this.label); | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are sorting priority, but there are 2
DnsLookupPrioritywith the samepriority. Should we update them to be different?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are the same type so the priority can be the same.