DomainDig/LookupSnapshot.swift
324 lines · 13973 bytes
1import Foundation
2
3struct LookupSnapshot {
4 let historyEntryID: UUID?
5 let domain: String
6 let timestamp: Date
7 let trackedDomainID: UUID?
8 let note: String?
9 let appVersion: String
10 let resolverDisplayName: String
11 let resolverURLString: String
12 let dataSources: [String]
13 let provenanceBySection: [LookupSectionKind: SectionProvenance]
14 let availabilityConfidence: ConfidenceLevel?
15 let ownershipConfidence: ConfidenceLevel?
16 let subdomainConfidence: ConfidenceLevel?
17 let emailSecurityConfidence: ConfidenceLevel?
18 let geolocationConfidence: ConfidenceLevel?
19 let errorDetails: [LookupSectionKind: InspectionFailure]
20 let isPartialSnapshot: Bool
21 let validationIssues: [String]
22 let totalLookupDurationMs: Int?
23 let snapshotIndex: Int?
24 let previousSnapshotID: UUID?
25 let changeCount: Int
26 let severitySummary: ChangeSeverity?
27 let dnsSections: [DNSSection]
28 let dnsError: String?
29 let availabilityResult: DomainAvailabilityResult?
30 let suggestions: [DomainSuggestionResult]
31 let sslInfo: SSLCertificateInfo?
32 let sslError: String?
33 let hstsPreloaded: Bool?
34 let httpHeaders: [HTTPHeader]
35 let httpSecurityGrade: String?
36 let httpStatusCode: Int?
37 let httpResponseTimeMs: Int?
38 let httpProtocol: String?
39 let http3Advertised: Bool
40 let httpHeadersError: String?
41 let reachabilityResults: [PortReachability]
42 let reachabilityError: String?
43 let ipGeolocation: IPGeolocation?
44 let ipGeolocationError: String?
45 let emailSecurity: EmailSecurityResult?
46 let emailSecurityError: String?
47 let ownership: DomainOwnership?
48 let ownershipError: String?
49 let ownershipHistory: [DomainOwnershipHistoryEvent]
50 let ownershipHistoryError: String?
51 let inferredProvider: InferredProviderFingerprint?
52 let priorProviders: [String]
53 let domainClassification: DomainClassificationSummary?
54 let ownershipTransitions: [OwnershipTransitionEvent]
55 let hostingTransitions: [HostingTransitionEvent]
56 let subdomainHistory: [SubdomainHistoryEntry]
57 let riskSignals: [IntelligenceRiskSignal]
58 let intelligenceTimeline: [IntelligenceTimelineEvent]
59 let ptrRecord: String?
60 let ptrError: String?
61 let redirectChain: [RedirectHop]
62 let redirectChainError: String?
63 let subdomains: [DiscoveredSubdomain]
64 let subdomainsError: String?
65 let extendedSubdomains: [DiscoveredSubdomain]
66 let extendedSubdomainsError: String?
67 let dnsHistory: [DNSHistoryEvent]
68 let dnsHistoryError: String?
69 let domainPricing: DomainPricingInsight?
70 let domainPricingError: String?
71 let reputation: DomainReputationResult?
72 let reputationError: String?
73 let portScanResults: [PortScanResult]
74 let portScanError: String?
75 let changeSummary: DomainChangeSummary?
76 let resultSource: LookupResultSource
77 let cachedSections: [LookupSectionKind]
78 let statusMessage: String?
79
80 var isLive: Bool {
81 resultSource == .live
82 }
83}
84
85extension HistoryEntry {
86 var snapshot: LookupSnapshot {
87 LookupSnapshot(
88 historyEntryID: id,
89 domain: domain,
90 timestamp: timestamp,
91 trackedDomainID: trackedDomainID,
92 note: note,
93 appVersion: appVersion,
94 resolverDisplayName: resolverDisplayName,
95 resolverURLString: resolverURLString,
96 dataSources: dataSources,
97 provenanceBySection: provenanceBySection,
98 availabilityConfidence: availabilityConfidence,
99 ownershipConfidence: ownershipConfidence,
100 subdomainConfidence: subdomainConfidence,
101 emailSecurityConfidence: emailSecurityConfidence,
102 geolocationConfidence: geolocationConfidence,
103 errorDetails: errorDetails,
104 isPartialSnapshot: isPartialSnapshot,
105 validationIssues: validationIssues,
106 totalLookupDurationMs: totalLookupDurationMs,
107 snapshotIndex: snapshotIndex,
108 previousSnapshotID: previousSnapshotID,
109 changeCount: changeCount,
110 severitySummary: severitySummary,
111 dnsSections: dnsSections,
112 dnsError: nil,
113 availabilityResult: availabilityResult,
114 suggestions: suggestions,
115 sslInfo: sslInfo,
116 sslError: sslError,
117 hstsPreloaded: hstsPreloaded,
118 httpHeaders: httpHeaders,
119 httpSecurityGrade: HTTPSecurityGrade.grade(for: httpHeaders).rawValue,
120 httpStatusCode: nil,
121 httpResponseTimeMs: nil,
122 httpProtocol: nil,
123 http3Advertised: false,
124 httpHeadersError: httpHeadersError,
125 reachabilityResults: reachabilityResults,
126 reachabilityError: reachabilityError,
127 ipGeolocation: ipGeolocation,
128 ipGeolocationError: ipGeolocationError,
129 emailSecurity: emailSecurity,
130 emailSecurityError: emailSecurityError,
131 ownership: ownership,
132 ownershipError: ownershipError,
133 ownershipHistory: ownershipHistory,
134 ownershipHistoryError: ownershipHistoryError,
135 inferredProvider: inferredProvider,
136 priorProviders: priorProviders,
137 domainClassification: domainClassification,
138 ownershipTransitions: ownershipTransitions,
139 hostingTransitions: hostingTransitions,
140 subdomainHistory: subdomainHistory,
141 riskSignals: riskSignals,
142 intelligenceTimeline: intelligenceTimeline,
143 ptrRecord: ptrRecord,
144 ptrError: ptrError,
145 redirectChain: redirectChain,
146 redirectChainError: redirectChainError,
147 subdomains: subdomains,
148 subdomainsError: subdomainsError,
149 extendedSubdomains: extendedSubdomains,
150 extendedSubdomainsError: extendedSubdomainsError,
151 dnsHistory: dnsHistory,
152 dnsHistoryError: dnsHistoryError,
153 domainPricing: domainPricing,
154 domainPricingError: domainPricingError,
155 reputation: reputation,
156 reputationError: reputationError,
157 portScanResults: portScanResults,
158 portScanError: portScanError,
159 changeSummary: changeSummary,
160 resultSource: resultSource,
161 cachedSections: [],
162 statusMessage: nil
163 )
164 }
165}
166
167extension LookupSnapshot {
168 /// Whether every section that failed did so for want of a connection (a
169 /// network error, a timeout, a rate limit) and nothing worth showing came
170 /// back, so the last snapshot should stand in for this one.
171 var needsFallback: Bool {
172 let candidateMessages = [
173 dnsError,
174 httpHeadersError,
175 sslError,
176 ownershipError,
177 subdomainsError,
178 redirectChainError,
179 ipGeolocationError
180 ]
181 .compactMap { $0?.lowercased() }
182
183 guard !candidateMessages.isEmpty else { return false }
184 let connectivityFailure = candidateMessages.allSatisfy { message in
185 message.hasPrefix("network error:")
186 || message.hasPrefix("timeout:")
187 || message.hasPrefix("rate limit:")
188 }
189
190 let hasMaterialData = !dnsSections.isEmpty
191 || !httpHeaders.isEmpty
192 || sslInfo != nil
193 || ownership != nil
194 || !subdomains.isEmpty
195
196 return connectivityFailure && !hasMaterialData
197 }
198
199 /// `previous`, marked as the last known result, when this lookup
200 /// ``needsFallback``; otherwise this lookup.
201 func resolvedAfterFallback(to previous: LookupSnapshot?) -> LookupSnapshot {
202 guard needsFallback, let previous else {
203 return self
204 }
205
206 return LookupSnapshot(
207 historyEntryID: previous.historyEntryID,
208 domain: previous.domain,
209 timestamp: previous.timestamp,
210 trackedDomainID: previous.trackedDomainID,
211 note: previous.note,
212 appVersion: previous.appVersion,
213 resolverDisplayName: previous.resolverDisplayName,
214 resolverURLString: previous.resolverURLString,
215 dataSources: previous.dataSources,
216 provenanceBySection: previous.provenanceBySection,
217 availabilityConfidence: previous.availabilityConfidence,
218 ownershipConfidence: previous.ownershipConfidence,
219 subdomainConfidence: previous.subdomainConfidence,
220 emailSecurityConfidence: previous.emailSecurityConfidence,
221 geolocationConfidence: previous.geolocationConfidence,
222 errorDetails: previous.errorDetails,
223 isPartialSnapshot: previous.isPartialSnapshot,
224 validationIssues: previous.validationIssues,
225 totalLookupDurationMs: previous.totalLookupDurationMs,
226 snapshotIndex: previous.snapshotIndex,
227 previousSnapshotID: previous.previousSnapshotID,
228 changeCount: previous.changeCount,
229 severitySummary: previous.severitySummary,
230 dnsSections: previous.dnsSections,
231 dnsError: previous.dnsError,
232 availabilityResult: previous.availabilityResult,
233 suggestions: previous.suggestions,
234 sslInfo: previous.sslInfo,
235 sslError: previous.sslError,
236 hstsPreloaded: previous.hstsPreloaded,
237 httpHeaders: previous.httpHeaders,
238 httpSecurityGrade: previous.httpSecurityGrade,
239 httpStatusCode: previous.httpStatusCode,
240 httpResponseTimeMs: previous.httpResponseTimeMs,
241 httpProtocol: previous.httpProtocol,
242 http3Advertised: previous.http3Advertised,
243 httpHeadersError: previous.httpHeadersError,
244 reachabilityResults: previous.reachabilityResults,
245 reachabilityError: previous.reachabilityError,
246 ipGeolocation: previous.ipGeolocation,
247 ipGeolocationError: previous.ipGeolocationError,
248 emailSecurity: previous.emailSecurity,
249 emailSecurityError: previous.emailSecurityError,
250 ownership: previous.ownership,
251 ownershipError: previous.ownershipError,
252 ownershipHistory: previous.ownershipHistory,
253 ownershipHistoryError: previous.ownershipHistoryError,
254 inferredProvider: previous.inferredProvider,
255 priorProviders: previous.priorProviders,
256 domainClassification: previous.domainClassification,
257 ownershipTransitions: previous.ownershipTransitions,
258 hostingTransitions: previous.hostingTransitions,
259 subdomainHistory: previous.subdomainHistory,
260 riskSignals: previous.riskSignals,
261 intelligenceTimeline: previous.intelligenceTimeline,
262 ptrRecord: previous.ptrRecord,
263 ptrError: previous.ptrError,
264 redirectChain: previous.redirectChain,
265 redirectChainError: previous.redirectChainError,
266 subdomains: previous.subdomains,
267 subdomainsError: previous.subdomainsError,
268 extendedSubdomains: previous.extendedSubdomains,
269 extendedSubdomainsError: previous.extendedSubdomainsError,
270 dnsHistory: previous.dnsHistory,
271 dnsHistoryError: previous.dnsHistoryError,
272 domainPricing: previous.domainPricing,
273 domainPricingError: previous.domainPricingError,
274 reputation: previous.reputation,
275 reputationError: previous.reputationError,
276 portScanResults: previous.portScanResults,
277 portScanError: previous.portScanError,
278 changeSummary: previous.changeSummary,
279 resultSource: .snapshot,
280 cachedSections: [],
281 statusMessage: "Last known result • \(previous.timestamp.formatted(date: .abbreviated, time: .shortened))"
282 )
283 }
284}
285
286/// The metadata one inspection produced: where each section came from, how
287/// confident it is, what failed, and whether the result is partial.
288///
289/// `DomainViewModel.currentSnapshot` used to recover all of this from the
290/// *saved history entry*, so it was empty until the snapshot had been written
291/// and `currentHistoryEntryID` pointed at it. Two consequences, both fixed by
292/// carrying it from the lookup instead: the cards rendered without provenance
293/// between applying a snapshot and saving it, and a lookup that returns before
294/// saving — any partial or failed one — showed no provenance for any section,
295/// which is the case where knowing the source matters most (A12).
296struct InspectionMetadata: Equatable {
297 var dataSources: [String]
298 var provenanceBySection: [LookupSectionKind: SectionProvenance]
299 var availabilityConfidence: ConfidenceLevel?
300 var ownershipConfidence: ConfidenceLevel?
301 var subdomainConfidence: ConfidenceLevel?
302 var emailSecurityConfidence: ConfidenceLevel?
303 var geolocationConfidence: ConfidenceLevel?
304 var errorDetails: [LookupSectionKind: InspectionFailure]
305 var isPartialSnapshot: Bool
306 var validationIssues: [String]
307 var snapshotIndex: Int?
308 var previousSnapshotID: UUID?
309
310 init(snapshot: LookupSnapshot) {
311 dataSources = snapshot.dataSources
312 provenanceBySection = snapshot.provenanceBySection
313 availabilityConfidence = snapshot.availabilityConfidence
314 ownershipConfidence = snapshot.ownershipConfidence
315 subdomainConfidence = snapshot.subdomainConfidence
316 emailSecurityConfidence = snapshot.emailSecurityConfidence
317 geolocationConfidence = snapshot.geolocationConfidence
318 errorDetails = snapshot.errorDetails
319 isPartialSnapshot = snapshot.isPartialSnapshot
320 validationIssues = snapshot.validationIssues
321 snapshotIndex = snapshot.snapshotIndex
322 previousSnapshotID = snapshot.previousSnapshotID
323 }
324}