DomainDig/Models.swift
645 lines · 20610 bytes
1import Foundation
2
3enum ServiceResult<Value> {
4 case success(Value)
5 case empty(String)
6 case error(String)
7}
8
9enum DomainAvailabilityStatus: String, Codable {
10 case available
11 case registered
12 case unknown
13}
14
15struct DomainAvailabilityResult: Codable {
16 let domain: String
17 let status: DomainAvailabilityStatus
18}
19
20struct DomainSuggestionResult: Identifiable, Codable {
21 let id: UUID
22 let domain: String
23 let status: DomainAvailabilityStatus
24
25 init(id: UUID = UUID(), domain: String, status: DomainAvailabilityStatus) {
26 self.id = id
27 self.domain = domain
28 self.status = status
29 }
30}
31
32struct WatchedDomain: Codable, Identifiable {
33 let id: UUID
34 let domain: String
35 let createdAt: Date
36 var lastKnownAvailability: DomainAvailabilityStatus?
37
38 init(
39 id: UUID = UUID(),
40 domain: String,
41 createdAt: Date = Date(),
42 lastKnownAvailability: DomainAvailabilityStatus? = nil
43 ) {
44 self.id = id
45 self.domain = domain
46 self.createdAt = createdAt
47 self.lastKnownAvailability = lastKnownAvailability
48 }
49}
50
51struct DomainChangeSummary: Codable, Equatable {
52 let hasChanges: Bool
53 let changedSections: [String]
54 let generatedAt: Date
55}
56
57enum BatchLookupSource: String, Codable {
58 case manual
59 case watchlistRefresh
60}
61
62enum BatchLookupStatus: String, Codable {
63 case pending
64 case running
65 case completed
66 case failed
67}
68
69struct BatchLookupResult: Identifiable, Codable, Equatable {
70 let id: UUID
71 let domain: String
72 let historyEntryID: UUID?
73 let availability: DomainAvailabilityStatus?
74 let primaryIP: String?
75 let quickStatus: String
76 let timestamp: Date
77 let status: BatchLookupStatus
78 let errorMessage: String?
79
80 init(
81 id: UUID = UUID(),
82 domain: String,
83 historyEntryID: UUID?,
84 availability: DomainAvailabilityStatus?,
85 primaryIP: String?,
86 quickStatus: String,
87 timestamp: Date,
88 status: BatchLookupStatus,
89 errorMessage: String? = nil
90 ) {
91 self.id = id
92 self.domain = domain
93 self.historyEntryID = historyEntryID
94 self.availability = availability
95 self.primaryIP = primaryIP
96 self.quickStatus = quickStatus
97 self.timestamp = timestamp
98 self.status = status
99 self.errorMessage = errorMessage
100 }
101}
102
103enum HistoryDateFilter: String, CaseIterable, Identifiable {
104 case today
105 case last7Days
106 case all
107
108 var id: String { rawValue }
109
110 var title: String {
111 switch self {
112 case .today:
113 return "Today"
114 case .last7Days:
115 return "Last 7 Days"
116 case .all:
117 return "All"
118 }
119 }
120}
121
122enum ChangeFilterOption: String, CaseIterable, Identifiable {
123 case all
124 case changed
125 case unchanged
126
127 var id: String { rawValue }
128
129 var title: String {
130 switch self {
131 case .all:
132 return "All"
133 case .changed:
134 return "Changed"
135 case .unchanged:
136 return "Unchanged"
137 }
138 }
139}
140
141enum HistorySortOption: String, CaseIterable, Identifiable {
142 case newest
143 case oldest
144 case domain
145
146 var id: String { rawValue }
147
148 var title: String {
149 switch self {
150 case .newest:
151 return "Newest"
152 case .oldest:
153 return "Oldest"
154 case .domain:
155 return "Domain A-Z"
156 }
157 }
158}
159
160enum WatchlistFilterOption: String, CaseIterable, Identifiable {
161 case all
162 case pinnedOnly
163 case changedOnly
164
165 var id: String { rawValue }
166
167 var title: String {
168 switch self {
169 case .all:
170 return "All"
171 case .pinnedOnly:
172 return "Pinned Only"
173 case .changedOnly:
174 return "Changed Only"
175 }
176 }
177}
178
179enum WatchlistSortOption: String, CaseIterable, Identifiable {
180 case pinned
181 case recentlyUpdated
182 case alphabetical
183
184 var id: String { rawValue }
185
186 var title: String {
187 switch self {
188 case .pinned:
189 return "Pinned"
190 case .recentlyUpdated:
191 return "Recently Updated"
192 case .alphabetical:
193 return "Alphabetical"
194 }
195 }
196}
197
198enum PremiumCapability: String, Codable {
199 case unlimitedTrackedDomains
200 case automatedMonitoring
201 case pushAlerts
202 case batchTracking
203 case advancedExports
204}
205
206struct TrackedDomain: Codable, Identifiable, Equatable {
207 let id: UUID
208 var domain: String
209 var createdAt: Date
210 var updatedAt: Date
211 var note: String?
212 var isPinned: Bool
213 var lastKnownAvailability: DomainAvailabilityStatus?
214 var lastSnapshotID: UUID?
215 var lastChangeSummary: DomainChangeSummary?
216
217 init(
218 id: UUID = UUID(),
219 domain: String,
220 createdAt: Date = Date(),
221 updatedAt: Date = Date(),
222 note: String? = nil,
223 isPinned: Bool = false,
224 lastKnownAvailability: DomainAvailabilityStatus? = nil,
225 lastSnapshotID: UUID? = nil,
226 lastChangeSummary: DomainChangeSummary? = nil
227 ) {
228 self.id = id
229 self.domain = domain
230 self.createdAt = createdAt
231 self.updatedAt = updatedAt
232 self.note = note
233 self.isPinned = isPinned
234 self.lastKnownAvailability = lastKnownAvailability
235 self.lastSnapshotID = lastSnapshotID
236 self.lastChangeSummary = lastChangeSummary
237 }
238}
239
240// MARK: - DNS Models
241
242enum DNSRecordType: String, CaseIterable, Codable {
243 case A
244 case AAAA
245 case MX
246 case NS
247 case TXT
248 case CNAME
249 case SOA
250 case SRV
251 case CAA
252 case DS
253 case PTR
254
255 var queryType: Int {
256 switch self {
257 case .A: return 1
258 case .AAAA: return 28
259 case .MX: return 15
260 case .NS: return 2
261 case .TXT: return 16
262 case .CNAME: return 5
263 case .SOA: return 6
264 case .SRV: return 33
265 case .CAA: return 257
266 case .DS: return 43
267 case .PTR: return 12
268 }
269 }
270
271 var usesRawDataValue: Bool {
272 switch self {
273 case .TXT, .SOA, .DS:
274 return true
275 default:
276 return false
277 }
278 }
279}
280
281struct DNSRecord: Identifiable, Codable {
282 var id = UUID()
283 let value: String
284 let ttl: Int
285}
286
287struct DNSSection: Identifiable, Codable {
288 var id = UUID()
289 let recordType: DNSRecordType
290 var records: [DNSRecord]
291 var wildcardRecords: [DNSRecord] = []
292 var dnssecSigned: Bool?
293 var error: String?
294}
295
296// MARK: - SSL Models
297
298struct SSLCertificateInfo: Codable {
299 struct CertChainEntry: Codable {
300 let subject: String
301 let issuer: String
302 }
303
304 let commonName: String
305 let subjectAltNames: [String]
306 let issuer: String
307 let validFrom: Date
308 let validUntil: Date
309 let daysUntilExpiry: Int
310 let chainDepth: Int
311 let tlsVersion: String?
312 let cipherSuite: String?
313 let chain: [CertChainEntry]
314
315 init(
316 commonName: String,
317 subjectAltNames: [String],
318 issuer: String,
319 validFrom: Date,
320 validUntil: Date,
321 daysUntilExpiry: Int,
322 chainDepth: Int,
323 tlsVersion: String? = nil,
324 cipherSuite: String? = nil,
325 chain: [CertChainEntry] = []
326 ) {
327 self.commonName = commonName
328 self.subjectAltNames = subjectAltNames
329 self.issuer = issuer
330 self.validFrom = validFrom
331 self.validUntil = validUntil
332 self.daysUntilExpiry = daysUntilExpiry
333 self.chainDepth = chainDepth
334 self.tlsVersion = tlsVersion
335 self.cipherSuite = cipherSuite
336 self.chain = chain
337 }
338
339 init(from decoder: Decoder) throws {
340 let container = try decoder.container(keyedBy: CodingKeys.self)
341 commonName = try container.decode(String.self, forKey: .commonName)
342 subjectAltNames = try container.decode([String].self, forKey: .subjectAltNames)
343 issuer = try container.decode(String.self, forKey: .issuer)
344 validFrom = try container.decode(Date.self, forKey: .validFrom)
345 validUntil = try container.decode(Date.self, forKey: .validUntil)
346 daysUntilExpiry = try container.decode(Int.self, forKey: .daysUntilExpiry)
347 chainDepth = try container.decode(Int.self, forKey: .chainDepth)
348 tlsVersion = try container.decodeIfPresent(String.self, forKey: .tlsVersion)
349 cipherSuite = try container.decodeIfPresent(String.self, forKey: .cipherSuite)
350 chain = try container.decodeIfPresent([CertChainEntry].self, forKey: .chain) ?? []
351 }
352}
353
354// MARK: - HTTP Headers Models
355
356struct HTTPHeader: Identifiable, Codable {
357 var id = UUID()
358 let name: String
359 let value: String
360
361 static let securityHeaders: Set<String> = [
362 "strict-transport-security",
363 "x-frame-options",
364 "x-content-type-options",
365 "content-security-policy",
366 "referrer-policy"
367 ]
368
369 var isSecurityHeader: Bool {
370 Self.securityHeaders.contains(name.lowercased())
371 }
372}
373
374// MARK: - Reachability Models
375
376struct PortReachability: Identifiable, Codable {
377 var id = UUID()
378 let port: UInt16
379 let reachable: Bool
380 let latencyMs: Int?
381}
382
383// MARK: - IP Geolocation Models
384
385struct IPGeolocation: Codable {
386 let ip: String
387 let city: String?
388 let region: String?
389 let country_name: String?
390 let org: String?
391 let latitude: Double?
392 let longitude: Double?
393}
394
395// MARK: - Email Security Models
396
397struct EmailSecurityResult: Codable {
398 let spf: EmailSecurityRecord
399 let dmarc: EmailSecurityRecord
400 let dkim: EmailSecurityRecord
401 let bimi: EmailSecurityRecord
402 let mtaSts: MTASTSResult?
403
404 init(
405 spf: EmailSecurityRecord,
406 dmarc: EmailSecurityRecord,
407 dkim: EmailSecurityRecord,
408 bimi: EmailSecurityRecord = EmailSecurityRecord(found: false, value: nil),
409 mtaSts: MTASTSResult? = nil
410 ) {
411 self.spf = spf
412 self.dmarc = dmarc
413 self.dkim = dkim
414 self.bimi = bimi
415 self.mtaSts = mtaSts
416 }
417
418 init(from decoder: Decoder) throws {
419 let container = try decoder.container(keyedBy: CodingKeys.self)
420 spf = try container.decode(EmailSecurityRecord.self, forKey: .spf)
421 dmarc = try container.decode(EmailSecurityRecord.self, forKey: .dmarc)
422 dkim = try container.decode(EmailSecurityRecord.self, forKey: .dkim)
423 bimi = try container.decodeIfPresent(EmailSecurityRecord.self, forKey: .bimi)
424 ?? EmailSecurityRecord(found: false, value: nil)
425 mtaSts = try container.decodeIfPresent(MTASTSResult.self, forKey: .mtaSts)
426 }
427}
428
429struct EmailSecurityRecord: Codable {
430 let found: Bool
431 let value: String?
432 let matchedSelector: String?
433
434 init(found: Bool, value: String?, matchedSelector: String? = nil) {
435 self.found = found
436 self.value = value
437 self.matchedSelector = matchedSelector
438 }
439
440 init(from decoder: Decoder) throws {
441 let container = try decoder.container(keyedBy: CodingKeys.self)
442 found = try container.decode(Bool.self, forKey: .found)
443 value = try container.decodeIfPresent(String.self, forKey: .value)
444 matchedSelector = try container.decodeIfPresent(String.self, forKey: .matchedSelector)
445 }
446}
447
448struct MTASTSResult: Codable {
449 let txtFound: Bool
450 let policyMode: String?
451}
452
453// MARK: - Redirect Chain Models
454
455struct RedirectHop: Identifiable, Codable {
456 var id = UUID()
457 let stepNumber: Int
458 let statusCode: Int
459 let url: String
460 let isFinal: Bool
461}
462
463// MARK: - Port Scan Models
464
465enum PortScanKind: String, Codable {
466 case standard
467 case custom
468}
469
470struct PortScanResult: Identifiable, Codable {
471 var id = UUID()
472 let port: UInt16
473 let service: String
474 let open: Bool
475 var banner: String?
476 let kind: PortScanKind
477 let durationMs: Int?
478
479 nonisolated init(
480 port: UInt16,
481 service: String,
482 open: Bool,
483 banner: String? = nil,
484 kind: PortScanKind = .standard,
485 durationMs: Int? = nil
486 ) {
487 self.port = port
488 self.service = service
489 self.open = open
490 self.banner = banner
491 self.kind = kind
492 self.durationMs = durationMs
493 }
494
495 init(from decoder: Decoder) throws {
496 let container = try decoder.container(keyedBy: CodingKeys.self)
497 id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
498 port = try container.decode(UInt16.self, forKey: .port)
499 service = try container.decode(String.self, forKey: .service)
500 open = try container.decode(Bool.self, forKey: .open)
501 banner = try container.decodeIfPresent(String.self, forKey: .banner)
502 kind = try container.decodeIfPresent(PortScanKind.self, forKey: .kind) ?? .standard
503 durationMs = try container.decodeIfPresent(Int.self, forKey: .durationMs)
504 }
505}
506
507// MARK: - History Models
508
509struct HistoryEntry: Identifiable, Codable {
510 var id = UUID()
511 let domain: String
512 let timestamp: Date
513 var trackedDomainID: UUID?
514 let dnsSections: [DNSSection]
515 let sslInfo: SSLCertificateInfo?
516 let httpHeaders: [HTTPHeader]
517 let reachabilityResults: [PortReachability]
518 let ipGeolocation: IPGeolocation?
519 var emailSecurity: EmailSecurityResult?
520 var mtaSts: MTASTSResult?
521 var ptrRecord: String?
522 var redirectChain: [RedirectHop]
523 var portScanResults: [PortScanResult]
524 var hstsPreloaded: Bool?
525 var availabilityResult: DomainAvailabilityResult?
526 var suggestions: [DomainSuggestionResult]
527 var resolverDisplayName: String
528 var resolverURLString: String
529 var totalLookupDurationMs: Int?
530 var primaryIP: String?
531 var finalRedirectURL: String?
532 var tlsStatusSummary: String?
533 var emailSecuritySummary: String?
534 var httpGradeSummary: String?
535 var changeSummary: DomainChangeSummary?
536 var sslError: String?
537 var httpHeadersError: String?
538 var reachabilityError: String?
539 var ipGeolocationError: String?
540 var emailSecurityError: String?
541 var ptrError: String?
542 var redirectChainError: String?
543 var portScanError: String?
544
545 init(domain: String, timestamp: Date, trackedDomainID: UUID? = nil, dnsSections: [DNSSection],
546 sslInfo: SSLCertificateInfo?, httpHeaders: [HTTPHeader],
547 reachabilityResults: [PortReachability], ipGeolocation: IPGeolocation?,
548 emailSecurity: EmailSecurityResult? = nil, mtaSts: MTASTSResult? = nil, ptrRecord: String? = nil,
549 redirectChain: [RedirectHop] = [], portScanResults: [PortScanResult] = [],
550 hstsPreloaded: Bool? = nil, availabilityResult: DomainAvailabilityResult? = nil,
551 suggestions: [DomainSuggestionResult] = [], resolverDisplayName: String, resolverURLString: String,
552 totalLookupDurationMs: Int? = nil, primaryIP: String? = nil, finalRedirectURL: String? = nil,
553 tlsStatusSummary: String? = nil, emailSecuritySummary: String? = nil, httpGradeSummary: String? = nil,
554 changeSummary: DomainChangeSummary? = nil, sslError: String? = nil, httpHeadersError: String? = nil,
555 reachabilityError: String? = nil, ipGeolocationError: String? = nil,
556 emailSecurityError: String? = nil, ptrError: String? = nil,
557 redirectChainError: String? = nil, portScanError: String? = nil) {
558 self.domain = domain
559 self.timestamp = timestamp
560 self.trackedDomainID = trackedDomainID
561 self.dnsSections = dnsSections
562 self.sslInfo = sslInfo
563 self.httpHeaders = httpHeaders
564 self.reachabilityResults = reachabilityResults
565 self.ipGeolocation = ipGeolocation
566 self.emailSecurity = emailSecurity
567 self.mtaSts = mtaSts ?? emailSecurity?.mtaSts
568 self.ptrRecord = ptrRecord
569 self.redirectChain = redirectChain
570 self.portScanResults = portScanResults
571 self.hstsPreloaded = hstsPreloaded
572 self.availabilityResult = availabilityResult
573 self.suggestions = suggestions
574 self.resolverDisplayName = resolverDisplayName
575 self.resolverURLString = resolverURLString
576 self.totalLookupDurationMs = totalLookupDurationMs
577 self.primaryIP = primaryIP
578 self.finalRedirectURL = finalRedirectURL
579 self.tlsStatusSummary = tlsStatusSummary
580 self.emailSecuritySummary = emailSecuritySummary
581 self.httpGradeSummary = httpGradeSummary
582 self.changeSummary = changeSummary
583 self.sslError = sslError
584 self.httpHeadersError = httpHeadersError
585 self.reachabilityError = reachabilityError
586 self.ipGeolocationError = ipGeolocationError
587 self.emailSecurityError = emailSecurityError
588 self.ptrError = ptrError
589 self.redirectChainError = redirectChainError
590 self.portScanError = portScanError
591 }
592
593 init(from decoder: Decoder) throws {
594 let container = try decoder.container(keyedBy: CodingKeys.self)
595 id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
596 domain = try container.decode(String.self, forKey: .domain)
597 timestamp = try container.decode(Date.self, forKey: .timestamp)
598 trackedDomainID = try container.decodeIfPresent(UUID.self, forKey: .trackedDomainID)
599 dnsSections = try container.decode([DNSSection].self, forKey: .dnsSections)
600 sslInfo = try container.decodeIfPresent(SSLCertificateInfo.self, forKey: .sslInfo)
601 httpHeaders = try container.decode([HTTPHeader].self, forKey: .httpHeaders)
602 reachabilityResults = try container.decode([PortReachability].self, forKey: .reachabilityResults)
603 ipGeolocation = try container.decodeIfPresent(IPGeolocation.self, forKey: .ipGeolocation)
604 emailSecurity = try container.decodeIfPresent(EmailSecurityResult.self, forKey: .emailSecurity)
605 mtaSts = try container.decodeIfPresent(MTASTSResult.self, forKey: .mtaSts) ?? emailSecurity?.mtaSts
606 ptrRecord = try container.decodeIfPresent(String.self, forKey: .ptrRecord)
607 redirectChain = try container.decodeIfPresent([RedirectHop].self, forKey: .redirectChain) ?? []
608 portScanResults = try container.decodeIfPresent([PortScanResult].self, forKey: .portScanResults) ?? []
609 hstsPreloaded = try container.decodeIfPresent(Bool.self, forKey: .hstsPreloaded)
610 availabilityResult = try container.decodeIfPresent(DomainAvailabilityResult.self, forKey: .availabilityResult)
611 suggestions = try container.decodeIfPresent([DomainSuggestionResult].self, forKey: .suggestions) ?? []
612 resolverDisplayName = try container.decodeIfPresent(String.self, forKey: .resolverDisplayName) ?? "Cloudflare"
613 resolverURLString = try container.decodeIfPresent(String.self, forKey: .resolverURLString) ?? DNSResolverOption.defaultURLString
614 totalLookupDurationMs = try container.decodeIfPresent(Int.self, forKey: .totalLookupDurationMs)
615 primaryIP = try container.decodeIfPresent(String.self, forKey: .primaryIP)
616 finalRedirectURL = try container.decodeIfPresent(String.self, forKey: .finalRedirectURL)
617 tlsStatusSummary = try container.decodeIfPresent(String.self, forKey: .tlsStatusSummary)
618 emailSecuritySummary = try container.decodeIfPresent(String.self, forKey: .emailSecuritySummary)
619 httpGradeSummary = try container.decodeIfPresent(String.self, forKey: .httpGradeSummary)
620 changeSummary = try container.decodeIfPresent(DomainChangeSummary.self, forKey: .changeSummary)
621 sslError = try container.decodeIfPresent(String.self, forKey: .sslError)
622 httpHeadersError = try container.decodeIfPresent(String.self, forKey: .httpHeadersError)
623 reachabilityError = try container.decodeIfPresent(String.self, forKey: .reachabilityError)
624 ipGeolocationError = try container.decodeIfPresent(String.self, forKey: .ipGeolocationError)
625 emailSecurityError = try container.decodeIfPresent(String.self, forKey: .emailSecurityError)
626 ptrError = try container.decodeIfPresent(String.self, forKey: .ptrError)
627 redirectChainError = try container.decodeIfPresent(String.self, forKey: .redirectChainError)
628 portScanError = try container.decodeIfPresent(String.self, forKey: .portScanError)
629 }
630}
631
632// MARK: - Cloudflare DNS-over-HTTPS Response
633
634struct CloudflareDNSResponse: Decodable {
635 let Status: Int
636 let AD: Bool?
637 let Answer: [CloudflareDNSAnswer]?
638
639 struct CloudflareDNSAnswer: Decodable {
640 let name: String
641 let type: Int
642 let TTL: Int
643 let data: String
644 }
645}