diff --git a/ios/NebulaNetworkExtension/PacketTunnelProvider.swift b/ios/NebulaNetworkExtension/PacketTunnelProvider.swift index 215cf02..336b71f 100644 --- a/ios/NebulaNetworkExtension/PacketTunnelProvider.swift +++ b/ios/NebulaNetworkExtension/PacketTunnelProvider.swift @@ -7,6 +7,7 @@ enum VPNStartError: Error { case noManagers case couldNotFindManager case noTunFileDescriptor + case noProviderConfig } class PacketTunnelProvider: NEPacketTunnelProvider { @@ -115,15 +116,20 @@ class PacketTunnelProvider: NEPacketTunnelProvider { // } private func findManager() async throws -> NETunnelProviderManager { - let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol; - let targetID = targetProtoConfig?.providerConfiguration!["id"] as? String; + let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol + 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 let managers = try await NETunnelProviderManager.loadAllFromPreferences() for manager in managers { - let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol; - let id = mgrProtoConfig?.providerConfiguration!["id"] as? String; - + let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol + guard let mgrProviderConfig = mgrProtoConfig?.providerConfiguration else { + throw VPNStartError.noProviderConfig + } + let id = mgrProviderConfig["id"] as? String if (id == targetID) { return manager } diff --git a/ios/NebulaNetworkExtension/Site.swift b/ios/NebulaNetworkExtension/Site.swift index ef1a3b1..8f034ec 100644 --- a/ios/NebulaNetworkExtension/Site.swift +++ b/ios/NebulaNetworkExtension/Site.swift @@ -489,6 +489,8 @@ struct IncomingSite: Codable { // Stuff our details in the protocol let proto = manager.protocolConfiguration as? NETunnelProviderProtocol ?? NETunnelProviderProtocol() 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.serverAddress = "Nebula"