Skip to content

Commit cbbcc8b

Browse files
committed
fixup
1 parent 9b48664 commit cbbcc8b

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

Coder Desktop/Coder Desktop/Preview Content/PreviewVPN.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import SwiftUI
55
final class PreviewVPN: Coder_Desktop.VPNService {
66
@Published var state: Coder_Desktop.VPNServiceState = .connected
77
@Published var menuState: VPNMenuState = .init(agents: [
8-
UUID(): Agent(id: UUID(), name: "dev", status: .error, fqdns: ["asdf.coder"], wsName: "dogfood2",
8+
UUID(): Agent(id: UUID(), name: "dev", status: .error, hosts: ["asdf.coder"], wsName: "dogfood2",
99
wsID: UUID()),
10-
UUID(): Agent(id: UUID(), name: "dev", status: .okay, fqdns: ["asdf.coder"],
10+
UUID(): Agent(id: UUID(), name: "dev", status: .okay, hosts: ["asdf.coder"],
1111
wsName: "testing-a-very-long-name", wsID: UUID()),
12-
UUID(): Agent(id: UUID(), name: "dev", status: .warn, fqdns: ["asdf.coder"], wsName: "opensrc",
12+
UUID(): Agent(id: UUID(), name: "dev", status: .warn, hosts: ["asdf.coder"], wsName: "opensrc",
1313
wsID: UUID()),
14-
UUID(): Agent(id: UUID(), name: "dev", status: .off, fqdns: ["asdf.coder"], wsName: "gvisor",
14+
UUID(): Agent(id: UUID(), name: "dev", status: .off, hosts: ["asdf.coder"], wsName: "gvisor",
1515
wsID: UUID()),
16-
UUID(): Agent(id: UUID(), name: "dev", status: .off, fqdns: ["asdf.coder"], wsName: "example",
16+
UUID(): Agent(id: UUID(), name: "dev", status: .off, hosts: ["asdf.coder"], wsName: "example",
1717
wsID: UUID()),
18-
UUID(): Agent(id: UUID(), name: "dev", status: .error, fqdns: ["asdf.coder"], wsName: "dogfood2",
18+
UUID(): Agent(id: UUID(), name: "dev", status: .error, hosts: ["asdf.coder"], wsName: "dogfood2",
1919
wsID: UUID()),
20-
UUID(): Agent(id: UUID(), name: "dev", status: .okay, fqdns: ["asdf.coder"],
20+
UUID(): Agent(id: UUID(), name: "dev", status: .okay, hosts: ["asdf.coder"],
2121
wsName: "testing-a-very-long-name", wsID: UUID()),
22-
UUID(): Agent(id: UUID(), name: "dev", status: .warn, fqdns: ["asdf.coder"], wsName: "opensrc",
22+
UUID(): Agent(id: UUID(), name: "dev", status: .warn, hosts: ["asdf.coder"], wsName: "opensrc",
2323
wsID: UUID()),
24-
UUID(): Agent(id: UUID(), name: "dev", status: .off, fqdns: ["asdf.coder"], wsName: "gvisor",
24+
UUID(): Agent(id: UUID(), name: "dev", status: .off, hosts: ["asdf.coder"], wsName: "gvisor",
2525
wsID: UUID()),
26-
UUID(): Agent(id: UUID(), name: "dev", status: .off, fqdns: ["asdf.coder"], wsName: "example",
26+
UUID(): Agent(id: UUID(), name: "dev", status: .off, hosts: ["asdf.coder"], wsName: "example",
2727
wsID: UUID()),
2828
], workspaces: [:])
2929
let shouldFail: Bool

Coder Desktop/Coder Desktop/VPNMenuState.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct Agent: Identifiable, Equatable, Comparable {
66
let id: UUID
77
let name: String
88
let status: AgentStatus
9-
let fqdns: [String]
9+
let hosts: [String]
1010
let wsName: String
1111
let wsID: UUID
1212

@@ -18,10 +18,7 @@ struct Agent: Identifiable, Equatable, Comparable {
1818
return lhs.wsName.localizedCompare(rhs.wsName) == .orderedAscending
1919
}
2020

21-
// Choose the shortest hostname, and remove trailing dot if present
22-
var primaryHost: String? { fqdns.min(by: { $0.count < $1.count })
23-
.map { $0.hasSuffix(".") ? String($0.dropLast()) : $0 }
24-
}
21+
var primaryHost: String? { hosts.min(by: { $0.count < $1.count }) }
2522
}
2623

2724
enum AgentStatus: Int, Equatable, Comparable {
@@ -72,7 +69,8 @@ struct VPNMenuState {
7269
name: agent.name,
7370
// If last handshake was not within last five minutes, the agent is unhealthy
7471
status: agent.lastHandshake.date > Date.now.addingTimeInterval(-300) ? .okay : .warn,
75-
fqdns: agent.fqdn,
72+
// Remove trailing dot if present
73+
hosts: agent.fqdn.map { $0.hasSuffix(".") ? String($0.dropLast()) : $0 },
7674
wsName: wsName,
7775
wsID: wsID
7876
)

Coder Desktop/Coder Desktop/VPNService.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ final class CoderVPNService: NSObject, VPNService {
7070
Task {
7171
await loadNetworkExtensionConfig()
7272
}
73-
xpc.connect()
74-
xpc.getPeerState()
7573
NotificationCenter.default.addObserver(
7674
self,
7775
selector: #selector(vpnDidUpdate(_:)),
@@ -93,8 +91,6 @@ final class CoderVPNService: NSObject, VPNService {
9391
}
9492

9593
await startTunnel()
96-
xpc.connect()
97-
xpc.ping()
9894
logger.debug("network extension enabled")
9995
}
10096

@@ -162,6 +158,7 @@ final class CoderVPNService: NSObject, VPNService {
162158
}
163159

164160
extension CoderVPNService {
161+
// swiftlint:disable:next cyclomatic_complexity
165162
@objc private func vpnDidUpdate(_ notification: Notification) {
166163
guard let connection = notification.object as? NETunnelProviderSession else {
167164
return
@@ -176,9 +173,21 @@ extension CoderVPNService {
176173
}
177174
}
178175
case .connecting:
179-
tunnelState = .connecting
176+
// If transitioning to 'connecting' from any other state,
177+
// then the network extension is running, and we can connect over XPC
178+
if tunnelState != .connecting {
179+
xpc.connect()
180+
xpc.ping()
181+
tunnelState = .connecting
182+
}
180183
case .connected:
181-
tunnelState = .connected
184+
// If transitioning to 'connected' from any other state, the tunnel has
185+
// finished starting, and we can learn the peer state
186+
if tunnelState != .connected {
187+
xpc.connect()
188+
xpc.getPeerState()
189+
tunnelState = .connected
190+
}
182191
case .reasserting:
183192
tunnelState = .connecting
184193
case .disconnecting:

Coder Desktop/Coder DesktopTests/AgentsTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct AgentsTests {
2424
id: UUID(),
2525
name: "dev",
2626
status: .okay,
27-
fqdns: ["a\($0).coder"],
27+
hosts: ["a\($0).coder"],
2828
wsName: "ws\($0)",
2929
wsID: UUID()
3030
)

0 commit comments

Comments
 (0)