-
Notifications
You must be signed in to change notification settings - Fork 3
chore: support operating the VPN without the app #36
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
Changes from all commits
cf9cfd0
951fc1a
f617f6f
b7c9f58
27ab4a7
c0a46aa
95bf6b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,6 @@ enum VPNServiceError: Error, Equatable { | |
final class CoderVPNService: NSObject, VPNService { | ||
var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "vpn") | ||
lazy var xpc: VPNXPCInterface = .init(vpn: self) | ||
var terminating = false | ||
var workspaces: [UUID: String] = [:] | ||
|
||
@Published var tunnelState: VPNServiceState = .disabled | ||
|
@@ -68,8 +67,14 @@ final class CoderVPNService: NSObject, VPNService { | |
super.init() | ||
installSystemExtension() | ||
Task { | ||
await loadNetworkExtension() | ||
neState = if await hasNetworkExtensionConfig() { | ||
.disabled | ||
} else { | ||
.unconfigured | ||
} | ||
} | ||
xpc.connect() | ||
xpc.getPeerState() | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(vpnDidUpdate(_:)), | ||
|
@@ -82,6 +87,11 @@ final class CoderVPNService: NSObject, VPNService { | |
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
func clearPeers() { | ||
agents = [:] | ||
workspaces = [:] | ||
} | ||
|
||
func start() async { | ||
switch tunnelState { | ||
case .disabled, .failed: | ||
|
@@ -90,31 +100,18 @@ final class CoderVPNService: NSObject, VPNService { | |
return | ||
} | ||
|
||
await enableNetworkExtension() | ||
// this ping is somewhat load bearing since it causes xpc to init | ||
await startTunnel() | ||
xpc.connect() | ||
xpc.ping() | ||
logger.debug("network extension enabled") | ||
} | ||
|
||
func stop() async { | ||
guard tunnelState == .connected else { return } | ||
await disableNetworkExtension() | ||
await stopTunnel() | ||
logger.info("network extension stopped") | ||
} | ||
|
||
// Instructs the service to stop the VPN and then quit once the stop event | ||
// is read over XPC. | ||
// MUST only be called from `NSApplicationDelegate.applicationShouldTerminate` | ||
// MUST eventually call `NSApp.reply(toApplicationShouldTerminate: true)` | ||
func quit() async { | ||
guard tunnelState == .connected else { | ||
NSApp.reply(toApplicationShouldTerminate: true) | ||
return | ||
} | ||
terminating = true | ||
await stop() | ||
} | ||
|
||
func configureTunnelProviderProtocol(proto: NETunnelProviderProtocol?) { | ||
Task { | ||
if let proto { | ||
|
@@ -145,6 +142,22 @@ final class CoderVPNService: NSObject, VPNService { | |
} | ||
} | ||
|
||
func onExtensionPeerState(_ data: Data?) { | ||
guard let data else { | ||
logger.error("could not retrieve peer state from network extension, it may not be running") | ||
return | ||
} | ||
logger.info("received network extension peer state") | ||
do { | ||
let msg = try Vpn_PeerUpdate(serializedBytes: data) | ||
debugPrint(msg) | ||
clearPeers() | ||
applyPeerUpdate(with: msg) | ||
Comment on lines
+154
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this clear call make handling the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} catch { | ||
logger.error("failed to decode peer update \(error)") | ||
} | ||
} | ||
|
||
func applyPeerUpdate(with update: Vpn_PeerUpdate) { | ||
// Delete agents | ||
update.deletedAgents | ||
|
@@ -204,9 +217,6 @@ extension CoderVPNService { | |
} | ||
switch connection.status { | ||
case .disconnected: | ||
if terminating { | ||
NSApp.reply(toApplicationShouldTerminate: true) | ||
} | ||
connection.fetchLastDisconnectError { err in | ||
self.tunnelState = if let err { | ||
.failed(.internalError(err.localizedDescription)) | ||
|
Uh oh!
There was an error while loading. Please reload this page.