|
| 1 | +import Foundation |
| 2 | +import os |
| 3 | +import SystemExtensions |
| 4 | + |
| 5 | +enum SystemExtensionState: Equatable, Sendable { |
| 6 | + case uninstalled |
| 7 | + case needsUserApproval |
| 8 | + case installed |
| 9 | + case failed(String) |
| 10 | + |
| 11 | + var description: String { |
| 12 | + switch self { |
| 13 | + case .uninstalled: |
| 14 | + return "VPN SystemExtension is waiting to be activated" |
| 15 | + case .needsUserApproval: |
| 16 | + return "VPN SystemExtension needs user approval to activate" |
| 17 | + case .installed: |
| 18 | + return "VPN SystemExtension is installed" |
| 19 | + case let .failed(error): |
| 20 | + return "VPN SystemExtension failed with error: \(error)" |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +protocol SystemExtensionAsyncRecorder: Sendable { |
| 26 | + func recordSystemExtensionState(_ state: SystemExtensionState) async |
| 27 | +} |
| 28 | + |
| 29 | +extension CoderVPNService: SystemExtensionAsyncRecorder { |
| 30 | + func recordSystemExtensionState(_ state: SystemExtensionState) async { |
| 31 | + sysExtnState = state |
| 32 | + } |
| 33 | + |
| 34 | + var extensionBundle: Bundle { |
| 35 | + let extensionsDirectoryURL = URL( |
| 36 | + fileURLWithPath: "Contents/Library/SystemExtensions", |
| 37 | + relativeTo: Bundle.main.bundleURL |
| 38 | + ) |
| 39 | + let extensionURLs: [URL] |
| 40 | + do { |
| 41 | + extensionURLs = try FileManager.default.contentsOfDirectory(at: extensionsDirectoryURL, |
| 42 | + includingPropertiesForKeys: nil, |
| 43 | + options: .skipsHiddenFiles) |
| 44 | + } catch { |
| 45 | + fatalError("Failed to get the contents of " + |
| 46 | + "\(extensionsDirectoryURL.absoluteString): \(error.localizedDescription)") |
| 47 | + } |
| 48 | + |
| 49 | + // here we're just going to assume that there is only ever going to be one SystemExtension |
| 50 | + // packaged up in the application bundle. If we ever need to ship multiple versions or have |
| 51 | + // multiple extensions, we'll need to revisit this assumption. |
| 52 | + guard let extensionURL = extensionURLs.first else { |
| 53 | + fatalError("Failed to find any system extensions") |
| 54 | + } |
| 55 | + |
| 56 | + guard let extensionBundle = Bundle(url: extensionURL) else { |
| 57 | + fatalError("Failed to create a bundle with URL \(extensionURL.absoluteString)") |
| 58 | + } |
| 59 | + |
| 60 | + return extensionBundle |
| 61 | + } |
| 62 | + |
| 63 | + func installSystemExtension() { |
| 64 | + logger.info("activating SystemExtension") |
| 65 | + guard let bundleID = extensionBundle.bundleIdentifier else { |
| 66 | + logger.error("Bundle has no identifier") |
| 67 | + return |
| 68 | + } |
| 69 | + let request = OSSystemExtensionRequest.activationRequest( |
| 70 | + forExtensionWithIdentifier: bundleID, |
| 71 | + queue: .main |
| 72 | + ) |
| 73 | + let delegate = SystemExtensionDelegate(asyncDelegate: self) |
| 74 | + request.delegate = delegate |
| 75 | + OSSystemExtensionManager.shared.submitRequest(request) |
| 76 | + logger.info("submitted SystemExtension request with bundleID: \(bundleID)") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// A delegate for the OSSystemExtensionRequest that maps the callbacks to async calls on the |
| 81 | +/// AsyncDelegate (CoderVPNService in production). |
| 82 | +class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>: |
| 83 | + NSObject, OSSystemExtensionRequestDelegate |
| 84 | +{ |
| 85 | + private var logger = Logger(subsystem: "com.coder.Coder-Desktop", category: "vpn-installer") |
| 86 | + private var asyncDelegate: AsyncDelegate |
| 87 | + |
| 88 | + init(asyncDelegate: AsyncDelegate) { |
| 89 | + self.asyncDelegate = asyncDelegate |
| 90 | + logger.info("SystemExtensionDelegate initialized") |
| 91 | + } |
| 92 | + |
| 93 | + func request( |
| 94 | + _: OSSystemExtensionRequest, |
| 95 | + didFinishWithResult result: OSSystemExtensionRequest.Result |
| 96 | + ) { |
| 97 | + guard result == .completed else { |
| 98 | + logger.error("Unexpected result \(result.rawValue) for system extension request") |
| 99 | + let state = SystemExtensionState.failed("system extension not installed: \(result.rawValue)") |
| 100 | + Task { [asyncDelegate] in |
| 101 | + await asyncDelegate.recordSystemExtensionState(state) |
| 102 | + } |
| 103 | + return |
| 104 | + } |
| 105 | + logger.info("SystemExtension activated") |
| 106 | + Task { [asyncDelegate] in |
| 107 | + await asyncDelegate.recordSystemExtensionState(SystemExtensionState.installed) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + func request(_: OSSystemExtensionRequest, didFailWithError error: Error) { |
| 112 | + logger.error("System extension request failed: \(error.localizedDescription)") |
| 113 | + Task { [asyncDelegate] in |
| 114 | + await asyncDelegate.recordSystemExtensionState( |
| 115 | + SystemExtensionState.failed(error.localizedDescription)) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) { |
| 120 | + logger.error("Extension \(request.identifier) requires user approval") |
| 121 | + Task { [asyncDelegate] in |
| 122 | + await asyncDelegate.recordSystemExtensionState(SystemExtensionState.needsUserApproval) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + func request( |
| 127 | + _ request: OSSystemExtensionRequest, |
| 128 | + actionForReplacingExtension existing: OSSystemExtensionProperties, |
| 129 | + withExtension extension: OSSystemExtensionProperties |
| 130 | + ) -> OSSystemExtensionRequest.ReplacementAction { |
| 131 | + // swiftlint: disable line_length |
| 132 | + logger.info("Replacing \(request.identifier) v\(existing.bundleShortVersion) with v\(`extension`.bundleShortVersion)") |
| 133 | + // swiftlint: enable line_length |
| 134 | + return .replace |
| 135 | + } |
| 136 | +} |
0 commit comments