Skip to content

Commit 4f05f5c

Browse files
committed
fix: connect to xpc on vpn start
1 parent 7d6933e commit 4f05f5c

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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:

0 commit comments

Comments
 (0)