-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.swift
65 lines (53 loc) · 2.1 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Foundation
import NetworkExtension
import os
import VPNLib
let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "provider")
final class XPCListenerDelegate: NSObject, NSXPCListenerDelegate, @unchecked Sendable {
let vpnXPCInterface = XPCInterface()
private var activeConnection: NSXPCConnection?
private var connMutex: NSLock = .init()
var conn: VPNXPCClientCallbackProtocol? {
connMutex.lock()
defer { connMutex.unlock() }
let conn = activeConnection?.remoteObjectProxy as? VPNXPCClientCallbackProtocol
return conn
}
func setActiveConnection(_ connection: NSXPCConnection?) {
connMutex.lock()
defer { connMutex.unlock() }
activeConnection = connection
}
func listener(_: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
newConnection.exportedInterface = NSXPCInterface(with: VPNXPCProtocol.self)
newConnection.exportedObject = vpnXPCInterface
newConnection.remoteObjectInterface = NSXPCInterface(with: VPNXPCClientCallbackProtocol.self)
newConnection.invalidationHandler = { [weak self] in
logger.info("active connection dead")
self?.setActiveConnection(nil)
}
newConnection.interruptionHandler = { [weak self] in
logger.debug("connection interrupted")
self?.setActiveConnection(nil)
}
logger.info("new active connection")
setActiveConnection(newConnection)
newConnection.resume()
return true
}
}
guard
let netExt = Bundle.main.object(forInfoDictionaryKey: "NetworkExtension") as? [String: Any],
let serviceName = netExt["NEMachServiceName"] as? String
else {
fatalError("Missing NEMachServiceName in Info.plist")
}
logger.debug("listening on machServiceName: \(serviceName)")
autoreleasepool {
NEProvider.startSystemExtensionMode()
}
let globalXPCListenerDelegate = XPCListenerDelegate()
let xpcListener = NSXPCListener(machServiceName: serviceName)
xpcListener.delegate = globalXPCListenerDelegate
xpcListener.resume()
dispatchMain()