DomainDig/NetworkSectionView.swift
249 lines · 11761 bytes
1import MapKit
2import SwiftUI
3
4// One inspection section: the reachability, location and ports cards. Pure
5// presentation over the view model's report data; the shared primitives it
6// builds on (CardView, SectionTitleView, LabeledValueRow, ResultColors,
7// appLoadingStyle) live in ResultPrimitives.swift.
8
9struct NetworkSectionView: View {
10 @Environment(\.appDensity) private var appDensity
11 @Binding var isCollapsed: Bool
12 @Binding var customPortsExpanded: Bool
13 @Binding var customPortInput: String
14 let onScanCustomPorts: () -> Void
15
16 private let reachabilityRows: [ReachabilityRowViewData]
17 private let reachabilityLoading: Bool
18 private let reachabilityError: String?
19 private let reachabilityProvenance: SectionProvenance?
20 private let locationRows: [InfoRowViewData]
21 private let geolocation: IPGeolocation?
22 private let geolocationLoading: Bool
23 private let geolocationError: String?
24 private let geolocationProvenance: SectionProvenance?
25 private let geolocationConfidence: ConfidenceLevel?
26 private let standardPortRows: [PortScanRowViewData]
27 private let customPortRows: [PortScanRowViewData]
28 private let portScanLoading: Bool
29 private let portScanError: String?
30 private let portScanProvenance: SectionProvenance?
31 private let customPortScanLoading: Bool
32 private let customPortScanError: String?
33 private let isCloudflareProxied: Bool
34
35 init(
36 isCollapsed: Binding<Bool>,
37 customPortsExpanded: Binding<Bool>,
38 customPortInput: Binding<String>,
39 data: NetworkSectionViewData,
40 onScanCustomPorts: @escaping () -> Void
41 ) {
42 _isCollapsed = isCollapsed
43 _customPortsExpanded = customPortsExpanded
44 _customPortInput = customPortInput
45 self.onScanCustomPorts = onScanCustomPorts
46 reachabilityRows = data.reachabilityRows
47 reachabilityLoading = data.reachability.isLoading
48 reachabilityError = data.reachability.error
49 reachabilityProvenance = data.reachability.provenance
50 locationRows = data.locationRows
51 geolocation = data.geolocation
52 geolocationLoading = data.location.isLoading
53 geolocationError = data.location.error
54 geolocationProvenance = data.location.provenance
55 geolocationConfidence = data.location.confidence
56 standardPortRows = data.standardPortRows
57 customPortRows = data.customPortRows
58 portScanLoading = data.portScan.isLoading
59 portScanError = data.portScan.error
60 portScanProvenance = data.portScan.provenance
61 customPortScanLoading = data.customPortScan.isLoading
62 customPortScanError = data.customPortScan.error
63 isCloudflareProxied = data.isCloudflareProxied
64 }
65
66 var body: some View {
67 CollapsibleSectionView(title: "Network", isCollapsed: $isCollapsed) {
68 CardView {
69 Text("Reachability")
70 .font(appDensity.font(.subheadline, weight: .semibold))
71 .foregroundStyle(ThemeToken.info)
72 SectionTrustMetadataView(provenance: reachabilityProvenance, confidence: nil)
73 if reachabilityLoading {
74 ProgressView("Checking ports…")
75 .appLoadingStyle()
76 } else if let reachabilityError {
77 MessageRowView(text: reachabilityError, isError: true)
78 } else {
79 ForEach(reachabilityRows) { row in
80 HStack {
81 Text(row.portLabel)
82 .font(appDensity.font(.caption))
83 Spacer()
84 Text(row.latencyLabel)
85 .font(appDensity.font(.caption2))
86 .foregroundStyle(ThemeToken.textSecondary)
87 AppStatusBadgeView(model: reachabilityBadge(row))
88 }
89 }
90 }
91 }
92
93 CardView(allowsHorizontalScroll: false) {
94 Text("Location")
95 .font(appDensity.font(.subheadline, weight: .semibold))
96 .foregroundStyle(ThemeToken.info)
97 SectionTrustMetadataView(provenance: geolocationProvenance, confidence: geolocationConfidence)
98 if geolocationLoading {
99 ProgressView("Looking up location…")
100 .appLoadingStyle()
101 } else if let geolocationError, geolocation == nil {
102 MessageRowView(text: geolocationError, isError: geolocationError != "No A record available")
103 } else if let geolocation {
104 ForEach(locationRows) { row in
105 LabeledValueRow(row: row)
106 }
107 if let latitude = geolocation.latitude, let longitude = geolocation.longitude {
108 let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
109 Map(initialPosition: .region(MKCoordinateRegion(
110 center: coordinate,
111 span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)
112 ))) {
113 Marker(geolocation.ip, coordinate: coordinate)
114 }
115 .mapStyle(.standard)
116 .frame(maxWidth: .infinity)
117 .frame(height: 180)
118 .cornerRadius(8)
119 }
120 } else {
121 MessageRowView(text: "No location data available", isError: false)
122 }
123 }
124
125 CardView(allowsHorizontalScroll: false) {
126 Text("Port Scan")
127 .font(appDensity.font(.subheadline, weight: .semibold))
128 .foregroundStyle(ThemeToken.info)
129 SectionTrustMetadataView(provenance: portScanProvenance, confidence: nil)
130
131 if isCloudflareProxied {
132 Text("Domain is behind Cloudflare's proxy. Results reflect the edge, not the origin.")
133 .font(appDensity.font(.caption2))
134 .foregroundStyle(ThemeToken.warning)
135 .fixedSize(horizontal: false, vertical: true)
136 }
137
138 if portScanLoading {
139 ProgressView("Scanning ports…")
140 .appLoadingStyle()
141 } else if let portScanError, standardPortRows.isEmpty {
142 MessageRowView(text: portScanError, isError: true)
143 } else {
144 Text("Standard Ports")
145 .font(.system(.caption, design: .monospaced))
146 .foregroundStyle(ThemeToken.textSecondary)
147 PortRowsView(rows: standardPortRows)
148 }
149
150 DisclosureGroup("Custom Ports", isExpanded: $customPortsExpanded) {
151 VStack(alignment: .leading, spacing: 10) {
152 TextField("8888, 9000, 27017", text: $customPortInput)
153 .font(appDensity.font(.caption))
154 .textInputAutocapitalization(.never)
155 .autocorrectionDisabled()
156 .keyboardType(.numberPad)
157 .padding(10)
158 .background(ThemeToken.surface)
159 .clipShape(RoundedRectangle(cornerRadius: appDensity.metrics.cardCornerRadius))
160
161 Button("Scan") {
162 AppHaptics.refresh()
163 onScanCustomPorts()
164 }
165 .buttonStyle(.borderedProminent)
166 .tint(ThemeToken.accent)
167 .disabled(customPortScanLoading)
168
169 if customPortScanLoading {
170 ProgressView("Scanning custom ports…")
171 .appLoadingStyle()
172 } else if let customPortScanError {
173 MessageRowView(text: customPortScanError, isError: true)
174 } else {
175 PortRowsView(rows: customPortRows)
176 }
177 }
178 .padding(.top, 8)
179 }
180 .font(.system(.caption, design: .monospaced))
181 .tint(.secondary)
182 }
183 }
184 }
185
186 private func reachabilityBadge(_ row: ReachabilityRowViewData) -> AppStatusBadgeModel {
187 switch row.statusTone {
188 case .success:
189 return .init(title: row.statusLabel, systemImage: "checkmark.circle.fill", foreground: .positive, background: .positiveSurface)
190 case .warning:
191 return .init(title: row.statusLabel, systemImage: "exclamationmark.triangle.fill", foreground: .warning, background: .warningSurface)
192 case .failure:
193 return .init(title: row.statusLabel, systemImage: "xmark.circle.fill", foreground: .critical, background: .criticalSurface)
194 case .primary, .secondary:
195 return .init(title: row.statusLabel, systemImage: "circle", foreground: .textSecondary, background: .surfaceElevated)
196 }
197 }
198}
199
200struct PortRowsView: View {
201 @Environment(\.appDensity) private var appDensity
202 let rows: [PortScanRowViewData]
203
204 var body: some View {
205 if rows.isEmpty {
206 MessageRowView(text: "No results", isError: false)
207 } else {
208 ForEach(rows) { row in
209 VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing - 1) {
210 HStack {
211 Text(row.portLabel)
212 .font(appDensity.font(.caption))
213 .frame(width: 52, alignment: .leading)
214 Text(row.service)
215 .font(appDensity.font(.caption))
216 .foregroundStyle(.primary)
217 Spacer()
218 if let durationLabel = row.durationLabel {
219 Text(durationLabel)
220 .font(appDensity.font(.caption2))
221 .foregroundStyle(ThemeToken.textSecondary)
222 }
223 AppStatusBadgeView(model: portBadge(row))
224 }
225 if let banner = row.banner {
226 Text(banner)
227 .font(appDensity.font(.caption2))
228 .foregroundStyle(ThemeToken.textSecondary)
229 .padding(.leading, 8)
230 }
231 }
232 .frame(minHeight: appDensity.metrics.rowMinHeight, alignment: .topLeading)
233 }
234 }
235 }
236
237 private func portBadge(_ row: PortScanRowViewData) -> AppStatusBadgeModel {
238 switch row.statusTone {
239 case .success:
240 return .init(title: row.statusLabel, systemImage: "checkmark.circle.fill", foreground: .positive, background: .positiveSurface)
241 case .warning:
242 return .init(title: row.statusLabel, systemImage: "exclamationmark.triangle.fill", foreground: .warning, background: .warningSurface)
243 case .failure:
244 return .init(title: row.statusLabel, systemImage: "xmark.circle.fill", foreground: .critical, background: .criticalSurface)
245 case .primary, .secondary:
246 return .init(title: row.statusLabel, systemImage: "circle", foreground: .textSecondary, background: .surfaceElevated)
247 }
248 }
249}