From 1343fa592fedd09c189fe95d0e5b96baf8e034ac Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 27 Jan 2021 01:36:48 +0100 Subject: [PATCH] Log time intervals better --- TunnelKit/Sources/Core/Utils.swift | 16 ++++++++++++++++ .../Protocols/OpenVPN/Configuration.swift | 6 +++--- .../Protocols/OpenVPN/OpenVPNSession.swift | 10 +++++----- TunnelKit/Tests/OpenVPN/AppExtensionTests.swift | 10 ++++++++-- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/TunnelKit/Sources/Core/Utils.swift b/TunnelKit/Sources/Core/Utils.swift index 49d4b7a..873c682 100644 --- a/TunnelKit/Sources/Core/Utils.swift +++ b/TunnelKit/Sources/Core/Utils.swift @@ -59,3 +59,19 @@ public extension Encodable { return dictionary } } + +/// :nodoc: +extension TimeInterval { + var asTimeString: String { + var ticks = Int(self) + let hours = ticks / 3600 + ticks %= 3600 + let minutes = ticks / 60 + let seconds = ticks % 60 + + return [(hours, "h"), (minutes, "m"), (seconds, "s")] + .filter { $0.0 > 0 } + .map { "\($0.0)\($0.1)" } + .joined() + } +} diff --git a/TunnelKit/Sources/Protocols/OpenVPN/Configuration.swift b/TunnelKit/Sources/Protocols/OpenVPN/Configuration.swift index 9727ba6..f3a2873 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/Configuration.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/Configuration.swift @@ -581,17 +581,17 @@ extension OpenVPN.Configuration { log.info("\tTLS security level: default") } if let keepAliveSeconds = keepAliveInterval, keepAliveSeconds > 0 { - log.info("\tKeep-alive interval: \(keepAliveSeconds) seconds") + log.info("\tKeep-alive interval: \(keepAliveSeconds.asTimeString)") } else { log.info("\tKeep-alive interval: never") } if let keepAliveTimeoutSeconds = keepAliveTimeout, keepAliveTimeoutSeconds > 0 { - log.info("\tKeep-alive timeout: \(keepAliveTimeoutSeconds) seconds") + log.info("\tKeep-alive timeout: \(keepAliveTimeoutSeconds.asTimeString)") } else { log.info("\tKeep-alive timeout: never") } if let renegotiatesAfterSeconds = renegotiatesAfter, renegotiatesAfterSeconds > 0 { - log.info("\tRenegotiation: \(renegotiatesAfterSeconds) seconds") + log.info("\tRenegotiation: \(renegotiatesAfterSeconds.asTimeString)") } else { log.info("\tRenegotiation: never") } diff --git a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift index 4f453dd..8070618 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift @@ -573,10 +573,10 @@ public class OpenVPNSession: Session { let interval: TimeInterval if let keepAliveInterval = keepAliveInterval { interval = keepAliveInterval - log.verbose("Schedule ping after \(interval) seconds") + log.verbose("Schedule ping after \(interval.asTimeString)") } else { interval = CoreConfiguration.OpenVPN.pingTimeoutCheckInterval - log.verbose("Schedule ping timeout check after \(interval) seconds") + log.verbose("Schedule ping timeout check after \(interval.asTimeString)") } queue.asyncAfter(deadline: .now() + interval) { [weak self] in log.verbose("Running ping block") @@ -739,7 +739,7 @@ public class OpenVPNSession: Session { let elapsed = -negotiationKey.startTime.timeIntervalSinceNow if (elapsed > renegotiatesAfter) { - log.debug("Renegotiating after \(elapsed) seconds") + log.debug("Renegotiating after \(elapsed.asTimeString)") softReset(isServerInitiated: false) } } @@ -1099,10 +1099,10 @@ public class OpenVPNSession: Session { log.info("\tNegotiated compression algorithm: \(negCompression)") } if let negPing = pushReply.options.keepAliveInterval { - log.info("\tNegotiated keep-alive interval: \(negPing) seconds") + log.info("\tNegotiated keep-alive interval: \(negPing.asTimeString)") } if let negPingRestart = pushReply.options.keepAliveTimeout { - log.info("\tNegotiated keep-alive timeout: \(negPingRestart) seconds") + log.info("\tNegotiated keep-alive timeout: \(negPingRestart.asTimeString)") } let bridge: OpenVPN.EncryptionBridge diff --git a/TunnelKit/Tests/OpenVPN/AppExtensionTests.swift b/TunnelKit/Tests/OpenVPN/AppExtensionTests.swift index d0dd441..e6540cf 100644 --- a/TunnelKit/Tests/OpenVPN/AppExtensionTests.swift +++ b/TunnelKit/Tests/OpenVPN/AppExtensionTests.swift @@ -57,6 +57,7 @@ class AppExtensionTests: XCTestCase { let identifier = "com.example.Provider" let appGroup = "group.com.algoritmico.TunnelKit" let hostname = "example.com" + let context = "foobar" let credentials = OpenVPN.Credentials("foo", "bar") var sessionBuilder = OpenVPN.ConfigurationBuilder() @@ -71,13 +72,18 @@ class AppExtensionTests: XCTestCase { cfg = builder.build() - let proto = try? cfg.generatedTunnelProtocol(withBundleIdentifier: identifier, appGroup: appGroup, credentials: credentials) + let proto = try? cfg.generatedTunnelProtocol( + withBundleIdentifier: identifier, + appGroup: appGroup, + context: context, + username: credentials.username + ) XCTAssertNotNil(proto) XCTAssertEqual(proto?.providerBundleIdentifier, identifier) XCTAssertEqual(proto?.serverAddress, hostname) XCTAssertEqual(proto?.username, credentials.username) - XCTAssertEqual(proto?.passwordReference, try? Keychain(group: appGroup).passwordReference(for: credentials.username)) + XCTAssertEqual(proto?.passwordReference, try? Keychain(group: appGroup).passwordReference(for: credentials.username, context: context)) guard let pc = proto?.providerConfiguration else { return