Customize app extension log path

Store path into App Group. Do not read it from UserDefaults in
app extension because value is immediately available in provider
configuration.
This commit is contained in:
Davide De Rosa 2022-06-16 20:52:29 +02:00
parent 4eb9a92c2e
commit 83a2842214
5 changed files with 65 additions and 77 deletions

View File

@ -147,7 +147,10 @@ class OpenVPNViewController: UIViewController {
guard let cfg = cfg else { guard let cfg = cfg else {
return return
} }
textLog.text = cfg.debugLog guard let url = cfg.urlForDebugLog else {
return
}
textLog.text = try? String(contentsOf: url)
} }
func updateButton() { func updateButton() {

View File

@ -100,8 +100,6 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
// MARK: Constants // MARK: Constants
private var logFile: FileDestination?
private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility) private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility)
private let prngSeedLength = 64 private let prngSeedLength = 64
@ -823,13 +821,14 @@ extension OpenVPNTunnelProvider {
log.addDestination(console) log.addDestination(console)
} }
let file = FileDestination(logFileURL: cfg.urlForDebugLog) let file = FileDestination(logFileURL: cfg._appexDebugLogURL)
file.minLevel = logLevel file.minLevel = logLevel
file.format = logFormat file.format = logFormat
file.logFileMaxSize = maxLogSize file.logFileMaxSize = maxLogSize
log.addDestination(file) log.addDestination(file)
logFile = file // store path for clients
cfg._appexSetDebugLogPath()
} }
private func flushLog() { private func flushLog() {

View File

@ -37,11 +37,9 @@ extension OpenVPN {
/// Specific configuration for OpenVPN. /// Specific configuration for OpenVPN.
public struct ProviderConfiguration: Codable { public struct ProviderConfiguration: Codable {
fileprivate enum Filenames: String {
case debugLog = "OpenVPN.Tunnel.log"
}
fileprivate enum Keys: String { fileprivate enum Keys: String {
case logPath = "OpenVPN.LogPath"
case dataCount = "OpenVPN.DataCount" case dataCount = "OpenVPN.DataCount"
case serverConfiguration = "OpenVPN.ServerConfiguration" case serverConfiguration = "OpenVPN.ServerConfiguration"
@ -67,6 +65,9 @@ extension OpenVPN {
/// Enables debugging. /// Enables debugging.
public var shouldDebug = false public var shouldDebug = false
/// Debug log path.
public var debugLogPath: String? = nil
/// Optional debug log format (SwiftyBeaver format). /// Optional debug log format (SwiftyBeaver format).
public var debugLogFormat: String? = nil public var debugLogFormat: String? = nil
@ -147,16 +148,9 @@ extension OpenVPN.ProviderConfiguration {
The URL of the latest debug log. The URL of the latest debug log.
*/ */
public var urlForDebugLog: URL? { public var urlForDebugLog: URL? {
return FileManager.default.openVPNURLForDebugLog(appGroup: appGroup) return defaults?.openVPNURLForDebugLog(appGroup: appGroup)
} }
/**
The content of the latest debug log.
*/
public var debugLog: String? {
return FileManager.default.openVPNDebugLog(appGroup: appGroup)
}
private var defaults: UserDefaults? { private var defaults: UserDefaults? {
return UserDefaults(suiteName: appGroup) return UserDefaults(suiteName: appGroup)
} }
@ -175,10 +169,30 @@ extension OpenVPN.ProviderConfiguration {
public func _appexSetLastError(_ newValue: OpenVPNProviderError?) { public func _appexSetLastError(_ newValue: OpenVPNProviderError?) {
defaults?.openVPNLastError = newValue defaults?.openVPNLastError = newValue
} }
public var _appexDebugLogURL: URL? {
guard let path = debugLogPath else {
return nil
}
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
.appendingPathComponent(path)
}
public func _appexSetDebugLogPath() {
defaults?.setValue(debugLogPath, forKey: OpenVPN.ProviderConfiguration.Keys.logPath.rawValue)
}
} }
/// :nodoc: /// :nodoc:
extension UserDefaults { extension UserDefaults {
public func openVPNURLForDebugLog(appGroup: String) -> URL? {
guard let path = string(forKey: OpenVPN.ProviderConfiguration.Keys.logPath.rawValue) else {
return nil
}
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
.appendingPathComponent(path)
}
public fileprivate(set) var openVPNDataCount: DataCount? { public fileprivate(set) var openVPNDataCount: DataCount? {
get { get {
guard let rawValue = openVPNDataCountArray else { guard let rawValue = openVPNDataCountArray else {
@ -255,27 +269,3 @@ extension UserDefaults {
} }
} }
} }
/// :nodoc:
extension FileManager {
public func openVPNURLForDebugLog(appGroup: String) -> URL? {
return documentsURL(appGroup: appGroup)?
.appendingPathComponent(OpenVPN.ProviderConfiguration.Filenames.debugLog.rawValue)
}
public func openVPNDebugLog(appGroup: String) -> String? {
guard let url = openVPNURLForDebugLog(appGroup: appGroup) else {
return nil
}
do {
return try String(contentsOf: url)
} catch {
log.error("Unable to access debug log: \(error)")
return nil
}
}
private func documentsURL(appGroup: String) -> URL? {
return containerURL(forSecurityApplicationGroupIdentifier: appGroup)
}
}

View File

@ -137,11 +137,14 @@ extension WireGuardTunnelProvider {
SwiftyBeaver.addDestination(console) SwiftyBeaver.addDestination(console)
} }
let file = FileDestination(logFileURL: cfg.urlForDebugLog) let file = FileDestination(logFileURL: cfg._appexDebugLogURL)
file.minLevel = logLevel file.minLevel = logLevel
file.format = logFormat file.format = logFormat
file.logFileMaxSize = 20000 file.logFileMaxSize = 20000
SwiftyBeaver.addDestination(file) SwiftyBeaver.addDestination(file)
// store path for clients
cfg._appexSetDebugLogPath()
} }
} }

View File

@ -28,8 +28,8 @@ import NetworkExtension
import TunnelKitManager import TunnelKitManager
import TunnelKitWireGuardCore import TunnelKitWireGuardCore
import WireGuardKit import WireGuardKit
import __TunnelKitUtils
import SwiftyBeaver import SwiftyBeaver
import __TunnelKitUtils
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
@ -37,11 +37,9 @@ extension WireGuard {
/// Specific configuration for WireGuard. /// Specific configuration for WireGuard.
public struct ProviderConfiguration: Codable { public struct ProviderConfiguration: Codable {
fileprivate enum Filenames: String {
case debugLog = "WireGuard.Tunnel.log"
}
fileprivate enum Keys: String { fileprivate enum Keys: String {
case logPath = "WireGuard.LogPath"
case lastError = "WireGuard.LastError" case lastError = "WireGuard.LastError"
} }
@ -53,6 +51,8 @@ extension WireGuard {
public var shouldDebug = false public var shouldDebug = false
public var debugLogPath: String? = nil
public var debugLogFormat: String? = nil public var debugLogFormat: String? = nil
public init(_ title: String, appGroup: String, configuration: WireGuard.Configuration) { public init(_ title: String, appGroup: String, configuration: WireGuard.Configuration) {
@ -95,16 +95,13 @@ extension WireGuard.ProviderConfiguration {
return defaults?.wireGuardLastError return defaults?.wireGuardLastError
} }
private var defaults: UserDefaults? {
return UserDefaults(suiteName: appGroup)
}
public var urlForDebugLog: URL? { public var urlForDebugLog: URL? {
return FileManager.default.wireGuardURLForDebugLog(appGroup: appGroup) return defaults?.wireGuardURLForDebugLog(appGroup: appGroup)
} }
public var debugLog: String? { private var defaults: UserDefaults? {
return FileManager.default.wireGuardDebugLog(appGroup: appGroup) return UserDefaults(suiteName: appGroup)
} }
} }
@ -113,10 +110,30 @@ extension WireGuard.ProviderConfiguration {
public func _appexSetLastError(_ newValue: WireGuardProviderError?) { public func _appexSetLastError(_ newValue: WireGuardProviderError?) {
defaults?.wireGuardLastError = newValue defaults?.wireGuardLastError = newValue
} }
public var _appexDebugLogURL: URL? {
guard let path = debugLogPath else {
return nil
}
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
.appendingPathComponent(path)
}
public func _appexSetDebugLogPath() {
defaults?.setValue(debugLogPath, forKey: WireGuard.ProviderConfiguration.Keys.logPath.rawValue)
}
} }
/// :nodoc: /// :nodoc:
extension UserDefaults { extension UserDefaults {
public func wireGuardURLForDebugLog(appGroup: String) -> URL? {
guard let path = string(forKey: WireGuard.ProviderConfiguration.Keys.logPath.rawValue) else {
return nil
}
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
.appendingPathComponent(path)
}
public fileprivate(set) var wireGuardLastError: WireGuardProviderError? { public fileprivate(set) var wireGuardLastError: WireGuardProviderError? {
get { get {
guard let rawValue = string(forKey: WireGuard.ProviderConfiguration.Keys.lastError.rawValue) else { guard let rawValue = string(forKey: WireGuard.ProviderConfiguration.Keys.lastError.rawValue) else {
@ -133,27 +150,3 @@ extension UserDefaults {
} }
} }
} }
/// :nodoc:
extension FileManager {
public func wireGuardURLForDebugLog(appGroup: String) -> URL? {
return documentsURL(appGroup: appGroup)?
.appendingPathComponent(WireGuard.ProviderConfiguration.Filenames.debugLog.rawValue)
}
public func wireGuardDebugLog(appGroup: String) -> String? {
guard let url = wireGuardURLForDebugLog(appGroup: appGroup) else {
return nil
}
do {
return try String(contentsOf: url)
} catch {
log.error("Unable to access debug log: \(error)")
return nil
}
}
private func documentsURL(appGroup: String) -> URL? {
return containerURL(forSecurityApplicationGroupIdentifier: appGroup)
}
}