Add safety around missing providerConfiguration

This commit is contained in:
Ian VanSchooten 2024-10-18 11:48:00 -04:00
parent d25834d69d
commit b0331ed111
2 changed files with 13 additions and 5 deletions

View File

@ -7,6 +7,7 @@ enum VPNStartError: Error {
case noManagers case noManagers
case couldNotFindManager case couldNotFindManager
case noTunFileDescriptor case noTunFileDescriptor
case noProviderConfig
} }
class PacketTunnelProvider: NEPacketTunnelProvider { class PacketTunnelProvider: NEPacketTunnelProvider {
@ -115,15 +116,20 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
// } // }
private func findManager() async throws -> NETunnelProviderManager { private func findManager() async throws -> NETunnelProviderManager {
let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol; let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol
let targetID = targetProtoConfig?.providerConfiguration!["id"] as? String; guard let targetProviderConfig = targetProtoConfig?.providerConfiguration else {
throw VPNStartError.noProviderConfig
}
let targetID = targetProviderConfig["id"] as? String
// Load vpn configs from system, and find the manager matching the one being started // Load vpn configs from system, and find the manager matching the one being started
let managers = try await NETunnelProviderManager.loadAllFromPreferences() let managers = try await NETunnelProviderManager.loadAllFromPreferences()
for manager in managers { for manager in managers {
let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol; let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol
let id = mgrProtoConfig?.providerConfiguration!["id"] as? String; guard let mgrProviderConfig = mgrProtoConfig?.providerConfiguration else {
throw VPNStartError.noProviderConfig
}
let id = mgrProviderConfig["id"] as? String
if (id == targetID) { if (id == targetID) {
return manager return manager
} }

View File

@ -489,6 +489,8 @@ struct IncomingSite: Codable {
// Stuff our details in the protocol // Stuff our details in the protocol
let proto = manager.protocolConfiguration as? NETunnelProviderProtocol ?? NETunnelProviderProtocol() let proto = manager.protocolConfiguration as? NETunnelProviderProtocol ?? NETunnelProviderProtocol()
proto.providerBundleIdentifier = "net.defined.mobileNebula.NebulaNetworkExtension"; proto.providerBundleIdentifier = "net.defined.mobileNebula.NebulaNetworkExtension";
// WARN: If we stop setting providerConfiguration["id"] here, we'll need to use something else to match
// managers in PacketTunnelProvider.findManager
proto.providerConfiguration = ["id": self.id] proto.providerConfiguration = ["id": self.id]
proto.serverAddress = "Nebula" proto.serverAddress = "Nebula"