Log time intervals better

This commit is contained in:
Davide De Rosa 2021-01-27 01:36:48 +01:00
parent 4490f0c116
commit 1343fa592f
4 changed files with 32 additions and 10 deletions

View File

@ -59,3 +59,19 @@ public extension Encodable {
return dictionary 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()
}
}

View File

@ -581,17 +581,17 @@ extension OpenVPN.Configuration {
log.info("\tTLS security level: default") log.info("\tTLS security level: default")
} }
if let keepAliveSeconds = keepAliveInterval, keepAliveSeconds > 0 { if let keepAliveSeconds = keepAliveInterval, keepAliveSeconds > 0 {
log.info("\tKeep-alive interval: \(keepAliveSeconds) seconds") log.info("\tKeep-alive interval: \(keepAliveSeconds.asTimeString)")
} else { } else {
log.info("\tKeep-alive interval: never") log.info("\tKeep-alive interval: never")
} }
if let keepAliveTimeoutSeconds = keepAliveTimeout, keepAliveTimeoutSeconds > 0 { if let keepAliveTimeoutSeconds = keepAliveTimeout, keepAliveTimeoutSeconds > 0 {
log.info("\tKeep-alive timeout: \(keepAliveTimeoutSeconds) seconds") log.info("\tKeep-alive timeout: \(keepAliveTimeoutSeconds.asTimeString)")
} else { } else {
log.info("\tKeep-alive timeout: never") log.info("\tKeep-alive timeout: never")
} }
if let renegotiatesAfterSeconds = renegotiatesAfter, renegotiatesAfterSeconds > 0 { if let renegotiatesAfterSeconds = renegotiatesAfter, renegotiatesAfterSeconds > 0 {
log.info("\tRenegotiation: \(renegotiatesAfterSeconds) seconds") log.info("\tRenegotiation: \(renegotiatesAfterSeconds.asTimeString)")
} else { } else {
log.info("\tRenegotiation: never") log.info("\tRenegotiation: never")
} }

View File

@ -573,10 +573,10 @@ public class OpenVPNSession: Session {
let interval: TimeInterval let interval: TimeInterval
if let keepAliveInterval = keepAliveInterval { if let keepAliveInterval = keepAliveInterval {
interval = keepAliveInterval interval = keepAliveInterval
log.verbose("Schedule ping after \(interval) seconds") log.verbose("Schedule ping after \(interval.asTimeString)")
} else { } else {
interval = CoreConfiguration.OpenVPN.pingTimeoutCheckInterval 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 queue.asyncAfter(deadline: .now() + interval) { [weak self] in
log.verbose("Running ping block") log.verbose("Running ping block")
@ -739,7 +739,7 @@ public class OpenVPNSession: Session {
let elapsed = -negotiationKey.startTime.timeIntervalSinceNow let elapsed = -negotiationKey.startTime.timeIntervalSinceNow
if (elapsed > renegotiatesAfter) { if (elapsed > renegotiatesAfter) {
log.debug("Renegotiating after \(elapsed) seconds") log.debug("Renegotiating after \(elapsed.asTimeString)")
softReset(isServerInitiated: false) softReset(isServerInitiated: false)
} }
} }
@ -1099,10 +1099,10 @@ public class OpenVPNSession: Session {
log.info("\tNegotiated compression algorithm: \(negCompression)") log.info("\tNegotiated compression algorithm: \(negCompression)")
} }
if let negPing = pushReply.options.keepAliveInterval { 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 { 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 let bridge: OpenVPN.EncryptionBridge

View File

@ -57,6 +57,7 @@ class AppExtensionTests: XCTestCase {
let identifier = "com.example.Provider" let identifier = "com.example.Provider"
let appGroup = "group.com.algoritmico.TunnelKit" let appGroup = "group.com.algoritmico.TunnelKit"
let hostname = "example.com" let hostname = "example.com"
let context = "foobar"
let credentials = OpenVPN.Credentials("foo", "bar") let credentials = OpenVPN.Credentials("foo", "bar")
var sessionBuilder = OpenVPN.ConfigurationBuilder() var sessionBuilder = OpenVPN.ConfigurationBuilder()
@ -71,13 +72,18 @@ class AppExtensionTests: XCTestCase {
cfg = builder.build() 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) XCTAssertNotNil(proto)
XCTAssertEqual(proto?.providerBundleIdentifier, identifier) XCTAssertEqual(proto?.providerBundleIdentifier, identifier)
XCTAssertEqual(proto?.serverAddress, hostname) XCTAssertEqual(proto?.serverAddress, hostname)
XCTAssertEqual(proto?.username, credentials.username) 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 { guard let pc = proto?.providerConfiguration else {
return return