@@ -5,8 +5,8 @@ import VPNLib
55
66// This listener handles XPC connections from the Coder Desktop System Network
77// Extension (`com.coder.Coder-Desktop.VPN`).
8- class HelperNEXPCListener : NSObject , NSXPCListenerDelegate , HelperNEXPCInterface , @unchecked Sendable {
9- private var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " HelperNEXPCListener " )
8+ class HelperNEXPCServer : NSObject , NSXPCListenerDelegate , @unchecked Sendable {
9+ private var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " HelperNEXPCServer " )
1010 private var conns : [ NSXPCConnection ] = [ ]
1111
1212 // Hold a reference to the tun file handle
@@ -37,6 +37,43 @@ class HelperNEXPCListener: NSObject, NSXPCListenerDelegate, HelperNEXPCInterface
3737 return true
3838 }
3939
40+ func cancelProvider( error: Error ? ) async throws {
41+ try await withCheckedThrowingContinuation { continuation in
42+ guard let proxy = conns. last? . remoteObjectProxyWithErrorHandler ( { err in
43+ self . logger. error ( " failed to connect to HelperNEXPC \( err. localizedDescription, privacy: . public) " )
44+ continuation. resume ( throwing: err)
45+ } ) as? NEXPCInterface else {
46+ self . logger. error ( " failed to get proxy for HelperNEXPCInterface " )
47+ continuation. resume ( throwing: XPCError . wrongProxyType)
48+ return
49+ }
50+ proxy. cancelProvider ( error: error) {
51+ self . logger. info ( " provider cancelled " )
52+ continuation. resume ( )
53+ }
54+ } as Void
55+ }
56+
57+ func applyTunnelNetworkSettings( diff: Vpn_NetworkSettingsRequest ) async throws {
58+ let bytes = try diff. serializedData ( )
59+ return try await withCheckedThrowingContinuation { continuation in
60+ guard let proxy = conns. last? . remoteObjectProxyWithErrorHandler ( { err in
61+ self . logger. error ( " failed to connect to HelperNEXPC \( err. localizedDescription, privacy: . public) " )
62+ continuation. resume ( throwing: err)
63+ } ) as? NEXPCInterface else {
64+ self . logger. error ( " failed to get proxy for HelperNEXPCInterface " )
65+ continuation. resume ( throwing: XPCError . wrongProxyType)
66+ return
67+ }
68+ proxy. applyTunnelNetworkSettings ( diff: bytes) {
69+ self . logger. info ( " applied tunnel network setting " )
70+ continuation. resume ( )
71+ }
72+ }
73+ }
74+ }
75+
76+ extension HelperNEXPCServer : HelperNEXPCInterface {
4077 func startDaemon(
4178 accessURL: URL ,
4279 token: String ,
@@ -88,49 +125,10 @@ class HelperNEXPCListener: NSObject, NSXPCListenerDelegate, HelperNEXPCInterface
88125 }
89126}
90127
91- // These methods are called to send updates to the Coder Desktop System Network
92- // Extension.
93- extension HelperNEXPCListener {
94- func cancelProvider( error: Error ? ) async throws {
95- try await withCheckedThrowingContinuation { continuation in
96- guard let proxy = conns. last? . remoteObjectProxyWithErrorHandler ( { err in
97- self . logger. error ( " failed to connect to HelperNEXPC \( err. localizedDescription, privacy: . public) " )
98- continuation. resume ( throwing: err)
99- } ) as? NEXPCInterface else {
100- self . logger. error ( " failed to get proxy for HelperNEXPCInterface " )
101- continuation. resume ( throwing: XPCError . wrongProxyType)
102- return
103- }
104- proxy. cancelProvider ( error: error) {
105- self . logger. info ( " provider cancelled " )
106- continuation. resume ( )
107- }
108- } as Void
109- }
110-
111- func applyTunnelNetworkSettings( diff: Vpn_NetworkSettingsRequest ) async throws {
112- let bytes = try diff. serializedData ( )
113- return try await withCheckedThrowingContinuation { continuation in
114- guard let proxy = conns. last? . remoteObjectProxyWithErrorHandler ( { err in
115- self . logger. error ( " failed to connect to HelperNEXPC \( err. localizedDescription, privacy: . public) " )
116- continuation. resume ( throwing: err)
117- } ) as? NEXPCInterface else {
118- self . logger. error ( " failed to get proxy for HelperNEXPCInterface " )
119- continuation. resume ( throwing: XPCError . wrongProxyType)
120- return
121- }
122- proxy. applyTunnelNetworkSettings ( diff: bytes) {
123- self . logger. info ( " applied tunnel network setting " )
124- continuation. resume ( )
125- }
126- }
127- }
128- }
129-
130128// This listener handles XPC connections from the Coder Desktop App
131129// (`com.coder.Coder-Desktop`).
132- class HelperAppXPCListener : NSObject , NSXPCListenerDelegate , HelperAppXPCInterface , @unchecked Sendable {
133- private var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " HelperAppXPCListener " )
130+ class HelperAppXPCServer : NSObject , NSXPCListenerDelegate , @unchecked Sendable {
131+ private var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " HelperAppXPCServer " )
134132 private var conns : [ NSXPCConnection ] = [ ]
135133
136134 override init ( ) {
@@ -152,22 +150,6 @@ class HelperAppXPCListener: NSObject, NSXPCListenerDelegate, HelperAppXPCInterfa
152150 return true
153151 }
154152
155- func getPeerState( with reply: @escaping ( Data ? ) -> Void ) {
156- logger. info ( " getPeerState called " )
157- let reply = CallbackWrapper ( reply)
158- Task { @MainActor in
159- let data = try ? await globalManager? . getPeerState ( ) . serializedData ( )
160- reply ( data)
161- }
162- }
163-
164- func ping( reply: @escaping ( ) -> Void ) {
165- reply ( )
166- }
167- }
168-
169- // These methods are called to send updates to the Coder Desktop App.
170- extension HelperAppXPCListener {
171153 func onPeerUpdate( update: Vpn_PeerUpdate ) async throws {
172154 let bytes = try update. serializedData ( )
173155 return try await withCheckedThrowingContinuation { continuation in
@@ -203,3 +185,18 @@ extension HelperAppXPCListener {
203185 } as Void
204186 }
205187}
188+
189+ extension HelperAppXPCServer : HelperAppXPCInterface {
190+ func getPeerState( with reply: @escaping ( Data ? ) -> Void ) {
191+ logger. info ( " getPeerState called " )
192+ let reply = CallbackWrapper ( reply)
193+ Task { @MainActor in
194+ let data = try ? await globalManager? . getPeerState ( ) . serializedData ( )
195+ reply ( data)
196+ }
197+ }
198+
199+ func ping( reply: @escaping ( ) -> Void ) {
200+ reply ( )
201+ }
202+ }
0 commit comments