Skip to content
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 @@
Task {
await loadNetworkExtensionConfig()
}
xpc.connect()
xpc.getPeerState()
NotificationCenter.default.addObserver(
self,
selector: #selector(vpnDidUpdate(_:)),
Expand All @@ -93,8 +91,6 @@
}

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

Expand Down Expand Up @@ -162,28 +158,47 @@
}

extension CoderVPNService {
// The number of NETunnelProviderSession states makes the excessive branching

Check warning on line 161 in Coder Desktop/Coder Desktop/VPNService.swift

View workflow job for this annotation

GitHub Actions / fmt

Remove trailing space at end of a line. (trailingSpace)
// 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