kean / VPN

Sample custom VPN client/server in Swift
184 stars 45 forks source link

How to reconfigure NEPacketTunnelNetworkSettings on network changes? #3

Closed dudutwizer closed 3 years ago

dudutwizer commented 4 years ago

Hi!

First of all, thank you, your code is super helpful.

I need to change the tunSettings.dnsSettings = NEDNSSettings(servers: []) based on the user current IP address (wifi or cellular ) -- I'm listen to network events with Reachability library and together with on-demand rules I thought I can simply do

reachability.whenReachable = { reachability in if reachability.connection == .wifi { self.stopTunnel(with: .none, completionHandler: {print("asdasd")}) } else { self.stopTunnel(with: .none, completionHandler: {print("asdasd")}) } }

but it doesn't stop the tunnel, when I debug it, I can see that it's been called , but the tunnel stays connected..

when I manually turn the connection off, the on-demand rules kick off and my startTunnel function called.

To summarize, I want to be able to reconfigure NEPacketTunnelNetworkSettings object every time the user changes his public ip.

Thank you very much!