Report error status to shared defaults

Retain after disposal, unless manually stopped.
This commit is contained in:
Davide De Rosa 2018-10-21 23:30:55 +02:00
parent 7ffb997904
commit 1ad4a62593
1 changed files with 37 additions and 1 deletions

View File

@ -176,6 +176,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider {
) )
log.info("Starting tunnel...") log.info("Starting tunnel...")
clearErrorStatus()
guard SessionProxy.EncryptionBridge.prepareRandomNumberGenerator(seedLength: prngSeedLength) else { guard SessionProxy.EncryptionBridge.prepareRandomNumberGenerator(seedLength: prngSeedLength) else {
completionHandler(ProviderConfigurationError.prngInitialization) completionHandler(ProviderConfigurationError.prngInitialization)
@ -259,6 +260,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider {
open override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { open override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
pendingStartHandler = nil pendingStartHandler = nil
log.info("Stopping tunnel...") log.info("Stopping tunnel...")
clearErrorStatus()
guard let proxy = proxy else { guard let proxy = proxy else {
flushLog() flushLog()
@ -324,6 +326,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider {
private func connectTunnel(via socket: GenericSocket) { private func connectTunnel(via socket: GenericSocket) {
log.info("Will connect to \(socket)") log.info("Will connect to \(socket)")
clearErrorStatus()
log.debug("Socket type is \(type(of: socket))") log.debug("Socket type is \(type(of: socket))")
self.socket = socket self.socket = socket
@ -342,6 +345,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider {
if let error = error { if let error = error {
log.error("Tunnel did stop (error: \(error))") log.error("Tunnel did stop (error: \(error))")
setErrorStatus(with: error)
} else { } else {
log.info("Tunnel did stop on request") log.info("Tunnel did stop on request")
} }
@ -349,7 +353,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider {
private func disposeTunnel(error: Error?) { private func disposeTunnel(error: Error?) {
flushLog() flushLog()
// failed to start // failed to start
if (pendingStartHandler != nil) { if (pendingStartHandler != nil) {
@ -599,6 +603,38 @@ extension TunnelKitProvider {
} }
} }
private func setErrorStatus(with error: Error) {
guard let lastErrorKey = cfg.lastErrorKey else {
return
}
let providerError: ProviderError
if let se = error as? SessionError {
switch se {
case .badCredentials:
providerError = .authenticationFailed
case .peerVerification, .tlsError:
providerError = .tlsFailed
case .negotiationTimeout, .pingTimeout:
providerError = .timeout
default:
providerError = .unexpectedReply
}
} else {
providerError = error as? ProviderError ?? .linkError
}
defaults?.set(providerError.rawValue, forKey: lastErrorKey)
}
private func clearErrorStatus() {
guard let lastErrorKey = cfg.lastErrorKey else {
return
}
defaults?.removeObject(forKey: lastErrorKey)
}
private func logCurrentSSID() { private func logCurrentSSID() {
if let ssid = observer.currentWifiNetworkName() { if let ssid = observer.currentWifiNetworkName() {
log.debug("Current SSID: '\(ssid)'") log.debug("Current SSID: '\(ssid)'")