@@ -3,112 +3,100 @@ import NetworkExtension
3
3
import os
4
4
import VPNLib
5
5
6
- @objc final class VPNXPCInterface : NSObject , VPNXPCClientCallbackProtocol , @unchecked Sendable {
6
+ @objc final class AppXPCListener : NSObject , AppXPCInterface , @unchecked Sendable {
7
7
private 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 ?
10
10
11
11
init ( vpn: CoderVPNService ) {
12
12
svc = vpn
13
13
super. init ( )
14
14
}
15
15
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
21
19
}
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. remoteObjectProxy as? VPNXPCProtocol else {
28
- fatalError ( " invalid xpc cast " )
29
- }
30
- xpc = proxy
31
-
32
- logger. debug ( " connecting to machServiceName: \( machServiceName!) " )
33
20
34
- xpcConn. exportedObject = self
35
- xpcConn. invalidationHandler = { [ logger] in
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 = {
36
29
Task { @MainActor in
37
- logger. error ( " VPN XPC connection invalidated. " )
38
- self . xpc = nil
39
- self . connect ( )
30
+ self . logger. error ( " XPC connection invalidated " )
31
+ self . connection = nil
32
+ _ = self . connect ( )
40
33
}
41
34
}
42
- xpcConn . interruptionHandler = { [ logger ] in
35
+ connection . interruptionHandler = {
43
36
Task { @MainActor in
44
- logger. error ( " VPN XPC connection interrupted. " )
45
- self . xpc = nil
46
- self . connect ( )
37
+ self . logger. error ( " XPC connection interrupted " )
38
+ self . connection = nil
39
+ _ = self . connect ( )
47
40
}
48
41
}
49
- xpcConn. resume ( )
42
+ connection. resume ( )
43
+ self . connection = connection
44
+ return connection
50
45
}
51
46
52
- func ping( ) {
53
- xpc? . ping {
54
- Task { @MainActor in
55
- self . logger. info ( " Connected to NE over XPC " )
47
+ func ping( ) async throws {
48
+ let conn = connect ( )
49
+ return try await withCheckedThrowingContinuation { continuation in
50
+ guard let proxy = conn. remoteObjectProxyWithErrorHandler ( { err in
51
+ self . logger. error ( " failed to connect to HelperXPC \( err. localizedDescription, privacy: . public) " )
52
+ continuation. resume ( throwing: err)
53
+ } ) as? HelperAppXPCInterface else {
54
+ self . logger. error ( " failed to get proxy for HelperXPC " )
55
+ continuation. resume ( throwing: XPCError . wrongProxyType)
56
+ return
57
+ }
58
+ proxy. ping {
59
+ Task { @MainActor in
60
+ self . logger. info ( " Connected to Helper over XPC " )
61
+ continuation. resume ( )
62
+ }
56
63
}
57
64
}
58
65
}
59
66
60
- func getPeerState( ) {
61
- xpc? . getPeerState { data in
62
- Task { @MainActor in
63
- self . svc. onExtensionPeerState ( data)
67
+ func getPeerState( ) async throws {
68
+ let conn = connect ( )
69
+ return try await withCheckedThrowingContinuation { continuation in
70
+ guard let proxy = conn. remoteObjectProxyWithErrorHandler ( { err in
71
+ self . logger. error ( " failed to connect to HelperXPC \( err. localizedDescription, privacy: . public) " )
72
+ continuation. resume ( throwing: err)
73
+ } ) as? HelperAppXPCInterface else {
74
+ self . logger. error ( " failed to get proxy for HelperXPC " )
75
+ continuation. resume ( throwing: XPCError . wrongProxyType)
76
+ return
77
+ }
78
+ proxy. getPeerState { data in
79
+ Task { @MainActor in
80
+ self . svc. onExtensionPeerState ( data)
81
+ continuation. resume ( )
82
+ }
64
83
}
65
84
}
66
85
}
67
86
68
- func onPeerUpdate( _ data: Data ) {
87
+ func onPeerUpdate( _ diff: Data , reply: @escaping ( ) -> Void ) {
88
+ let reply = CompletionWrapper ( reply)
69
89
Task { @MainActor in
70
- svc. onExtensionPeerUpdate ( data)
90
+ svc. onExtensionPeerUpdate ( diff)
91
+ reply ( )
71
92
}
72
93
}
73
94
74
- func onProgress( stage: ProgressStage , downloadProgress: DownloadProgress ? ) {
95
+ func onProgress( stage: ProgressStage , downloadProgress: DownloadProgress ? , reply: @escaping ( ) -> Void ) {
96
+ let reply = CompletionWrapper ( reply)
75
97
Task { @MainActor in
76
98
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 { continuation in
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)
99
+ reply ( )
112
100
}
113
101
}
114
102
}
0 commit comments