diff --git a/TunnelKit/Sources/Protocols/OpenVPN/CoreConfiguration+OpenVPN.swift b/TunnelKit/Sources/Protocols/OpenVPN/CoreConfiguration+OpenVPN.swift index 4a8e35d..03f53e9 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/CoreConfiguration+OpenVPN.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/CoreConfiguration+OpenVPN.swift @@ -53,6 +53,8 @@ extension CoreConfiguration { static let pushRequestInterval = 2.0 + static let pingTimeoutCheckInterval = 10.0 + static let pingTimeout = 120.0 static let retransmissionLimit = 0.1 diff --git a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift index 6ec894f..b61ba0c 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift @@ -555,19 +555,26 @@ public class OpenVPNSession: Session { return } - log.debug("Send ping") - sendDataPackets([OpenVPN.DataPacket.pingString]) - lastPing.outbound = Date() + // is keep-alive enabled? + if let _ = keepAliveInterval { + log.debug("Send ping") + sendDataPackets([OpenVPN.DataPacket.pingString]) + lastPing.outbound = Date() + } + // schedule even just to check for ping timeout scheduleNextPing() } private func scheduleNextPing() { - guard let interval = keepAliveInterval else { - log.verbose("Skip ping, keep-alive not set") - return + let interval: TimeInterval + if let keepAliveInterval = keepAliveInterval { + interval = keepAliveInterval + log.verbose("Schedule ping after \(interval) seconds") + } else { + interval = CoreConfiguration.OpenVPN.pingTimeoutCheckInterval + log.verbose("Schedule ping timeout check after \(interval) seconds") } - log.verbose("Schedule ping after \(interval) seconds") queue.asyncAfter(deadline: .now() + interval) { [weak self] in log.verbose("Running ping block") self?.ping()