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 | struct DNSLookupService { | 65 | struct DNSLookupService { |
| 60 | private static let internetClass = 1 | 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 | private struct DNSLookupResponse { | 547 | private struct DNSLookupResponse { |
| 536 | let answers: [CloudflareDNSResponse.CloudflareDNSAnswer] | 548 | let answers: [CloudflareDNSResponse.CloudflareDNSAnswer] |
| 537 | let authenticatedData: Bool | 549 | let authenticatedData: Bool |
DomainDig/DomainAvailabilityService.swift +19 −10
| @@ -3,14 +3,18 @@ import Foundation | |||
| 3 | struct DomainAvailabilityService { | 3 | struct DomainAvailabilityService { |
| 4 | private static let suggestionTLDs = ["net", "io", "dev", "app", "co", "org"] | 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 | let normalizedDomain = normalize(domain) | 11 | let normalizedDomain = normalize(domain) |
| 8 | guard !normalizedDomain.isEmpty else { | 12 | guard !normalizedDomain.isEmpty else { |
| 9 | return DomainAvailabilityResult(domain: domain, status: .unknown) | 13 | return DomainAvailabilityResult(domain: domain, status: .unknown) |
| 10 | } | 14 | } |
| 11 | 15 | ||
| 12 | // RDAP is the registry's own record, so it settles both directions. | 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 | return DomainAvailabilityResult(domain: normalizedDomain, status: registryStatus) | 18 | return DomainAvailabilityResult(domain: normalizedDomain, status: registryStatus) |
| 15 | } | 19 | } |
| 16 | 20 | ||
| @@ -18,11 +22,16 @@ struct DomainAvailabilityService { | |||
| 18 | // resolves is registered. Absence of records is not evidence of | 22 | // resolves is registered. Absence of records is not evidence of |
| 19 | // availability, because the DoH layer drops the response code, so | 23 | // availability, because the DoH layer drops the response code, so |
| 20 | // NXDOMAIN and a resolver failure arrive here identically. | 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 | return DomainAvailabilityResult(domain: normalizedDomain, status: fallbackStatus) | 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 | let normalizedDomain = normalize(domain) | 35 | let normalizedDomain = normalize(domain) |
| 27 | let candidates = suggestionCandidates(for: normalizedDomain, limit: limit) | 36 | let candidates = suggestionCandidates(for: normalizedDomain, limit: limit) |
| 28 | guard !candidates.isEmpty else { return [] } | 37 | guard !candidates.isEmpty else { return [] } |
| @@ -30,20 +39,20 @@ struct DomainAvailabilityService { | |||
| 30 | var results: [DomainSuggestionResult] = [] | 39 | var results: [DomainSuggestionResult] = [] |
| 31 | for candidate in candidates { | 40 | for candidate in candidates { |
| 32 | if Task.isCancelled { break } | 41 | if Task.isCancelled { break } |
| 33 | let result = await check(domain: candidate) | 42 | let result = await check(domain: candidate, rdapTransport: rdapTransport, dns: dns) |
| 34 | results.append(DomainSuggestionResult(domain: result.domain, status: result.status)) | 43 | results.append(DomainSuggestionResult(domain: result.domain, status: result.status)) |
| 35 | } | 44 | } |
| 36 | return results | 45 | return results |
| 37 | } | 46 | } |
| 38 | 47 | ||
| 39 | private static func checkViaRDAP(domain: String) async -> DomainAvailabilityStatus? { | 48 | private static func checkViaRDAP(domain: String, transport: RDAPTransport) async -> DomainAvailabilityStatus? { |
| 40 | let status = await RDAPService.registrationStatus(for: domain) | 49 | let status = await RDAPService.registrationStatus(for: domain, transport: transport) |
| 41 | return status | 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 | do { | 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 | if !aRecords.isEmpty { | 56 | if !aRecords.isEmpty { |
| 48 | return .registered | 57 | return .registered |
| 49 | } | 58 | } |
| @@ -52,7 +61,7 @@ struct DomainAvailabilityService { | |||
| 52 | } | 61 | } |
| 53 | 62 | ||
| 54 | do { | 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 | if !nsRecords.isEmpty { | 65 | if !nsRecords.isEmpty { |
| 57 | return .registered | 66 | return .registered |
| 58 | } | 67 | } |
DomainDig/RDAPService.swift +24 −6
| @@ -1,13 +1,28 @@ | |||
| 1 | import Foundation | 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 | enum RDAPService { | 15 | enum RDAPService { |
| 4 | /// The registry's answer about `domain`, or nil when the registry did not | 16 | /// The registry's answer about `domain`, or nil when the registry did not |
| 5 | /// give one. | 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 | let normalizedDomain = normalize(domain) | 22 | let normalizedDomain = normalize(domain) |
| 8 | guard !normalizedDomain.isEmpty else { return nil } | 23 | guard !normalizedDomain.isEmpty else { return nil } |
| 9 | 24 | ||
| 10 | switch await fetchRDAPResponse(for: normalizedDomain) { | 25 | switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) { |
| 11 | case .found: | 26 | case .found: |
| 12 | return .registered | 27 | return .registered |
| 13 | case .notRegistered: | 28 | case .notRegistered: |
| @@ -29,13 +44,16 @@ enum RDAPService { | |||
| 29 | return host != bootstrapHost && !host.hasSuffix(".\(bootstrapHost)") | 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 | let normalizedDomain = normalize(domain) | 51 | let normalizedDomain = normalize(domain) |
| 34 | guard !normalizedDomain.isEmpty else { | 52 | guard !normalizedDomain.isEmpty else { |
| 35 | return .empty("Unavailable") | 53 | return .empty("Unavailable") |
| 36 | } | 54 | } |
| 37 | 55 | ||
| 38 | switch await fetchRDAPResponse(for: normalizedDomain) { | 56 | switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) { |
| 39 | case let .found(response): | 57 | case let .found(response): |
| 40 | let ownership = DomainOwnership( | 58 | let ownership = DomainOwnership( |
| 41 | registrar: response.registrarName, | 59 | registrar: response.registrarName, |
| @@ -77,7 +95,7 @@ private enum RDAPFetchOutcome { | |||
| 77 | case failed(String) | 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 | let startedAt = DomainDebugLog.signpostStart("RDAP.fetch", domain: domain) | 99 | let startedAt = DomainDebugLog.signpostStart("RDAP.fetch", domain: domain) |
| 82 | guard let url = URL(string: "https://\(bootstrapHost)/domain/\(domain)") else { | 100 | guard let url = URL(string: "https://\(bootstrapHost)/domain/\(domain)") else { |
| 83 | return .failed("Unavailable") | 101 | return .failed("Unavailable") |
| @@ -88,7 +106,7 @@ private func fetchRDAPResponse(for domain: String) async -> RDAPFetchOutcome { | |||
| 88 | request.setValue("application/rdap+json, application/json", forHTTPHeaderField: "Accept") | 106 | request.setValue("application/rdap+json, application/json", forHTTPHeaderField: "Accept") |
| 89 | DomainDebugLog.debug("RDAP.request url=\(url.absoluteString) timeout=8") | 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 | guard let httpResponse = response as? HTTPURLResponse else { | 110 | guard let httpResponse = response as? HTTPURLResponse else { |
| 93 | DomainDebugLog.error("RDAP.badResponse domain=\(domain) response=nil") | 111 | DomainDebugLog.error("RDAP.badResponse domain=\(domain) response=nil") |
| 94 | return .failed(URLError(.badServerResponse).localizedDescription) | 112 | return .failed(URLError(.badServerResponse).localizedDescription) |