DomainDig/ContentView.swift
950 lines · 37791 bytes
1import MapKit
2import SwiftUI
3
4struct ContentView: View {
5 @State private var viewModel = DomainViewModel()
6 @FocusState private var domainFieldFocused: Bool
7 @State private var customPortInput = ""
8 @State private var customPortsExpanded = false
9
10 var body: some View {
11 NavigationStack {
12 ScrollView(.vertical) {
13 VStack(spacing: 0) {
14 inputSection
15 if viewModel.hasRun {
16 actionButtons
17 SummaryView(fields: viewModel.summaryFields)
18 .padding(.top, 8)
19 DomainSectionView(
20 rows: viewModel.domainRows,
21 suggestions: viewModel.suggestionRows,
22 showSuggestions: viewModel.availabilityResult?.status == .registered || viewModel.suggestionsLoading,
23 availabilityLoading: viewModel.availabilityLoading,
24 suggestionsLoading: viewModel.suggestionsLoading,
25 isWatched: viewModel.isCurrentDomainWatched,
26 onToggleWatch: { viewModel.toggleWatchedDomain() }
27 )
28 .padding(.top, 16)
29 DNSSectionView(
30 dnssecLabel: viewModel.dnssecLabel,
31 sections: viewModel.dnsRows,
32 ptrMessage: viewModel.ptrMessage,
33 loading: viewModel.dnsLoading || viewModel.ptrLoading,
34 sectionError: viewModel.dnsError
35 )
36 .padding(.top, 16)
37 WebSectionView(
38 certificateRows: viewModel.webCertificateRows,
39 sslInfo: viewModel.sslInfo,
40 sslLoading: viewModel.sslLoading || viewModel.hstsLoading,
41 sslError: viewModel.sslError,
42 responseRows: viewModel.webResponseRows,
43 headers: viewModel.httpHeaders,
44 headersLoading: viewModel.httpHeadersLoading,
45 headersError: viewModel.httpHeadersError,
46 redirects: viewModel.redirectRows,
47 redirectLoading: viewModel.redirectChainLoading,
48 redirectError: viewModel.redirectChainError,
49 finalURL: viewModel.currentSnapshot.redirectChain.last?.url
50 )
51 .padding(.top, 16)
52 EmailSectionView(
53 rows: viewModel.emailRows,
54 loading: viewModel.emailSecurityLoading,
55 error: viewModel.emailSecurityError
56 )
57 .padding(.top, 16)
58 NetworkSectionView(
59 reachabilityRows: viewModel.reachabilityRows,
60 reachabilityLoading: viewModel.reachabilityLoading,
61 reachabilityError: viewModel.reachabilityError,
62 locationRows: viewModel.locationRows,
63 geolocation: viewModel.ipGeolocation,
64 geolocationLoading: viewModel.ipGeolocationLoading,
65 geolocationError: viewModel.ipGeolocationError,
66 standardPortRows: viewModel.standardPortRows,
67 customPortRows: viewModel.customPortRows,
68 portScanLoading: viewModel.portScanLoading,
69 portScanError: viewModel.portScanError,
70 customPortScanLoading: viewModel.customPortScanLoading,
71 customPortScanError: viewModel.customPortScanError,
72 isCloudflareProxied: viewModel.isCloudflareProxied,
73 customPortsExpanded: $customPortsExpanded,
74 customPortInput: $customPortInput,
75 onScanCustomPorts: runCustomPortScan
76 )
77 .padding(.top, 16)
78 } else if !viewModel.recentSearches.isEmpty {
79 recentSearchesSection
80 }
81 }
82 .padding(.horizontal)
83 .padding(.bottom, 32)
84 }
85 .background(Color.black)
86 .navigationTitle("DomainDig")
87 .toolbarColorScheme(.dark, for: .navigationBar)
88 .preferredColorScheme(.dark)
89 .toolbar {
90 ToolbarItemGroup(placement: .topBarTrailing) {
91 if viewModel.hasRun {
92 Button {
93 viewModel.reset()
94 } label: {
95 Image(systemName: "xmark.circle")
96 .foregroundStyle(.secondary)
97 }
98 }
99 NavigationLink {
100 SavedDomainsView(viewModel: viewModel)
101 } label: {
102 Image(systemName: "bookmark")
103 .foregroundStyle(.secondary)
104 }
105 NavigationLink {
106 HistoryView(viewModel: viewModel)
107 } label: {
108 Image(systemName: "clock.arrow.trianglehead.counterclockwise.rotate.90")
109 .foregroundStyle(.secondary)
110 }
111 NavigationLink {
112 WatchlistView(viewModel: viewModel)
113 } label: {
114 Image(systemName: "eye")
115 .foregroundStyle(.secondary)
116 }
117 NavigationLink {
118 SettingsView()
119 } label: {
120 Image(systemName: "gearshape")
121 .foregroundStyle(.secondary)
122 }
123 }
124 }
125 }
126 .onAppear {
127 domainFieldFocused = true
128 }
129 }
130
131 private var inputSection: some View {
132 VStack(spacing: 12) {
133 TextField("e.g. cleberg.net", text: $viewModel.domain)
134 .font(.system(.title3, design: .monospaced))
135 .textInputAutocapitalization(.never)
136 .autocorrectionDisabled()
137 .keyboardType(.URL)
138 .padding(12)
139 .background(Color(.systemGray6))
140 .cornerRadius(8)
141 .focused($domainFieldFocused)
142 .onSubmit { viewModel.run() }
143
144 Button {
145 domainFieldFocused = false
146 viewModel.run()
147 } label: {
148 Text("Run")
149 .font(.headline)
150 .frame(maxWidth: .infinity)
151 .padding(.vertical, 12)
152 }
153 .buttonStyle(.borderedProminent)
154 .disabled(viewModel.trimmedDomain.isEmpty)
155 }
156 .padding(.vertical, 16)
157 }
158
159 private var actionButtons: some View {
160 HStack {
161 Spacer()
162 if viewModel.resultsLoaded {
163 Button {
164 viewModel.toggleSavedDomain()
165 } label: {
166 Image(systemName: viewModel.isCurrentDomainSaved ? "bookmark.fill" : "bookmark")
167 .font(.system(.body))
168 .foregroundStyle(viewModel.isCurrentDomainSaved ? .yellow : .secondary)
169 }
170 Button {
171 shareResults()
172 } label: {
173 Image(systemName: "square.and.arrow.up")
174 .font(.system(.body))
175 .foregroundStyle(.secondary)
176 }
177 }
178 }
179 }
180
181 private var recentSearchesSection: some View {
182 VStack(alignment: .leading, spacing: 8) {
183 HStack {
184 Text("RECENT")
185 .font(.system(.caption2, design: .monospaced))
186 .foregroundStyle(.secondary)
187 Spacer()
188 Button("Clear") {
189 viewModel.clearRecentSearches()
190 }
191 .font(.system(.caption2, design: .monospaced))
192 .foregroundStyle(.secondary)
193 }
194
195 ForEach(viewModel.recentSearches, id: \.self) { domain in
196 Button {
197 viewModel.domain = domain
198 domainFieldFocused = false
199 viewModel.run()
200 } label: {
201 Text(domain)
202 .font(.system(.callout, design: .monospaced))
203 .foregroundStyle(.primary)
204 .frame(maxWidth: .infinity, alignment: .leading)
205 .padding(.vertical, 6)
206 .padding(.horizontal, 10)
207 .background(Color(.systemGray6).opacity(0.5))
208 .cornerRadius(6)
209 }
210 }
211 }
212 .padding(.top, 8)
213 }
214
215 private func runCustomPortScan() {
216 let ports = parsedCustomPorts(from: customPortInput)
217 Task {
218 await viewModel.runCustomPortScan(ports: ports)
219 }
220 }
221
222 private func parsedCustomPorts(from input: String) -> [UInt16] {
223 let parts = input.split(separator: ",", omittingEmptySubsequences: true)
224 var seen = Set<UInt16>()
225 var ports: [UInt16] = []
226
227 for part in parts {
228 let trimmed = part.trimmingCharacters(in: .whitespacesAndNewlines)
229 guard let value = UInt16(trimmed), seen.insert(value).inserted else {
230 continue
231 }
232 ports.append(value)
233 if ports.count == 20 {
234 break
235 }
236 }
237
238 return ports
239 }
240
241 private func shareResults() {
242 let text = viewModel.exportText()
243 let dateFmt = DateFormatter()
244 dateFmt.dateFormat = "yyyyMMdd_HHmmss"
245 let timestamp = dateFmt.string(from: Date())
246 let filename = "\(timestamp)_domaindigresults.txt"
247 let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(filename)
248
249 do {
250 try text.write(to: tempURL, atomically: true, encoding: .utf8)
251 } catch {
252 return
253 }
254
255 let activityVC = UIActivityViewController(activityItems: [tempURL], applicationActivities: nil)
256 guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
257 let rootVC = windowScene.keyWindow?.rootViewController else { return }
258 var presenter = rootVC
259 while let presented = presenter.presentedViewController {
260 presenter = presented
261 }
262 activityVC.popoverPresentationController?.sourceView = presenter.view
263 presenter.present(activityVC, animated: true)
264 }
265}
266
267struct SummaryView: View {
268 let fields: [SummaryFieldViewData]
269
270 var body: some View {
271 VStack(alignment: .leading, spacing: 12) {
272 SectionTitleView(title: "Summary")
273 LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 8) {
274 ForEach(fields) { field in
275 VStack(alignment: .leading, spacing: 4) {
276 Text(field.label)
277 .font(.system(.caption2, design: .monospaced))
278 .foregroundStyle(.secondary)
279 Text(field.value)
280 .font(.system(.caption, design: .monospaced))
281 .foregroundStyle(ResultColors.color(for: field.tone))
282 .lineLimit(2)
283 .textSelection(.enabled)
284 }
285 .frame(maxWidth: .infinity, alignment: .leading)
286 .padding(10)
287 .background(Color(.systemGray6).opacity(0.5))
288 .cornerRadius(6)
289 }
290 }
291 }
292 }
293}
294
295struct DomainSectionView: View {
296 let rows: [InfoRowViewData]
297 let suggestions: [DomainSuggestionViewData]
298 let showSuggestions: Bool
299 let availabilityLoading: Bool
300 let suggestionsLoading: Bool
301 let isWatched: Bool
302 let onToggleWatch: () -> Void
303
304 var body: some View {
305 VStack(alignment: .leading, spacing: 12) {
306 HStack {
307 SectionTitleView(title: "Domain")
308 Spacer()
309 Button(isWatched ? "Watching" : "Watch") {
310 onToggleWatch()
311 }
312 .buttonStyle(.bordered)
313 .font(.system(.caption, design: .monospaced))
314 }
315 CardView {
316 ForEach(rows) { row in
317 LabeledValueRow(row: row)
318 }
319 if availabilityLoading {
320 ProgressView("Checking availability…")
321 .appLoadingStyle()
322 .padding(.top, 4)
323 }
324 if showSuggestions {
325 Text("Suggestions")
326 .font(.system(.caption, design: .monospaced))
327 .foregroundStyle(.secondary)
328 .padding(.top, 4)
329 if suggestionsLoading {
330 ProgressView("Checking alternatives…")
331 .appLoadingStyle()
332 } else if suggestions.isEmpty {
333 MessageRowView(text: "No suggestions", isError: false)
334 } else {
335 ForEach(suggestions) { suggestion in
336 HStack {
337 Text(suggestion.domain)
338 .font(.system(.caption, design: .monospaced))
339 .foregroundStyle(.primary)
340 .textSelection(.enabled)
341 Spacer()
342 Text(suggestion.status)
343 .font(.system(.caption2, design: .monospaced))
344 .foregroundStyle(ResultColors.color(for: suggestion.tone))
345 }
346 }
347 }
348 }
349 }
350 }
351 }
352}
353
354struct DNSSectionView: View {
355 let dnssecLabel: String?
356 let sections: [DNSRecordSectionViewData]
357 let ptrMessage: SectionMessageViewData?
358 let loading: Bool
359 let sectionError: String?
360
361 var body: some View {
362 VStack(alignment: .leading, spacing: 12) {
363 HStack(alignment: .top, spacing: 8) {
364 SectionTitleView(title: "DNS")
365 Spacer()
366 if let dnssecLabel {
367 Text(dnssecLabel)
368 .font(.system(.caption2, design: .monospaced))
369 .foregroundStyle(.secondary)
370 .multilineTextAlignment(.trailing)
371 }
372 }
373
374 if loading {
375 LoadingCardView(text: "Querying DNS…")
376 } else if let sectionError, sections.isEmpty {
377 MessageCardView(text: sectionError, isError: true)
378 } else {
379 ForEach(sections) { section in
380 CardView {
381 Text(section.title)
382 .font(.system(.subheadline, design: .monospaced))
383 .fontWeight(.semibold)
384 .foregroundStyle(.cyan)
385
386 if let message = section.message {
387 MessageRowView(text: message.text, isError: message.isError)
388 }
389
390 ForEach(section.rows) { row in
391 LabeledValueRow(row: row)
392 }
393
394 if let wildcardTitle = section.wildcardTitle {
395 Text(wildcardTitle)
396 .font(.system(.caption, design: .monospaced))
397 .foregroundStyle(.secondary)
398 .padding(.top, 4)
399 ForEach(section.wildcardRows) { row in
400 LabeledValueRow(row: row)
401 }
402 }
403 }
404 }
405
406 if let ptrMessage {
407 CardView {
408 Text("PTR")
409 .font(.system(.subheadline, design: .monospaced))
410 .fontWeight(.semibold)
411 .foregroundStyle(.cyan)
412 MessageRowView(text: ptrMessage.text, isError: ptrMessage.isError)
413 }
414 }
415 }
416 }
417 }
418}
419
420struct WebSectionView: View {
421 let certificateRows: [InfoRowViewData]
422 let sslInfo: SSLCertificateInfo?
423 let sslLoading: Bool
424 let sslError: String?
425 let responseRows: [InfoRowViewData]
426 let headers: [HTTPHeader]
427 let headersLoading: Bool
428 let headersError: String?
429 let redirects: [RedirectHopViewData]
430 let redirectLoading: Bool
431 let redirectError: String?
432 let finalURL: String?
433
434 var body: some View {
435 VStack(alignment: .leading, spacing: 12) {
436 SectionTitleView(title: "Web")
437
438 CardView {
439 Text("TLS")
440 .font(.system(.subheadline, design: .monospaced))
441 .fontWeight(.semibold)
442 .foregroundStyle(.cyan)
443 if sslLoading {
444 ProgressView("Checking certificate…")
445 .appLoadingStyle()
446 } else if let sslError {
447 MessageRowView(text: sslError, isError: true)
448 } else {
449 ForEach(certificateRows) { row in
450 LabeledValueRow(row: row)
451 }
452 if let sslInfo, !sslInfo.subjectAltNames.isEmpty {
453 Text("SANs")
454 .font(.system(.caption2, design: .monospaced))
455 .foregroundStyle(.secondary)
456 ForEach(sslInfo.subjectAltNames, id: \.self) { san in
457 Text(san)
458 .font(.system(.caption, design: .monospaced))
459 .textSelection(.enabled)
460 }
461 }
462 }
463 }
464
465 CardView {
466 Text("Headers")
467 .font(.system(.subheadline, design: .monospaced))
468 .fontWeight(.semibold)
469 .foregroundStyle(.cyan)
470 if headersLoading {
471 ProgressView("Fetching headers…")
472 .appLoadingStyle()
473 } else if let headersError {
474 MessageRowView(text: headersError, isError: true)
475 } else {
476 ForEach(responseRows) { row in
477 LabeledValueRow(row: row)
478 }
479 if headers.isEmpty {
480 MessageRowView(text: "No HTTP headers returned", isError: false)
481 } else {
482 ForEach(headers) { header in
483 HStack(alignment: .top, spacing: 4) {
484 Text(header.name + ":")
485 .font(.system(.caption, design: .monospaced))
486 .foregroundStyle(header.isSecurityHeader ? .yellow : .cyan)
487 Text(header.value)
488 .font(.system(.caption, design: .monospaced))
489 .foregroundStyle(.primary)
490 .textSelection(.enabled)
491 }
492 }
493 }
494 }
495 }
496
497 CardView {
498 Text("Redirects")
499 .font(.system(.subheadline, design: .monospaced))
500 .fontWeight(.semibold)
501 .foregroundStyle(.cyan)
502 if redirectLoading {
503 ProgressView("Tracing redirects…")
504 .appLoadingStyle()
505 } else if let redirectError {
506 MessageRowView(text: redirectError, isError: true)
507 } else if redirects.isEmpty {
508 MessageRowView(text: "No redirect data available", isError: false)
509 } else {
510 if let finalURL {
511 LabeledValueRow(row: InfoRowViewData(label: "Final URL", value: finalURL, tone: .secondary))
512 }
513 ForEach(redirects) { redirect in
514 HStack(alignment: .top, spacing: 6) {
515 Text(redirect.stepLabel)
516 .font(.system(.caption, design: .monospaced))
517 .foregroundStyle(.secondary)
518 .frame(width: 16, alignment: .trailing)
519 Text(redirect.statusCode)
520 .font(.system(.caption, design: .monospaced))
521 .foregroundStyle(.cyan)
522 .frame(width: 36, alignment: .leading)
523 Text(redirect.url)
524 .font(.system(.caption, design: .monospaced))
525 .textSelection(.enabled)
526 if redirect.isFinal {
527 Text("(final)")
528 .font(.system(.caption2, design: .monospaced))
529 .foregroundStyle(.secondary)
530 }
531 }
532 }
533 }
534 }
535 }
536 }
537}
538
539struct EmailSectionView: View {
540 let rows: [EmailRowViewData]
541 let loading: Bool
542 let error: String?
543
544 var body: some View {
545 VStack(alignment: .leading, spacing: 12) {
546 SectionTitleView(title: "Email")
547 CardView {
548 if loading {
549 ProgressView("Checking email records…")
550 .appLoadingStyle()
551 } else if let error {
552 MessageRowView(text: error, isError: true)
553 } else if rows.isEmpty {
554 MessageRowView(text: "No email security records found", isError: false)
555 } else {
556 ForEach(rows) { row in
557 VStack(alignment: .leading, spacing: 4) {
558 HStack(spacing: 8) {
559 Text(row.label)
560 .font(.system(.caption, design: .monospaced))
561 .foregroundStyle(.cyan)
562 .frame(width: 76, alignment: .leading)
563 Text(row.status)
564 .font(.system(.caption, design: .monospaced))
565 .foregroundStyle(ResultColors.color(for: row.statusTone))
566 }
567 Text(row.detail)
568 .font(.system(.caption2, design: .monospaced))
569 .foregroundStyle(.primary)
570 .textSelection(.enabled)
571 if let auxiliaryDetail = row.auxiliaryDetail {
572 Text(auxiliaryDetail)
573 .font(.system(.caption2, design: .monospaced))
574 .foregroundStyle(.secondary)
575 }
576 }
577 }
578 }
579 }
580 }
581 }
582}
583
584struct NetworkSectionView: View {
585 let reachabilityRows: [ReachabilityRowViewData]
586 let reachabilityLoading: Bool
587 let reachabilityError: String?
588 let locationRows: [InfoRowViewData]
589 let geolocation: IPGeolocation?
590 let geolocationLoading: Bool
591 let geolocationError: String?
592 let standardPortRows: [PortScanRowViewData]
593 let customPortRows: [PortScanRowViewData]
594 let portScanLoading: Bool
595 let portScanError: String?
596 let customPortScanLoading: Bool
597 let customPortScanError: String?
598 let isCloudflareProxied: Bool
599 @Binding var customPortsExpanded: Bool
600 @Binding var customPortInput: String
601 let onScanCustomPorts: () -> Void
602
603 var body: some View {
604 VStack(alignment: .leading, spacing: 12) {
605 SectionTitleView(title: "Network")
606
607 CardView {
608 Text("Reachability")
609 .font(.system(.subheadline, design: .monospaced))
610 .fontWeight(.semibold)
611 .foregroundStyle(.cyan)
612 if reachabilityLoading {
613 ProgressView("Checking ports…")
614 .appLoadingStyle()
615 } else if let reachabilityError {
616 MessageRowView(text: reachabilityError, isError: true)
617 } else {
618 ForEach(reachabilityRows) { row in
619 HStack {
620 Text(row.portLabel)
621 .font(.system(.caption, design: .monospaced))
622 Spacer()
623 Text(row.latencyLabel)
624 .font(.system(.caption2, design: .monospaced))
625 .foregroundStyle(.secondary)
626 Text(row.statusLabel)
627 .font(.system(.caption, design: .monospaced))
628 .foregroundStyle(ResultColors.color(for: row.statusTone))
629 }
630 }
631 }
632 }
633
634 CardView(allowsHorizontalScroll: false) {
635 Text("Location")
636 .font(.system(.subheadline, design: .monospaced))
637 .fontWeight(.semibold)
638 .foregroundStyle(.cyan)
639 if geolocationLoading {
640 ProgressView("Looking up location…")
641 .appLoadingStyle()
642 } else if let geolocationError, geolocation == nil {
643 MessageRowView(text: geolocationError, isError: geolocationError != "No A record available")
644 } else if let geolocation {
645 ForEach(locationRows) { row in
646 LabeledValueRow(row: row)
647 }
648 if let latitude = geolocation.latitude, let longitude = geolocation.longitude {
649 let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
650 Map(initialPosition: .region(MKCoordinateRegion(
651 center: coordinate,
652 span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)
653 ))) {
654 Marker(geolocation.ip, coordinate: coordinate)
655 }
656 .mapStyle(.standard)
657 .frame(maxWidth: .infinity)
658 .frame(height: 180)
659 .cornerRadius(8)
660 }
661 } else {
662 MessageRowView(text: "No location data available", isError: false)
663 }
664 }
665
666 CardView(allowsHorizontalScroll: false) {
667 Text("Port Scan")
668 .font(.system(.subheadline, design: .monospaced))
669 .fontWeight(.semibold)
670 .foregroundStyle(.cyan)
671
672 if isCloudflareProxied {
673 Text("Domain is behind Cloudflare's proxy. Results reflect the edge, not the origin.")
674 .font(.system(.caption2, design: .monospaced))
675 .foregroundStyle(.orange)
676 .fixedSize(horizontal: false, vertical: true)
677 }
678
679 if portScanLoading {
680 ProgressView("Scanning ports…")
681 .appLoadingStyle()
682 } else if let portScanError, standardPortRows.isEmpty {
683 MessageRowView(text: portScanError, isError: true)
684 } else {
685 Text("Standard Ports")
686 .font(.system(.caption, design: .monospaced))
687 .foregroundStyle(.secondary)
688 PortRowsView(rows: standardPortRows)
689 }
690
691 DisclosureGroup("Custom Ports", isExpanded: $customPortsExpanded) {
692 VStack(alignment: .leading, spacing: 10) {
693 TextField("8888, 9000, 27017", text: $customPortInput)
694 .font(.system(.caption, design: .monospaced))
695 .textInputAutocapitalization(.never)
696 .autocorrectionDisabled()
697 .keyboardType(.numberPad)
698 .padding(10)
699 .background(Color(.systemGray6).opacity(0.5))
700 .cornerRadius(6)
701
702 Button("Scan") {
703 onScanCustomPorts()
704 }
705 .buttonStyle(.borderedProminent)
706 .disabled(customPortScanLoading)
707
708 if customPortScanLoading {
709 ProgressView("Scanning custom ports…")
710 .appLoadingStyle()
711 } else if let customPortScanError {
712 MessageRowView(text: customPortScanError, isError: true)
713 } else {
714 PortRowsView(rows: customPortRows)
715 }
716 }
717 .padding(.top, 8)
718 }
719 .font(.system(.caption, design: .monospaced))
720 .tint(.secondary)
721 }
722 }
723 }
724}
725
726struct PortRowsView: View {
727 let rows: [PortScanRowViewData]
728
729 var body: some View {
730 if rows.isEmpty {
731 MessageRowView(text: "No results", isError: false)
732 } else {
733 ForEach(rows) { row in
734 VStack(alignment: .leading, spacing: 2) {
735 HStack {
736 Text(row.portLabel)
737 .font(.system(.caption, design: .monospaced))
738 .frame(width: 52, alignment: .leading)
739 Text(row.service)
740 .font(.system(.caption, design: .monospaced))
741 .foregroundStyle(.primary)
742 Spacer()
743 if let durationLabel = row.durationLabel {
744 Text(durationLabel)
745 .font(.system(.caption2, design: .monospaced))
746 .foregroundStyle(.secondary)
747 }
748 Text(row.statusLabel)
749 .font(.system(.caption2, design: .monospaced))
750 .foregroundStyle(ResultColors.color(for: row.statusTone))
751 }
752 if let banner = row.banner {
753 Text(banner)
754 .font(.system(.caption2, design: .monospaced))
755 .foregroundStyle(.secondary)
756 .padding(.leading, 8)
757 }
758 }
759 }
760 }
761 }
762}
763
764struct SectionTitleView: View {
765 let title: String
766
767 var body: some View {
768 Text(title)
769 .font(.system(.headline))
770 .foregroundStyle(.white)
771 }
772}
773
774struct CardView<Content: View>: View {
775 let allowsHorizontalScroll: Bool
776 let content: Content
777
778 init(allowsHorizontalScroll: Bool = true, @ViewBuilder content: () -> Content) {
779 self.allowsHorizontalScroll = allowsHorizontalScroll
780 self.content = content()
781 }
782
783 var body: some View {
784 Group {
785 if allowsHorizontalScroll {
786 ScrollView(.horizontal) {
787 cardContent
788 .scrollTargetLayout()
789 }
790 .scrollBounceBehavior(.basedOnSize, axes: .horizontal)
791 } else {
792 cardContent
793 .frame(maxWidth: .infinity, alignment: .leading)
794 }
795 }
796 .frame(maxWidth: .infinity, alignment: .leading)
797 .padding(10)
798 .background(Color(.systemGray6).opacity(0.5))
799 .cornerRadius(6)
800 }
801
802 private var cardContent: some View {
803 VStack(alignment: .leading, spacing: 6) {
804 content
805 }
806 }
807}
808
809struct LoadingCardView: View {
810 let text: String
811
812 var body: some View {
813 CardView {
814 ProgressView(text)
815 .appLoadingStyle()
816 .frame(maxWidth: .infinity, alignment: .center)
817 }
818 }
819}
820
821struct MessageCardView: View {
822 let text: String
823 let isError: Bool
824
825 var body: some View {
826 CardView {
827 MessageRowView(text: text, isError: isError)
828 }
829 }
830}
831
832struct MessageRowView: View {
833 let text: String
834 let isError: Bool
835
836 var body: some View {
837 Label(text, systemImage: isError ? "exclamationmark.triangle.fill" : "info.circle")
838 .font(.system(.caption, design: .monospaced))
839 .foregroundStyle(isError ? .red : .secondary)
840 }
841}
842
843struct LabeledValueRow: View {
844 let row: InfoRowViewData
845
846 var body: some View {
847 VStack(alignment: .leading, spacing: 2) {
848 Text(row.label)
849 .font(.system(.caption2, design: .monospaced))
850 .foregroundStyle(.secondary)
851 Text(row.value)
852 .font(.system(.caption, design: .monospaced))
853 .foregroundStyle(ResultColors.color(for: row.tone))
854 .textSelection(.enabled)
855 }
856 }
857}
858
859enum ResultColors {
860 static func color(for tone: ResultTone) -> Color {
861 switch tone {
862 case .primary:
863 return .primary
864 case .secondary:
865 return .secondary
866 case .success:
867 return .green
868 case .warning:
869 return .yellow
870 case .failure:
871 return .red
872 }
873 }
874}
875
876extension DateFormatter {
877 static let certDate: DateFormatter = {
878 let formatter = DateFormatter()
879 formatter.dateStyle = .medium
880 formatter.timeStyle = .short
881 return formatter
882 }()
883}
884
885private extension View {
886 func appLoadingStyle() -> some View {
887 font(.system(.caption, design: .monospaced))
888 }
889}
890
891private struct SettingsView: View {
892 @AppStorage(DNSResolverOption.userDefaultsKey)
893 private var storedResolverURL = DNSResolverOption.defaultURLString
894
895 @State private var resolverOption: DNSResolverOption = .cloudflare
896 @State private var customResolverURL = DNSResolverOption.defaultURLString
897
898 private var customResolverError: String? {
899 guard resolverOption == .custom else {
900 return nil
901 }
902 return DNSResolverOption.isValidCustomURL(customResolverURL) ? nil : "Resolver URL must start with https://"
903 }
904
905 var body: some View {
906 Form {
907 Section {
908 Picker("Resolver", selection: $resolverOption) {
909 ForEach(DNSResolverOption.allCases) { option in
910 Text(option.title).tag(option)
911 }
912 }
913
914 if resolverOption == .custom {
915 TextField("https://resolver.example/dns-query", text: $customResolverURL)
916 .textInputAutocapitalization(.never)
917 .autocorrectionDisabled()
918 .keyboardType(.URL)
919
920 if let customResolverError {
921 Text(customResolverError)
922 .font(.caption)
923 .foregroundStyle(.red)
924 }
925 }
926 }
927 }
928 .navigationTitle("Settings")
929 .onAppear {
930 let currentResolverURL = storedResolverURL.trimmingCharacters(in: .whitespacesAndNewlines)
931 resolverOption = DNSResolverOption.option(for: currentResolverURL)
932 customResolverURL = resolverOption == .custom ? currentResolverURL : DNSResolverOption.defaultURLString
933 }
934 .onChange(of: resolverOption) { _, newValue in
935 guard let presetURL = newValue.urlString else {
936 storedResolverURL = customResolverURL.trimmingCharacters(in: .whitespacesAndNewlines)
937 return
938 }
939 storedResolverURL = presetURL
940 }
941 .onChange(of: customResolverURL) { _, newValue in
942 guard resolverOption == .custom else { return }
943 storedResolverURL = newValue.trimmingCharacters(in: .whitespacesAndNewlines)
944 }
945 }
946}
947
948#Preview {
949 ContentView()
950}