DomainDig/DomainDiffService.swift
211 lines · 8090 bytes
1import Foundation
2
3enum DiffChangeType: String, Codable {
4 case added
5 case removed
6 case changed
7 case unchanged
8}
9
10struct DomainDiffItem: Identifiable, Equatable {
11 let id = UUID()
12 let label: String
13 let changeType: DiffChangeType
14 let oldValue: String?
15 let newValue: String?
16
17 var hasChanges: Bool {
18 changeType != .unchanged
19 }
20}
21
22struct DomainDiffSection: Identifiable, Equatable {
23 let id = UUID()
24 let title: String
25 let items: [DomainDiffItem]
26
27 var hasChanges: Bool {
28 items.contains(where: \.hasChanges)
29 }
30}
31
32enum DomainDiffService {
33 static func diff(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> [DomainDiffSection] {
34 [
35 section(title: "Availability", item: compare(
36 label: "Status",
37 oldValue: availabilityLabel(oldSnapshot.availabilityResult?.status),
38 newValue: availabilityLabel(newSnapshot.availabilityResult?.status)
39 )),
40 section(title: "Primary IP", item: compare(
41 label: "Address",
42 oldValue: primaryIP(from: oldSnapshot),
43 newValue: primaryIP(from: newSnapshot)
44 )),
45 dnsSection(from: oldSnapshot, to: newSnapshot),
46 section(title: "Redirect", item: compare(
47 label: "Final Target",
48 oldValue: finalRedirectURL(from: oldSnapshot),
49 newValue: finalRedirectURL(from: newSnapshot)
50 )),
51 tlsSection(from: oldSnapshot, to: newSnapshot),
52 httpSection(from: oldSnapshot, to: newSnapshot),
53 emailSection(from: oldSnapshot, to: newSnapshot)
54 ]
55 .filter { !$0.items.isEmpty }
56 }
57
58 static func summary(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot, generatedAt: Date = Date()) -> DomainChangeSummary {
59 let changedSections = diff(from: oldSnapshot, to: newSnapshot)
60 .filter { $0.items.contains(where: { $0.changeType != .unchanged }) }
61 .map(\.title)
62
63 return DomainChangeSummary(
64 hasChanges: !changedSections.isEmpty,
65 changedSections: changedSections,
66 generatedAt: generatedAt
67 )
68 }
69
70 private static func section(title: String, item: DomainDiffItem?) -> DomainDiffSection {
71 DomainDiffSection(title: title, items: item.map { [$0] } ?? [])
72 }
73
74 private static func dnsSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
75 let oldValue = normalizedDNSSummary(from: oldSnapshot)
76 let newValue = normalizedDNSSummary(from: newSnapshot)
77 return section(title: "DNS Records", item: compare(label: "Records", oldValue: oldValue, newValue: newValue))
78 }
79
80 private static func tlsSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
81 var items: [DomainDiffItem] = []
82 if let item = compare(label: "Issuer", oldValue: normalized(oldSnapshot.sslInfo?.issuer), newValue: normalized(newSnapshot.sslInfo?.issuer)) {
83 items.append(item)
84 }
85 if let item = compare(label: "Certificate", oldValue: tlsSummary(from: oldSnapshot), newValue: tlsSummary(from: newSnapshot)) {
86 items.append(item)
87 }
88 return DomainDiffSection(title: "TLS Certificate", items: items)
89 }
90
91 private static func httpSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
92 var items: [DomainDiffItem] = []
93 if let item = compare(label: "HTTP Status", oldValue: httpStatusSummary(from: oldSnapshot), newValue: httpStatusSummary(from: newSnapshot)) {
94 items.append(item)
95 }
96 if let item = compare(label: "Security Grade", oldValue: normalized(oldSnapshot.httpSecurityGrade), newValue: normalized(newSnapshot.httpSecurityGrade)) {
97 items.append(item)
98 }
99 return DomainDiffSection(title: "HTTP", items: items)
100 }
101
102 private static func emailSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
103 section(
104 title: "Email Security",
105 item: compare(
106 label: "Summary",
107 oldValue: normalized(emailSummary(from: oldSnapshot)),
108 newValue: normalized(emailSummary(from: newSnapshot))
109 )
110 )
111 }
112
113 private static func compare(label: String, oldValue: String?, newValue: String?) -> DomainDiffItem? {
114 let oldValue = normalized(oldValue)
115 let newValue = normalized(newValue)
116 let normalizedOldValue = comparisonValue(oldValue)
117 let normalizedNewValue = comparisonValue(newValue)
118
119 guard oldValue != nil || newValue != nil else {
120 return nil
121 }
122
123 let changeType: DiffChangeType
124 switch (normalizedOldValue, normalizedNewValue) {
125 case let (old?, new?) where old == new:
126 changeType = .unchanged
127 case (nil, _?):
128 changeType = .added
129 case (_?, nil):
130 changeType = .removed
131 default:
132 changeType = .changed
133 }
134
135 return DomainDiffItem(label: label, changeType: changeType, oldValue: oldValue, newValue: newValue)
136 }
137
138 private static func normalized(_ value: String?) -> String? {
139 guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else {
140 return nil
141 }
142 return value
143 }
144
145 private static func comparisonValue(_ value: String?) -> String? {
146 value?.lowercased()
147 }
148
149 private static func availabilityLabel(_ status: DomainAvailabilityStatus?) -> String? {
150 switch status {
151 case .available:
152 return "available"
153 case .registered:
154 return "registered"
155 case .unknown:
156 return "unknown"
157 case .none:
158 return nil
159 }
160 }
161
162 private static func primaryIP(from snapshot: LookupSnapshot) -> String? {
163 snapshot.dnsSections.first(where: { $0.recordType == .A })?.records.first?.value
164 }
165
166 private static func finalRedirectURL(from snapshot: LookupSnapshot) -> String? {
167 snapshot.redirectChain.last?.url
168 }
169
170 private static func tlsSummary(from snapshot: LookupSnapshot) -> String? {
171 if let sslInfo = snapshot.sslInfo {
172 return "\(sslInfo.commonName) | \(sslInfo.validUntil.formatted(date: .abbreviated, time: .omitted))"
173 }
174 return snapshot.sslError
175 }
176
177 private static func httpStatusSummary(from snapshot: LookupSnapshot) -> String? {
178 if let httpStatusCode = snapshot.httpStatusCode {
179 return "\(httpStatusCode)"
180 }
181 return snapshot.httpHeadersError
182 }
183
184 private static func emailSummary(from snapshot: LookupSnapshot) -> String? {
185 if let emailSecurity = snapshot.emailSecurity {
186 return [
187 "spf:\(emailSecurity.spf.found)",
188 "dmarc:\(emailSecurity.dmarc.found)",
189 "dkim:\(emailSecurity.dkim.found)",
190 "bimi:\(emailSecurity.bimi.found)",
191 "mta-sts:\(emailSecurity.mtaSts?.txtFound == true)"
192 ].joined(separator: "|")
193 }
194 return snapshot.emailSecurityError
195 }
196
197 private static func normalizedDNSSummary(from snapshot: LookupSnapshot) -> String? {
198 let parts = snapshot.dnsSections
199 .sorted { $0.recordType.rawValue < $1.recordType.rawValue }
200 .map { section in
201 let values = (section.records + section.wildcardRecords)
202 .map(\.value)
203 .map { $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
204 .sorted()
205 .joined(separator: ",")
206 return "\(section.recordType.rawValue):\(values)"
207 }
208 .filter { !$0.hasSuffix(":") }
209 return parts.isEmpty ? nil : parts.joined(separator: "|")
210 }
211}