From f5d12300f9adcdb9c1e968673d4656f20faa185c Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 24 Oct 2018 19:21:44 +0200 Subject: [PATCH 1/4] Save debug log to file in app group container Don't bog UserDefaults. Reuse debugLogKey for the log filename. --- .../AppExtension/MemoryDestination.swift | 6 +++--- .../TunnelKitProvider+Configuration.swift | 18 ++++++++++++++---- .../AppExtension/TunnelKitProvider.swift | 11 ++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/TunnelKit/Sources/AppExtension/MemoryDestination.swift b/TunnelKit/Sources/AppExtension/MemoryDestination.swift index 4630a23..973c795 100644 --- a/TunnelKit/Sources/AppExtension/MemoryDestination.swift +++ b/TunnelKit/Sources/AppExtension/MemoryDestination.swift @@ -54,11 +54,11 @@ class MemoryDestination: BaseDestination, CustomStringConvertible { } } - func flush(to: UserDefaults, with key: String) { + func flush(to url: URL) { execute(synchronously: true) { - to.set(self.buffer, forKey: key) + let content = self.buffer.joined(separator: "\n") + try? content.write(to: url, atomically: true, encoding: .utf8) } - to.synchronize() } var description: String { diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift index 8b9b297..1489e38 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift @@ -159,7 +159,7 @@ extension TunnelKitProvider { /// Enables debugging. If `true`, then `debugLogKey` is a mandatory field. public var shouldDebug: Bool - /// The key in `defaults` where the latest debug log snapshot is stored. Ignored if `shouldDebug` is `false`. + /// The filename in group container where the latest debug log snapshot is stored. Ignored if `shouldDebug` is `false`. public var debugLogKey: String? /// Optional debug log format (SwiftyBeaver format). @@ -403,12 +403,22 @@ extension TunnelKitProvider { public let lastErrorKey: String? // MARK: Shortcuts - - func existingLog(in defaults: UserDefaults) -> [String]? { + + func urlForLog(in appGroup: String) -> URL? { guard shouldDebug, let key = debugLogKey else { return nil } - return defaults.array(forKey: key) as? [String] + guard let parentURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else { + return nil + } + return parentURL.appendingPathComponent("\(key).log") + } + + func existingLog(in appGroup: String) -> String? { + guard let url = urlForLog(in: appGroup) else { + return nil + } + return try? String(contentsOf: url) } // MARK: API diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift index a8c3594..35e867c 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift @@ -94,12 +94,12 @@ open class TunnelKitProvider: NEPacketTunnelProvider { private var appGroup: String! + private lazy var defaults = UserDefaults(suiteName: appGroup) + private var cfg: Configuration! private var strategy: ConnectionStrategy! - private lazy var defaults = UserDefaults(suiteName: appGroup) - // MARK: Internal state private var proxy: SessionProxy? @@ -157,7 +157,8 @@ open class TunnelKitProvider: NEPacketTunnelProvider { strategy = ConnectionStrategy(hostname: hostname, configuration: cfg) - if let defaults = defaults, var existingLog = cfg.existingLog(in: defaults) { + if let content = cfg.existingLog(in: appGroup) { + var existingLog = content.components(separatedBy: "\n") if let i = existingLog.index(of: logSeparator) { existingLog.removeFirst(i + 2) } @@ -595,8 +596,8 @@ extension TunnelKitProvider { private func flushLog() { log.debug("Flushing log...") - if let defaults = defaults, let key = cfg.debugLogKey { - memoryLog.flush(to: defaults, with: key) + if let url = cfg.urlForLog(in: appGroup) { + memoryLog.flush(to: url) } } From f1efac073cdd3fd921079222742cd78131f9b88e Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 24 Oct 2018 19:28:15 +0200 Subject: [PATCH 2/4] Export and document log shortcuts in Configuration --- .../TunnelKitProvider+Configuration.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift index 1489e38..6656453 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift @@ -404,7 +404,13 @@ extension TunnelKitProvider { // MARK: Shortcuts - func urlForLog(in appGroup: String) -> URL? { + /** + Returns the URL of the latest debug log. + + - Parameter in: The app group where to locate the log file. + - Returns: The URL of the debug log, if any. + */ + public func urlForLog(in appGroup: String) -> URL? { guard shouldDebug, let key = debugLogKey else { return nil } @@ -414,7 +420,13 @@ extension TunnelKitProvider { return parentURL.appendingPathComponent("\(key).log") } - func existingLog(in appGroup: String) -> String? { + /** + Returns the content of the latest debug log. + + - Parameter in: The app group where to locate the log file. + - Returns: The content of the debug log, if any. + */ + public func existingLog(in appGroup: String) -> String? { guard let url = urlForLog(in: appGroup) else { return nil } From 617a72fbeaad5a2c92536687b61c32e7cdbf6619 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 24 Oct 2018 19:53:38 +0200 Subject: [PATCH 3/4] Bump version and fix latest CHANGELOG --- CHANGELOG.md | 16 +++++++++++----- TunnelKit.podspec | 2 +- TunnelKit.xcodeproj/project.pbxproj | 12 ++++++------ TunnelKitHost/Info.plist | 2 +- TunnelKitTests/Info.plist | 2 +- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0bc288..8c762f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,20 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- Debug log is saved to group container rather than `UserDefaults`. [#43](https://github.com/keeshux/tunnelkit/pull/43) + +### Fixed + +- Handle server-initiated renegotiation. [#41](https://github.com/keeshux/tunnelkit/pull/41) +- Potentially private data (e.g. Internet addresses) is now masked in debug log. [#42](https://github.com/keeshux/tunnelkit/pull/42) + +## 1.2.1 (2018-10-24) + ### Added - Configuration key `lastErrorKey` for reporting errors to host app. [#40](https://github.com/keeshux/tunnelkit/pull/40) - Server extended key usage validation (EKU). [#27](https://github.com/keeshux/tunnelkit/issues/27) -### Changed - -- Potentially private data (e.g. Internet addresses) is now masked in debug log. [#42](https://github.com/keeshux/tunnelkit/pull/42) - ### Fixed - CA file was not closed after MD5 calculation when using PIA patches. - Mitigated an issue with MTU in TCP mode during negotiation. [#39](https://github.com/keeshux/tunnelkit/issues/39) -- Handle server-initiated renegotiation. [#41](https://github.com/keeshux/tunnelkit/pull/41) ## 1.2.0 (2018-10-20) diff --git a/TunnelKit.podspec b/TunnelKit.podspec index 5b55096..2982fb2 100644 --- a/TunnelKit.podspec +++ b/TunnelKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "TunnelKit" - s.version = "1.2.1" + s.version = "1.2.2" s.summary = "Non-official OpenVPN client for Apple platforms." s.homepage = "https://github.com/keeshux/tunnelkit" diff --git a/TunnelKit.xcodeproj/project.pbxproj b/TunnelKit.xcodeproj/project.pbxproj index 995d89f..615d2d2 100644 --- a/TunnelKit.xcodeproj/project.pbxproj +++ b/TunnelKit.xcodeproj/project.pbxproj @@ -1144,7 +1144,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 245; + CURRENT_PROJECT_VERSION = 273; DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; @@ -1208,7 +1208,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 245; + CURRENT_PROJECT_VERSION = 273; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; @@ -1241,7 +1241,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 245; + DYLIB_CURRENT_VERSION = 273; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = "$(SRCROOT)/TunnelKit-iOS/Info.plist"; @@ -1264,7 +1264,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 245; + DYLIB_CURRENT_VERSION = 273; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = "$(SRCROOT)/TunnelKit-iOS/Info.plist"; @@ -1287,7 +1287,7 @@ COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 245; + DYLIB_CURRENT_VERSION = 273; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_VERSION = A; INFOPLIST_FILE = "$(SRCROOT)/TunnelKit-macOS/Info.plist"; @@ -1310,7 +1310,7 @@ COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 245; + DYLIB_CURRENT_VERSION = 273; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_VERSION = A; INFOPLIST_FILE = "$(SRCROOT)/TunnelKit-macOS/Info.plist"; diff --git a/TunnelKitHost/Info.plist b/TunnelKitHost/Info.plist index b2518c5..587f3d4 100644 --- a/TunnelKitHost/Info.plist +++ b/TunnelKitHost/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.2.1 + 1.2.2 CFBundleVersion 1 LSRequiresIPhoneOS diff --git a/TunnelKitTests/Info.plist b/TunnelKitTests/Info.plist index 0bbce46..88f2dd1 100644 --- a/TunnelKitTests/Info.plist +++ b/TunnelKitTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 1.2.1 + 1.2.2 CFBundleVersion 1 From 6995b8812d63b0c5be03f9ddfc9ea3c03cafe36b Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 24 Oct 2018 19:03:19 +0200 Subject: [PATCH 4/4] Update pods in Demo --- Demo/Podfile.lock | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Demo/Podfile.lock b/Demo/Podfile.lock index 29ebc88..c4e5a6b 100644 --- a/Demo/Podfile.lock +++ b/Demo/Podfile.lock @@ -1,13 +1,13 @@ PODS: - OpenSSL-Apple (1.1.0i-v2) - SwiftyBeaver (1.6.1) - - TunnelKit (1.1.2): - - TunnelKit/AppExtension (= 1.1.2) - - TunnelKit/Core (= 1.1.2) - - TunnelKit/AppExtension (1.1.2): + - TunnelKit (1.2.2): + - TunnelKit/AppExtension (= 1.2.2) + - TunnelKit/Core (= 1.2.2) + - TunnelKit/AppExtension (1.2.2): - SwiftyBeaver - TunnelKit/Core - - TunnelKit/Core (1.1.2): + - TunnelKit/Core (1.2.2): - OpenSSL-Apple (~> 1.1.0h) - SwiftyBeaver @@ -26,8 +26,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: OpenSSL-Apple: a93b8f2eec8783ff40d9a9304de180ab68bb647c SwiftyBeaver: ccfcdf85a04d429f1633f668650b0ce8020bda3a - TunnelKit: 392e78dd45cded30a6f814200b8334c33901f677 + TunnelKit: 15c88f0cef7b926883566a9455e912a1e55f4048 PODFILE CHECKSUM: f66dfaaa92a8d04ab2743f3caeab0ac9f9f25859 -COCOAPODS: 1.6.0.beta.1 +COCOAPODS: 1.6.0.beta.2