|
| 1 | +import Foundation |
| 2 | +import NetworkExtension |
| 3 | +import os |
| 4 | +import VPNLib |
| 5 | + |
| 6 | +// This is the client for the app to communicate with the privileged helper. |
| 7 | +@objc final class HelperXPCClient: NSObject, @unchecked Sendable { |
| 8 | + private var svc: CoderVPNService |
| 9 | + private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "HelperXPCClient") |
| 10 | + private var connection: NSXPCConnection? |
| 11 | + |
| 12 | + init(vpn: CoderVPNService) { |
| 13 | + svc = vpn |
| 14 | + super.init() |
| 15 | + } |
| 16 | + |
| 17 | + func connect() -> NSXPCConnection { |
| 18 | + if let connection { |
| 19 | + return connection |
| 20 | + } |
| 21 | + |
| 22 | + let connection = NSXPCConnection( |
| 23 | + machServiceName: helperAppMachServiceName, |
| 24 | + options: .privileged |
| 25 | + ) |
| 26 | + connection.remoteObjectInterface = NSXPCInterface(with: HelperAppXPCInterface.self) |
| 27 | + connection.exportedInterface = NSXPCInterface(with: AppXPCInterface.self) |
| 28 | + connection.exportedObject = self |
| 29 | + connection.invalidationHandler = { |
| 30 | + self.logger.error("XPC connection invalidated") |
| 31 | + self.connection = nil |
| 32 | + _ = self.connect() |
| 33 | + } |
| 34 | + connection.interruptionHandler = { |
| 35 | + self.logger.error("XPC connection interrupted") |
| 36 | + self.connection = nil |
| 37 | + _ = self.connect() |
| 38 | + } |
| 39 | + logger.info("connecting to \(helperAppMachServiceName)") |
| 40 | + connection.resume() |
| 41 | + self.connection = connection |
| 42 | + return connection |
| 43 | + } |
| 44 | + |
| 45 | + // Establishes a connection to the Helper, so it can send messages back. |
| 46 | + func ping() async throws { |
| 47 | + let conn = connect() |
| 48 | + return try await withCheckedThrowingContinuation { continuation in |
| 49 | + guard let proxy = conn.remoteObjectProxyWithErrorHandler({ err in |
| 50 | + self.logger.error("failed to connect to HelperXPC \(err.localizedDescription, privacy: .public)") |
| 51 | + continuation.resume(throwing: err) |
| 52 | + }) as? HelperAppXPCInterface else { |
| 53 | + self.logger.error("failed to get proxy for HelperXPC") |
| 54 | + continuation.resume(throwing: XPCError.wrongProxyType) |
| 55 | + return |
| 56 | + } |
| 57 | + proxy.ping { |
| 58 | + self.logger.info("Connected to Helper over XPC") |
| 59 | + continuation.resume() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + func getPeerState() async throws { |
| 65 | + let conn = connect() |
| 66 | + return try await withCheckedThrowingContinuation { continuation in |
| 67 | + guard let proxy = conn.remoteObjectProxyWithErrorHandler({ err in |
| 68 | + self.logger.error("failed to connect to HelperXPC \(err.localizedDescription, privacy: .public)") |
| 69 | + continuation.resume(throwing: err) |
| 70 | + }) as? HelperAppXPCInterface else { |
| 71 | + self.logger.error("failed to get proxy for HelperXPC") |
| 72 | + continuation.resume(throwing: XPCError.wrongProxyType) |
| 73 | + return |
| 74 | + } |
| 75 | + proxy.getPeerState { data in |
| 76 | + Task { @MainActor in |
| 77 | + self.svc.onExtensionPeerState(data) |
| 78 | + } |
| 79 | + continuation.resume() |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// These methods are called by the Helper over XPC |
| 86 | +extension HelperXPCClient: AppXPCInterface { |
| 87 | + func onPeerUpdate(_ diff: Data, reply: @escaping () -> Void) { |
| 88 | + let reply = CompletionWrapper(reply) |
| 89 | + Task { @MainActor in |
| 90 | + svc.onExtensionPeerUpdate(diff) |
| 91 | + reply() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + func onProgress(stage: ProgressStage, downloadProgress: DownloadProgress?, reply: @escaping () -> Void) { |
| 96 | + let reply = CompletionWrapper(reply) |
| 97 | + Task { @MainActor in |
| 98 | + svc.onProgress(stage: stage, downloadProgress: downloadProgress) |
| 99 | + reply() |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments