summaryrefslogtreecommitdiff
path: root/DomainDig/ExternalDataService.swift
blob: 43cc489c372127a4a04546b7a09ba3f32161e1b4 (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
import Foundation

actor ExternalDataService {
    static let shared = ExternalDataService()

    private enum RequestKey: Hashable {
        case ownershipHistory(String)
        case dnsHistory(String)
        case extendedSubdomains(String)
        case pricing(String)
        case reputation(String)
    }

    private enum RateLimitBucket: Hashable {
        case history
        case subdomains
        case pricing
        case reputation

        var minimumSpacing: TimeInterval {
            switch self {
            case .history:
                return 1.0
            case .subdomains:
                return 1.5
            case .pricing:
                return 1.0
            case .reputation:
                return 1.0
            }
        }
    }

    private enum CachedPayload {
        case ownershipHistory(ServiceResult<[DomainOwnershipHistoryEvent]>)
        case dnsHistory(ServiceResult<[DNSHistoryEvent]>)
        case extendedSubdomains(ServiceResult<[DiscoveredSubdomain]>)
        case pricing(ServiceResult<DomainPricingInsight>)
        case reputation(ServiceResult<DomainReputationResult>)
    }

    private struct CacheEntry {
        let payload: CachedPayload
        let expiresAt: Date
    }

    private struct Configuration {
        let ownershipHistoryURL: String?
        let dnsHistoryURL: String?
        let extendedSubdomainsURL: String?
        let pricingURL: String?
        let reputationURL: String?
    }

    private let ttl: TimeInterval = 900
    private let session: URLSession = .shared
    private var cache: [RequestKey: CacheEntry] = [:]
    private var inFlight: [RequestKey: Task<CachedPayload, Never>] = [:]
    private var nextAllowedAt: [RateLimitBucket: Date] = [:]

    func clearCache() {
        cache.removeAll()
        inFlight.values.forEach { $0.cancel() }
        inFlight.removeAll()
        nextAllowedAt.removeAll()
    }

    func ownershipHistory(
        domain: String,
        currentOwnership: DomainOwnership?,
        historyEntries: [HistoryEntry]
    ) async -> CachedLookupResult<ServiceResult<[DomainOwnershipHistoryEvent]>> {
        let normalizedDomain = Self.normalize(domain)
        return await execute(
            key: .ownershipHistory(normalizedDomain),
            rateLimitBucket: .history,
            extract: { payload in
                guard case let .ownershipHistory(result) = payload else { return nil }
                return result
            },
            operation: { [configuration = configuration()] in
                let localEvents = Self.localOwnershipHistory(
                    domain: normalizedDomain,
                    currentOwnership: currentOwnership,
                    historyEntries: historyEntries
                )
                let externalEvents = await self.fetchOwnershipHistory(domain: normalizedDomain, configuration: configuration)
                return .ownershipHistory(Self.mergeOwnershipHistory(localEvents: localEvents, externalEvents: externalEvents))
            }
        )
    }

    func dnsHistory(
        domain: String,
        dnsSections: [DNSSection],
        historyEntries: [HistoryEntry]
    ) async -> CachedLookupResult<ServiceResult<[DNSHistoryEvent]>> {
        let normalizedDomain = Self.normalize(domain)
        return await execute(
            key: .dnsHistory(normalizedDomain),
            rateLimitBucket: .history,
            extract: { payload in
                guard case let .dnsHistory(result) = payload else { return nil }
                return result
            },
            operation: { [configuration = configuration()] in
                let localEvents = Self.localDNSHistory(
                    domain: normalizedDomain,
                    dnsSections: dnsSections,
                    historyEntries: historyEntries
                )
                let externalEvents = await self.fetchDNSHistory(domain: normalizedDomain, configuration: configuration)
                return .dnsHistory(Self.mergeDNSHistory(localEvents: localEvents, externalEvents: externalEvents))
            }
        )
    }

    func extendedSubdomains(
        domain: String,
        existing: [DiscoveredSubdomain]
    ) async -> CachedLookupResult<ServiceResult<[DiscoveredSubdomain]>> {
        let normalizedDomain = Self.normalize(domain)
        return await execute(
            key: .extendedSubdomains(normalizedDomain),
            rateLimitBucket: .subdomains,
            extract: { payload in
                guard case let .extendedSubdomains(result) = payload else { return nil }
                return result
            },
            operation: { [configuration = configuration()] in
                var merged = existing
                switch await SubdomainDiscoveryService.discover(for: normalizedDomain, limit: 100) {
                case let .success(results):
                    merged = Self.mergeSubdomains(primary: merged, additional: results.map {
                        DiscoveredSubdomain(hostname: $0.hostname, source: $0.source ?? "crt.sh", isExtended: true)
                    })
                case .empty, .error:
                    break
                }

                if let external = await self.fetchExtendedSubdomains(domain: normalizedDomain, configuration: configuration) {
                    merged = Self.mergeSubdomains(primary: merged, additional: external)
                }

                if merged.count <= existing.count {
                    return .extendedSubdomains(.empty("No extended subdomains available"))
                }

                let onlyExtended = merged.filter(\.isExtended)
                return .extendedSubdomains(.success(onlyExtended.sorted { $0.hostname < $1.hostname }))
            }
        )
    }

    func pricing(domain: String) async -> CachedLookupResult<ServiceResult<DomainPricingInsight>> {
        let normalizedDomain = Self.normalize(domain)
        return await execute(
            key: .pricing(normalizedDomain),
            rateLimitBucket: .pricing,
            extract: { payload in
                guard case let .pricing(result) = payload else { return nil }
                return result
            },
            operation: { [configuration = configuration()] in
                .pricing(await self.fetchPricing(domain: normalizedDomain, configuration: configuration))
            }
        )
    }

    /// Checks a domain against a configured blocklist/reputation endpoint. With
    /// no endpoint configured (the default, since DomainDig ships no bundled
    /// third-party reputation dependency) this resolves to `.empty` and callers
    /// treat the result as unavailable rather than "clean".
    func reputation(domain: String) async -> CachedLookupResult<ServiceResult<DomainReputationResult>> {
        let normalizedDomain = Self.normalize(domain)
        return await execute(
            key: .reputation(normalizedDomain),
            rateLimitBucket: .reputation,
            extract: { payload in
                guard case let .reputation(result) = payload else { return nil }
                return result
            },
            operation: { [configuration = configuration()] in
                .reputation(await self.fetchReputation(domain: normalizedDomain, configuration: configuration))
            }
        )
    }

    private func execute<T>(
        key: RequestKey,
        rateLimitBucket: RateLimitBucket,
        extract: @escaping (CachedPayload) -> T?,
        operation: @escaping @Sendable () async -> CachedPayload
    ) async -> CachedLookupResult<T> {
        if let cachedEntry = cache[key], cachedEntry.expiresAt > Date(), let value = extract(cachedEntry.payload) {
            return CachedLookupResult(value: value, source: .cached)
        }

        if let task = inFlight[key], let value = extract(await task.value) {
            return CachedLookupResult(value: value, source: .mixed)
        }

        let task = Task<CachedPayload, Never> {
            await self.enforceRateLimit(for: rateLimitBucket)
            return await operation()
        }
        inFlight[key] = task

        let payload = await task.value
        cache[key] = CacheEntry(payload: payload, expiresAt: Date().addingTimeInterval(ttl))
        inFlight[key] = nil

        guard let value = extract(payload) else {
            fatalError("ExternalDataService payload extraction mismatch")
        }

        return CachedLookupResult(value: value, source: .live)
    }

    private func enforceRateLimit(for bucket: RateLimitBucket) async {
        let now = Date()
        if let nextAllowed = nextAllowedAt[bucket], nextAllowed > now {
            let delay = nextAllowed.timeIntervalSince(now)
            if delay > 0 {
                try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
            }
        }
        nextAllowedAt[bucket] = Date().addingTimeInterval(bucket.minimumSpacing)
    }

    private func configuration() -> Configuration {
        let defaults = UserDefaults.standard
        return Configuration(
            ownershipHistoryURL: defaults.string(forKey: "externalData.ownershipHistoryURL")
                ?? Bundle.main.object(forInfoDictionaryKey: "ExternalOwnershipHistoryURL") as? String,
            dnsHistoryURL: defaults.string(forKey: "externalData.dnsHistoryURL")
                ?? Bundle.main.object(forInfoDictionaryKey: "ExternalDNSHistoryURL") as? String,
            extendedSubdomainsURL: defaults.string(forKey: "externalData.extendedSubdomainsURL")
                ?? Bundle.main.object(forInfoDictionaryKey: "ExternalExtendedSubdomainsURL") as? String,
            pricingURL: defaults.string(forKey: "externalData.pricingURL")
                ?? Bundle.main.object(forInfoDictionaryKey: "ExternalPricingURL") as? String,
            reputationURL: defaults.string(forKey: "externalData.reputationURL")
                ?? Bundle.main.object(forInfoDictionaryKey: "ExternalReputationURL") as? String
        )
    }

    private func fetchOwnershipHistory(
        domain: String,
        configuration: Configuration
    ) async -> [DomainOwnershipHistoryEvent] {
        guard let template = configuration.ownershipHistoryURL,
              let url = Self.url(from: template, domain: domain) else {
            return []
        }

        switch await requestData(url: url) {
        case let .success(data):
            return parseOwnershipHistoryEvents(from: data)
        case .empty, .error:
            return []
        }
    }

    private func fetchDNSHistory(
        domain: String,
        configuration: Configuration
    ) async -> [DNSHistoryEvent] {
        guard let template = configuration.dnsHistoryURL,
              let url = Self.url(from: template, domain: domain) else {
            return []
        }

        switch await requestData(url: url) {
        case let .success(data):
            return parseDNSHistoryEvents(from: data)
        case .empty, .error:
            return []
        }
    }

    private func fetchExtendedSubdomains(
        domain: String,
        configuration: Configuration
    ) async -> [DiscoveredSubdomain]? {
        guard let template = configuration.extendedSubdomainsURL,
              let url = Self.url(from: template, domain: domain) else {
            return nil
        }

        switch await requestData(url: url) {
        case let .success(data):
            return parseSubdomains(from: data)
        case .empty, .error:
            return nil
        }
    }

    private func fetchPricing(
        domain: String,
        configuration: Configuration
    ) async -> ServiceResult<DomainPricingInsight> {
        guard let template = configuration.pricingURL,
              let url = Self.url(from: template, domain: domain) else {
            return .empty("External pricing unavailable")
        }

        switch await requestData(url: url) {
        case let .success(data):
            guard let pricing = parsePricing(from: data) else {
                return .error("Invalid external response")
            }
            return .success(pricing)
        case let .empty(message):
            return .empty(message)
        case let .error(message):
            return .error(message)
        }
    }

    private func fetchReputation(
        domain: String,
        configuration: Configuration
    ) async -> ServiceResult<DomainReputationResult> {
        guard let template = configuration.reputationURL,
              let url = Self.url(from: template, domain: domain) else {
            return .empty("No reputation source configured")
        }

        switch await requestData(url: url) {
        case let .success(data):
            guard let reputation = parseReputation(from: data) else {
                return .error("Invalid external response")
            }
            return .success(reputation)
        case let .empty(message):
            return .empty(message)
        case let .error(message):
            return .error(message)
        }
    }

    private func requestData(url: URL) async -> ServiceResult<Data> {
        for attempt in 0..<2 {
            do {
                let (data, response) = try await session.data(for: URLRequest(url: url, timeoutInterval: 8))
                guard let httpResponse = response as? HTTPURLResponse else {
                    return .error("External data unavailable")
                }

                switch httpResponse.statusCode {
                case 200:
                    return .success(data)
                case 204, 404:
                    return .empty("No external data available")
                case 429:
                    return .error("Rate limit reached")
                case 500...599 where attempt == 0:
                    continue
                default:
                    return .error("External provider failed")
                }
            } catch is DecodingError {
                return .error("Invalid external response")
            } catch {
                if attempt == 0 {
                    continue
                }
                return .error(error.localizedDescription)
            }
        }

        return .error("External provider failed")
    }

    private func parseOwnershipHistoryEvents(from data: Data) -> [DomainOwnershipHistoryEvent] {
        guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
              let rawEvents = json["events"] as? [[String: Any]] else {
            return []
        }

        return rawEvents.compactMap { event in
            guard let dateString = event["date"] as? String,
                  let date = Self.iso8601DateFormatter.date(from: dateString) else {
                return nil
            }

            return DomainOwnershipHistoryEvent(
                date: date,
                summary: event["summary"] as? String ?? "Ownership change observed",
                registrar: event["registrar"] as? String,
                registrant: event["registrant"] as? String,
                nameservers: event["nameservers"] as? [String] ?? [],
                source: event["source"] as? String ?? "Configured external history feed",
                isExternal: true
            )
        }
    }

    private func parseDNSHistoryEvents(from data: Data) -> [DNSHistoryEvent] {
        guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
              let rawEvents = json["events"] as? [[String: Any]] else {
            return []
        }

        return rawEvents.compactMap { event in
            guard let dateString = event["date"] as? String,
                  let date = Self.iso8601DateFormatter.date(from: dateString) else {
                return nil
            }

            return DNSHistoryEvent(
                date: date,
                summary: event["summary"] as? String ?? "DNS change observed",
                aRecords: event["a_records"] as? [String] ?? [],
                nameservers: event["nameservers"] as? [String] ?? [],
                recordSnapshots: parseDNSRecordSnapshots(from: event),
                changedRecordTypes: parseDNSChangedRecordTypes(from: event),
                source: event["source"] as? String ?? "Configured external history feed",
                isExternal: true
            )
        }
    }

    private func parseSubdomains(from data: Data) -> [DiscoveredSubdomain] {
        guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
              let rawSubdomains = json["subdomains"] as? [[String: Any]] else {
            return []
        }

        return rawSubdomains.compactMap { item in
            guard let hostname = item["hostname"] as? String else { return nil }
            return DiscoveredSubdomain(
                hostname: hostname,
                source: item["source"] as? String ?? "Configured external subdomain feed",
                isExtended: true
            )
        }
    }

    private func parsePricing(from data: Data) -> DomainPricingInsight? {
        guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            return nil
        }

        return DomainPricingInsight(
            estimatedPrice: json["estimated_price"] as? String,
            premiumIndicator: json["premium"] as? Bool,
            resaleSignal: json["resale_signal"] as? String,
            auctionSignal: json["auction_signal"] as? String,
            source: json["source"] as? String ?? "Configured external pricing feed",
            collectedAt: Date()
        )
    }

    private func parseReputation(from data: Data) -> DomainReputationResult? {
        guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            return nil
        }

        let listedSources = json["listed_sources"] as? [String] ?? []
        let status: DomainReputationStatus
        if let statusString = json["status"] as? String, let parsed = DomainReputationStatus(rawValue: statusString) {
            status = parsed
        } else {
            status = listedSources.isEmpty ? .clean : .listed
        }

        return DomainReputationResult(status: status, listedSources: listedSources, checkedAt: Date())
    }

    private static func localOwnershipHistory(
        domain: String,
        currentOwnership: DomainOwnership?,
        historyEntries: [HistoryEntry]
    ) -> [DomainOwnershipHistoryEvent] {
        let domainHistory = historyEntries
            .filter { $0.domain.caseInsensitiveCompare(domain) == .orderedSame }
            .sorted { $0.timestamp < $1.timestamp }

        var events: [DomainOwnershipHistoryEvent] = []
        var previousOwnership: DomainOwnership?

        for entry in domainHistory {
            guard let ownership = entry.ownership else { continue }
            let summary = ownershipSummaryChange(previous: previousOwnership, current: ownership)
            if let summary {
                events.append(
                    DomainOwnershipHistoryEvent(
                        date: entry.timestamp,
                        summary: summary,
                        registrar: ownership.registrar,
                        registrant: ownership.registrant,
                        nameservers: ownership.nameservers,
                        source: "Local observations",
                        isExternal: false
                    )
                )
            }
            previousOwnership = ownership
        }

        if let currentOwnership, events.isEmpty {
            events.append(
                DomainOwnershipHistoryEvent(
                    date: Date(),
                    summary: "Current ownership snapshot",
                    registrar: currentOwnership.registrar,
                    registrant: currentOwnership.registrant,
                    nameservers: currentOwnership.nameservers,
                    source: "Local observations",
                    isExternal: false
                )
            )
        }

        return events.sorted { $0.date > $1.date }
    }

    private static func localDNSHistory(
        domain: String,
        dnsSections: [DNSSection],
        historyEntries: [HistoryEntry]
    ) -> [DNSHistoryEvent] {
        let domainHistory = historyEntries
            .filter { $0.domain.caseInsensitiveCompare(domain) == .orderedSame }
            .sorted { $0.timestamp < $1.timestamp }

        var events: [DNSHistoryEvent] = []
        var previousRecordValues: [DNSRecordType: [String]] = [:]

        for entry in domainHistory {
            let currentRecordValues = Self.historyRecordValues(in: entry.dnsSections)
            let changedRecordTypes = Self.changedRecordTypes(previous: previousRecordValues, current: currentRecordValues)
            let summary = dnsSummaryChange(previous: previousRecordValues, current: currentRecordValues)

            if let summary {
                events.append(
                    DNSHistoryEvent(
                        date: entry.timestamp,
                        summary: summary,
                        aRecords: currentRecordValues[.A] ?? [],
                        nameservers: currentRecordValues[.NS] ?? [],
                        recordSnapshots: currentRecordValues.map { DNSHistoryRecordSnapshot(recordType: $0.key, values: $0.value) }
                            .sorted { $0.recordType.rawValue < $1.recordType.rawValue },
                        changedRecordTypes: changedRecordTypes,
                        source: "Local observations",
                        isExternal: false
                    )
                )
            }

            previousRecordValues = currentRecordValues
        }

        if events.isEmpty {
            let currentRecordValues = Self.historyRecordValues(in: dnsSections)
            if !currentRecordValues.isEmpty {
                events.append(
                    DNSHistoryEvent(
                        date: Date(),
                        summary: "Current DNS snapshot",
                        aRecords: currentRecordValues[.A] ?? [],
                        nameservers: currentRecordValues[.NS] ?? [],
                        recordSnapshots: currentRecordValues.map { DNSHistoryRecordSnapshot(recordType: $0.key, values: $0.value) }
                            .sorted { $0.recordType.rawValue < $1.recordType.rawValue },
                        changedRecordTypes: Array(currentRecordValues.keys).sorted { $0.rawValue < $1.rawValue },
                        source: "Local observations",
                        isExternal: false
                    )
                )
            }
        }

        return events.sorted { $0.date > $1.date }
    }

    private static func mergeOwnershipHistory(
        localEvents: [DomainOwnershipHistoryEvent],
        externalEvents: [DomainOwnershipHistoryEvent]
    ) -> ServiceResult<[DomainOwnershipHistoryEvent]> {
        let merged = (externalEvents + localEvents)
            .sorted { $0.date > $1.date }
            .reduce(into: [DomainOwnershipHistoryEvent]()) { partialResult, event in
                let duplicate = partialResult.contains {
                    $0.date == event.date
                        && $0.summary == event.summary
                        && $0.registrar == event.registrar
                        && $0.nameservers == event.nameservers
                }
                if !duplicate {
                    partialResult.append(event)
                }
            }

        return merged.isEmpty ? .empty("No ownership history available") : .success(merged)
    }

    private static func mergeDNSHistory(
        localEvents: [DNSHistoryEvent],
        externalEvents: [DNSHistoryEvent]
    ) -> ServiceResult<[DNSHistoryEvent]> {
        let merged = (externalEvents + localEvents)
            .sorted { $0.date > $1.date }
            .reduce(into: [DNSHistoryEvent]()) { partialResult, event in
                let duplicate = partialResult.contains {
                    $0.date == event.date
                        && $0.summary == event.summary
                        && compareDNSRecordSnapshots($0.recordSnapshots, event.recordSnapshots)
                }
                if !duplicate {
                    partialResult.append(event)
                }
            }

        return merged.isEmpty ? .empty("No DNS history available") : .success(merged)
    }

    private static func mergeSubdomains(
        primary: [DiscoveredSubdomain],
        additional: [DiscoveredSubdomain]
    ) -> [DiscoveredSubdomain] {
        var seen = Set(primary.map { $0.hostname.lowercased() })
        var merged = primary

        for subdomain in additional where seen.insert(subdomain.hostname.lowercased()).inserted {
            merged.append(subdomain)
        }

        return merged.sorted { $0.hostname < $1.hostname }
    }

    private static func ownershipSummaryChange(previous: DomainOwnership?, current: DomainOwnership) -> String? {
        guard let previous else {
            return "Initial ownership observation"
        }

        var changes: [String] = []
        if previous.registrar != current.registrar {
            changes.append("Registrar changed")
        }
        if previous.registrant != current.registrant, current.registrant != nil {
            changes.append("Ownership changed")
        }
        if previous.nameservers != current.nameservers {
            changes.append("Nameservers changed")
        }

        return changes.isEmpty ? nil : changes.joined(separator: " • ")
    }

    private static func dnsSummaryChange(
        previous: [DNSRecordType: [String]],
        current: [DNSRecordType: [String]]
    ) -> String? {
        var changes: [String] = []
        for type in [DNSRecordType.A, .AAAA, .MX, .NS, .TXT, .CNAME] {
            let previousValues = previous[type] ?? []
            let currentValues = current[type] ?? []
            if previousValues != currentValues, !currentValues.isEmpty {
                changes.append("\(type.rawValue) records changed")
            }
        }
        if previous.isEmpty && !current.isEmpty {
            changes.append("Initial DNS observation")
        }
        return changes.isEmpty ? nil : changes.joined(separator: " • ")
    }

    private static func url(from template: String, domain: String) -> URL? {
        URL(string: template.replacingOccurrences(of: "{domain}", with: domain))
    }

    private static func normalize(_ domain: String) -> String {
        domain.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
    }

    private static func dnsValues(for type: DNSRecordType, in sections: [DNSSection]) -> [String] {
        sections
            .first(where: { $0.recordType == type })?
            .records
            .map(\.value)
            .sorted() ?? []
    }

    private func parseDNSRecordSnapshots(from event: [String: Any]) -> [DNSHistoryRecordSnapshot] {
        if let snapshots = event["record_snapshots"] as? [[String: Any]] {
            return snapshots.compactMap { item in
                guard let typeName = item["type"] as? String,
                      let type = DNSRecordType(rawValue: typeName) else {
                    return nil
                }
                return DNSHistoryRecordSnapshot(recordType: type, values: item["values"] as? [String] ?? [])
            }
        }
        var snapshots: [DNSHistoryRecordSnapshot] = []
        if let aRecords = event["a_records"] as? [String], !aRecords.isEmpty {
            snapshots.append(DNSHistoryRecordSnapshot(recordType: .A, values: aRecords))
        }
        if let nameservers = event["nameservers"] as? [String], !nameservers.isEmpty {
            snapshots.append(DNSHistoryRecordSnapshot(recordType: .NS, values: nameservers))
        }
        return snapshots
    }

    private func parseDNSChangedRecordTypes(from event: [String: Any]) -> [DNSRecordType] {
        if let rawTypes = event["changed_record_types"] as? [String] {
            return rawTypes.compactMap(DNSRecordType.init(rawValue:))
        }
        return parseDNSRecordSnapshots(from: event).map(\.recordType)
    }

    private static func historyRecordValues(in sections: [DNSSection]) -> [DNSRecordType: [String]] {
        let trackedTypes: [DNSRecordType] = [.A, .AAAA, .MX, .NS, .TXT, .CNAME]
        return trackedTypes.reduce(into: [DNSRecordType: [String]]()) { result, type in
            let values = dnsValues(for: type, in: sections)
            if !values.isEmpty {
                result[type] = values
            }
        }
    }

    private static func changedRecordTypes(
        previous: [DNSRecordType: [String]],
        current: [DNSRecordType: [String]]
    ) -> [DNSRecordType] {
        Array(Set(previous.keys).union(current.keys))
            .filter { previous[$0] != current[$0] }
            .sorted { $0.rawValue < $1.rawValue }
    }

    private static func compareDNSRecordSnapshots(
        _ lhs: [DNSHistoryRecordSnapshot],
        _ rhs: [DNSHistoryRecordSnapshot]
    ) -> Bool {
        guard lhs.count == rhs.count else { return false }
        return zip(lhs, rhs).allSatisfy { left, right in
            left.recordType == right.recordType && left.values == right.values
        }
    }

    private static let iso8601DateFormatter: ISO8601DateFormatter = {
        let formatter = ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
        return formatter
    }()
}