How to remove all existing managers from NETunnelProviderManager?

Viewed 86

I have come across a problem when using a NetworkExtension in Swift to utilise OpenVPN or Wireguard with the NETunnelProviderManager.

I have the following code in the ViewDidLoad of my mainViewController. I don't load it twice, and it works fine when using the Apple integrated IKEv2.

NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NEVPNStatusDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(vpnStatusDidChange(_:)), name: NSNotification.Name.NEVPNStatusDidChange, object: nil)

But when I use OpenVPN or Wireguard via the Networkextension, these network-status-change-events keep firing n+1. First time it fires once. Second time I connect and disconnect, I see the network-statuses firing twice, then three times etc.

I have been researching a lot and it seems the problem is in the code below:

func loadTunnelProviderManager(completion:@escaping () -> Void) {
        NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
            if error == nil {
                self.tunnelProviderManager = managers?.first ?? NETunnelProviderManager()
                completion()
            }
        }
    }

If I don't retrieve managers?.first and instantiate it each time instead:

self.tunnelProviderManager = NETunnelProviderManager()

Then the issue is resolved. However every time I connect to the VPN, inevitably a new VPN config is also created in the settings, which is not ideal.

TL;DR

Long story short, how do I delete all existing managers before instantiating a new NETunnelProviderManager? I know this might go against the convention, but it will solve the issue.

0 Answers
Related