DomainDig/Models.swift
433 lines · 14758 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
51// MARK: - DNS Models
52
53enum DNSRecordType: String, CaseIterable, Codable {
54 case A
55 case AAAA
56 case MX
57 case NS
58 case TXT
59 case CNAME
60 case SOA
61 case SRV
62 case CAA
63 case DS
64 case PTR
65
66 var queryType: Int {
67 switch self {
68 case .A: return 1
69 case .AAAA: return 28
70 case .MX: return 15
71 case .NS: return 2
72 case .TXT: return 16
73 case .CNAME: return 5
74 case .SOA: return 6
75 case .SRV: return 33
76 case .CAA: return 257
77 case .DS: return 43
78 case .PTR: return 12
79 }
80 }
81
82 var usesRawDataValue: Bool {
83 switch self {
84 case .TXT, .SOA, .DS:
85 return true
86 default:
87 return false
88 }
89 }
90}
91
92struct DNSRecord: Identifiable, Codable {
93 var id = UUID()
94 let value: String
95 let ttl: Int
96}
97
98struct DNSSection: Identifiable, Codable {
99 var id = UUID()
100 let recordType: DNSRecordType
101 var records: [DNSRecord]
102 var wildcardRecords: [DNSRecord] = []
103 var dnssecSigned: Bool?
104 var error: String?
105}
106
107// MARK: - SSL Models
108
109struct SSLCertificateInfo: Codable {
110 struct CertChainEntry: Codable {
111 let subject: String
112 let issuer: String
113 }
114
115 let commonName: String
116 let subjectAltNames: [String]
117 let issuer: String
118 let validFrom: Date
119 let validUntil: Date
120 let daysUntilExpiry: Int
121 let chainDepth: Int
122 let tlsVersion: String?
123 let cipherSuite: String?
124 let chain: [CertChainEntry]
125
126 init(
127 commonName: String,
128 subjectAltNames: [String],
129 issuer: String,
130 validFrom: Date,
131 validUntil: Date,
132 daysUntilExpiry: Int,
133 chainDepth: Int,
134 tlsVersion: String? = nil,
135 cipherSuite: String? = nil,
136 chain: [CertChainEntry] = []
137 ) {
138 self.commonName = commonName
139 self.subjectAltNames = subjectAltNames
140 self.issuer = issuer
141 self.validFrom = validFrom
142 self.validUntil = validUntil
143 self.daysUntilExpiry = daysUntilExpiry
144 self.chainDepth = chainDepth
145 self.tlsVersion = tlsVersion
146 self.cipherSuite = cipherSuite
147 self.chain = chain
148 }
149
150 init(from decoder: Decoder) throws {
151 let container = try decoder.container(keyedBy: CodingKeys.self)
152 commonName = try container.decode(String.self, forKey: .commonName)
153 subjectAltNames = try container.decode([String].self, forKey: .subjectAltNames)
154 issuer = try container.decode(String.self, forKey: .issuer)
155 validFrom = try container.decode(Date.self, forKey: .validFrom)
156 validUntil = try container.decode(Date.self, forKey: .validUntil)
157 daysUntilExpiry = try container.decode(Int.self, forKey: .daysUntilExpiry)
158 chainDepth = try container.decode(Int.self, forKey: .chainDepth)
159 tlsVersion = try container.decodeIfPresent(String.self, forKey: .tlsVersion)
160 cipherSuite = try container.decodeIfPresent(String.self, forKey: .cipherSuite)
161 chain = try container.decodeIfPresent([CertChainEntry].self, forKey: .chain) ?? []
162 }
163}
164
165// MARK: - HTTP Headers Models
166
167struct HTTPHeader: Identifiable, Codable {
168 var id = UUID()
169 let name: String
170 let value: String
171
172 static let securityHeaders: Set<String> = [
173 "strict-transport-security",
174 "x-frame-options",
175 "x-content-type-options",
176 "content-security-policy",
177 "referrer-policy"
178 ]
179
180 var isSecurityHeader: Bool {
181 Self.securityHeaders.contains(name.lowercased())
182 }
183}
184
185// MARK: - Reachability Models
186
187struct PortReachability: Identifiable, Codable {
188 var id = UUID()
189 let port: UInt16
190 let reachable: Bool
191 let latencyMs: Int?
192}
193
194// MARK: - IP Geolocation Models
195
196struct IPGeolocation: Codable {
197 let ip: String
198 let city: String?
199 let region: String?
200 let country_name: String?
201 let org: String?
202 let latitude: Double?
203 let longitude: Double?
204}
205
206// MARK: - Email Security Models
207
208struct EmailSecurityResult: Codable {
209 let spf: EmailSecurityRecord
210 let dmarc: EmailSecurityRecord
211 let dkim: EmailSecurityRecord
212 let bimi: EmailSecurityRecord
213 let mtaSts: MTASTSResult?
214
215 init(
216 spf: EmailSecurityRecord,
217 dmarc: EmailSecurityRecord,
218 dkim: EmailSecurityRecord,
219 bimi: EmailSecurityRecord = EmailSecurityRecord(found: false, value: nil),
220 mtaSts: MTASTSResult? = nil
221 ) {
222 self.spf = spf
223 self.dmarc = dmarc
224 self.dkim = dkim
225 self.bimi = bimi
226 self.mtaSts = mtaSts
227 }
228
229 init(from decoder: Decoder) throws {
230 let container = try decoder.container(keyedBy: CodingKeys.self)
231 spf = try container.decode(EmailSecurityRecord.self, forKey: .spf)
232 dmarc = try container.decode(EmailSecurityRecord.self, forKey: .dmarc)
233 dkim = try container.decode(EmailSecurityRecord.self, forKey: .dkim)
234 bimi = try container.decodeIfPresent(EmailSecurityRecord.self, forKey: .bimi)
235 ?? EmailSecurityRecord(found: false, value: nil)
236 mtaSts = try container.decodeIfPresent(MTASTSResult.self, forKey: .mtaSts)
237 }
238}
239
240struct EmailSecurityRecord: Codable {
241 let found: Bool
242 let value: String?
243 let matchedSelector: String?
244
245 init(found: Bool, value: String?, matchedSelector: String? = nil) {
246 self.found = found
247 self.value = value
248 self.matchedSelector = matchedSelector
249 }
250
251 init(from decoder: Decoder) throws {
252 let container = try decoder.container(keyedBy: CodingKeys.self)
253 found = try container.decode(Bool.self, forKey: .found)
254 value = try container.decodeIfPresent(String.self, forKey: .value)
255 matchedSelector = try container.decodeIfPresent(String.self, forKey: .matchedSelector)
256 }
257}
258
259struct MTASTSResult: Codable {
260 let txtFound: Bool
261 let policyMode: String?
262}
263
264// MARK: - Redirect Chain Models
265
266struct RedirectHop: Identifiable, Codable {
267 var id = UUID()
268 let stepNumber: Int
269 let statusCode: Int
270 let url: String
271 let isFinal: Bool
272}
273
274// MARK: - Port Scan Models
275
276enum PortScanKind: String, Codable {
277 case standard
278 case custom
279}
280
281struct PortScanResult: Identifiable, Codable {
282 var id = UUID()
283 let port: UInt16
284 let service: String
285 let open: Bool
286 var banner: String?
287 let kind: PortScanKind
288 let durationMs: Int?
289
290 nonisolated init(
291 port: UInt16,
292 service: String,
293 open: Bool,
294 banner: String? = nil,
295 kind: PortScanKind = .standard,
296 durationMs: Int? = nil
297 ) {
298 self.port = port
299 self.service = service
300 self.open = open
301 self.banner = banner
302 self.kind = kind
303 self.durationMs = durationMs
304 }
305
306 init(from decoder: Decoder) throws {
307 let container = try decoder.container(keyedBy: CodingKeys.self)
308 id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
309 port = try container.decode(UInt16.self, forKey: .port)
310 service = try container.decode(String.self, forKey: .service)
311 open = try container.decode(Bool.self, forKey: .open)
312 banner = try container.decodeIfPresent(String.self, forKey: .banner)
313 kind = try container.decodeIfPresent(PortScanKind.self, forKey: .kind) ?? .standard
314 durationMs = try container.decodeIfPresent(Int.self, forKey: .durationMs)
315 }
316}
317
318// MARK: - History Models
319
320struct HistoryEntry: Identifiable, Codable {
321 var id = UUID()
322 let domain: String
323 let timestamp: Date
324 let dnsSections: [DNSSection]
325 let sslInfo: SSLCertificateInfo?
326 let httpHeaders: [HTTPHeader]
327 let reachabilityResults: [PortReachability]
328 let ipGeolocation: IPGeolocation?
329 var emailSecurity: EmailSecurityResult?
330 var mtaSts: MTASTSResult?
331 var ptrRecord: String?
332 var redirectChain: [RedirectHop]
333 var portScanResults: [PortScanResult]
334 var hstsPreloaded: Bool?
335 var availabilityResult: DomainAvailabilityResult?
336 var suggestions: [DomainSuggestionResult]
337 var resolverDisplayName: String
338 var resolverURLString: String
339 var totalLookupDurationMs: Int?
340 var sslError: String?
341 var httpHeadersError: String?
342 var reachabilityError: String?
343 var ipGeolocationError: String?
344 var emailSecurityError: String?
345 var ptrError: String?
346 var redirectChainError: String?
347 var portScanError: String?
348
349 init(domain: String, timestamp: Date, dnsSections: [DNSSection],
350 sslInfo: SSLCertificateInfo?, httpHeaders: [HTTPHeader],
351 reachabilityResults: [PortReachability], ipGeolocation: IPGeolocation?,
352 emailSecurity: EmailSecurityResult? = nil, mtaSts: MTASTSResult? = nil, ptrRecord: String? = nil,
353 redirectChain: [RedirectHop] = [], portScanResults: [PortScanResult] = [],
354 hstsPreloaded: Bool? = nil, availabilityResult: DomainAvailabilityResult? = nil,
355 suggestions: [DomainSuggestionResult] = [], resolverDisplayName: String, resolverURLString: String,
356 totalLookupDurationMs: Int? = nil, sslError: String? = nil, httpHeadersError: String? = nil,
357 reachabilityError: String? = nil, ipGeolocationError: String? = nil,
358 emailSecurityError: String? = nil, ptrError: String? = nil,
359 redirectChainError: String? = nil, portScanError: String? = nil) {
360 self.domain = domain
361 self.timestamp = timestamp
362 self.dnsSections = dnsSections
363 self.sslInfo = sslInfo
364 self.httpHeaders = httpHeaders
365 self.reachabilityResults = reachabilityResults
366 self.ipGeolocation = ipGeolocation
367 self.emailSecurity = emailSecurity
368 self.mtaSts = mtaSts ?? emailSecurity?.mtaSts
369 self.ptrRecord = ptrRecord
370 self.redirectChain = redirectChain
371 self.portScanResults = portScanResults
372 self.hstsPreloaded = hstsPreloaded
373 self.availabilityResult = availabilityResult
374 self.suggestions = suggestions
375 self.resolverDisplayName = resolverDisplayName
376 self.resolverURLString = resolverURLString
377 self.totalLookupDurationMs = totalLookupDurationMs
378 self.sslError = sslError
379 self.httpHeadersError = httpHeadersError
380 self.reachabilityError = reachabilityError
381 self.ipGeolocationError = ipGeolocationError
382 self.emailSecurityError = emailSecurityError
383 self.ptrError = ptrError
384 self.redirectChainError = redirectChainError
385 self.portScanError = portScanError
386 }
387
388 init(from decoder: Decoder) throws {
389 let container = try decoder.container(keyedBy: CodingKeys.self)
390 id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
391 domain = try container.decode(String.self, forKey: .domain)
392 timestamp = try container.decode(Date.self, forKey: .timestamp)
393 dnsSections = try container.decode([DNSSection].self, forKey: .dnsSections)
394 sslInfo = try container.decodeIfPresent(SSLCertificateInfo.self, forKey: .sslInfo)
395 httpHeaders = try container.decode([HTTPHeader].self, forKey: .httpHeaders)
396 reachabilityResults = try container.decode([PortReachability].self, forKey: .reachabilityResults)
397 ipGeolocation = try container.decodeIfPresent(IPGeolocation.self, forKey: .ipGeolocation)
398 emailSecurity = try container.decodeIfPresent(EmailSecurityResult.self, forKey: .emailSecurity)
399 mtaSts = try container.decodeIfPresent(MTASTSResult.self, forKey: .mtaSts) ?? emailSecurity?.mtaSts
400 ptrRecord = try container.decodeIfPresent(String.self, forKey: .ptrRecord)
401 redirectChain = try container.decodeIfPresent([RedirectHop].self, forKey: .redirectChain) ?? []
402 portScanResults = try container.decodeIfPresent([PortScanResult].self, forKey: .portScanResults) ?? []
403 hstsPreloaded = try container.decodeIfPresent(Bool.self, forKey: .hstsPreloaded)
404 availabilityResult = try container.decodeIfPresent(DomainAvailabilityResult.self, forKey: .availabilityResult)
405 suggestions = try container.decodeIfPresent([DomainSuggestionResult].self, forKey: .suggestions) ?? []
406 resolverDisplayName = try container.decodeIfPresent(String.self, forKey: .resolverDisplayName) ?? "Cloudflare"
407 resolverURLString = try container.decodeIfPresent(String.self, forKey: .resolverURLString) ?? DNSResolverOption.defaultURLString
408 totalLookupDurationMs = try container.decodeIfPresent(Int.self, forKey: .totalLookupDurationMs)
409 sslError = try container.decodeIfPresent(String.self, forKey: .sslError)
410 httpHeadersError = try container.decodeIfPresent(String.self, forKey: .httpHeadersError)
411 reachabilityError = try container.decodeIfPresent(String.self, forKey: .reachabilityError)
412 ipGeolocationError = try container.decodeIfPresent(String.self, forKey: .ipGeolocationError)
413 emailSecurityError = try container.decodeIfPresent(String.self, forKey: .emailSecurityError)
414 ptrError = try container.decodeIfPresent(String.self, forKey: .ptrError)
415 redirectChainError = try container.decodeIfPresent(String.self, forKey: .redirectChainError)
416 portScanError = try container.decodeIfPresent(String.self, forKey: .portScanError)
417 }
418}
419
420// MARK: - Cloudflare DNS-over-HTTPS Response
421
422struct CloudflareDNSResponse: Decodable {
423 let Status: Int
424 let AD: Bool?
425 let Answer: [CloudflareDNSAnswer]?
426
427 struct CloudflareDNSAnswer: Decodable {
428 let name: String
429 let type: Int
430 let TTL: Int
431 let data: String
432 }
433}