DomainDig/InspectionScope.swift

131 lines · 5281 bytes

5 symbols in this file
  1import Foundation
  2
  3/// How much of an inspection to run.
  4///
  5/// Opted out of the project's MainActor default isolation, along with
  6/// `InspectionScope` below: monitoring and the batch pipeline build a scope
  7/// from a background context.
  8nonisolated enum InspectionProfile: String, Codable, CaseIterable, Sendable, Identifiable {
  9    /// Registration, ownership, TLS, HSTS, HTTP, redirects and PTR: the
 10    /// sections that answer "what is this domain" without probing ports or
 11    /// enumerating hosts.
 12    case quick
 13    /// Every section.
 14    case full
 15
 16    var id: String { rawValue }
 17
 18    var title: String {
 19        switch self {
 20        case .quick:
 21            return "Quick"
 22        case .full:
 23            return "Full"
 24        }
 25    }
 26
 27    var detail: String {
 28        switch self {
 29        case .quick:
 30            return "DNS, registration, ownership, TLS and HTTP. Skips port scanning, reachability, subdomain discovery, IP geolocation and name suggestions."
 31        case .full:
 32            return "Every section."
 33        }
 34    }
 35}
 36
 37/// What a single inspection is allowed to do.
 38///
 39/// This is a parameter of `inspectSnapshot` with no default value, so each of
 40/// the six call sites has to state its scope and none can inherit one silently.
 41nonisolated struct InspectionScope: Sendable, Equatable {
 42    var profile: InspectionProfile
 43    /// Whether the inspection may contact `rdap.org`, `crt.sh` and `ipapi.co`.
 44    var allowsThirdPartyEnrichment: Bool
 45
 46    static let profileDefaultsKey = "inspection.profile"
 47    static let enrichmentDefaultsKey = "inspection.allowsThirdPartyEnrichment"
 48
 49    /// Every section against every service. The Local API answers with this
 50    /// unless a request asks otherwise, so `v1` consumers see no change.
 51    static let full = InspectionScope(profile: .full, allowsThirdPartyEnrichment: true)
 52
 53    /// The scope the user has chosen, for the app's own lookups and for
 54    /// monitoring. Both settings default on, as they behaved before they
 55    /// existed.
 56    static func current(defaults: UserDefaults = .standard) -> InspectionScope {
 57        let storedProfile = defaults.string(forKey: profileDefaultsKey)
 58        return InspectionScope(
 59            profile: storedProfile.flatMap(InspectionProfile.init(rawValue:)) ?? .full,
 60            allowsThirdPartyEnrichment: defaults.object(forKey: enrichmentDefaultsKey) as? Bool ?? true
 61        )
 62    }
 63
 64    /// The scope a Local API request asks for.
 65    ///
 66    /// The profile defaults to Full, so a `v1` consumer that sends no `profile`
 67    /// gets what it always did. The enrichment setting is the device owner's
 68    /// consent decision rather than a per-request option, so it applies here
 69    /// too and a request cannot override it.
 70    ///
 71    /// Returns `nil` for a `profile` value that names no known profile, which
 72    /// the caller answers as a request error rather than silently running Full.
 73    static func requested(
 74        from queryItems: [URLQueryItem],
 75        defaults: UserDefaults = .standard
 76    ) -> InspectionScope? {
 77        let requested = queryItems.first { $0.name == "profile" }?.value?
 78            .trimmingCharacters(in: .whitespacesAndNewlines)
 79            .lowercased()
 80        let profile: InspectionProfile
 81        if let requested, !requested.isEmpty {
 82            guard let parsed = InspectionProfile(rawValue: requested) else { return nil }
 83            profile = parsed
 84        } else {
 85            profile = .full
 86        }
 87        return InspectionScope(
 88            profile: profile,
 89            allowsThirdPartyEnrichment: current(defaults: defaults).allowsThirdPartyEnrichment
 90        )
 91    }
 92
 93    /// Whether this scope runs `section`.
 94    ///
 95    /// `availability` always runs: declining enrichment drops its RDAP half,
 96    /// leaving the DNS fallback, which still recognises a domain that resolves.
 97    func runs(_ section: LookupSectionKind) -> Bool {
 98        switch section {
 99        case .dns, .availability, .ssl, .hsts, .httpHeaders, .emailSecurity, .ptr, .redirectChain:
100            return true
101        case .ownership:
102            return allowsThirdPartyEnrichment
103        case .reachability, .portScan:
104            return profile == .full
105        case .ipGeolocation, .subdomains, .suggestions:
106            return profile == .full && allowsThirdPartyEnrichment
107        }
108    }
109
110    /// Whether `DomainAvailabilityService` may ask RDAP, as opposed to deciding
111    /// registration from DNS alone.
112    var allowsRDAP: Bool { allowsThirdPartyEnrichment }
113
114    /// What to show in place of a section this scope did not run.
115    func skipReason(for section: LookupSectionKind) -> String? {
116        guard !runs(section) else { return nil }
117        if !allowsThirdPartyEnrichment, Self.needsThirdParty(section) {
118            return "Not run: third-party enrichment is off in Settings."
119        }
120        return "Not run in the Quick profile."
121    }
122
123    private static func needsThirdParty(_ section: LookupSectionKind) -> Bool {
124        switch section {
125        case .ownership, .ipGeolocation, .subdomains, .suggestions:
126            return true
127        case .dns, .availability, .ssl, .hsts, .httpHeaders, .emailSecurity, .ptr, .redirectChain, .reachability, .portScan:
128            return false
129        }
130    }
131}