summaryrefslogtreecommitdiff
path: root/DomainDig/DomainDiffService.swift
blob: 340873d8415e36d9d2312dc79355d648778904e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import Foundation

enum DiffChangeType: String, Codable {
    case added
    case removed
    case changed
    case unchanged
}

struct DomainDiffItem: Identifiable, Equatable {
    let id = UUID()
    let label: String
    let changeType: DiffChangeType
    let oldValue: String?
    let newValue: String?
    let severity: ChangeSeverity

    var hasChanges: Bool {
        changeType != .unchanged
    }

    var isMeaningful: Bool {
        hasChanges && severity >= .medium
    }
}

struct DomainDiffSection: Identifiable, Equatable {
    let id = UUID()
    let title: String
    let items: [DomainDiffItem]

    var hasChanges: Bool {
        items.contains(where: \.hasChanges)
    }

    var severity: ChangeSeverity {
        items.map(\.severity).max() ?? .low
    }
}

enum DomainDiffService {
    static func diff(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> [DomainDiffSection] {
        [
            availabilitySection(from: oldSnapshot, to: newSnapshot),
            primaryIPSection(from: oldSnapshot, to: newSnapshot),
            ownershipSection(from: oldSnapshot, to: newSnapshot),
            dnsSection(from: oldSnapshot, to: newSnapshot),
            redirectSection(from: oldSnapshot, to: newSnapshot),
            tlsSection(from: oldSnapshot, to: newSnapshot),
            httpSection(from: oldSnapshot, to: newSnapshot),
            emailSection(from: oldSnapshot, to: newSnapshot),
            subdomainSection(from: oldSnapshot, to: newSnapshot)
        ]
        .filter { !$0.items.isEmpty }
    }

    static func summary(
        from oldSnapshot: LookupSnapshot,
        to newSnapshot: LookupSnapshot,
        generatedAt: Date = Date()
    ) -> DomainChangeSummary {
        let sections = diff(from: oldSnapshot, to: newSnapshot)
        let allChangedItems = sections
            .flatMap(\.items)
            .filter(\.hasChanges)

        let highlights = summaryHighlights(from: allChangedItems)
        let severity = allChangedItems.map(\.severity).max() ?? .low
        let message = summaryMessage(from: allChangedItems, highlights: highlights)

        return DomainChangeSummary(
            hasChanges: !allChangedItems.isEmpty,
            changedSections: highlights,
            message: message,
            severity: severity,
            generatedAt: generatedAt
        )
    }

    static func certificateWarningLevel(for snapshot: LookupSnapshot) -> CertificateWarningLevel {
        guard let days = snapshot.sslInfo?.daysUntilExpiry else {
            return .none
        }
        if days < 14 {
            return .critical
        }
        if days < 30 {
            return .warning
        }
        return .none
    }

    private static func availabilitySection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Availability",
            items: [
                compare(
                    label: "Availability",
                    oldValue: availabilityLabel(oldSnapshot.availabilityResult?.status),
                    newValue: availabilityLabel(newSnapshot.availabilityResult?.status),
                    severity: .high
                )
            ].compactMap { $0 }
        )
    }

    private static func primaryIPSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Primary IP",
            items: [
                compare(
                    label: "Primary IP",
                    oldValue: primaryIP(from: oldSnapshot),
                    newValue: primaryIP(from: newSnapshot),
                    severity: .high
                )
            ].compactMap { $0 }
        )
    }

    private static func dnsSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        let oldSections = Dictionary(uniqueKeysWithValues: oldSnapshot.dnsSections.map { ($0.recordType, $0) })
        let newSections = Dictionary(uniqueKeysWithValues: newSnapshot.dnsSections.map { ($0.recordType, $0) })
        let types = Set(oldSections.keys).union(newSections.keys).sorted { $0.rawValue < $1.rawValue }

        var items: [DomainDiffItem] = []
        for type in types {
            let oldSection = oldSections[type]
            let newSection = newSections[type]

            if let recordChange = compare(
                label: "\(type.rawValue) Records",
                oldValue: normalizedRecordValues(for: oldSection),
                newValue: normalizedRecordValues(for: newSection),
                severity: .medium
            ) {
                items.append(recordChange)
            }

            if let ttlChange = compare(
                label: "\(type.rawValue) TTL",
                oldValue: normalizedTTLValues(for: oldSection),
                newValue: normalizedTTLValues(for: newSection),
                severity: .low
            ), let oldSection, let newSection,
               normalizedRecordValues(for: oldSection) == normalizedRecordValues(for: newSection) {
                items.append(ttlChange)
            }
        }

        return DomainDiffSection(title: "DNS", items: items)
    }

    private static func ownershipSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Ownership",
            items: [
                compare(
                    label: "Registrar",
                    oldValue: normalized(oldSnapshot.ownership?.registrar),
                    newValue: normalized(newSnapshot.ownership?.registrar),
                    severity: .high
                ),
                compare(
                    label: "Registration Date",
                    oldValue: ownershipDateLabel(oldSnapshot.ownership?.createdDate),
                    newValue: ownershipDateLabel(newSnapshot.ownership?.createdDate),
                    severity: .low
                ),
                compare(
                    label: "Expiration Date",
                    oldValue: ownershipDateLabel(oldSnapshot.ownership?.expirationDate),
                    newValue: ownershipDateLabel(newSnapshot.ownership?.expirationDate),
                    severity: .low
                ),
                compare(
                    label: "Ownership Status",
                    oldValue: ownershipList(oldSnapshot.ownership?.status),
                    newValue: ownershipList(newSnapshot.ownership?.status),
                    severity: .low
                ),
                compare(
                    label: "Nameservers",
                    oldValue: ownershipList(oldSnapshot.ownership?.nameservers),
                    newValue: ownershipList(newSnapshot.ownership?.nameservers),
                    severity: .medium
                ),
                compare(
                    label: "Abuse Contact",
                    oldValue: normalized(oldSnapshot.ownership?.abuseEmail),
                    newValue: normalized(newSnapshot.ownership?.abuseEmail),
                    severity: .low
                )
            ].compactMap { $0 }
        )
    }

    private static func redirectSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Redirect",
            items: [
                compare(
                    label: "Redirect Target",
                    oldValue: finalRedirectURL(from: oldSnapshot),
                    newValue: finalRedirectURL(from: newSnapshot),
                    severity: .high
                )
            ].compactMap { $0 }
        )
    }

    private static func tlsSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        var items: [DomainDiffItem] = []

        if let issuerChange = compare(
            label: "TLS Issuer",
            oldValue: normalized(oldSnapshot.sslInfo?.issuer),
            newValue: normalized(newSnapshot.sslInfo?.issuer),
            severity: .medium
        ) {
            items.append(issuerChange)
        }

        if let expiryChange = compare(
            label: "TLS Expiration",
            oldValue: expirationLabel(oldSnapshot.sslInfo),
            newValue: expirationLabel(newSnapshot.sslInfo),
            severity: .medium
        ) {
            items.append(expiryChange)
        }

        let oldWarning = certificateWarningLevel(for: oldSnapshot)
        let newWarning = certificateWarningLevel(for: newSnapshot)
        if oldWarning != newWarning, newWarning != .none {
            let days = newSnapshot.sslInfo?.daysUntilExpiry ?? 0
            items.append(
                DomainDiffItem(
                    label: "Certificate Warning",
                    changeType: .changed,
                    oldValue: oldWarning.title,
                    newValue: "Certificate expires in \(days) days",
                    severity: newWarning == .critical ? .high : .medium
                )
            )
        }

        return DomainDiffSection(title: "TLS", items: items)
    }

    private static func httpSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        var items: [DomainDiffItem] = []

        if let statusChange = compare(
            label: "HTTP Status",
            oldValue: httpStatusSummary(from: oldSnapshot),
            newValue: httpStatusSummary(from: newSnapshot),
            severity: .medium
        ) {
            items.append(statusChange)
        }

        if let gradeChange = compare(
            label: "Security Grade",
            oldValue: normalized(oldSnapshot.httpSecurityGrade),
            newValue: normalized(newSnapshot.httpSecurityGrade),
            severity: .low
        ) {
            items.append(gradeChange)
        }

        if let headerChange = compare(
            label: "Headers",
            oldValue: normalizedHeaders(from: oldSnapshot),
            newValue: normalizedHeaders(from: newSnapshot),
            severity: .low
        ) {
            items.append(headerChange)
        }

        return DomainDiffSection(title: "HTTP", items: items)
    }

    private static func emailSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Email Security",
            items: [
                compare(
                    label: "Email Security",
                    oldValue: normalized(emailSummary(from: oldSnapshot)),
                    newValue: normalized(emailSummary(from: newSnapshot)),
                    severity: .medium
                )
            ].compactMap { $0 }
        )
    }

    private static func subdomainSection(from oldSnapshot: LookupSnapshot, to newSnapshot: LookupSnapshot) -> DomainDiffSection {
        DomainDiffSection(
            title: "Subdomains",
            items: [
                compare(
                    label: "Passive Subdomains",
                    oldValue: subdomainList(from: oldSnapshot),
                    newValue: subdomainList(from: newSnapshot),
                    severity: .low
                )
            ].compactMap { $0 }
        )
    }

    private static func compare(
        label: String,
        oldValue: String?,
        newValue: String?,
        severity: ChangeSeverity
    ) -> DomainDiffItem? {
        let oldValue = normalized(oldValue)
        let newValue = normalized(newValue)
        let normalizedOldValue = comparisonValue(oldValue)
        let normalizedNewValue = comparisonValue(newValue)

        guard oldValue != nil || newValue != nil else {
            return nil
        }

        let changeType: DiffChangeType
        switch (normalizedOldValue, normalizedNewValue) {
        case let (old?, new?) where old == new:
            changeType = .unchanged
        case (nil, _?):
            changeType = .added
        case (_?, nil):
            changeType = .removed
        default:
            changeType = .changed
        }

        return DomainDiffItem(
            label: label,
            changeType: changeType,
            oldValue: oldValue,
            newValue: newValue,
            severity: severity
        )
    }

    private static func summaryHighlights(from items: [DomainDiffItem]) -> [String] {
        var highlights: [String] = []

        let labels = Set(items.map(\.label))
        if labels.contains("Availability") {
            highlights.append("Availability changed")
        }
        if labels.contains("Primary IP"), labels.contains(where: { $0.hasSuffix("Records") }) {
            highlights.append("IP changed")
            highlights.append("DNS changed")
            return highlights
        }
        if labels.contains("Primary IP") {
            highlights.append("IP changed")
        }
        if labels.contains("Redirect Target") {
            highlights.append("Redirect target changed")
        }
        if labels.contains("Registrar") {
            highlights.append("Registrar changed")
        } else if labels.contains("Nameservers") {
            highlights.append("Nameservers changed")
        } else if labels.contains("Expiration Date") || labels.contains("Registration Date") || labels.contains("Ownership Status") || labels.contains("Abuse Contact") {
            highlights.append("Ownership metadata changed")
        }
        if let certificateItem = items.first(where: { $0.label == "Certificate Warning" }),
           let message = certificateItem.newValue {
            highlights.append(message)
        } else if labels.contains("TLS Issuer") {
            highlights.append("TLS issuer changed")
        } else if labels.contains("TLS Expiration") {
            highlights.append("Certificate expiration changed")
        }
        if labels.contains(where: { $0.hasSuffix("Records") }) {
            highlights.append("DNS changed")
        }
        if labels.contains("HTTP Status") {
            highlights.append("HTTP status changed")
        }
        if labels.contains("Email Security") {
            highlights.append("Email security changed")
        }
        if labels.contains("Passive Subdomains") {
            highlights.append("Subdomains changed")
        }

        var deduplicated: [String] = []
        for highlight in highlights where !deduplicated.contains(highlight) {
            deduplicated.append(highlight)
        }
        return deduplicated
    }

    private static func summaryMessage(from items: [DomainDiffItem], highlights: [String]) -> String {
        guard !items.isEmpty, !highlights.isEmpty else {
            return "No meaningful changes"
        }

        if highlights.count == 1 {
            return highlights[0]
        }

        return "\(highlights[0]) and \(highlights[1].lowercased())"
    }

    private static func normalized(_ value: String?) -> String? {
        guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else {
            return nil
        }
        return value
    }

    private static func comparisonValue(_ value: String?) -> String? {
        value?.lowercased()
    }

    private static func availabilityLabel(_ status: DomainAvailabilityStatus?) -> String? {
        switch status {
        case .available:
            return "available"
        case .registered:
            return "registered"
        case .unknown:
            return "unknown"
        case .none:
            return nil
        }
    }

    private static func primaryIP(from snapshot: LookupSnapshot) -> String? {
        snapshot.dnsSections.first(where: { $0.recordType == .A })?.records.first?.value
    }

    private static func finalRedirectURL(from snapshot: LookupSnapshot) -> String? {
        snapshot.redirectChain.last?.url
    }

    private static func expirationLabel(_ sslInfo: SSLCertificateInfo?) -> String? {
        guard let sslInfo else { return nil }
        return "\(sslInfo.validUntil.formatted(date: .abbreviated, time: .omitted)) (\(sslInfo.daysUntilExpiry)d)"
    }

    private static func ownershipDateLabel(_ date: Date?) -> String? {
        date?.formatted(date: .abbreviated, time: .omitted)
    }

    private static func httpStatusSummary(from snapshot: LookupSnapshot) -> String? {
        if let httpStatusCode = snapshot.httpStatusCode {
            return "\(httpStatusCode)"
        }
        return snapshot.httpHeadersError
    }

    private static func emailSummary(from snapshot: LookupSnapshot) -> String? {
        if let emailSecurity = snapshot.emailSecurity {
            return [
                "spf:\(emailSecurity.spf.found)",
                "dmarc:\(emailSecurity.dmarc.found)",
                "dkim:\(emailSecurity.dkim.found)",
                "bimi:\(emailSecurity.bimi.found)",
                "mta-sts:\(emailSecurity.mtaSts?.txtFound == true)"
            ].joined(separator: "|")
        }
        return snapshot.emailSecurityError
    }

    private static func normalizedRecordValues(for section: DNSSection?) -> String? {
        guard let section else { return nil }
        let values = (section.records + section.wildcardRecords)
            .map(\.value)
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
            .sorted()
        return values.isEmpty ? nil : values.joined(separator: ",")
    }

    private static func normalizedTTLValues(for section: DNSSection?) -> String? {
        guard let section else { return nil }
        let values = (section.records + section.wildcardRecords)
            .map { "\($0.value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()):\($0.ttl)" }
            .sorted()
        return values.isEmpty ? nil : values.joined(separator: ",")
    }

    private static func normalizedHeaders(from snapshot: LookupSnapshot) -> String? {
        let headers = snapshot.httpHeaders
            .map { "\($0.name.lowercased()):\($0.value.trimmingCharacters(in: .whitespacesAndNewlines))" }
            .sorted()
        return headers.isEmpty ? nil : headers.joined(separator: "|")
    }

    private static func ownershipList(_ values: [String]?) -> String? {
        guard let values else { return nil }
        let normalizedValues = values
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
            .filter { !$0.isEmpty }
            .sorted()
        return normalizedValues.isEmpty ? nil : normalizedValues.joined(separator: ",")
    }

    private static func subdomainList(from snapshot: LookupSnapshot) -> String? {
        let values = snapshot.subdomains
            .map(\.hostname)
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
            .sorted()
        return values.isEmpty ? nil : values.joined(separator: ",")
    }
}