How to keep my VPN alive?

Hi, I use NEPacketTunnelProvider to create a always-on VPN tunnel in my AppExtension. In my case, the device need to send a heartbeat packet to the VPN server in order to keep tunnel alive. But sometimes the device will be kick-off by the server, beacause it can't connect to VPN server. My question is:

  1. Is there some way or mesure to reconnect the VPN server?
  2. I notice there are wake() and sleep() two methodes in NEPacketTunnelProvider, do they work in my case? What are they use for?

Do you have an observer setup for NEVPNStatusDidChange to monitor the connection of the VPN changing? If so, can you reconnect after the app has gone into the .disconnected status?

| 1. Is there some way or mesure to reconnect the VPN server?


The wake() and sleep() methods found in NEProvider are functions called by the framework right before the system wakes up from being asleep and right before it goes to sleep. I can imagine these functions being for useful for cleanup or re-connection routines when the app is passing through one of these states.

| 2. I notice there are wake() and sleep() two methodes in NEPacketTunnelProvider, do they

| work in my case? What are they use for?



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Thanks for your answer, meaton. I have observed VPN status to reconnected my VPN. But it looks like my extension can not connect to VPN server when my iPhone is locked sometimes. So I try to add reconnect event in wake() method. The problem is that I have wrote a log info in wake() method, and then I got more than 10 times wake() log informations even through I just locked my iPhone for a whole night.

Could you please give me more information about the wake mechansim?

Interesting. In the console where you are reviewing the os_log messages, do you see any other logging activity around your app being woken up in the background even though your phone my be asleep and locked? I am wondering if the system is just checking in with your process and that is why you are seeing the repeated log messages.



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Thanks for your support.

The wake/sleep happens when the device goes into "real" sleep, e.g. it is NOT the same as locked. I think that the OS wakes the phone when e.g. some app have scheduled background fetch or when you receive background push (maybe the system periodically wakes as well). During sleep you won't get any cpu time at all (according to my observations).


You should note that the OS shuts off WiFi when going into sleep and starts it when waking up.


Depending on how often you need to send the heartbeat, you should probably re-establish the tunnel upon waking up.

Is this a 9777 port takeover issue?

KEEPALIVE) 210706 15:00:51.752 [INFO][9777.Tunnel.sleep] 210706 15:00:57.494 [INFO][9777.Tunnel.wake] 210706 15:01:02.514 [INFO][9777.Tunnel.sleep] 210706 15:01:03.868 [INFO][9777.Tunnel.wake] 210706 15:01:07.262 [INFO][9777.Tunnel.checkLastWriteTime] Time since last send: 24.440653920173645 210706 15:01:07.263 [INFO][9777.Warp.log_func] BoringTun: Sending handshake_initiation 210706 15:01:07.330 [INFO][9777.Warp.log_func] BoringTun: Received handshake_response 210706 15:01:07.331 [INFO][9777.Warp.log_func] BoringTun: New session 210706 15:01:07.331 [INFO][9777.Warp.log_func] BoringTun: Sending keepalive 210706 15:01:08.865 [INFO][9777.Tunnel.sleep] 210706 15:01:29.654 [INFO][9777.Tunnel.wake] 210706 15:01:31.690 [INFO][9777.Tunnel.checkLastWriteTime] Time since last send: 24.426890015602112 210706 15:01:31.691 [INFO][9777.Warp.log_func] BoringTun: Sending handshake_initiation 210706 15:01:31.721 [INFO][9777.Warp.log_func] BoringTun: Received handshake_response 210706 15:01:31.722 [INFO][9777.Warp.log_func] BoringTun: New session 210706 15:01:31.722 [INFO][9777.Warp.log_func] BoringTun: Sending keepalive 210706 15:01:33.246 [INFO][9777.Warp.log_func] BoringTun: KEEPALIVE(PERSISTENT_KEEPALIVE) 210706 15:01:45.356 [INFO][9777.Warp.log_func] BoringTun: SESSION_EXPIRED(REJECT_AFTER_TIME) 210706 15:01:46.166 [INFO][9777.Warp.log_func] BoringTun: HANDSHAKE(KEEPALIVE + REKEY_TIMEOUT) 210706 15:01:46.168 [INFO][9777.Warp.log_func] BoringTun: Sending handshake_initiation 210706 15:01:46.196 [INFO][9777.Warp.log_func] BoringTun: Received handshake_response 210706 15:01:46.197 [INFO][9777.Warp.log_func] BoringTun: New session 210706 15:01:46.197 [INFO][9777.Warp.log_func] BoringTun: Sending keepalive 210706 15:01:55.227 [INFO][9777.DNS.handleQueryResponse] Using fallback resolver for query: id: 18192; query; status: noError; questions: 1; answers: 0; auth: 0; addition: 0 210706 15:01:55.227 [INFO][9777.DNS.handleQueryResponse] Using fallback resolver for query: id: 461; query; status: noError; questions: 1; answers: 0; auth: 0; addition: 0 210706 15:01:56.295 [INFO][9777.Tunnel.startBetterPathProbe] Warp: can upgrade connection 210706 15:01:56.295 [INFO][9777.Tunnel.warpConnect] Warp: connecting 210706 15:01:56.313 [INFO][9777.LogExtension.networkInfo] Cellular: [OPTUS, MCC: 505, MNC: 02] - WiFi: [WiFi-0363-5G 74:da:88:18:3:65] - Path DNS servers: ["192.168.1.1", "0.0.0.0"] 210706 15:01:56.315 [INFO][9777.WarpLegacyUDPSession.happyEyeballs] Warp: happy eyeballs sessions created 210706 15:01:56.315 [INFO][9777.WarpLegacyUDPSession.happyEyeballs] Warp: happy eyeballs racer registered for IPv6 210706 15:01:56.315 [INFO][9777.WarpLegacyUDPSession.happyEyeballs] Warp: happy eyeballs racer registered for IPv4 210706 15:01:56.316 [INFO][9777.WarpLegacyUDPSession.happyEyeballs] Warp: happy eyeballs for IPv4 state changed to: UDPSessionStateReady (The session is ready for reading and writing data

How to keep my VPN alive?
 
 
Q