@@ -6,7 +6,7 @@ import VPNLib
6
6
@MainActor
7
7
protocol VPNService : ObservableObject {
8
8
var state : VPNServiceState { get }
9
- var agents : [ UUID : Agent ] { get }
9
+ var menuState : VPNMenuState { get }
10
10
func start( ) async
11
11
func stop( ) async
12
12
func configureTunnelProviderProtocol( proto: NETunnelProviderProtocol ? )
@@ -41,7 +41,6 @@ enum VPNServiceError: Error, Equatable {
41
41
final class CoderVPNService : NSObject , VPNService {
42
42
var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " vpn " )
43
43
lazy var xpc : VPNXPCInterface = . init( vpn: self )
44
- var workspaces : [ UUID : String ] = [ : ]
45
44
46
45
@Published var tunnelState : VPNServiceState = . disabled
47
46
@Published var sysExtnState : SystemExtensionState = . uninstalled
@@ -56,7 +55,7 @@ final class CoderVPNService: NSObject, VPNService {
56
55
return tunnelState
57
56
}
58
57
59
- @Published var agents : [ UUID : Agent ] = [ : ]
58
+ @Published var menuState : VPNMenuState = . init ( )
60
59
61
60
// systemExtnDelegate holds a reference to the SystemExtensionDelegate so that it doesn't get
62
61
// garbage collected while the OSSystemExtensionRequest is in flight, since the OS framework
@@ -85,11 +84,6 @@ final class CoderVPNService: NSObject, VPNService {
85
84
NotificationCenter . default. removeObserver ( self )
86
85
}
87
86
88
- func clearPeers( ) {
89
- agents = [ : ]
90
- workspaces = [ : ]
91
- }
92
-
93
87
func start( ) async {
94
88
switch tunnelState {
95
89
case . disabled, . failed:
@@ -150,7 +144,7 @@ final class CoderVPNService: NSObject, VPNService {
150
144
do {
151
145
let msg = try Vpn_PeerUpdate ( serializedBytes: data)
152
146
debugPrint ( msg)
153
- clearPeers ( )
147
+ menuState . clear ( )
154
148
applyPeerUpdate ( with: msg)
155
149
} catch {
156
150
logger. error ( " failed to decode peer update \( error) " )
@@ -159,53 +153,11 @@ final class CoderVPNService: NSObject, VPNService {
159
153
160
154
func applyPeerUpdate( with update: Vpn_PeerUpdate ) {
161
155
// Delete agents
162
- update. deletedAgents
163
- . compactMap { UUID ( uuidData: $0. id) }
164
- . forEach { agentID in
165
- agents [ agentID] = nil
166
- }
167
- update. deletedWorkspaces
168
- . compactMap { UUID ( uuidData: $0. id) }
169
- . forEach { workspaceID in
170
- workspaces [ workspaceID] = nil
171
- for (id, agent) in agents where agent. wsID == workspaceID {
172
- agents [ id] = nil
173
- }
174
- }
175
-
176
- // Update workspaces
177
- for workspaceProto in update. upsertedWorkspaces {
178
- if let workspaceID = UUID ( uuidData: workspaceProto. id) {
179
- workspaces [ workspaceID] = workspaceProto. name
180
- }
181
- }
182
-
183
- for agentProto in update. upsertedAgents {
184
- guard let agentID = UUID ( uuidData: agentProto. id) else {
185
- continue
186
- }
187
- guard let workspaceID = UUID ( uuidData: agentProto. workspaceID) else {
188
- continue
189
- }
190
- let workspaceName = workspaces [ workspaceID] ?? " Unknown Workspace "
191
- let newAgent = Agent (
192
- id: agentID,
193
- name: agentProto. name,
194
- // If last handshake was not within last five minutes, the agent is unhealthy
195
- status: agentProto. lastHandshake. date > Date . now. addingTimeInterval ( - 300 ) ? . okay : . off,
196
- copyableDNS: agentProto. fqdn. first ?? " UNKNOWN " ,
197
- wsName: workspaceName,
198
- wsID: workspaceID
199
- )
200
-
201
- // An existing agent with the same name, belonging to the same workspace
202
- // is from a previous workspace build, and should be removed.
203
- agents
204
- . filter { $0. value. name == agentProto. name && $0. value. wsID == workspaceID }
205
- . forEach { agents [ $0. key] = nil }
206
-
207
- agents [ agentID] = newAgent
208
- }
156
+ update. deletedAgents. forEach { menuState. deleteAgent ( withId: $0. id) }
157
+ update. deletedWorkspaces. forEach { menuState. deleteWorkspace ( withId: $0. id) }
158
+ // Upsert workspaces before agents to populate agent workspace names
159
+ update. upsertedWorkspaces. forEach { menuState. upsertWorkspace ( $0) }
160
+ update. upsertedAgents. forEach { menuState. upsertAgent ( $0) }
209
161
}
210
162
}
211
163
0 commit comments