DomainDig/ContentView.swift
1320 lines · 53788 bytes
1import MapKit
2import SwiftUI
3
4enum LookupInputMode: String, CaseIterable, Identifiable {
5 case single
6 case bulk
7
8 var id: String { rawValue }
9}
10
11struct ContentView: View {
12 @State private var viewModel = DomainViewModel()
13 @State private var navigationPath = NavigationPath()
14 @FocusState private var domainFieldFocused: Bool
15 @State private var customPortInput = ""
16 @State private var customPortsExpanded = false
17 @State private var trackingNoteDraft = ""
18 @State private var editingTrackedDomain: TrackedDomain?
19 @State private var showTrackLimitAlert = false
20 @State private var inputMode: LookupInputMode = .single
21
22 var body: some View {
23 NavigationStack(path: $navigationPath) {
24 ScrollView(.vertical) {
25 VStack(spacing: 0) {
26 inputSection
27 if !viewModel.batchResults.isEmpty || viewModel.batchLookupRunning {
28 batchSection
29 .padding(.top, 8)
30 }
31 if viewModel.hasRun {
32 actionButtons
33 SummaryView(fields: viewModel.summaryFields)
34 .padding(.top, 8)
35 if let changeSummary = viewModel.currentChangeSummary {
36 DomainChangeSummaryView(summary: changeSummary)
37 .padding(.top, 12)
38 }
39 DomainSectionView(
40 rows: viewModel.domainRows,
41 suggestions: viewModel.suggestionRows,
42 showSuggestions: viewModel.availabilityResult?.status == .registered || viewModel.suggestionsLoading,
43 availabilityLoading: viewModel.availabilityLoading,
44 suggestionsLoading: viewModel.suggestionsLoading,
45 trackedDomain: viewModel.currentTrackedDomain,
46 trackingLimitMessage: viewModel.trackingLimitMessage,
47 onTrack: {
48 if !viewModel.trackCurrentDomain() {
49 showTrackLimitAlert = true
50 }
51 },
52 onTogglePinned: {
53 guard let trackedDomain = viewModel.currentTrackedDomain else { return }
54 viewModel.togglePinned(for: trackedDomain)
55 },
56 onEditNote: {
57 guard let trackedDomain = viewModel.currentTrackedDomain else { return }
58 trackingNoteDraft = trackedDomain.note ?? ""
59 editingTrackedDomain = trackedDomain
60 }
61 )
62 .padding(.top, 16)
63 if !viewModel.currentDiffSections.isEmpty {
64 DomainDiffView(
65 title: "Latest Changes",
66 sections: viewModel.currentDiffSections,
67 showsUnchanged: false
68 )
69 .padding(.top, 16)
70 }
71 DNSSectionView(
72 dnssecLabel: viewModel.dnssecLabel,
73 sections: viewModel.dnsRows,
74 ptrMessage: viewModel.ptrMessage,
75 loading: viewModel.dnsLoading || viewModel.ptrLoading,
76 sectionError: viewModel.dnsError
77 )
78 .padding(.top, 16)
79 WebSectionView(
80 certificateRows: viewModel.webCertificateRows,
81 sslInfo: viewModel.sslInfo,
82 sslLoading: viewModel.sslLoading || viewModel.hstsLoading,
83 sslError: viewModel.sslError,
84 responseRows: viewModel.webResponseRows,
85 headers: viewModel.httpHeaders,
86 headersLoading: viewModel.httpHeadersLoading,
87 headersError: viewModel.httpHeadersError,
88 redirects: viewModel.redirectRows,
89 redirectLoading: viewModel.redirectChainLoading,
90 redirectError: viewModel.redirectChainError,
91 finalURL: viewModel.currentSnapshot.redirectChain.last?.url
92 )
93 .padding(.top, 16)
94 EmailSectionView(
95 rows: viewModel.emailRows,
96 loading: viewModel.emailSecurityLoading,
97 error: viewModel.emailSecurityError
98 )
99 .padding(.top, 16)
100 NetworkSectionView(
101 reachabilityRows: viewModel.reachabilityRows,
102 reachabilityLoading: viewModel.reachabilityLoading,
103 reachabilityError: viewModel.reachabilityError,
104 locationRows: viewModel.locationRows,
105 geolocation: viewModel.ipGeolocation,
106 geolocationLoading: viewModel.ipGeolocationLoading,
107 geolocationError: viewModel.ipGeolocationError,
108 standardPortRows: viewModel.standardPortRows,
109 customPortRows: viewModel.customPortRows,
110 portScanLoading: viewModel.portScanLoading,
111 portScanError: viewModel.portScanError,
112 customPortScanLoading: viewModel.customPortScanLoading,
113 customPortScanError: viewModel.customPortScanError,
114 isCloudflareProxied: viewModel.isCloudflareProxied,
115 customPortsExpanded: $customPortsExpanded,
116 customPortInput: $customPortInput,
117 onScanCustomPorts: runCustomPortScan
118 )
119 .padding(.top, 16)
120 } else if !viewModel.recentSearches.isEmpty {
121 recentSearchesSection
122 }
123 }
124 .padding(.horizontal)
125 .padding(.bottom, 32)
126 }
127 .background(Color.black)
128 .navigationTitle("DomainDig")
129 .toolbarColorScheme(.dark, for: .navigationBar)
130 .preferredColorScheme(.dark)
131 .toolbar {
132 ToolbarItemGroup(placement: .topBarTrailing) {
133 if viewModel.hasRun {
134 Button {
135 viewModel.reset()
136 } label: {
137 Image(systemName: "xmark.circle")
138 .foregroundStyle(.secondary)
139 }
140 }
141 NavigationLink {
142 WatchlistView(viewModel: viewModel)
143 } label: {
144 Image(systemName: "eye")
145 .foregroundStyle(.secondary)
146 }
147 Menu {
148 NavigationLink {
149 HistoryView(viewModel: viewModel)
150 } label: {
151 Label("History", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
152 }
153
154 NavigationLink {
155 SavedDomainsView(viewModel: viewModel)
156 } label: {
157 Label("Saved Domains", systemImage: "bookmark")
158 }
159
160 NavigationLink {
161 SettingsView()
162 } label: {
163 Label("Settings", systemImage: "gearshape")
164 }
165 } label: {
166 Image(systemName: "ellipsis.circle")
167 .foregroundStyle(.secondary)
168 }
169 }
170 }
171 }
172 .onAppear {
173 domainFieldFocused = true
174 }
175 .onChange(of: viewModel.rerunNavigationToken) { _, _ in
176 navigationPath = NavigationPath()
177 domainFieldFocused = false
178 }
179 .alert("Tracking limit reached", isPresented: $showTrackLimitAlert) {
180 Button("OK", role: .cancel) {}
181 } message: {
182 Text("Free version supports up to 3 tracked domains. More tracked domains will be available in a future Pro upgrade.")
183 }
184 .sheet(item: $editingTrackedDomain) { trackedDomain in
185 NavigationStack {
186 Form {
187 Section("Tracking Note") {
188 TextField("Optional note", text: $trackingNoteDraft, axis: .vertical)
189 .lineLimit(3...6)
190 .textInputAutocapitalization(.never)
191 .autocorrectionDisabled()
192 }
193 }
194 .navigationTitle(trackedDomain.domain)
195 .toolbar {
196 ToolbarItem(placement: .cancellationAction) {
197 Button("Cancel") {
198 editingTrackedDomain = nil
199 }
200 }
201 ToolbarItem(placement: .confirmationAction) {
202 Button("Save") {
203 viewModel.updateNote(trackingNoteDraft, for: trackedDomain)
204 editingTrackedDomain = nil
205 }
206 }
207 }
208 }
209 }
210 }
211
212 private var inputSection: some View {
213 VStack(spacing: 12) {
214 Picker("Mode", selection: $inputMode) {
215 Text("Single").tag(LookupInputMode.single)
216 Text("Bulk").tag(LookupInputMode.bulk)
217 }
218 .pickerStyle(.segmented)
219
220 if inputMode == .single {
221 TextField("e.g. cleberg.net", text: $viewModel.domain)
222 .font(.system(.title3, design: .monospaced))
223 .textInputAutocapitalization(.never)
224 .autocorrectionDisabled()
225 .keyboardType(.URL)
226 .padding(12)
227 .background(Color(.systemGray6))
228 .cornerRadius(8)
229 .focused($domainFieldFocused)
230 .onSubmit { viewModel.run() }
231
232 Button {
233 domainFieldFocused = false
234 viewModel.run()
235 } label: {
236 Text("Run")
237 .font(.headline)
238 .frame(maxWidth: .infinity)
239 .padding(.vertical, 12)
240 }
241 .buttonStyle(.borderedProminent)
242 .disabled(viewModel.trimmedDomain.isEmpty)
243 } else {
244 VStack(alignment: .leading, spacing: 8) {
245 Text("Paste domains separated by new lines or commas.")
246 .font(.system(.caption, design: .monospaced))
247 .foregroundStyle(.secondary)
248
249 TextField(
250 "example.com\napple.com, openai.com",
251 text: $viewModel.bulkInput,
252 axis: .vertical
253 )
254 .font(.system(.body, design: .monospaced))
255 .textInputAutocapitalization(.never)
256 .autocorrectionDisabled()
257 .keyboardType(.URL)
258 .lineLimit(4...10)
259 .padding(12)
260 .background(Color(.systemGray6))
261 .cornerRadius(8)
262
263 Button {
264 domainFieldFocused = false
265 viewModel.runBulkLookup()
266 } label: {
267 Text(viewModel.batchLookupRunning ? "Running Batch…" : "Run Batch")
268 .font(.headline)
269 .frame(maxWidth: .infinity)
270 .padding(.vertical, 12)
271 }
272 .buttonStyle(.borderedProminent)
273 .disabled(viewModel.bulkInput.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.batchLookupRunning)
274 }
275 }
276 }
277 .padding(.vertical, 16)
278 }
279
280 private var actionButtons: some View {
281 HStack {
282 Spacer()
283 if viewModel.resultsLoaded {
284 Button {
285 viewModel.toggleSavedDomain()
286 } label: {
287 Image(systemName: viewModel.isCurrentDomainSaved ? "bookmark.fill" : "bookmark")
288 .font(.system(.body))
289 .foregroundStyle(viewModel.isCurrentDomainSaved ? .yellow : .secondary)
290 }
291 Menu {
292 Button("Export TXT") {
293 shareSingleResults(asCSV: false)
294 }
295 Button("Export CSV") {
296 shareSingleResults(asCSV: true)
297 }
298 } label: {
299 Image(systemName: "square.and.arrow.up")
300 .font(.system(.body))
301 .foregroundStyle(.secondary)
302 }
303 }
304 }
305 }
306
307 private var batchSection: some View {
308 VStack(alignment: .leading, spacing: 12) {
309 HStack {
310 Spacer()
311 if !viewModel.currentBatchResultEntries.isEmpty {
312 Menu {
313 Button("Export Batch TXT") {
314 shareBatchResults(asCSV: false)
315 }
316 Button("Export Batch CSV") {
317 shareBatchResults(asCSV: true)
318 }
319 } label: {
320 Label("Export", systemImage: "square.and.arrow.up")
321 .font(.system(.caption, design: .monospaced))
322 }
323 .buttonStyle(.bordered)
324 }
325 }
326
327 BatchResultsView(
328 viewModel: viewModel,
329 title: viewModel.batchLookupSource == .watchlistRefresh ? "Tracked Domain Refresh" : "Batch Results"
330 )
331 }
332 }
333
334 private var recentSearchesSection: some View {
335 VStack(alignment: .leading, spacing: 8) {
336 HStack {
337 Text("RECENT")
338 .font(.system(.caption2, design: .monospaced))
339 .foregroundStyle(.secondary)
340 Spacer()
341 Button("Clear") {
342 viewModel.clearRecentSearches()
343 }
344 .font(.system(.caption2, design: .monospaced))
345 .foregroundStyle(.secondary)
346 }
347
348 ForEach(viewModel.recentSearches, id: \.self) { domain in
349 Button {
350 viewModel.domain = domain
351 domainFieldFocused = false
352 viewModel.run()
353 } label: {
354 Text(domain)
355 .font(.system(.callout, design: .monospaced))
356 .foregroundStyle(.primary)
357 .frame(maxWidth: .infinity, alignment: .leading)
358 .padding(.vertical, 6)
359 .padding(.horizontal, 10)
360 .background(Color(.systemGray6).opacity(0.5))
361 .cornerRadius(6)
362 }
363 }
364 }
365 .padding(.top, 8)
366 }
367
368 private func runCustomPortScan() {
369 let ports = parsedCustomPorts(from: customPortInput)
370 Task {
371 await viewModel.runCustomPortScan(ports: ports)
372 }
373 }
374
375 private func parsedCustomPorts(from input: String) -> [UInt16] {
376 let parts = input.split(separator: ",", omittingEmptySubsequences: true)
377 var seen = Set<UInt16>()
378 var ports: [UInt16] = []
379
380 for part in parts {
381 let trimmed = part.trimmingCharacters(in: .whitespacesAndNewlines)
382 guard let value = UInt16(trimmed), seen.insert(value).inserted else {
383 continue
384 }
385 ports.append(value)
386 if ports.count == 20 {
387 break
388 }
389 }
390
391 return ports
392 }
393
394 private func shareSingleResults(asCSV: Bool) {
395 let (filename, contents) = exportPayload(
396 prefix: "domaindig_single",
397 text: viewModel.exportText(),
398 csv: viewModel.exportCSV(),
399 asCSV: asCSV
400 )
401 ExportPresenter.share(filename: filename, contents: contents)
402 }
403
404 private func shareBatchResults(asCSV: Bool) {
405 let (filename, contents) = exportPayload(
406 prefix: "domaindig_batch",
407 text: viewModel.exportBatchText(),
408 csv: viewModel.exportBatchCSV(),
409 asCSV: asCSV
410 )
411 ExportPresenter.share(filename: filename, contents: contents)
412 }
413
414 private func exportPayload(prefix: String, text: String, csv: String, asCSV: Bool) -> (String, String) {
415 let formatter = DateFormatter()
416 formatter.dateFormat = "yyyyMMdd_HHmmss"
417 let timestamp = formatter.string(from: Date())
418 let fileExtension = asCSV ? "csv" : "txt"
419 let filename = "\(timestamp)_\(prefix).\(fileExtension)"
420 return (filename, asCSV ? csv : text)
421 }
422}
423
424struct SummaryView: View {
425 let fields: [SummaryFieldViewData]
426
427 var body: some View {
428 VStack(alignment: .leading, spacing: 12) {
429 SectionTitleView(title: "Summary")
430 LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 8) {
431 ForEach(fields) { field in
432 VStack(alignment: .leading, spacing: 4) {
433 Text(field.label)
434 .font(.system(.caption2, design: .monospaced))
435 .foregroundStyle(.secondary)
436 Text(field.value)
437 .font(.system(.caption, design: .monospaced))
438 .foregroundStyle(ResultColors.color(for: field.tone))
439 .lineLimit(2)
440 .textSelection(.enabled)
441 }
442 .frame(maxWidth: .infinity, alignment: .leading)
443 .padding(10)
444 .background(Color(.systemGray6).opacity(0.5))
445 .cornerRadius(6)
446 }
447 }
448 }
449 }
450}
451
452struct DomainChangeSummaryView: View {
453 let summary: DomainChangeSummary
454
455 var body: some View {
456 CardView(allowsHorizontalScroll: false) {
457 HStack {
458 Label(summary.hasChanges ? "Changed" : "Unchanged", systemImage: summary.hasChanges ? "arrow.triangle.2.circlepath" : "checkmark.circle")
459 .font(.system(.caption, design: .monospaced))
460 .foregroundStyle(summary.hasChanges ? .yellow : .green)
461 Spacer()
462 Text(summary.generatedAt, style: .time)
463 .font(.system(.caption2, design: .monospaced))
464 .foregroundStyle(.secondary)
465 }
466
467 Text(summary.changedSections.isEmpty ? "No meaningful changes detected." : summary.changedSections.joined(separator: " • "))
468 .font(.system(.caption, design: .monospaced))
469 .foregroundStyle(.primary)
470 }
471 }
472}
473
474struct DomainDiffView: View {
475 let title: String
476 let sections: [DomainDiffSection]
477 let showsUnchanged: Bool
478 @State private var collapsedSections = Set<UUID>()
479
480 private var filteredSections: [DomainDiffSection] {
481 guard showsUnchanged else {
482 return sections
483 .map { section in
484 DomainDiffSection(
485 title: section.title,
486 items: section.items.filter(\.hasChanges)
487 )
488 }
489 .filter { !$0.items.isEmpty }
490 }
491 return sections
492 }
493
494 var body: some View {
495 VStack(alignment: .leading, spacing: 12) {
496 SectionTitleView(title: title)
497 if filteredSections.isEmpty {
498 MessageCardView(text: "No comparison data available", isError: false)
499 } else {
500 ForEach(filteredSections) { section in
501 CardView(allowsHorizontalScroll: false) {
502 DisclosureGroup(isExpanded: binding(for: section)) {
503 let visibleItems = showsUnchanged ? section.items : section.items.filter(\.hasChanges)
504
505 ForEach(visibleItems) { item in
506 VStack(alignment: .leading, spacing: 6) {
507 HStack {
508 Text(item.label)
509 .font(.system(.caption, design: .monospaced))
510 .foregroundStyle(.secondary)
511 Spacer()
512 Text(changeLabel(for: item.changeType))
513 .font(.system(.caption2, design: .monospaced))
514 .foregroundStyle(changeColor(for: item.changeType))
515 .padding(.horizontal, 8)
516 .padding(.vertical, 4)
517 .background(changeColor(for: item.changeType).opacity(0.16))
518 .clipShape(Capsule())
519 }
520
521 if let oldValue = item.oldValue {
522 VStack(alignment: .leading, spacing: 2) {
523 Text("Old")
524 .font(.system(.caption2, design: .monospaced))
525 .foregroundStyle(.secondary)
526 Text(oldValue)
527 .font(.system(.caption2, design: .monospaced))
528 .foregroundStyle(.secondary)
529 .textSelection(.enabled)
530 }
531 }
532
533 if let newValue = item.newValue {
534 VStack(alignment: .leading, spacing: 2) {
535 Text("New")
536 .font(.system(.caption2, design: .monospaced))
537 .foregroundStyle(.secondary)
538 Text(newValue)
539 .font(.system(.caption, design: .monospaced))
540 .foregroundStyle(item.hasChanges ? .primary : .secondary)
541 .textSelection(.enabled)
542 }
543 }
544 }
545 .padding(10)
546 .background(item.hasChanges ? changeColor(for: item.changeType).opacity(0.08) : Color(.systemGray6).opacity(0.25))
547 .cornerRadius(8)
548 }
549 } label: {
550 HStack {
551 Text(section.title)
552 .font(.system(.subheadline, design: .monospaced))
553 .fontWeight(.semibold)
554 .foregroundStyle(.cyan)
555 Spacer()
556 Text(section.hasChanges ? "Changed" : "Unchanged")
557 .font(.system(.caption2, design: .monospaced))
558 .foregroundStyle(section.hasChanges ? .yellow : .secondary)
559 }
560 }
561 }
562 }
563 }
564 }
565 }
566
567 private func changeLabel(for changeType: DiffChangeType) -> String {
568 switch changeType {
569 case .added:
570 return "Added"
571 case .removed:
572 return "Removed"
573 case .changed:
574 return "Changed"
575 case .unchanged:
576 return "Unchanged"
577 }
578 }
579
580 private func changeColor(for changeType: DiffChangeType) -> Color {
581 switch changeType {
582 case .added:
583 return .green
584 case .removed:
585 return .red
586 case .changed:
587 return .yellow
588 case .unchanged:
589 return .secondary
590 }
591 }
592
593 private func binding(for section: DomainDiffSection) -> Binding<Bool> {
594 Binding(
595 get: { !collapsedSections.contains(section.id) },
596 set: { isExpanded in
597 if isExpanded {
598 collapsedSections.remove(section.id)
599 } else {
600 collapsedSections.insert(section.id)
601 }
602 }
603 )
604 }
605}
606
607struct TrackedDomainDetailHeaderView: View {
608 let trackedDomain: TrackedDomain
609
610 var body: some View {
611 VStack(alignment: .leading, spacing: 4) {
612 if let note = trackedDomain.note?.nilIfEmpty {
613 LabeledValueRow(row: InfoRowViewData(label: "Tracking Note", value: note, tone: .secondary))
614 }
615 HStack(spacing: 8) {
616 if trackedDomain.isPinned {
617 Label("Pinned", systemImage: "pin.fill")
618 }
619 Text("Last refresh \(trackedDomain.updatedAt.formatted(date: .abbreviated, time: .shortened))")
620 }
621 .font(.system(.caption2, design: .monospaced))
622 .foregroundStyle(.secondary)
623 }
624 }
625}
626
627struct DomainSectionView: View {
628 let rows: [InfoRowViewData]
629 let suggestions: [DomainSuggestionViewData]
630 let showSuggestions: Bool
631 let availabilityLoading: Bool
632 let suggestionsLoading: Bool
633 let trackedDomain: TrackedDomain?
634 let trackingLimitMessage: String?
635 let onTrack: () -> Void
636 let onTogglePinned: () -> Void
637 let onEditNote: (() -> Void)?
638
639 var body: some View {
640 VStack(alignment: .leading, spacing: 12) {
641 HStack {
642 SectionTitleView(title: "Domain")
643 Spacer()
644 if let trackedDomain {
645 HStack(spacing: 8) {
646 Text("Tracked")
647 .font(.system(.caption, design: .monospaced))
648 .foregroundStyle(.green)
649 Button {
650 onTogglePinned()
651 } label: {
652 Image(systemName: trackedDomain.isPinned ? "pin.fill" : "pin")
653 }
654 .buttonStyle(.bordered)
655 .font(.system(.caption, design: .monospaced))
656 if let onEditNote {
657 Button("Note") {
658 onEditNote()
659 }
660 .buttonStyle(.bordered)
661 .font(.system(.caption, design: .monospaced))
662 }
663 }
664 } else {
665 Button("Track") {
666 onTrack()
667 }
668 .buttonStyle(.bordered)
669 .font(.system(.caption, design: .monospaced))
670 }
671 }
672 CardView(allowsHorizontalScroll: false) {
673 ForEach(rows) { row in
674 LabeledValueRow(row: row)
675 }
676 if let trackedDomain {
677 TrackedDomainDetailHeaderView(trackedDomain: trackedDomain)
678 .padding(.top, 4)
679 } else if let trackingLimitMessage {
680 MessageRowView(text: trackingLimitMessage, isError: false)
681 .padding(.top, 4)
682 }
683 if availabilityLoading {
684 ProgressView("Checking availability…")
685 .appLoadingStyle()
686 .padding(.top, 4)
687 }
688 if showSuggestions {
689 Text("Suggestions")
690 .font(.system(.caption, design: .monospaced))
691 .foregroundStyle(.secondary)
692 .padding(.top, 4)
693 if suggestionsLoading {
694 ProgressView("Checking alternatives…")
695 .appLoadingStyle()
696 } else if suggestions.isEmpty {
697 MessageRowView(text: "No suggestions", isError: false)
698 } else {
699 ForEach(suggestions) { suggestion in
700 HStack {
701 Text(suggestion.domain)
702 .font(.system(.caption, design: .monospaced))
703 .foregroundStyle(.primary)
704 .textSelection(.enabled)
705 Spacer()
706 Text(suggestion.status)
707 .font(.system(.caption2, design: .monospaced))
708 .foregroundStyle(ResultColors.color(for: suggestion.tone))
709 }
710 }
711 }
712 }
713 }
714 }
715 }
716}
717
718struct DNSSectionView: View {
719 let dnssecLabel: String?
720 let sections: [DNSRecordSectionViewData]
721 let ptrMessage: SectionMessageViewData?
722 let loading: Bool
723 let sectionError: String?
724
725 var body: some View {
726 VStack(alignment: .leading, spacing: 12) {
727 HStack(alignment: .top, spacing: 8) {
728 SectionTitleView(title: "DNS")
729 Spacer()
730 if let dnssecLabel {
731 Text(dnssecLabel)
732 .font(.system(.caption2, design: .monospaced))
733 .foregroundStyle(.secondary)
734 .multilineTextAlignment(.trailing)
735 }
736 }
737
738 if loading {
739 LoadingCardView(text: "Querying DNS…")
740 } else if let sectionError, sections.isEmpty {
741 MessageCardView(text: sectionError, isError: true)
742 } else {
743 ForEach(sections) { section in
744 CardView {
745 Text(section.title)
746 .font(.system(.subheadline, design: .monospaced))
747 .fontWeight(.semibold)
748 .foregroundStyle(.cyan)
749
750 if let message = section.message {
751 MessageRowView(text: message.text, isError: message.isError)
752 }
753
754 ForEach(section.rows) { row in
755 LabeledValueRow(row: row)
756 }
757
758 if let wildcardTitle = section.wildcardTitle {
759 Text(wildcardTitle)
760 .font(.system(.caption, design: .monospaced))
761 .foregroundStyle(.secondary)
762 .padding(.top, 4)
763 ForEach(section.wildcardRows) { row in
764 LabeledValueRow(row: row)
765 }
766 }
767 }
768 }
769
770 if let ptrMessage {
771 CardView {
772 Text("PTR")
773 .font(.system(.subheadline, design: .monospaced))
774 .fontWeight(.semibold)
775 .foregroundStyle(.cyan)
776 MessageRowView(text: ptrMessage.text, isError: ptrMessage.isError)
777 }
778 }
779 }
780 }
781 }
782}
783
784struct WebSectionView: View {
785 let certificateRows: [InfoRowViewData]
786 let sslInfo: SSLCertificateInfo?
787 let sslLoading: Bool
788 let sslError: String?
789 let responseRows: [InfoRowViewData]
790 let headers: [HTTPHeader]
791 let headersLoading: Bool
792 let headersError: String?
793 let redirects: [RedirectHopViewData]
794 let redirectLoading: Bool
795 let redirectError: String?
796 let finalURL: String?
797
798 var body: some View {
799 VStack(alignment: .leading, spacing: 12) {
800 SectionTitleView(title: "Web")
801
802 CardView {
803 Text("TLS")
804 .font(.system(.subheadline, design: .monospaced))
805 .fontWeight(.semibold)
806 .foregroundStyle(.cyan)
807 if sslLoading {
808 ProgressView("Checking certificate…")
809 .appLoadingStyle()
810 } else if let sslError {
811 MessageRowView(text: sslError, isError: true)
812 } else {
813 ForEach(certificateRows) { row in
814 LabeledValueRow(row: row)
815 }
816 if let sslInfo, !sslInfo.subjectAltNames.isEmpty {
817 Text("SANs")
818 .font(.system(.caption2, design: .monospaced))
819 .foregroundStyle(.secondary)
820 ForEach(sslInfo.subjectAltNames, id: \.self) { san in
821 Text(san)
822 .font(.system(.caption, design: .monospaced))
823 .textSelection(.enabled)
824 }
825 }
826 }
827 }
828
829 CardView {
830 Text("Headers")
831 .font(.system(.subheadline, design: .monospaced))
832 .fontWeight(.semibold)
833 .foregroundStyle(.cyan)
834 if headersLoading {
835 ProgressView("Fetching headers…")
836 .appLoadingStyle()
837 } else if let headersError {
838 MessageRowView(text: headersError, isError: true)
839 } else {
840 ForEach(responseRows) { row in
841 LabeledValueRow(row: row)
842 }
843 if headers.isEmpty {
844 MessageRowView(text: "No HTTP headers returned", isError: false)
845 } else {
846 ForEach(headers) { header in
847 HStack(alignment: .top, spacing: 4) {
848 Text(header.name + ":")
849 .font(.system(.caption, design: .monospaced))
850 .foregroundStyle(header.isSecurityHeader ? .yellow : .cyan)
851 Text(header.value)
852 .font(.system(.caption, design: .monospaced))
853 .foregroundStyle(.primary)
854 .textSelection(.enabled)
855 }
856 }
857 }
858 }
859 }
860
861 CardView {
862 Text("Redirects")
863 .font(.system(.subheadline, design: .monospaced))
864 .fontWeight(.semibold)
865 .foregroundStyle(.cyan)
866 if redirectLoading {
867 ProgressView("Tracing redirects…")
868 .appLoadingStyle()
869 } else if let redirectError {
870 MessageRowView(text: redirectError, isError: true)
871 } else if redirects.isEmpty {
872 MessageRowView(text: "No redirect data available", isError: false)
873 } else {
874 if let finalURL {
875 LabeledValueRow(row: InfoRowViewData(label: "Final URL", value: finalURL, tone: .secondary))
876 }
877 ForEach(redirects) { redirect in
878 HStack(alignment: .top, spacing: 6) {
879 Text(redirect.stepLabel)
880 .font(.system(.caption, design: .monospaced))
881 .foregroundStyle(.secondary)
882 .frame(width: 16, alignment: .trailing)
883 Text(redirect.statusCode)
884 .font(.system(.caption, design: .monospaced))
885 .foregroundStyle(.cyan)
886 .frame(width: 36, alignment: .leading)
887 Text(redirect.url)
888 .font(.system(.caption, design: .monospaced))
889 .textSelection(.enabled)
890 if redirect.isFinal {
891 Text("(final)")
892 .font(.system(.caption2, design: .monospaced))
893 .foregroundStyle(.secondary)
894 }
895 }
896 }
897 }
898 }
899 }
900 }
901}
902
903struct EmailSectionView: View {
904 let rows: [EmailRowViewData]
905 let loading: Bool
906 let error: String?
907
908 var body: some View {
909 VStack(alignment: .leading, spacing: 12) {
910 SectionTitleView(title: "Email")
911 CardView {
912 if loading {
913 ProgressView("Checking email records…")
914 .appLoadingStyle()
915 } else if let error {
916 MessageRowView(text: error, isError: true)
917 } else if rows.isEmpty {
918 MessageRowView(text: "No email security records found", isError: false)
919 } else {
920 ForEach(rows) { row in
921 VStack(alignment: .leading, spacing: 4) {
922 HStack(spacing: 8) {
923 Text(row.label)
924 .font(.system(.caption, design: .monospaced))
925 .foregroundStyle(.cyan)
926 .frame(width: 76, alignment: .leading)
927 Text(row.status)
928 .font(.system(.caption, design: .monospaced))
929 .foregroundStyle(ResultColors.color(for: row.statusTone))
930 }
931 Text(row.detail)
932 .font(.system(.caption2, design: .monospaced))
933 .foregroundStyle(.primary)
934 .textSelection(.enabled)
935 if let auxiliaryDetail = row.auxiliaryDetail {
936 Text(auxiliaryDetail)
937 .font(.system(.caption2, design: .monospaced))
938 .foregroundStyle(.secondary)
939 }
940 }
941 }
942 }
943 }
944 }
945 }
946}
947
948struct NetworkSectionView: View {
949 let reachabilityRows: [ReachabilityRowViewData]
950 let reachabilityLoading: Bool
951 let reachabilityError: String?
952 let locationRows: [InfoRowViewData]
953 let geolocation: IPGeolocation?
954 let geolocationLoading: Bool
955 let geolocationError: String?
956 let standardPortRows: [PortScanRowViewData]
957 let customPortRows: [PortScanRowViewData]
958 let portScanLoading: Bool
959 let portScanError: String?
960 let customPortScanLoading: Bool
961 let customPortScanError: String?
962 let isCloudflareProxied: Bool
963 @Binding var customPortsExpanded: Bool
964 @Binding var customPortInput: String
965 let onScanCustomPorts: () -> Void
966
967 var body: some View {
968 VStack(alignment: .leading, spacing: 12) {
969 SectionTitleView(title: "Network")
970
971 CardView {
972 Text("Reachability")
973 .font(.system(.subheadline, design: .monospaced))
974 .fontWeight(.semibold)
975 .foregroundStyle(.cyan)
976 if reachabilityLoading {
977 ProgressView("Checking ports…")
978 .appLoadingStyle()
979 } else if let reachabilityError {
980 MessageRowView(text: reachabilityError, isError: true)
981 } else {
982 ForEach(reachabilityRows) { row in
983 HStack {
984 Text(row.portLabel)
985 .font(.system(.caption, design: .monospaced))
986 Spacer()
987 Text(row.latencyLabel)
988 .font(.system(.caption2, design: .monospaced))
989 .foregroundStyle(.secondary)
990 Text(row.statusLabel)
991 .font(.system(.caption, design: .monospaced))
992 .foregroundStyle(ResultColors.color(for: row.statusTone))
993 }
994 }
995 }
996 }
997
998 CardView(allowsHorizontalScroll: false) {
999 Text("Location")
1000 .font(.system(.subheadline, design: .monospaced))
1001 .fontWeight(.semibold)
1002 .foregroundStyle(.cyan)
1003 if geolocationLoading {
1004 ProgressView("Looking up location…")
1005 .appLoadingStyle()
1006 } else if let geolocationError, geolocation == nil {
1007 MessageRowView(text: geolocationError, isError: geolocationError != "No A record available")
1008 } else if let geolocation {
1009 ForEach(locationRows) { row in
1010 LabeledValueRow(row: row)
1011 }
1012 if let latitude = geolocation.latitude, let longitude = geolocation.longitude {
1013 let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
1014 Map(initialPosition: .region(MKCoordinateRegion(
1015 center: coordinate,
1016 span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)
1017 ))) {
1018 Marker(geolocation.ip, coordinate: coordinate)
1019 }
1020 .mapStyle(.standard)
1021 .frame(maxWidth: .infinity)
1022 .frame(height: 180)
1023 .cornerRadius(8)
1024 }
1025 } else {
1026 MessageRowView(text: "No location data available", isError: false)
1027 }
1028 }
1029
1030 CardView(allowsHorizontalScroll: false) {
1031 Text("Port Scan")
1032 .font(.system(.subheadline, design: .monospaced))
1033 .fontWeight(.semibold)
1034 .foregroundStyle(.cyan)
1035
1036 if isCloudflareProxied {
1037 Text("Domain is behind Cloudflare's proxy. Results reflect the edge, not the origin.")
1038 .font(.system(.caption2, design: .monospaced))
1039 .foregroundStyle(.orange)
1040 .fixedSize(horizontal: false, vertical: true)
1041 }
1042
1043 if portScanLoading {
1044 ProgressView("Scanning ports…")
1045 .appLoadingStyle()
1046 } else if let portScanError, standardPortRows.isEmpty {
1047 MessageRowView(text: portScanError, isError: true)
1048 } else {
1049 Text("Standard Ports")
1050 .font(.system(.caption, design: .monospaced))
1051 .foregroundStyle(.secondary)
1052 PortRowsView(rows: standardPortRows)
1053 }
1054
1055 DisclosureGroup("Custom Ports", isExpanded: $customPortsExpanded) {
1056 VStack(alignment: .leading, spacing: 10) {
1057 TextField("8888, 9000, 27017", text: $customPortInput)
1058 .font(.system(.caption, design: .monospaced))
1059 .textInputAutocapitalization(.never)
1060 .autocorrectionDisabled()
1061 .keyboardType(.numberPad)
1062 .padding(10)
1063 .background(Color(.systemGray6).opacity(0.5))
1064 .cornerRadius(6)
1065
1066 Button("Scan") {
1067 onScanCustomPorts()
1068 }
1069 .buttonStyle(.borderedProminent)
1070 .disabled(customPortScanLoading)
1071
1072 if customPortScanLoading {
1073 ProgressView("Scanning custom ports…")
1074 .appLoadingStyle()
1075 } else if let customPortScanError {
1076 MessageRowView(text: customPortScanError, isError: true)
1077 } else {
1078 PortRowsView(rows: customPortRows)
1079 }
1080 }
1081 .padding(.top, 8)
1082 }
1083 .font(.system(.caption, design: .monospaced))
1084 .tint(.secondary)
1085 }
1086 }
1087 }
1088}
1089
1090struct PortRowsView: View {
1091 let rows: [PortScanRowViewData]
1092
1093 var body: some View {
1094 if rows.isEmpty {
1095 MessageRowView(text: "No results", isError: false)
1096 } else {
1097 ForEach(rows) { row in
1098 VStack(alignment: .leading, spacing: 2) {
1099 HStack {
1100 Text(row.portLabel)
1101 .font(.system(.caption, design: .monospaced))
1102 .frame(width: 52, alignment: .leading)
1103 Text(row.service)
1104 .font(.system(.caption, design: .monospaced))
1105 .foregroundStyle(.primary)
1106 Spacer()
1107 if let durationLabel = row.durationLabel {
1108 Text(durationLabel)
1109 .font(.system(.caption2, design: .monospaced))
1110 .foregroundStyle(.secondary)
1111 }
1112 Text(row.statusLabel)
1113 .font(.system(.caption2, design: .monospaced))
1114 .foregroundStyle(ResultColors.color(for: row.statusTone))
1115 }
1116 if let banner = row.banner {
1117 Text(banner)
1118 .font(.system(.caption2, design: .monospaced))
1119 .foregroundStyle(.secondary)
1120 .padding(.leading, 8)
1121 }
1122 }
1123 }
1124 }
1125 }
1126}
1127
1128struct SectionTitleView: View {
1129 let title: String
1130
1131 var body: some View {
1132 Text(title)
1133 .font(.system(.headline))
1134 .foregroundStyle(.white)
1135 }
1136}
1137
1138struct CardView<Content: View>: View {
1139 let allowsHorizontalScroll: Bool
1140 let content: Content
1141
1142 init(allowsHorizontalScroll: Bool = true, @ViewBuilder content: () -> Content) {
1143 self.allowsHorizontalScroll = allowsHorizontalScroll
1144 self.content = content()
1145 }
1146
1147 var body: some View {
1148 Group {
1149 if allowsHorizontalScroll {
1150 ScrollView(.horizontal) {
1151 cardContent
1152 .scrollTargetLayout()
1153 }
1154 .scrollBounceBehavior(.basedOnSize, axes: .horizontal)
1155 } else {
1156 cardContent
1157 .frame(maxWidth: .infinity, alignment: .leading)
1158 }
1159 }
1160 .frame(maxWidth: .infinity, alignment: .leading)
1161 .padding(10)
1162 .background(Color(.systemGray6).opacity(0.5))
1163 .cornerRadius(6)
1164 }
1165
1166 private var cardContent: some View {
1167 VStack(alignment: .leading, spacing: 6) {
1168 content
1169 }
1170 }
1171}
1172
1173struct LoadingCardView: View {
1174 let text: String
1175
1176 var body: some View {
1177 CardView {
1178 ProgressView(text)
1179 .appLoadingStyle()
1180 .frame(maxWidth: .infinity, alignment: .center)
1181 }
1182 }
1183}
1184
1185struct MessageCardView: View {
1186 let text: String
1187 let isError: Bool
1188
1189 var body: some View {
1190 CardView {
1191 MessageRowView(text: text, isError: isError)
1192 }
1193 }
1194}
1195
1196struct MessageRowView: View {
1197 let text: String
1198 let isError: Bool
1199
1200 var body: some View {
1201 Label(text, systemImage: isError ? "exclamationmark.triangle.fill" : "info.circle")
1202 .font(.system(.caption, design: .monospaced))
1203 .foregroundStyle(isError ? .red : .secondary)
1204 }
1205}
1206
1207struct LabeledValueRow: View {
1208 let row: InfoRowViewData
1209
1210 var body: some View {
1211 VStack(alignment: .leading, spacing: 2) {
1212 Text(row.label)
1213 .font(.system(.caption2, design: .monospaced))
1214 .foregroundStyle(.secondary)
1215 Text(row.value)
1216 .font(.system(.caption, design: .monospaced))
1217 .foregroundStyle(ResultColors.color(for: row.tone))
1218 .textSelection(.enabled)
1219 }
1220 }
1221}
1222
1223enum ResultColors {
1224 static func color(for tone: ResultTone) -> Color {
1225 switch tone {
1226 case .primary:
1227 return .primary
1228 case .secondary:
1229 return .secondary
1230 case .success:
1231 return .green
1232 case .warning:
1233 return .yellow
1234 case .failure:
1235 return .red
1236 }
1237 }
1238}
1239
1240extension DateFormatter {
1241 static let certDate: DateFormatter = {
1242 let formatter = DateFormatter()
1243 formatter.dateStyle = .medium
1244 formatter.timeStyle = .short
1245 return formatter
1246 }()
1247}
1248
1249private extension View {
1250 func appLoadingStyle() -> some View {
1251 font(.system(.caption, design: .monospaced))
1252 }
1253}
1254
1255private extension String {
1256 var nilIfEmpty: String? {
1257 isEmpty ? nil : self
1258 }
1259}
1260
1261private struct SettingsView: View {
1262 @AppStorage(DNSResolverOption.userDefaultsKey)
1263 private var storedResolverURL = DNSResolverOption.defaultURLString
1264
1265 @State private var resolverOption: DNSResolverOption = .cloudflare
1266 @State private var customResolverURL = DNSResolverOption.defaultURLString
1267
1268 private var customResolverError: String? {
1269 guard resolverOption == .custom else {
1270 return nil
1271 }
1272 return DNSResolverOption.isValidCustomURL(customResolverURL) ? nil : "Resolver URL must start with https://"
1273 }
1274
1275 var body: some View {
1276 Form {
1277 Section {
1278 Picker("Resolver", selection: $resolverOption) {
1279 ForEach(DNSResolverOption.allCases) { option in
1280 Text(option.title).tag(option)
1281 }
1282 }
1283
1284 if resolverOption == .custom {
1285 TextField("https://resolver.example/dns-query", text: $customResolverURL)
1286 .textInputAutocapitalization(.never)
1287 .autocorrectionDisabled()
1288 .keyboardType(.URL)
1289
1290 if let customResolverError {
1291 Text(customResolverError)
1292 .font(.caption)
1293 .foregroundStyle(.red)
1294 }
1295 }
1296 }
1297 }
1298 .navigationTitle("Settings")
1299 .onAppear {
1300 let currentResolverURL = storedResolverURL.trimmingCharacters(in: .whitespacesAndNewlines)
1301 resolverOption = DNSResolverOption.option(for: currentResolverURL)
1302 customResolverURL = resolverOption == .custom ? currentResolverURL : DNSResolverOption.defaultURLString
1303 }
1304 .onChange(of: resolverOption) { _, newValue in
1305 guard let presetURL = newValue.urlString else {
1306 storedResolverURL = customResolverURL.trimmingCharacters(in: .whitespacesAndNewlines)
1307 return
1308 }
1309 storedResolverURL = presetURL
1310 }
1311 .onChange(of: customResolverURL) { _, newValue in
1312 guard resolverOption == .custom else { return }
1313 storedResolverURL = newValue.trimmingCharacters(in: .whitespacesAndNewlines)
1314 }
1315 }
1316}
1317
1318#Preview {
1319 ContentView()
1320}