From 1cd00f9459613b265b680410c35fd151f92fffd9 Mon Sep 17 00:00:00 2001 From: Robert Patchett Date: Mon, 28 Oct 2019 14:01:13 +0100 Subject: [PATCH] Call cancelTunnelWithError(_:) if a connection fails and won't be retried --- .../OpenVPN/AppExtension/OpenVPNTunnelProvider.swift | 12 ++++++++++++ .../Sources/Protocols/OpenVPN/OpenVPNSession.swift | 11 +++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift b/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift index 1b85c55..eb9f586 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift @@ -579,6 +579,18 @@ extension OpenVPNTunnelProvider: OpenVPNSessionDelegate { public func sessionDidStop(_: OpenVPNSession, shouldReconnect: Bool) { log.info("Session did stop") + stopSession(shouldReconnect: shouldReconnect) + } + + /// :nodoc: + public func sessionFailed(_: OpenVPNSession, error: Error) { + log.info("Session failed") + + cancelTunnelWithError(error) + stopSession(shouldReconnect: false) + } + + private func stopSession(shouldReconnect: Bool) { isCountingData = false refreshDataCount() diff --git a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift index d781147..5234336 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift @@ -59,6 +59,11 @@ public protocol OpenVPNSessionDelegate: class { - Seealso: `OpenVPNSession.reconnect(...)` */ func sessionDidStop(_: OpenVPNSession, shouldReconnect: Bool) + + /** + Called after a session failed to start. + */ + func sessionFailed(_: OpenVPNSession, error: Error) } /// Provides methods to set up and maintain an OpenVPN session. @@ -1275,13 +1280,15 @@ public class OpenVPNSession: Session { } private func doShutdown(error: Error?) { + stopError = error + if let error = error { log.error("Trigger shutdown (error: \(error))") + delegate?.sessionFailed(self, error: error) } else { log.info("Trigger shutdown on request") + delegate?.sessionDidStop(self, shouldReconnect: false) } - stopError = error - delegate?.sessionDidStop(self, shouldReconnect: false) } private func doReconnect(error: Error?) {