-
Notifications
You must be signed in to change notification settings - Fork 3
chore: run coder connect networking from launchdaemon #203
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
Merged
+527
−486
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Foundation | ||
import NetworkExtension | ||
import os | ||
import VPNLib | ||
|
||
// This is the client for the app to communicate with the privileged helper. | ||
@objc final class HelperXPCClient: NSObject, @unchecked Sendable { | ||
private var svc: CoderVPNService | ||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "HelperXPCClient") | ||
private var connection: NSXPCConnection? | ||
|
||
init(vpn: CoderVPNService) { | ||
svc = vpn | ||
super.init() | ||
} | ||
|
||
func connect() -> NSXPCConnection { | ||
if let connection { | ||
return connection | ||
} | ||
|
||
let connection = NSXPCConnection( | ||
machServiceName: helperAppMachServiceName, | ||
options: .privileged | ||
) | ||
connection.remoteObjectInterface = NSXPCInterface(with: HelperAppXPCInterface.self) | ||
connection.exportedInterface = NSXPCInterface(with: AppXPCInterface.self) | ||
connection.exportedObject = self | ||
connection.invalidationHandler = { | ||
self.logger.error("XPC connection invalidated") | ||
self.connection = nil | ||
_ = self.connect() | ||
} | ||
connection.interruptionHandler = { | ||
self.logger.error("XPC connection interrupted") | ||
self.connection = nil | ||
_ = self.connect() | ||
} | ||
logger.info("connecting to \(helperAppMachServiceName)") | ||
connection.resume() | ||
self.connection = connection | ||
return connection | ||
} | ||
|
||
// Establishes a connection to the Helper, so it can send messages back. | ||
func ping() async throws { | ||
let conn = connect() | ||
return try await withCheckedThrowingContinuation { continuation in | ||
guard let proxy = conn.remoteObjectProxyWithErrorHandler({ err in | ||
self.logger.error("failed to connect to HelperXPC \(err.localizedDescription, privacy: .public)") | ||
continuation.resume(throwing: err) | ||
}) as? HelperAppXPCInterface else { | ||
self.logger.error("failed to get proxy for HelperXPC") | ||
continuation.resume(throwing: XPCError.wrongProxyType) | ||
return | ||
} | ||
proxy.ping { | ||
self.logger.info("Connected to Helper over XPC") | ||
continuation.resume() | ||
} | ||
} | ||
} | ||
|
||
func getPeerState() async throws { | ||
let conn = connect() | ||
return try await withCheckedThrowingContinuation { continuation in | ||
guard let proxy = conn.remoteObjectProxyWithErrorHandler({ err in | ||
self.logger.error("failed to connect to HelperXPC \(err.localizedDescription, privacy: .public)") | ||
continuation.resume(throwing: err) | ||
}) as? HelperAppXPCInterface else { | ||
self.logger.error("failed to get proxy for HelperXPC") | ||
continuation.resume(throwing: XPCError.wrongProxyType) | ||
return | ||
} | ||
proxy.getPeerState { data in | ||
Task { @MainActor in | ||
self.svc.onExtensionPeerState(data) | ||
} | ||
continuation.resume() | ||
} | ||
} | ||
} | ||
} | ||
|
||
// These methods are called by the Helper over XPC | ||
extension HelperXPCClient: AppXPCInterface { | ||
func onPeerUpdate(_ diff: Data, reply: @escaping () -> Void) { | ||
let reply = CompletionWrapper(reply) | ||
Task { @MainActor in | ||
svc.onExtensionPeerUpdate(diff) | ||
reply() | ||
} | ||
} | ||
|
||
func onProgress(stage: ProgressStage, downloadProgress: DownloadProgress?, reply: @escaping () -> Void) { | ||
let reply = CompletionWrapper(reply) | ||
Task { @MainActor in | ||
svc.onProgress(stage: stage, downloadProgress: downloadProgress) | ||
reply() | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.