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
|
import Foundation
struct DomainInspectionService {
private let reportBuilder = DomainReportBuilder()
private let runtime: LookupRuntime
init(runtime: LookupRuntime = .shared) {
self.runtime = runtime
}
func inspect(domain: String, previousSnapshot: LookupSnapshot? = nil) async -> DomainReport {
let snapshot = await inspectSnapshot(domain: domain, previousSnapshot: previousSnapshot)
return reportBuilder.build(from: snapshot)
}
func inspectSnapshot(domain: String, previousSnapshot: LookupSnapshot? = nil) async -> LookupSnapshot {
let normalizedDomain = normalize(domain)
let startedAt = Date()
let resolverDisplayName = DNSLookupService.currentResolverDisplayName()
let resolverURLString = DNSLookupService.currentResolverURLString()
var cachedSections = Set<LookupSectionKind>()
var sectionSources: [LookupResultSource] = []
async let dnsFetch = runtime.dns(domain: normalizedDomain)
async let availabilityFetch = runtime.availability(domain: normalizedDomain)
async let sslFetch = runtime.ssl(domain: normalizedDomain)
async let hstsFetch = runtime.hsts(domain: normalizedDomain)
async let httpFetch = runtime.http(domain: normalizedDomain)
async let reachabilityFetch = runtime.reachability(domain: normalizedDomain)
async let ownershipFetch = runtime.ownership(domain: normalizedDomain)
async let redirectFetch = runtime.redirectChain(domain: normalizedDomain)
async let subdomainFetch = runtime.subdomains(domain: normalizedDomain)
async let portScanFetch = runtime.portScan(domain: normalizedDomain)
let resolvedDNS = await dnsFetch
let dnsResult = normalizeErrors(in: resolvedDNS.value)
track(.dns, source: resolvedDNS.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let availability = await availabilityFetch
track(.availability, source: availability.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let resolvedSSL = await sslFetch
let sslResult = normalizeErrors(in: resolvedSSL.value)
track(.ssl, source: resolvedSSL.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let hsts = await hstsFetch
track(.hsts, source: hsts.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let http = await httpFetch
let httpResult = normalizeErrors(in: http.value)
track(.httpHeaders, source: http.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let reachability = await reachabilityFetch
let reachabilityResult = normalizeErrors(in: reachability.value)
track(.reachability, source: reachability.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let resolvedOwnership = await ownershipFetch
let ownershipResult = normalizeErrors(in: resolvedOwnership.value)
track(.ownership, source: resolvedOwnership.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let redirects = await redirectFetch
let redirectResult = normalizeErrors(in: redirects.value)
track(.redirectChain, source: redirects.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let resolvedSubdomains = await subdomainFetch
let subdomainResult = normalizeErrors(in: resolvedSubdomains.value)
track(.subdomains, source: resolvedSubdomains.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let ports = await portScanFetch
let portScanResult = normalizeErrors(in: ports.value)
track(.portScan, source: ports.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let dnsSections = mapServiceResult(dnsResult, emptyValue: [])
let sslInfo = mapOptionalValueServiceResult(sslResult)
let httpHeadersResult = mapHTTPResult(httpResult)
let reachabilityResultValue = mapServiceResult(reachabilityResult, emptyValue: [])
let redirectChain = mapServiceResult(redirectResult, emptyValue: [])
let ownership = mapOptionalValueServiceResult(ownershipResult)
let subdomains = mapServiceResult(subdomainResult, emptyValue: [])
let portScanResults = await mapPortScanResult(portScanResult, domain: normalizedDomain)
let txtRecords = dnsSections.value.first(where: { $0.recordType == .TXT })?.records ?? []
let primaryIP = dnsSections.value.first(where: { $0.recordType == .A })?.records.first?.value
let canReuseDNSDependents = canReuseDependentSections(from: previousSnapshot, dnsSections: dnsSections.value)
let canReuseIPDependents = canReuseIPBasedSections(from: previousSnapshot, primaryIP: primaryIP)
let emailOutcome: CachedLookupResult<ServiceResult<EmailSecurityResult>>
if canReuseDNSDependents, let previousSnapshot, let emailSecurity = previousSnapshot.emailSecurity {
emailOutcome = CachedLookupResult(value: .success(emailSecurity), source: .cached)
} else if canReuseDNSDependents, let previousSnapshot, let error = previousSnapshot.emailSecurityError {
emailOutcome = CachedLookupResult(value: .error(error), source: .cached)
} else {
emailOutcome = await runtime.email(domain: normalizedDomain, txtRecords: txtRecords)
}
track(.emailSecurity, source: emailOutcome.source, cachedSections: &cachedSections, sectionSources: §ionSources)
let suggestionsOutcome: CachedLookupResult<[DomainSuggestionResult]>
if availability.value.status == .registered {
suggestionsOutcome = await runtime.suggestions(domain: normalizedDomain)
track(.suggestions, source: suggestionsOutcome.source, cachedSections: &cachedSections, sectionSources: §ionSources)
} else {
suggestionsOutcome = CachedLookupResult(value: [], source: .live)
}
let ptrOutcome: CachedLookupResult<ServiceResult<String>>?
let geoOutcome: CachedLookupResult<ServiceResult<IPGeolocation>>?
if let primaryIP {
if canReuseIPDependents, let previousSnapshot, let ptrRecord = previousSnapshot.ptrRecord {
ptrOutcome = CachedLookupResult(value: .success(ptrRecord), source: .cached)
} else if canReuseIPDependents, let previousSnapshot, let ptrError = previousSnapshot.ptrError {
ptrOutcome = CachedLookupResult(value: .error(ptrError), source: .cached)
} else {
ptrOutcome = await runtime.ptr(ip: primaryIP, resolverURLString: resolverURLString)
}
if canReuseIPDependents, let previousSnapshot, let ipGeolocation = previousSnapshot.ipGeolocation {
geoOutcome = CachedLookupResult(value: .success(ipGeolocation), source: .cached)
} else if canReuseIPDependents, let previousSnapshot, let ipGeolocationError = previousSnapshot.ipGeolocationError {
geoOutcome = CachedLookupResult(value: .error(ipGeolocationError), source: .cached)
} else {
geoOutcome = await runtime.ipGeolocation(ip: primaryIP)
}
if let ptrOutcome {
track(.ptr, source: ptrOutcome.source, cachedSections: &cachedSections, sectionSources: §ionSources)
}
if let geoOutcome {
track(.ipGeolocation, source: geoOutcome.source, cachedSections: &cachedSections, sectionSources: §ionSources)
}
} else {
ptrOutcome = nil
geoOutcome = nil
}
let emailSecurity = mapOptionalValueServiceResult(normalizeErrors(in: emailOutcome.value))
let ptrRecord = mapOptionalServiceResult(ptrOutcome.map { normalizeErrors(in: $0.value) }, missingMessage: "No A record available")
let geolocation = mapOptionalServiceResult(geoOutcome.map { normalizeErrors(in: $0.value) }, missingMessage: "No A record available")
return LookupSnapshot(
historyEntryID: nil,
domain: availability.value.domain,
timestamp: Date(),
trackedDomainID: previousSnapshot?.trackedDomainID,
resolverDisplayName: resolverDisplayName,
resolverURLString: resolverURLString,
totalLookupDurationMs: Int(Date().timeIntervalSince(startedAt) * 1000),
dnsSections: dnsSections.value,
dnsError: dnsSections.message,
availabilityResult: availability.value,
suggestions: suggestionsOutcome.value,
sslInfo: sslInfo.value,
sslError: sslInfo.message,
hstsPreloaded: hsts.value,
httpHeaders: httpHeadersResult.headers,
httpSecurityGrade: httpHeadersResult.securityGrade,
httpStatusCode: httpHeadersResult.statusCode,
httpResponseTimeMs: httpHeadersResult.responseTimeMs,
httpProtocol: httpHeadersResult.httpProtocol,
http3Advertised: httpHeadersResult.http3Advertised,
httpHeadersError: httpHeadersResult.error,
reachabilityResults: reachabilityResultValue.value,
reachabilityError: reachabilityResultValue.message,
ipGeolocation: geolocation.value,
ipGeolocationError: geolocation.message,
emailSecurity: emailSecurity.value,
emailSecurityError: emailSecurity.message,
ownership: ownership.value,
ownershipError: ownership.message,
ptrRecord: ptrRecord.value,
ptrError: ptrRecord.message,
redirectChain: redirectChain.value,
redirectChainError: redirectChain.message,
subdomains: subdomains.value,
subdomainsError: subdomains.message,
portScanResults: portScanResults.value,
portScanError: portScanResults.message,
changeSummary: nil,
resultSource: aggregateSource(sectionSources),
cachedSections: Array(cachedSections).sorted { $0.rawValue < $1.rawValue },
statusMessage: nil
)
}
private func normalize(_ domain: String) -> String {
domain
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: "https://", with: "")
.replacingOccurrences(of: "http://", with: "")
.components(separatedBy: "/").first?
.lowercased() ?? domain.lowercased()
}
private func track(
_ section: LookupSectionKind,
source: LookupResultSource,
cachedSections: inout Set<LookupSectionKind>,
sectionSources: inout [LookupResultSource]
) {
sectionSources.append(source)
if source != .live {
cachedSections.insert(section)
}
}
private func aggregateSource(_ sectionSources: [LookupResultSource]) -> LookupResultSource {
let normalizedSources = sectionSources.map { source -> LookupResultSource in
source == .mixed ? .cached : source
}
let hasLive = normalizedSources.contains(.live)
let hasCached = normalizedSources.contains(.cached)
switch (hasLive, hasCached) {
case (true, true):
return .mixed
case (false, true):
return .cached
default:
return .live
}
}
private func canReuseDependentSections(from previousSnapshot: LookupSnapshot?, dnsSections: [DNSSection]) -> Bool {
guard let previousSnapshot else { return false }
return dnsSignature(for: previousSnapshot.dnsSections) == dnsSignature(for: dnsSections)
}
private func canReuseIPBasedSections(from previousSnapshot: LookupSnapshot?, primaryIP: String?) -> Bool {
guard let previousSnapshot else { return false }
let previousIP = previousSnapshot.dnsSections.first(where: { $0.recordType == .A })?.records.first?.value
return primaryIP == previousIP
}
private func dnsSignature(for sections: [DNSSection]) -> String {
sections
.sorted { $0.recordType.rawValue < $1.recordType.rawValue }
.map { section in
let records = section.records
.sorted { $0.value < $1.value }
.map { "\($0.value)|\($0.ttl)" }
.joined(separator: ",")
let wildcardRecords = section.wildcardRecords
.sorted { $0.value < $1.value }
.map { "\($0.value)|\($0.ttl)" }
.joined(separator: ",")
return [
section.recordType.rawValue,
records,
wildcardRecords,
section.dnssecSigned.map { $0 ? "signed" : "unsigned" } ?? "unknown",
section.error ?? ""
].joined(separator: "#")
}
.joined(separator: "||")
}
private func normalizeErrors<Value>(in result: ServiceResult<Value>) -> ServiceResult<Value> {
switch result {
case let .success(value):
return .success(value)
case let .empty(message):
return .empty(message)
case let .error(message):
return .error(classifiedMessage(from: message))
}
}
private func classifiedMessage(from message: String) -> String {
let normalizedMessage = message.trimmingCharacters(in: .whitespacesAndNewlines)
let lowercasedMessage = normalizedMessage.lowercased()
if lowercasedMessage.hasPrefix("network error:")
|| lowercasedMessage.hasPrefix("timeout:")
|| lowercasedMessage.hasPrefix("rate limit:")
|| lowercasedMessage.hasPrefix("parsing error:") {
return normalizedMessage
}
if lowercasedMessage.contains("timed out") {
return "Timeout: Request timed out"
}
if lowercasedMessage.contains("429")
|| lowercasedMessage.contains("too many requests")
|| lowercasedMessage.contains("rate limit") {
return "Rate limit: Try again shortly"
}
if lowercasedMessage.contains("cannot parse")
|| lowercasedMessage.contains("decoding")
|| lowercasedMessage.contains("json") {
return "Parsing error: Invalid server response"
}
if lowercasedMessage.contains("offline")
|| lowercasedMessage.contains("internet connection")
|| lowercasedMessage.contains("not connected")
|| lowercasedMessage.contains("network connection") {
return "Network error: Offline"
}
return "Network error: \(normalizedMessage)"
}
private func mapServiceResult<Value>(_ result: ServiceResult<Value>, emptyValue: Value) -> (value: Value, message: String?) {
switch result {
case let .success(value):
return (value, nil)
case let .empty(message), let .error(message):
return (emptyValue, message)
}
}
private func mapOptionalValueServiceResult<Value>(_ result: ServiceResult<Value>) -> (value: Value?, message: String?) {
switch result {
case let .success(value):
return (value, nil)
case let .empty(message), let .error(message):
return (nil, message)
}
}
private func mapOptionalServiceResult<Value>(
_ result: ServiceResult<Value>?,
missingMessage: String
) -> (value: Value?, message: String?) {
guard let result else {
return (nil, missingMessage)
}
switch result {
case let .success(value):
return (value, nil)
case let .empty(message), let .error(message):
return (nil, message)
}
}
private func mapPortScanResult(_ result: ServiceResult<[PortScanResult]>, domain: String) async -> (value: [PortScanResult], message: String?) {
switch result {
case let .success(results):
return (await enrichOpenPortBanners(in: results, domain: domain), nil)
case let .empty(message), let .error(message):
return ([], message)
}
}
private func mapHTTPResult(_ result: ServiceResult<HTTPHeadersResult>) -> (
headers: [HTTPHeader],
securityGrade: String?,
statusCode: Int?,
responseTimeMs: Int?,
httpProtocol: String?,
http3Advertised: Bool,
error: String?
) {
switch result {
case let .success(value):
return (
headers: value.headers,
securityGrade: HTTPSecurityGrade.grade(for: value.headers).rawValue,
statusCode: value.statusCode,
responseTimeMs: value.responseTimeMs,
httpProtocol: value.httpProtocol,
http3Advertised: value.http3Advertised,
error: nil
)
case let .empty(message), let .error(message):
return (
headers: [],
securityGrade: nil,
statusCode: nil,
responseTimeMs: nil,
httpProtocol: nil,
http3Advertised: false,
error: message
)
}
}
private func enrichOpenPortBanners(in results: [PortScanResult], domain: String) async -> [PortScanResult] {
let banners = await withTaskGroup(of: (UInt16, String?).self, returning: [UInt16: String].self) { group in
for result in results where result.open {
group.addTask {
let banner = await PortScanService.grabBanner(host: domain, port: result.port)
return (result.port, banner)
}
}
var collected: [UInt16: String] = [:]
for await (port, banner) in group {
if let banner {
collected[port] = banner
}
}
return collected
}
return results.map { result in
var updated = result
updated.banner = banners[result.port]
return updated
}
}
}
|