@@ -3,112 +3,96 @@ import NetworkExtension
33import os
44import VPNLib
55
6- @objc final class VPNXPCInterface : NSObject , VPNXPCClientCallbackProtocol , @unchecked Sendable {
6+ @objc final class AppXPCListener : NSObject , AppXPCInterface , @unchecked Sendable {
77private var svc : CoderVPNService
8- private let logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " VPNXPCInterface " )
9- private var xpc : VPNXPCProtocol ?
8+ private let logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " AppXPCListener " )
9+ private var connection : NSXPCConnection ?
1010
1111init ( vpn: CoderVPNService ) {
1212 svc= vpn
1313 super. init ( )
1414}
1515
16- func connect( ) {
17- logger. debug ( " VPN xpc connect called " )
18- guard xpc== nil else {
19- logger. debug ( " VPN xpc already exists " )
20- return
16+ func connect( ) -> NSXPCConnection {
17+ if let connection{
18+ return connection
2119}
22- let networkExtDict = Bundle . main. object ( forInfoDictionaryKey: " NetworkExtension " ) as? [ String : Any ]
23- let machServiceName = networkExtDict ? [ " NEMachServiceName " ] as? String
24- let xpcConn = NSXPCConnection ( machServiceName: machServiceName!)
25- xpcConn. remoteObjectInterface= NSXPCInterface ( with: VPNXPCProtocol . self)
26- xpcConn. exportedInterface= NSXPCInterface ( with: VPNXPCClientCallbackProtocol . self)
27- guard let proxy= xpcConn. remoteObjectProxyas? VPNXPCProtocol else {
28- fatalError ( " invalid xpc cast " )
29- }
30- xpc= proxy
31-
32- logger. debug ( " connecting to machServiceName: \( machServiceName!) " )
3320
34- xpcConn. exportedObject= self
35- xpcConn. invalidationHandler= { [ logger] in
36- Task { @MainActor in
37- logger. error ( " VPN XPC connection invalidated. " )
38- self . xpc= nil
39- self . connect ( )
40- }
21+ let connection = NSXPCConnection (
22+ machServiceName: helperAppMachServiceName,
23+ options: . privileged
24+ )
25+ connection. remoteObjectInterface= NSXPCInterface ( with: HelperAppXPCInterface . self)
26+ connection. exportedInterface= NSXPCInterface ( with: AppXPCInterface . self)
27+ connection. exportedObject= self
28+ connection. invalidationHandler= {
29+ self . logger. error ( " XPC connection invalidated " )
30+ self . connection= nil
31+ _= self . connect ( )
4132}
42- xpcConn. interruptionHandler= { [ logger] in
43- Task { @MainActor in
44- logger. error ( " VPN XPC connection interrupted. " )
45- self . xpc= nil
46- self . connect ( )
47- }
33+ connection. interruptionHandler= {
34+ self . logger. error ( " XPC connection interrupted " )
35+ self . connection= nil
36+ _= self . connect ( )
4837}
49- xpcConn. resume ( )
38+ logger. error ( " connecting to \( helperAppMachServiceName) " )
39+ connection. resume ( )
40+ self . connection= connection
41+ return connection
5042}
5143
52- func ping( ) {
53- xpc? . ping {
54- Task { @MainActor in
55- self . logger. info ( " Connected to NE over XPC " )
44+ func ping( ) async throws {
45+ let conn = connect ( )
46+ logger. error ( " pinging helper " )
47+ return try await withCheckedThrowingContinuation { continuationin
48+ guard let proxy= conn. remoteObjectProxyWithErrorHandler ( { errin
49+ self . logger. error ( " failed to connect to HelperXPC \( err. localizedDescription, privacy: . public) " )
50+ continuation. resume ( throwing: err)
51+ } ) as? HelperAppXPCInterface else {
52+ self . logger. error ( " failed to get proxy for HelperXPC " )
53+ continuation. resume ( throwing: XPCError . wrongProxyType)
54+ return
55+ }
56+ proxy. ping {
57+ self . logger. info ( " Connected to Helper over XPC " )
58+ continuation. resume ( )
5659}
5760}
5861}
5962
60- func getPeerState( ) {
61- xpc? . getPeerState { datain
62- Task { @MainActor in
63- self . svc. onExtensionPeerState ( data)
63+ func getPeerState( ) async throws {
64+ let conn = connect ( )
65+ return try await withCheckedThrowingContinuation { continuationin
66+ guard let proxy= conn. remoteObjectProxyWithErrorHandler ( { errin
67+ self . logger. error ( " failed to connect to HelperXPC \( err. localizedDescription, privacy: . public) " )
68+ continuation. resume ( throwing: err)
69+ } ) as? HelperAppXPCInterface else {
70+ self . logger. error ( " failed to get proxy for HelperXPC " )
71+ continuation. resume ( throwing: XPCError . wrongProxyType)
72+ return
73+ }
74+ proxy. getPeerState { datain
75+ Task { @MainActor in
76+ self . svc. onExtensionPeerState ( data)
77+ }
78+ continuation. resume ( )
6479}
6580}
6681}
6782
68- func onPeerUpdate( _ data: Data ) {
83+ func onPeerUpdate( _ diff: Data , reply: @escaping ( ) -> Void ) {
84+ let reply = CompletionWrapper ( reply)
6985Task { @MainActor in
70- svc. onExtensionPeerUpdate ( data)
86+ svc. onExtensionPeerUpdate ( diff)
87+ reply ( )
7188}
7289}
7390
74- func onProgress( stage: ProgressStage , downloadProgress: DownloadProgress ? ) {
91+ func onProgress( stage: ProgressStage , downloadProgress: DownloadProgress ? , reply: @escaping ( ) -> Void ) {
92+ let reply = CompletionWrapper ( reply)
7593Task { @MainActor in
7694 svc. onProgress ( stage: stage, downloadProgress: downloadProgress)
77- }
78- }
79-
80- // The NE has verified the dylib and knows better than Gatekeeper
81- func removeQuarantine( path: String , reply: @escaping ( Bool ) -> Void ) {
82- let reply = CallbackWrapper ( reply)
83- Task { @MainActor in
84- let prompt = """
85- Coder Desktop wants to execute code downloaded from \
86- \( svc. serverAddress?? " the Coder deployment " ) . The code has been \
87- verified to be signed by Coder.
88- """
89- let source = """
90- do shell script " xattr -d com.apple.quarantine \( path) " \
91- with prompt " \( prompt) " \
92- with administrator privileges
93- """
94- let success = await withCheckedContinuation { continuationin
95- guard let script= NSAppleScript ( source: source) else {
96- continuation. resume ( returning: false )
97- return
98- }
99- // Run on a background thread
100- Task . detached {
101- var error : NSDictionary ?
102- script. executeAndReturnError ( & error)
103- if let error{
104- self . logger. error ( " AppleScript error: \( error) " )
105- continuation. resume ( returning: false )
106- } else {
107- continuation. resume ( returning: true )
108- }
109- }
110- }
111- reply ( success)
95+ reply ( )
11296}
11397}
11498}