Hello, I am developing a vpn app using several protocols, one of them is Wireguard. When the app is terminated, there is a tunnel that vpn is still communicating with which is a subclass of NEPacketTunnelProvider. In some cases when stopping the tunnel using wireguardAdapter.stop(withcompletionHandler:) func it may throws an error of stopping the tunnel, in this case I want to force stop the tunnel, but when on-demand is on it will try to reconnect and the user will remains without internet connection on his wifi until he turns off the on-demand by his hand. I tried to call the NEVPNMnager and load preference and then turn on-demand of then stop the tunnel and it did not works. here is my code.
` let manager = NEVPNManager.shared()
manager.loadFromPreferences { error in
if let error = error {
self.wgLogger.log("Error loading from preferences: \(error, privacy: .public)")
return
}
manager.isOnDemandEnabled = false
manager.saveToPreferences { error in
if let error = error {
self.wgLogger.log("Error saving to preferences: \(error, privacy: .public)")
return
}
manager.connection.stopVPNTunnel()
self.wgLogger.log("VPN tunnel stopped successfully.")
}
}
i am just calling
stopTunnel
Oi vey!, that’s not good. Quoting the docs for stopTunnel(with:completionHandler:)
:
This method is called by the system to stop the network tunnel.
and:
Do not use this method to stop the tunnel from the Packet Tunnel Provider. Use -cancelTunnelWithError:
instead.
So, the system calls stopTunnel(with:completionHandler:)
when it wants you to stop the tunnel. You are meant to override that to actually stop the tunnel. You are not meant to call it directly. Rather, to stop the tunnel from within your provider, call cancelTunnelWithError(_:)
.
This is reiterated in the docs for cancelTunnelWithError(_:)
:
The Packet Tunnel Provider should call this method when an unrecoverable error occurs, such as the tunnel server going down or the VPN authentication session expiring.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"