- Notifications
You must be signed in to change notification settings - Fork3
fix: manually upgrade system extension#158
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,6 +22,35 @@ enum SystemExtensionState: Equatable, Sendable { | ||
} | ||
} | ||
let extensionBundle: Bundle = { | ||
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. This was previously a computed variable (the body was reran each time it was used), now it's just a lazy static constant. The body here is unchanged. | ||
let extensionsDirectoryURL = URL( | ||
fileURLWithPath: "Contents/Library/SystemExtensions", | ||
relativeTo: Bundle.main.bundleURL | ||
) | ||
let extensionURLs: [URL] | ||
do { | ||
extensionURLs = try FileManager.default.contentsOfDirectory(at: extensionsDirectoryURL, | ||
includingPropertiesForKeys: nil, | ||
options: .skipsHiddenFiles) | ||
} catch { | ||
fatalError("Failed to get the contents of " + | ||
"\(extensionsDirectoryURL.absoluteString): \(error.localizedDescription)") | ||
} | ||
// here we're just going to assume that there is only ever going to be one SystemExtension | ||
// packaged up in the application bundle. If we ever need to ship multiple versions or have | ||
// multiple extensions, we'll need to revisit this assumption. | ||
guard let extensionURL = extensionURLs.first else { | ||
fatalError("Failed to find any system extensions") | ||
} | ||
guard let extensionBundle = Bundle(url: extensionURL) else { | ||
fatalError("Failed to create a bundle with URL \(extensionURL.absoluteString)") | ||
} | ||
return extensionBundle | ||
}() | ||
protocol SystemExtensionAsyncRecorder: Sendable { | ||
func recordSystemExtensionState(_ state: SystemExtensionState) async | ||
} | ||
@@ -36,50 +65,9 @@ extension CoderVPNService: SystemExtensionAsyncRecorder { | ||
} | ||
} | ||
func installSystemExtension() { | ||
systemExtnDelegate = SystemExtensionDelegate(asyncDelegate: self) | ||
systemExtnDelegate!.installSystemExtension() | ||
} | ||
} | ||
@@ -90,13 +78,31 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>: | ||
{ | ||
private var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "vpn-installer") | ||
private var asyncDelegate: AsyncDelegate | ||
// The `didFinishWithResult` function is called for both activation, | ||
// deactivation, and replacement requests. The API provides no way to | ||
// differentiate them. https://developer.apple.com/forums/thread/684021 | ||
// This tracks the last request type made, to handle them accordingly. | ||
private var action: SystemExtensionDelegateAction = .none | ||
init(asyncDelegate: AsyncDelegate) { | ||
self.asyncDelegate = asyncDelegate | ||
super.init() | ||
logger.info("SystemExtensionDelegate initialized") | ||
} | ||
func installSystemExtension() { | ||
logger.info("activating SystemExtension") | ||
let bundleID = extensionBundle.bundleIdentifier! | ||
let request = OSSystemExtensionRequest.activationRequest( | ||
forExtensionWithIdentifier: bundleID, | ||
queue: .main | ||
) | ||
request.delegate = self | ||
action = .installing | ||
OSSystemExtensionManager.shared.submitRequest(request) | ||
logger.info("submitted SystemExtension request with bundleID: \(bundleID)") | ||
} | ||
func request( | ||
_: OSSystemExtensionRequest, | ||
didFinishWithResult result: OSSystemExtensionRequest.Result | ||
@@ -109,24 +115,53 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>: | ||
} | ||
return | ||
} | ||
switch action { | ||
case .installing: | ||
logger.info("SystemExtension installed") | ||
Task { [asyncDelegate] in | ||
await asyncDelegate.recordSystemExtensionState(.installed) | ||
} | ||
action = .none | ||
case .deleting: | ||
logger.info("SystemExtension deleted") | ||
Task { [asyncDelegate] in | ||
await asyncDelegate.recordSystemExtensionState(.uninstalled) | ||
} | ||
let request = OSSystemExtensionRequest.activationRequest( | ||
forExtensionWithIdentifier: extensionBundle.bundleIdentifier!, | ||
queue: .main | ||
) | ||
request.delegate = self | ||
action = .installing | ||
OSSystemExtensionManager.shared.submitRequest(request) | ||
case .replacing: | ||
logger.info("SystemExtension replaced") | ||
// The installed extension now has the same version strings as this | ||
// bundle, so sending the deactivationRequest will work. | ||
let request = OSSystemExtensionRequest.deactivationRequest( | ||
forExtensionWithIdentifier: extensionBundle.bundleIdentifier!, | ||
queue: .main | ||
) | ||
request.delegate = self | ||
action = .deleting | ||
OSSystemExtensionManager.shared.submitRequest(request) | ||
case .none: | ||
logger.warning("Received an unexpected request result") | ||
} | ||
} | ||
func request(_: OSSystemExtensionRequest, didFailWithError error: Error) { | ||
logger.error("System extension request failed: \(error.localizedDescription)") | ||
Task { [asyncDelegate] in | ||
await asyncDelegate.recordSystemExtensionState( | ||
.failed(error.localizedDescription)) | ||
} | ||
} | ||
func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) { | ||
logger.error("Extension \(request.identifier) requires user approval") | ||
Task { [asyncDelegate] in | ||
await asyncDelegate.recordSystemExtensionState(.needsUserApproval) | ||
} | ||
} | ||
@@ -135,8 +170,31 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>: | ||
actionForReplacingExtension existing: OSSystemExtensionProperties, | ||
withExtension extension: OSSystemExtensionProperties | ||
) -> OSSystemExtensionRequest.ReplacementAction { | ||
logger.info("Replacing \(request.identifier) v\(existing.bundleVersion) with v\(`extension`.bundleVersion)") | ||
// This is counterintuitive, but this function is only called if the | ||
// versions are the same in a dev environment. | ||
// In a release build, this only gets called when the version string is | ||
// different. We don't want to manually reinstall the extension in a dev | ||
// environment, because the bug doesn't happen. | ||
if existing.bundleVersion == `extension`.bundleVersion { | ||
return .replace | ||
} | ||
// To work around the bug described in | ||
// https://github.com/coder/coder-desktop-macos/issues/121, | ||
// we're going to manually reinstall after the replacement is done. | ||
// If we returned `.cancel` here the deactivation request will fail as | ||
// it looks for an extension with the *current* version string. | ||
// There's no way to modify the deactivate request to use a different | ||
// version string (i.e. `existing.bundleVersion`). | ||
logger.info("App upgrade detected, replacing and then reinstalling") | ||
action = .replacing | ||
return .replace | ||
} | ||
} | ||
enum SystemExtensionDelegateAction { | ||
case none | ||
case installing | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
case replacing | ||
case deleting | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -17,6 +17,10 @@ struct VPNState<VPN: VPNService>: View { | ||
Text("Sign in to use Coder Desktop") | ||
.font(.body) | ||
.foregroundColor(.secondary) | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
case (.failed(.networkExtensionError(.unconfigured)), _): | ||
Text("The system VPN requires reconfiguration.") | ||
.font(.body) | ||
.foregroundStyle(.secondary) | ||
case (.disabled, _): | ||
Text("Enable Coder Connect to see workspaces") | ||
.font(.body) | ||
@@ -38,7 +42,7 @@ struct VPNState<VPN: VPNService>: View { | ||
.padding(.horizontal, Theme.Size.trayInset) | ||
.padding(.vertical, Theme.Size.trayPadding) | ||
.frame(maxWidth: .infinity) | ||
case (.connected, true): | ||
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. Same behaviour, just more explicit. | ||
EmptyView() | ||
} | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.