Commit 6c3d3ae095
Verified · cmc
Layout: unified · split
DomainDig/DNSLookupService.swift +12
| @@ -56,6 +56,12 @@ enum DNSResolverOption: String, CaseIterable, Identifiable { | ||
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | /// A single record lookup, injectable so callers that branch on its result can | |
| 60 | /// be exercised without network. | |
| 61 | protocol DNSLookup { | |
| 62 | func lookup(domain: String, recordType: DNSRecordType) async throws -> [DNSRecord] | |
| 63 | } | |
| 64 | ||
| 59 | 65 | struct DNSLookupService { |
| 60 | 66 | private static let internetClass = 1 |
| 61 | 67 | |
| @@ -532,6 +538,12 @@ struct DNSLookupService { | ||
| 532 | 538 | } |
| 533 | 539 | } |
| 534 | 540 | |
| 541 | extension DNSLookupService: DNSLookup { | |
| 542 | func lookup(domain: String, recordType: DNSRecordType) async throws -> [DNSRecord] { | |
| 543 | try await Self.lookup(domain: domain, recordType: recordType) | |
| 544 | } | |
| 545 | } | |
| 546 | ||
| 535 | 547 | private struct DNSLookupResponse { |
| 536 | 548 | let answers: [CloudflareDNSResponse.CloudflareDNSAnswer] |
| 537 | 549 | let authenticatedData: Bool |
DomainDig/DomainAvailabilityService.swift +19 −10
| @@ -3,14 +3,18 @@ import Foundation | ||
| 3 | 3 | struct DomainAvailabilityService { |
| 4 | 4 | private static let suggestionTLDs = ["net", "io", "dev", "app", "co", "org"] |
| 5 | 5 | |
| 6 | static func check(domain: String) async -> DomainAvailabilityResult { | |
| 6 | static func check( | |
| 7 | domain: String, | |
| 8 | rdapTransport: RDAPTransport = URLSessionRDAPTransport(), | |
| 9 | dns: DNSLookup = DNSLookupService() | |
| 10 | ) async -> DomainAvailabilityResult { | |
| 7 | 11 | let normalizedDomain = normalize(domain) |
| 8 | 12 | guard !normalizedDomain.isEmpty else { |
| 9 | 13 | return DomainAvailabilityResult(domain: domain, status: .unknown) |
| 10 | 14 | } |
| 11 | 15 | |
| 12 | 16 | // RDAP is the registry's own record, so it settles both directions. |
| 13 | if let registryStatus = await checkViaRDAP(domain: normalizedDomain) { | |
| 17 | if let registryStatus = await checkViaRDAP(domain: normalizedDomain, transport: rdapTransport) { | |
| 14 | 18 | return DomainAvailabilityResult(domain: normalizedDomain, status: registryStatus) |
| 15 | 19 | } |
| 16 | 20 | |
| @@ -18,11 +22,16 @@ struct DomainAvailabilityService { | ||
| 18 | 22 | // resolves is registered. Absence of records is not evidence of |
| 19 | 23 | // availability, because the DoH layer drops the response code, so |
| 20 | 24 | // NXDOMAIN and a resolver failure arrive here identically. |
| 21 | let fallbackStatus = await checkViaDNSFallback(domain: normalizedDomain) | |
| 25 | let fallbackStatus = await checkViaDNSFallback(domain: normalizedDomain, dns: dns) | |
| 22 | 26 | return DomainAvailabilityResult(domain: normalizedDomain, status: fallbackStatus) |
| 23 | 27 | } |
| 24 | 28 | |
| 25 | static func suggestions(for domain: String, limit: Int = 6) async -> [DomainSuggestionResult] { | |
| 29 | static func suggestions( | |
| 30 | for domain: String, | |
| 31 | limit: Int = 6, | |
| 32 | rdapTransport: RDAPTransport = URLSessionRDAPTransport(), | |
| 33 | dns: DNSLookup = DNSLookupService() | |
| 34 | ) async -> [DomainSuggestionResult] { | |
| 26 | 35 | let normalizedDomain = normalize(domain) |
| 27 | 36 | let candidates = suggestionCandidates(for: normalizedDomain, limit: limit) |
| 28 | 37 | guard !candidates.isEmpty else { return [] } |
| @@ -30,20 +39,20 @@ struct DomainAvailabilityService { | ||
| 30 | 39 | var results: [DomainSuggestionResult] = [] |
| 31 | 40 | for candidate in candidates { |
| 32 | 41 | if Task.isCancelled { break } |
| 33 | let result = await check(domain: candidate) | |
| 42 | let result = await check(domain: candidate, rdapTransport: rdapTransport, dns: dns) | |
| 34 | 43 | results.append(DomainSuggestionResult(domain: result.domain, status: result.status)) |
| 35 | 44 | } |
| 36 | 45 | return results |
| 37 | 46 | } |
| 38 | 47 | |
| 39 | private static func checkViaRDAP(domain: String) async -> DomainAvailabilityStatus? { | |
| 40 | let status = await RDAPService.registrationStatus(for: domain) | |
| 48 | private static func checkViaRDAP(domain: String, transport: RDAPTransport) async -> DomainAvailabilityStatus? { | |
| 49 | let status = await RDAPService.registrationStatus(for: domain, transport: transport) | |
| 41 | 50 | return status |
| 42 | 51 | } |
| 43 | 52 | |
| 44 | private static func checkViaDNSFallback(domain: String) async -> DomainAvailabilityStatus { | |
| 53 | private static func checkViaDNSFallback(domain: String, dns: DNSLookup) async -> DomainAvailabilityStatus { | |
| 45 | 54 | do { |
| 46 | let aRecords = try await DNSLookupService.lookup(domain: domain, recordType: .A) | |
| 55 | let aRecords = try await dns.lookup(domain: domain, recordType: .A) | |
| 47 | 56 | if !aRecords.isEmpty { |
| 48 | 57 | return .registered |
| 49 | 58 | } |
| @@ -52,7 +61,7 @@ struct DomainAvailabilityService { | ||
| 52 | 61 | } |
| 53 | 62 | |
| 54 | 63 | do { |
| 55 | let nsRecords = try await DNSLookupService.lookup(domain: domain, recordType: .NS) | |
| 64 | let nsRecords = try await dns.lookup(domain: domain, recordType: .NS) | |
| 56 | 65 | if !nsRecords.isEmpty { |
| 57 | 66 | return .registered |
| 58 | 67 | } |
DomainDig/RDAPService.swift +24 −6
| @@ -1,13 +1,28 @@ | ||
| 1 | 1 | import Foundation |
| 2 | 2 | |
| 3 | /// The HTTP fetch behind an RDAP query, injectable so the status mapping can be | |
| 4 | /// exercised without network. | |
| 5 | protocol RDAPTransport { | |
| 6 | func fetch(_ request: URLRequest) async throws -> (Data, URLResponse) | |
| 7 | } | |
| 8 | ||
| 9 | struct URLSessionRDAPTransport: RDAPTransport { | |
| 10 | func fetch(_ request: URLRequest) async throws -> (Data, URLResponse) { | |
| 11 | try await URLSession.shared.data(for: request) | |
| 12 | } | |
| 13 | } | |
| 14 | ||
| 3 | 15 | enum RDAPService { |
| 4 | 16 | /// The registry's answer about `domain`, or nil when the registry did not |
| 5 | 17 | /// give one. |
| 6 | static func registrationStatus(for domain: String) async -> DomainAvailabilityStatus? { | |
| 18 | static func registrationStatus( | |
| 19 | for domain: String, | |
| 20 | transport: RDAPTransport = URLSessionRDAPTransport() | |
| 21 | ) async -> DomainAvailabilityStatus? { | |
| 7 | 22 | let normalizedDomain = normalize(domain) |
| 8 | 23 | guard !normalizedDomain.isEmpty else { return nil } |
| 9 | 24 | |
| 10 | switch await fetchRDAPResponse(for: normalizedDomain) { | |
| 25 | switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) { | |
| 11 | 26 | case .found: |
| 12 | 27 | return .registered |
| 13 | 28 | case .notRegistered: |
| @@ -29,13 +44,16 @@ enum RDAPService { | ||
| 29 | 44 | return host != bootstrapHost && !host.hasSuffix(".\(bootstrapHost)") |
| 30 | 45 | } |
| 31 | 46 | |
| 32 | static func ownership(for domain: String) async -> ServiceResult<DomainOwnership> { | |
| 47 | static func ownership( | |
| 48 | for domain: String, | |
| 49 | transport: RDAPTransport = URLSessionRDAPTransport() | |
| 50 | ) async -> ServiceResult<DomainOwnership> { | |
| 33 | 51 | let normalizedDomain = normalize(domain) |
| 34 | 52 | guard !normalizedDomain.isEmpty else { |
| 35 | 53 | return .empty("Unavailable") |
| 36 | 54 | } |
| 37 | 55 | |
| 38 | switch await fetchRDAPResponse(for: normalizedDomain) { | |
| 56 | switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) { | |
| 39 | 57 | case let .found(response): |
| 40 | 58 | let ownership = DomainOwnership( |
| 41 | 59 | registrar: response.registrarName, |
| @@ -77,7 +95,7 @@ private enum RDAPFetchOutcome { | ||
| 77 | 95 | case failed(String) |
| 78 | 96 | } |
| 79 | 97 | |
| 80 | private func fetchRDAPResponse(for domain: String) async -> RDAPFetchOutcome { | |
| 98 | private func fetchRDAPResponse(for domain: String, transport: RDAPTransport) async -> RDAPFetchOutcome { | |
| 81 | 99 | let startedAt = DomainDebugLog.signpostStart("RDAP.fetch", domain: domain) |
| 82 | 100 | guard let url = URL(string: "https://\(bootstrapHost)/domain/\(domain)") else { |
| 83 | 101 | return .failed("Unavailable") |
| @@ -88,7 +106,7 @@ private func fetchRDAPResponse(for domain: String) async -> RDAPFetchOutcome { | ||
| 88 | 106 | request.setValue("application/rdap+json, application/json", forHTTPHeaderField: "Accept") |
| 89 | 107 | DomainDebugLog.debug("RDAP.request url=\(url.absoluteString) timeout=8") |
| 90 | 108 | |
| 91 | let (data, response) = try await URLSession.shared.data(for: request) | |
| 109 | let (data, response) = try await transport.fetch(request) | |
| 92 | 110 | guard let httpResponse = response as? HTTPURLResponse else { |
| 93 | 111 | DomainDebugLog.error("RDAP.badResponse domain=\(domain) response=nil") |
| 94 | 112 | return .failed(URLError(.badServerResponse).localizedDescription) |