Skip to content

fix: connect to xpc on vpn start #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 12, 2025
37 changes: 26 additions & 11 deletions Coder Desktop/Coder Desktop/VPNService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ final class CoderVPNService: NSObject, VPNService {
Task {
await loadNetworkExtensionConfig()
}
xpc.connect()
xpc.getPeerState()
NotificationCenter.default.addObserver(
self,
selector: #selector(vpnDidUpdate(_:)),
Expand All @@ -93,8 +91,6 @@ final class CoderVPNService: NSObject, VPNService {
}

await startTunnel()
xpc.connect()
xpc.ping()
logger.debug("network extension enabled")
}

Expand Down Expand Up @@ -162,28 +158,47 @@ final class CoderVPNService: NSObject, VPNService {
}

extension CoderVPNService {
// The number of NETunnelProviderSession states makes the excessive branching
// necessary.
// swiftlint:disable:next cyclomatic_complexity
@objc private func vpnDidUpdate(_ notification: Notification) {
guard let connection = notification.object as? NETunnelProviderSession else {
return
}
switch connection.status {
case .disconnected:
switch (tunnelState, connection.status) {
// Any -> Disconnected: Update UI w/ error if present
case (_, .disconnected):
connection.fetchLastDisconnectError { err in
self.tunnelState = if let err {
.failed(.internalError(err.localizedDescription))
} else {
.disabled
}
}
case .connecting:
// Connecting -> Connecting: no-op
case (.connecting, .connecting):
break
// Connected -> Connected: no-op
case (.connected, .connected):
break
// Non-connecting -> Connecting: Establish XPC
case (_, .connecting):
xpc.connect()
xpc.ping()
tunnelState = .connecting
case .connected:
// Non-connected -> Connected: Retrieve Peers
case (_, .connected):
xpc.connect()
xpc.getPeerState()
tunnelState = .connected
case .reasserting:
// Any -> Reasserting
case (_, .reasserting):
tunnelState = .connecting
case .disconnecting:
// Any -> Disconnecting
case (_, .disconnecting):
tunnelState = .disconnecting
case .invalid:
// Any -> Invalid
case (_, .invalid):
tunnelState = .failed(.networkExtensionError(.unconfigured))
@unknown default:
tunnelState = .disabled
Expand Down
Loading