| @@ -3379,370 +3379,15 @@ final class DomainViewModel { |
| 3379 | 3379 | } |
| 3380 | 3380 | } |
| 3381 | 3381 | |
| 3382 | | static func formatBatchExportText( |
| 3383 | | title: String, |
| 3384 | | entries: [(snapshot: LookupSnapshot, trackedDomain: TrackedDomain?, changeSummary: DomainChangeSummary?, diffSections: [DomainDiffSection])] |
| 3385 | | ) -> String { |
| 3386 | | guard !entries.isEmpty else { |
| 3387 | | return "\(title)\nNo results available." |
| 3388 | | } |
| 3389 | | |
| 3390 | | var lines = [title, String(repeating: "=", count: title.count), ""] |
| 3391 | | for (index, entry) in entries.enumerated() { |
| 3392 | | if index > 0 { |
| 3393 | | lines.append("") |
| 3394 | | lines.append(String(repeating: "=", count: 48)) |
| 3395 | | lines.append("") |
| 3396 | | } |
| 3397 | | |
| 3398 | | lines.append( |
| 3399 | | formatExportText( |
| 3400 | | from: entry.snapshot, |
| 3401 | | trackedDomain: entry.trackedDomain, |
| 3402 | | changeSummary: entry.changeSummary, |
| 3403 | | diffSections: entry.diffSections |
| 3404 | | ) |
| 3405 | | ) |
| 3406 | | } |
| 3407 | | return lines.joined(separator: "\n") |
| 3408 | | } |
| 3409 | | |
| 3410 | | static func formatCSV(from snapshots: [LookupSnapshot]) -> String { |
| 3411 | | let headers = [ |
| 3412 | | "domain", |
| 3413 | | "availability", |
| 3414 | | "primary_ip", |
| 3415 | | "redirect_target", |
| 3416 | | "tls_status", |
| 3417 | | "http_status_grade", |
| 3418 | | "email_security_summary", |
| 3419 | | "registrar", |
| 3420 | | "ownership_expires", |
| 3421 | | "ownership_status", |
| 3422 | | "ownership_nameservers", |
| 3423 | | "subdomain_count", |
| 3424 | | "subdomains", |
| 3425 | | "last_updated" |
| 3426 | | ] |
| 3427 | | |
| 3428 | | let rows = snapshots.map { snapshot in |
| 3429 | | [ |
| 3430 | | snapshot.domain, |
| 3431 | | availabilityLabel(snapshot.availabilityResult?.status), |
| 3432 | | primaryIPAddress(from: snapshot) ?? "", |
| 3433 | | finalRedirectTarget(from: snapshot) ?? "", |
| 3434 | | httpsSummary(from: snapshot), |
| 3435 | | httpStatusGradeSummary(from: snapshot), |
| 3436 | | emailSummary(from: snapshot), |
| 3437 | | snapshot.ownership?.registrar ?? "", |
| 3438 | | snapshot.ownership?.expirationDate.map(csvDateFormatter.string(from:)) ?? "", |
| 3439 | | snapshot.ownership?.status.joined(separator: " | ") ?? "", |
| 3440 | | snapshot.ownership?.nameservers.joined(separator: " | ") ?? "", |
| 3441 | | "\(snapshot.subdomains.count)", |
| 3442 | | snapshot.subdomains.map(\.hostname).joined(separator: " | "), |
| 3443 | | csvDateFormatter.string(from: snapshot.timestamp) |
| 3444 | | ] |
| 3445 | | } |
| 3446 | | |
| 3447 | | return ([headers] + rows) |
| 3448 | | .map { row in row.map(csvEscaped).joined(separator: ",") } |
| 3449 | | .joined(separator: "\n") |
| 3450 | | } |
| 3451 | | |
| 3452 | | static func formatExportText( |
| 3453 | | from snapshot: LookupSnapshot, |
| 3454 | | trackedDomain: TrackedDomain?, |
| 3455 | | changeSummary: DomainChangeSummary?, |
| 3456 | | diffSections: [DomainDiffSection] |
| 3457 | | ) -> String { |
| 3458 | | let exportDateFormatter = DateFormatter() |
| 3459 | | exportDateFormatter.dateFormat = "yyyy-MM-dd HH:mm" |
| 3460 | | |
| 3461 | | var lines: [String] = [ |
| 3462 | | "DomainDig Export", |
| 3463 | | "Domain: \(snapshot.domain)", |
| 3464 | | "Date: \(exportDateFormatter.string(from: snapshot.timestamp))", |
| 3465 | | "Mode: \(snapshot.statusMessage ?? snapshot.resultSource.label)", |
| 3466 | | "Resolver: \(snapshot.resolverDisplayName)", |
| 3467 | | "Lookup Duration: \(durationLabel(snapshot.totalLookupDurationMs))", |
| 3468 | | "Tracked: \(trackedDomain == nil ? "No" : "Yes")" |
| 3469 | | ] |
| 3470 | | |
| 3471 | | if let note = trackedDomain?.note?.nilIfEmpty { |
| 3472 | | lines.append("Tracking Note: \(note)") |
| 3473 | | } |
| 3474 | | |
| 3475 | | func appendSection(_ title: String, _ body: [String]) { |
| 3476 | | lines.append("") |
| 3477 | | lines.append(title) |
| 3478 | | lines.append(String(repeating: "-", count: title.count)) |
| 3479 | | lines += body |
| 3480 | | } |
| 3481 | | |
| 3482 | | appendSection("Summary", exportSummaryLines(snapshot, changeSummary: changeSummary, exportDateFormatter: exportDateFormatter)) |
| 3483 | | appendSection("Tracking", exportTrackingLines(trackedDomain, exportDateFormatter: exportDateFormatter)) |
| 3484 | | appendSection("Diff Summary", exportDiffSummaryLines(diffSections)) |
| 3485 | | appendSection("Domain", exportDomainLines(snapshot)) |
| 3486 | | appendSection("Ownership", exportOwnershipLines(snapshot)) |
| 3487 | | appendSection("Subdomains", exportSubdomainsLines(snapshot)) |
| 3488 | | appendSection("DNS", exportDnsLines(snapshot)) |
| 3489 | | appendSection("Web", exportWebLines(snapshot)) |
| 3490 | | appendSection("Email", exportEmailLines(snapshot)) |
| 3491 | | appendSection("Network", exportNetworkLines(snapshot)) |
| 3492 | | |
| 3493 | | return lines.joined(separator: "\n") |
| 3494 | | } |
| 3495 | | |
| 3496 | | private static func exportSummaryLines( |
| 3497 | | _ snapshot: LookupSnapshot, |
| 3498 | | changeSummary: DomainChangeSummary?, |
| 3499 | | exportDateFormatter: DateFormatter |
| 3500 | | ) -> [String] { |
| 3501 | | var lines: [String] = [] |
| 3502 | | for item in summaryFields(from: snapshot) { |
| 3503 | | lines.append(" \(item.label): \(item.value)") |
| 3504 | | } |
| 3505 | | if let changeSummary { |
| 3506 | | lines.append(" Change Summary: \(changeSummary.message)") |
| 3507 | | lines.append(" Severity: \(changeSummary.severity.title)") |
| 3508 | | lines.append(" Changed Sections: \(changeSummary.changedSections.isEmpty ? "None" : changeSummary.changedSections.joined(separator: ", "))") |
| 3509 | | lines.append(" Compared At: \(exportDateFormatter.string(from: changeSummary.generatedAt))") |
| 3510 | | } |
| 3511 | | return lines |
| 3512 | | } |
| 3513 | | |
| 3514 | | private static func exportTrackingLines(_ trackedDomain: TrackedDomain?, exportDateFormatter: DateFormatter) -> [String] { |
| 3515 | | var lines: [String] = [] |
| 3516 | | if let trackedDomain { |
| 3517 | | lines.append(" Pinned: \(trackedDomain.isPinned ? "Yes" : "No")") |
| 3518 | | lines.append(" Last Refresh: \(exportDateFormatter.string(from: trackedDomain.updatedAt))") |
| 3519 | | lines.append(" Last Known Availability: \(availabilityLabel(trackedDomain.lastKnownAvailability))") |
| 3520 | | if let note = trackedDomain.note?.nilIfEmpty { |
| 3521 | | lines.append(" Note: \(note)") |
| 3522 | | } |
| 3523 | | } else { |
| 3524 | | lines.append(" This domain is not currently tracked.") |
| 3525 | | } |
| 3526 | | return lines |
| 3527 | | } |
| 3528 | | |
| 3529 | | private static func exportDiffSummaryLines(_ diffSections: [DomainDiffSection]) -> [String] { |
| 3530 | | var lines: [String] = [] |
| 3531 | | if diffSections.isEmpty { |
| 3532 | | lines.append(" No comparison available") |
| 3533 | | } else { |
| 3534 | | for section in diffSections where section.items.contains(where: { $0.changeType != .unchanged }) { |
| 3535 | | lines.append(" \(section.title)") |
| 3536 | | for item in section.items where item.changeType != .unchanged { |
| 3537 | | lines.append(" [\(item.changeType.rawValue.capitalized)] \(item.label): \(item.oldValue ?? "None") -> \(item.newValue ?? "None")") |
| 3538 | | } |
| 3539 | | } |
| 3540 | | } |
| 3541 | | return lines |
| 3542 | | } |
| 3543 | | |
| 3544 | | private static func exportDomainLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3545 | | var lines: [String] = [] |
| 3546 | | for row in domainRows(from: snapshot) { |
| 3547 | | lines.append(" \(row.label): \(row.value)") |
| 3548 | | } |
| 3549 | | if snapshot.suggestions.isEmpty { |
| 3550 | | lines.append(" Suggestions: None") |
| 3551 | | } else { |
| 3552 | | lines.append(" Suggestions:") |
| 3553 | | for suggestion in snapshot.suggestions { |
| 3554 | | lines.append(" \(suggestion.domain): \(availabilityLabel(suggestion.status))") |
| 3555 | | } |
| 3556 | | } |
| 3557 | | return lines |
| 3558 | | } |
| 3559 | | |
| 3560 | | private static func exportOwnershipLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3561 | | var lines: [String] = [] |
| 3562 | | for row in ownershipRows(from: snapshot) { |
| 3563 | | lines.append(" \(row.label): \(row.value)") |
| 3564 | | } |
| 3565 | | if let ownershipError = snapshot.ownershipError, snapshot.ownership == nil { |
| 3566 | | lines.append(" Source: \(ownershipError)") |
| 3567 | | } |
| 3568 | | return lines |
| 3569 | | } |
| 3570 | | |
| 3571 | | private static func exportSubdomainsLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3572 | | var lines: [String] = [] |
| 3573 | | lines.append(" Count: \(snapshot.subdomains.count)") |
| 3574 | | if snapshot.subdomains.isEmpty { |
| 3575 | | lines.append(" \(snapshot.subdomainsError ?? "No passive subdomains found")") |
| 3576 | | } else { |
| 3577 | | for subdomain in subdomainRows(from: snapshot) { |
| 3578 | | let marker = subdomain.isInteresting ? " [interesting]" : "" |
| 3579 | | lines.append(" \(subdomain.hostname)\(marker)") |
| 3580 | | } |
| 3581 | | } |
| 3582 | | if !FeatureAccessService.hasAccess(to: .extendedSubdomains) { |
| 3583 | | lines.append(" Extended subdomain discovery (Pro)") |
| 3584 | | } |
| 3585 | | return lines |
| 3586 | | } |
| 3587 | | |
| 3588 | | private static func exportDnsLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3589 | | var lines: [String] = [] |
| 3590 | | if let dnsError = snapshot.dnsError { |
| 3591 | | lines.append(" Error: \(dnsError)") |
| 3592 | | } |
| 3593 | | if let dnssecLabel = dnssecLabel(from: snapshot) { |
| 3594 | | lines.append(" \(dnssecLabel)") |
| 3595 | | } |
| 3596 | | for section in dnsRows(from: snapshot) { |
| 3597 | | lines += exportDNSSectionLines(section) |
| 3598 | | } |
| 3599 | | if let ptrRecord = snapshot.ptrRecord { |
| 3600 | | lines.append(" PTR: \(ptrRecord)") |
| 3601 | | } else if let ptrError = snapshot.ptrError { |
| 3602 | | lines.append(" PTR Error: \(ptrError)") |
| 3603 | | } |
| 3604 | | return lines |
| 3605 | | } |
| 3606 | | |
| 3607 | | private static func exportDNSSectionLines(_ section: DNSRecordSectionViewData) -> [String] { |
| 3608 | | var lines = [" \(section.title)"] |
| 3609 | | if let message = section.message { |
| 3610 | | lines.append(" \(message.isError ? "Error" : "Info"): \(message.text)") |
| 3611 | | } |
| 3612 | | for row in section.rows { |
| 3613 | | lines.append(" \(row.value) (\(row.label))") |
| 3614 | | } |
| 3615 | | if let wildcardTitle = section.wildcardTitle { |
| 3616 | | lines.append(" \(wildcardTitle)") |
| 3617 | | for row in section.wildcardRows { |
| 3618 | | lines.append(" \(row.value) (\(row.label))") |
| 3619 | | } |
| 3620 | | } |
| 3621 | | return lines |
| 3622 | | } |
| 3623 | | |
| 3624 | | private static func exportResponseLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3625 | | if let httpHeadersError = snapshot.httpHeadersError { |
| 3626 | | return [" Headers Error: \(httpHeadersError)"] |
| 3627 | | } |
| 3628 | | var lines = webResponseRows(from: snapshot).map { " \($0.label): \($0.value)" } |
| 3629 | | if snapshot.httpHeaders.isEmpty { |
| 3630 | | lines.append(" Headers: No headers returned") |
| 3631 | | } else { |
| 3632 | | lines.append(" Headers:") |
| 3633 | | for header in snapshot.httpHeaders { |
| 3634 | | lines.append(" \(header.name): \(header.value)") |
| 3635 | | } |
| 3636 | | } |
| 3637 | | return lines |
| 3638 | | } |
| 3639 | | |
| 3640 | | private static func exportWebLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3641 | | var lines: [String] = [] |
| 3642 | | if let sslError = snapshot.sslError { |
| 3643 | | lines.append(" TLS Error: \(sslError)") |
| 3644 | | } else { |
| 3645 | | for row in webCertificateRows(from: snapshot) { |
| 3646 | | lines.append(" \(row.label): \(row.value)") |
| 3647 | | } |
| 3648 | | } |
| 3649 | | lines += exportResponseLines(snapshot) |
| 3650 | | |
| 3651 | | if let redirectChainError = snapshot.redirectChainError { |
| 3652 | | lines.append(" Redirect Error: \(redirectChainError)") |
| 3653 | | } else if snapshot.redirectChain.isEmpty { |
| 3654 | | lines.append(" Redirects: No redirect data available") |
| 3655 | | } else { |
| 3656 | | lines.append(" Redirects:") |
| 3657 | | for hop in redirectRows(from: snapshot) { |
| 3658 | | lines.append(" \(hop.stepLabel). \(hop.statusCode) \(hop.url)\(hop.isFinal ? " (final)" : "")") |
| 3659 | | } |
| 3660 | | } |
| 3661 | | return lines |
| 3662 | | } |
| 3663 | | |
| 3664 | | private static func exportEmailLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3665 | | var lines: [String] = [] |
| 3666 | | if let emailSecurityError = snapshot.emailSecurityError { |
| 3667 | | lines.append(" Error: \(emailSecurityError)") |
| 3668 | | } else if emailRows(from: snapshot).isEmpty { |
| 3669 | | lines.append(" No email security records found") |
| 3670 | | } else { |
| 3671 | | for row in emailRows(from: snapshot) { |
| 3672 | | lines.append(" \(row.label): \(row.status)") |
| 3673 | | lines.append(" \(row.detail)") |
| 3674 | | if let auxiliaryDetail = row.auxiliaryDetail { |
| 3675 | | lines.append(" \(auxiliaryDetail)") |
| 3676 | | } |
| 3677 | | } |
| 3678 | | } |
| 3679 | | return lines |
| 3680 | | } |
| 3681 | | |
| 3682 | | private static func exportPortLines(_ rows: [PortScanRowViewData]) -> [String] { |
| 3683 | | guard !rows.isEmpty else { return [" No results"] } |
| 3684 | | var lines: [String] = [] |
| 3685 | | for row in rows { |
| 3686 | | lines.append(" \(row.portLabel) \(row.service): \(row.statusLabel)\(row.durationLabel.map { " \($0)" } ?? "")") |
| 3687 | | if let banner = row.banner { |
| 3688 | | lines.append(" Banner: \(banner)") |
| 3689 | | } |
| 3690 | | } |
| 3691 | | return lines |
| 3692 | | } |
| 3693 | | |
| 3694 | | private static func exportNetworkLines(_ snapshot: LookupSnapshot) -> [String] { |
| 3695 | | var lines: [String] = [] |
| 3696 | | if let reachabilityError = snapshot.reachabilityError { |
| 3697 | | lines.append(" Reachability Error: \(reachabilityError)") |
| 3698 | | } else if reachabilityRows(from: snapshot).isEmpty { |
| 3699 | | lines.append(" Reachability: No results") |
| 3700 | | } else { |
| 3701 | | lines.append(" Reachability:") |
| 3702 | | for row in reachabilityRows(from: snapshot) { |
| 3703 | | lines.append(" \(row.portLabel): \(row.statusLabel) \(row.latencyLabel)") |
| 3704 | | } |
| 3705 | | } |
| 3706 | | |
| 3707 | | if let ipGeolocationError = snapshot.ipGeolocationError, snapshot.ipGeolocation == nil { |
| 3708 | | lines.append(" Location Error: \(ipGeolocationError)") |
| 3709 | | } else if locationRows(from: snapshot).isEmpty { |
| 3710 | | lines.append(" Location: No data") |
| 3711 | | } else { |
| 3712 | | lines.append(" Location:") |
| 3713 | | for row in locationRows(from: snapshot) { |
| 3714 | | lines.append(" \(row.label): \(row.value)") |
| 3715 | | } |
| 3716 | | } |
| 3717 | | |
| 3718 | | if let portScanError = snapshot.portScanError, snapshot.portScanResults.isEmpty { |
| 3719 | | lines.append(" Port Scan Error: \(portScanError)") |
| 3720 | | } |
| 3721 | | |
| 3722 | | lines.append(" Standard Ports:") |
| 3723 | | lines += exportPortLines(portRows(from: snapshot, kind: .standard)) |
| 3724 | | lines.append(" Custom Ports:") |
| 3725 | | lines += exportPortLines(portRows(from: snapshot, kind: .custom)) |
| 3726 | | return lines |
| 3727 | | } |
| 3728 | | |
| 3729 | | private static func primaryIPAddress(from snapshot: LookupSnapshot) -> String? { |
| 3382 | static func primaryIPAddress(from snapshot: LookupSnapshot) -> String? { |
| 3730 | 3383 | snapshot.dnsSections.first(where: { $0.recordType == .A })?.records.first?.value |
| 3731 | 3384 | } |
| 3732 | 3385 | |
| 3733 | | private static func finalRedirectTarget(from snapshot: LookupSnapshot) -> String? { |
| 3386 | static func finalRedirectTarget(from snapshot: LookupSnapshot) -> String? { |
| 3734 | 3387 | snapshot.redirectChain.last?.url |
| 3735 | 3388 | } |
| 3736 | 3389 | |
| 3737 | | private static func httpStatusGradeSummary(from snapshot: LookupSnapshot) -> String { |
| 3738 | | let parts = [snapshot.httpStatusCode.map(String.init), snapshot.httpSecurityGrade].compactMap { $0 } |
| 3739 | | if !parts.isEmpty { |
| 3740 | | return parts.joined(separator: " / ") |
| 3741 | | } |
| 3742 | | return snapshot.httpHeadersError ?? "Unavailable" |
| 3743 | | } |
| 3744 | | |
| 3745 | | private static func httpsSummary(from snapshot: LookupSnapshot) -> String { |
| 3390 | static func httpsSummary(from snapshot: LookupSnapshot) -> String { |
| 3746 | 3391 | if snapshot.sslInfo != nil { |
| 3747 | 3392 | return "Valid" |
| 3748 | 3393 | } |
| @@ -3759,7 +3404,7 @@ final class DomainViewModel { |
| 3759 | 3404 | return snapshot.sslError == nil ? .secondary : .failure |
| 3760 | 3405 | } |
| 3761 | 3406 | |
| 3762 | | private static func emailSummary(from snapshot: LookupSnapshot) -> String { |
| 3407 | static func emailSummary(from snapshot: LookupSnapshot) -> String { |
| 3763 | 3408 | guard let emailSecurity = snapshot.emailSecurity else { |
| 3764 | 3409 | return snapshot.emailSecurityError ?? "Unavailable" |
| 3765 | 3410 | } |
| @@ -3803,7 +3448,7 @@ final class DomainViewModel { |
| 3803 | 3448 | return .success |
| 3804 | 3449 | } |
| 3805 | 3450 | |
| 3806 | | private static func availabilityLabel(_ status: DomainAvailabilityStatus?) -> String { |
| 3451 | static func availabilityLabel(_ status: DomainAvailabilityStatus?) -> String { |
| 3807 | 3452 | switch status { |
| 3808 | 3453 | case .available: |
| 3809 | 3454 | return "Available" |
| @@ -3866,7 +3511,7 @@ final class DomainViewModel { |
| 3866 | 3511 | } |
| 3867 | 3512 | } |
| 3868 | 3513 | |
| 3869 | | private static func durationLabel(_ durationMs: Int?) -> String { |
| 3514 | static func durationLabel(_ durationMs: Int?) -> String { |
| 3870 | 3515 | durationMs.map { "\($0) ms" } ?? "Unavailable" |
| 3871 | 3516 | } |
| 3872 | 3517 | |
| @@ -3884,12 +3529,6 @@ final class DomainViewModel { |
| 3884 | 3529 | return formatter |
| 3885 | 3530 | }() |
| 3886 | 3531 | |
| 3887 | | private static let csvDateFormatter: ISO8601DateFormatter = { |
| 3888 | | let formatter = ISO8601DateFormatter() |
| 3889 | | formatter.formatOptions = [.withInternetDateTime] |
| 3890 | | return formatter |
| 3891 | | }() |
| 3892 | | |
| 3893 | 3532 | private func conciseExternalMessage(_ message: String, fallback: String) -> String { |
| 3894 | 3533 | let trimmed = message.trimmingCharacters(in: .whitespacesAndNewlines) |
| 3895 | 3534 | if trimmed.isEmpty { |
| @@ -3907,11 +3546,6 @@ final class DomainViewModel { |
| 3907 | 3546 | return trimmed |
| 3908 | 3547 | } |
| 3909 | 3548 | |
| 3910 | | private static func csvEscaped(_ value: String) -> String { |
| 3911 | | let escaped = value.replacingOccurrences(of: "\"", with: "\"\"") |
| 3912 | | return "\"\(escaped)\"" |
| 3913 | | } |
| 3914 | | |
| 3915 | 3549 | private static func isInterestingSubdomain(_ hostname: String) -> Bool { |
| 3916 | 3550 | let keywords = ["admin", "api", "dev", "staging", "test", "internal"] |
| 3917 | 3551 | let labels = hostname.lowercased().split(separator: ".").map(String.init) |
| @@ -3921,6 +3555,9 @@ final class DomainViewModel { |
| 3921 | 3555 | } |
| 3922 | 3556 | } |
| 3923 | 3557 | |
| 3558 | // Private, and not shared with the identically named helpers elsewhere: |
| 3559 | // DomainInsightEngine's `nilIfEmpty` trims whitespace first and these do not, |
| 3560 | // so one internal version would quietly change behaviour there (A14). |
| 3924 | 3561 | private extension String { |
| 3925 | 3562 | var nilIfEmpty: String? { |
| 3926 | 3563 | isEmpty ? nil : self |