11import NetworkExtension
22import os
33import SwiftUI
4+ import VPNLib
45import VPNXPC
56
67@MainActor
@@ -42,7 +43,7 @@ enum VPNServiceError: Error, Equatable {
4243}
4344
4445@MainActor
45- final class CoderVPNService: NSObject , VPNService {
46+ final class CoderVPNService: NSObject , VPNService , @ preconcurrency VPNXPCClientCallbackProtocol {
4647 var logger = Logger ( subsystem: Bundle . main. bundleIdentifier!, category: " vpn " )
4748 var xpcConn : NSXPCConnection
4849 @Published var tunnelState : VPNServiceState = . disabled
@@ -66,11 +67,19 @@ final class CoderVPNService: NSObject, VPNService {
6667 var systemExtnDelegate : SystemExtensionDelegate < CoderVPNService > ?
6768
6869 override init ( ) {
69- xpcConn = NSXPCConnection ( serviceName: " com.coder.Coder-Desktop.VPNXPC " )
70+ let networkExtDict = Bundle . main. object ( forInfoDictionaryKey: " NetworkExtension " ) as? [ String : Any ]
71+ let machServiceName = networkExtDict ? [ " NEMachServiceName " ] as? String
72+ xpcConn = NSXPCConnection ( serviceName: machServiceName!)
7073 xpcConn. remoteObjectInterface = NSXPCInterface ( with: VPNXPCProtocol . self)
71- xpcConn. resume ( )
74+ xpcConn. exportedInterface = NSXPCInterface ( with : VPNXPCClientCallbackProtocol . self )
7275
7376 super. init ( )
77+ xpcConn. exportedObject = self
78+ // xpcConn.invalidationHandler = {
79+ // // self.logger.error("XPC connection invalidated.")
80+ // print("XPC connection invalidated")
81+ // }
82+ xpcConn. resume ( )
7483 installSystemExtension ( )
7584 Task {
7685 await loadNetworkExtension ( )
@@ -86,11 +95,6 @@ final class CoderVPNService: NSObject, VPNService {
8695 tunnelState = . connecting
8796 await enableNetworkExtension ( )
8897 logger. debug ( " network extension enabled " )
89- if let proxy = xpcConn. remoteObjectProxy as? VPNXPCProtocol {
90- proxy. start { _ in
91- self . tunnelState = . connected
92- }
93- }
9498 }
9599 defer { startTask = nil }
96100 await startTask? . value
@@ -106,9 +110,6 @@ final class CoderVPNService: NSObject, VPNService {
106110 }
107111 stopTask = Task {
108112 tunnelState = . disconnecting
109- if let proxy = xpcConn. remoteObjectProxy as? VPNXPCProtocol {
110- proxy. stop { _ in }
111- }
112113 await disableNetworkExtension ( )
113114 logger. info ( " network extension stopped " )
114115 tunnelState = . disabled
@@ -135,4 +136,26 @@ final class CoderVPNService: NSObject, VPNService {
135136 }
136137 }
137138 }
139+
140+ func onPeerUpdate( _ data: Data) {
141+ // TODO: handle peer update
142+ do {
143+ let msg = try Vpn_TunnelMessage ( serializedBytes: data)
144+ debugPrint ( msg)
145+ } catch {
146+ logger. error ( " failed to decode peer update \( error) " )
147+ }
148+ }
149+
150+ func onStart( ) {
151+ logger. info ( " network extension reported started " )
152+ tunnelState = . connected
153+ }
154+
155+ func onStop( ) {
156+ logger. info ( " network extension reported stopped " )
157+ tunnelState = . disabled
158+ }
159+
160+ func onError( _: NSError) { }
138161}
0 commit comments