Load site from existing preferences when starting

This commit is contained in:
Ian VanSchooten 2024-10-17 16:07:08 -04:00
parent 0c70c794cb
commit 0fde99a34f
1 changed files with 88 additions and 68 deletions

View File

@ -28,20 +28,34 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} }
private func start(completionHandler: @escaping (Error?) -> Void) { private func start(completionHandler: @escaping (Error?) -> Void) {
let proto = self.protocolConfiguration as! NETunnelProviderProtocol // Load vpn configs from system, and find the manager matching the one being started
NETunnelProviderManager.loadAllFromPreferences() { managers, error in
let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol;
let targetID = targetProtoConfig?.providerConfiguration!["id"] as? String;
guard let managers = managers else {
self.log.error("No managers were loaded")
return
}
for manager in managers {
var config: Data var config: Data
var key: String var key: String
let mgr = manager.protocolConfiguration as? NETunnelProviderProtocol;
let id = mgr?.providerConfiguration!["id"] as? String;
if (id == targetID) {
do { do {
site = try Site(proto: proto) self.site = try Site(manager: manager)
config = try site!.getConfig() config = try self.site!.getConfig()
} catch { } catch {
//TODO: need a way to notify the app //TODO: need a way to notify the app
log.error("Failed to render config from vpn object") self.log.error("Failed to render config from vpn object")
return completionHandler(error) return completionHandler(error)
} }
let _site = site! let _site = self.site!
do { do {
key = try _site.getKey() key = try _site.getKey()
@ -49,7 +63,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return completionHandler(error) return completionHandler(error)
} }
let fileDescriptor = tunnelFileDescriptor let fileDescriptor = self.tunnelFileDescriptor
if fileDescriptor == nil { if fileDescriptor == nil {
return completionHandler("Unable to locate the tun file descriptor") return completionHandler("Unable to locate the tun file descriptor")
} }
@ -98,6 +112,12 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
completionHandler(nil) completionHandler(nil)
}) })
// We're done looking through managers
continue
}
}
}
} }
private func handleDNUpdate(newSite: Site) { private func handleDNUpdate(newSite: Site) {