mirror of
https://github.com/immich-app/immich.git
synced 2025-02-03 01:22:44 +01:00
Merge d28aeee8e1
into c2ba1cc202
This commit is contained in:
commit
da1568f8e9
2 changed files with 63 additions and 2 deletions
mobile/lib/services
|
@ -10,9 +10,10 @@ import 'package:logging/logging.dart';
|
|||
import 'package:openapi/api.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
import 'package:dns_query_service.dart';
|
||||
|
||||
class ApiService implements Authentication {
|
||||
late ApiClient _apiClient;
|
||||
|
||||
late UsersApi usersApi;
|
||||
late AuthenticationApi authenticationApi;
|
||||
late OAuthApi oAuthApi;
|
||||
|
@ -31,7 +32,8 @@ class ApiService implements Authentication {
|
|||
late DownloadApi downloadApi;
|
||||
late TrashApi trashApi;
|
||||
late StacksApi stacksApi;
|
||||
|
||||
final DnsQueryService _dnsQueryService = DnsQueryService(); // Create DnsQueryService instance
|
||||
|
||||
ApiService() {
|
||||
final endpoint = Store.tryGet(StoreKey.serverEndpoint);
|
||||
if (endpoint != null && endpoint.isNotEmpty) {
|
||||
|
@ -82,6 +84,7 @@ class ApiService implements Authentication {
|
|||
/// host - required
|
||||
/// port - optional (default: based on schema)
|
||||
/// path - optional
|
||||
/// Resolve and set the API endpoint, with SRV record lookup based on domain
|
||||
Future<String> resolveEndpoint(String serverUrl) async {
|
||||
final url = sanitizeUrl(serverUrl);
|
||||
|
||||
|
@ -89,6 +92,17 @@ class ApiService implements Authentication {
|
|||
throw ApiException(503, "Server is not reachable");
|
||||
}
|
||||
|
||||
// Get domain name from serverUrl
|
||||
final domain = Uri.parse(serverUrl).host;
|
||||
|
||||
// Call DnsQueryService to fetch SRV records
|
||||
try {
|
||||
List<Map<String, dynamic>> srvRecords = await _dnsQueryService.fetchSrvRecords(domain);
|
||||
_log.info('Fetched SRV records for domain $domain: $srvRecords');
|
||||
} catch (e) {
|
||||
_log.severe('Failed to fetch SRV records for domain $domain: $e');
|
||||
}
|
||||
|
||||
// Check for /.well-known/immich
|
||||
final wellKnownEndpoint = await _getWellKnownEndpoint(url);
|
||||
if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
|
||||
|
|
47
mobile/lib/services/dns_query_service.dart
Normal file
47
mobile/lib/services/dns_query_service.dart
Normal file
|
@ -0,0 +1,47 @@
|
|||
import 'dart:async';
|
||||
import 'package:basic_utils/basic_utils.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class DnsQueryService {
|
||||
final Logger _log = Logger('DnsQueryService');
|
||||
|
||||
/// Validates the domain name format.
|
||||
bool _isValidDomain(String domain) {
|
||||
final regex = RegExp(r'^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$');
|
||||
return regex.hasMatch(domain);
|
||||
}
|
||||
|
||||
/// Fetch SRV records for a given domain.
|
||||
Future<List<Map<String, dynamic>>> fetchSrvRecords(String domain) async {
|
||||
if (!_isValidDomain(domain)) {
|
||||
_log.warning('Invalid domain name: $domain');
|
||||
throw Exception('Invalid domain name: $domain');
|
||||
}
|
||||
|
||||
try {
|
||||
_log.info('Attempting to fetch SRV records for domain: $domain');
|
||||
|
||||
// Perform the SRV record lookup
|
||||
final srvRecords = await DnsUtils.lookupRecord(domain, RRecordType.SRV);
|
||||
|
||||
_log.info('Successfully fetched SRV records for domain: $domain');
|
||||
|
||||
// Transform the result into a list of maps
|
||||
return srvRecords.map((record) {
|
||||
return {
|
||||
'priority': record.priority,
|
||||
'weight': record.weight,
|
||||
'port': record.port,
|
||||
'target': record.target,
|
||||
};
|
||||
}).toList();
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe(
|
||||
'Failed to fetch SRV records for domain: $domain',
|
||||
e,
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Failed to fetch SRV records: $e');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue