Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Do not clear reasserting flag when connection is not recoverable #128

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {

private var isCountingData = false

private var shouldReconnect = false

// MARK: NEPacketTunnelProvider (XPC queue)

open override var reasserting: Bool {
Expand Down Expand Up @@ -341,7 +343,7 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
}

private func finishTunnelDisconnection(error: Error?) {
if let session = session, !(reasserting && session.canRebindLink()) {
if let session = session, !(shouldReconnect && session.canRebindLink()) {
session.cleanup()
}

Expand Down Expand Up @@ -419,7 +421,7 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
/// :nodoc:
public func socketDidTimeout(_ socket: GenericSocket) {
log.debug("Socket timed out waiting for activity, cancelling...")
reasserting = true
shouldReconnect = true
socket.shutdown()

// fallback: TCP connection timeout suggests falling back
Expand All @@ -438,7 +440,7 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
}
if session.canRebindLink() {
session.rebindLink(producer.link(withMTU: cfg.mtu))
reasserting = false
shouldReconnect = false
} else {
session.setLink(producer.link(withMTU: cfg.mtu))
}
Expand Down Expand Up @@ -478,17 +480,18 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
}

// reconnect?
if reasserting {
if shouldReconnect {
log.debug("Disconnection is recoverable, tunnel will reconnect in \(reconnectionDelay) milliseconds...")
tunnelQueue.schedule(after: .milliseconds(reconnectionDelay)) {
log.debug("Tunnel is about to reconnect...")

// give up if reasserting cleared in the meantime
guard self.reasserting else {
log.warning("Reasserting flag was cleared in the meantime")
// give up if shouldReconnect cleared in the meantime
guard self.shouldReconnect else {
log.warning("Reconnection flag was cleared in the meantime")
return
}

log.debug("Tunnel is about to reconnect...")
self.reasserting = true
self.connectTunnel(upgradedSocket: upgradedSocket)
}
return
Expand Down Expand Up @@ -576,13 +579,17 @@ extension OpenVPNTunnelProvider: OpenVPNSessionDelegate {
}

/// :nodoc:
public func sessionDidStop(_: OpenVPNSession, shouldReconnect: Bool) {
log.info("Session did stop")
public func sessionDidStop(_: OpenVPNSession, withError error: Error?, shouldReconnect: Bool) {
if let error = error {
log.error("Session did stop with error: \(error)")
} else {
log.info("Session did stop")
}

isCountingData = false
refreshDataCount()

reasserting = shouldReconnect
self.shouldReconnect = shouldReconnect
socket?.shutdown()
}

Expand Down
7 changes: 4 additions & 3 deletions TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public protocol OpenVPNSessionDelegate: class {
/**
Called after stopping a session.

- Parameter error: An optional `Error` being the reason of the stop.
- Parameter shouldReconnect: When `true`, the session can/should be restarted. Usually because the stop reason was recoverable.
- Seealso: `OpenVPNSession.reconnect(...)`
*/
func sessionDidStop(_: OpenVPNSession, shouldReconnect: Bool)
func sessionDidStop(_: OpenVPNSession, withError error: Error?, shouldReconnect: Bool)
}

/// Provides methods to set up and maintain an OpenVPN session.
Expand Down Expand Up @@ -1281,7 +1282,7 @@ public class OpenVPNSession: Session {
log.info("Trigger shutdown on request")
}
stopError = error
delegate?.sessionDidStop(self, shouldReconnect: false)
delegate?.sessionDidStop(self, withError: error, shouldReconnect: false)
}

private func doReconnect(error: Error?) {
Expand All @@ -1291,6 +1292,6 @@ public class OpenVPNSession: Session {
log.info("Trigger reconnection on request")
}
stopError = error
delegate?.sessionDidStop(self, shouldReconnect: true)
delegate?.sessionDidStop(self, withError: error, shouldReconnect: true)
}
}