From b2a872022be56a2a7ad4dcdef3f883f84d581181 Mon Sep 17 00:00:00 2001 From: Jon Petersson Date: Tue, 5 Sep 2023 12:15:43 +0200 Subject: [PATCH] Fix Swiftlint warnings in PacketTunnelProvider --- ios/PacketTunnel/PacketTunnelProvider.swift | 52 ++++++++++++--------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/ios/PacketTunnel/PacketTunnelProvider.swift b/ios/PacketTunnel/PacketTunnelProvider.swift index d9438deb1283..df48ddf3a610 100644 --- a/ios/PacketTunnel/PacketTunnelProvider.swift +++ b/ios/PacketTunnel/PacketTunnelProvider.swift @@ -127,11 +127,14 @@ class PacketTunnelProvider: NEPacketTunnelProvider { override init() { var loggerBuilder = LoggerBuilder() let pid = ProcessInfo.processInfo.processIdentifier + loggerBuilder.metadata["pid"] = .string("\(pid)") loggerBuilder.addFileOutput(fileURL: ApplicationConfiguration.logFileURL(for: .packetTunnel)) + #if DEBUG loggerBuilder.addOSLogOutput(subsystem: ApplicationTarget.packetTunnel.bundleIdentifier) #endif + loggerBuilder.install() providerLogger = Logger(label: "PacketTunnelProvider") @@ -168,23 +171,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider { super.init() - adapter = WireGuardAdapter( - with: self, - shouldHandleReasserting: false, - logHandler: { [weak self] logLevel, message in - self?.dispatchQueue.async { - self?.tunnelLogger.log(level: logLevel.loggerLevel, "\(message)") - } - } - ) + adapter = createWireGuardAdapter() - tunnelMonitor = TunnelMonitor( - eventQueue: dispatchQueue, - pinger: Pinger(replyQueue: dispatchQueue), - tunnelDeviceInfo: WgAdapterDeviceInfo(adapter: adapter), - defaultPathObserver: PacketTunnelPathObserver(packetTunnelProvider: self), - timings: TunnelMonitorTimings() - ) + tunnelMonitor = createTunnelMonitor(wireGuardAdapter: adapter) tunnelMonitor.onEvent = { [weak self] event in self?.handleTunnelMonitorEvent(event) } @@ -288,10 +277,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider { do { message = try TunnelProviderMessage(messageData: messageData) } catch { - self.providerLogger.error( - error: error, - message: "Failed to decode the app message." - ) + self.providerLogger.error(error: error, message: "Failed to decode the app message.") completionHandler?(nil) return @@ -303,9 +289,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider { case let .reconnectTunnel(appSelectorResult): self.providerLogger.debug("Reconnecting the tunnel...") - let nextRelay: NextRelay = (appSelectorResult ?? self.selectorResult) - .map { .set($0) } ?? .automatic - + let nextRelay: NextRelay = (appSelectorResult ?? self.selectorResult).map { .set($0) } ?? .automatic self.reconnectTunnel(to: nextRelay, shouldStopTunnelMonitor: true) completionHandler?(nil) @@ -420,6 +404,28 @@ class PacketTunnelProvider: NEPacketTunnelProvider { // MARK: - Private + private func createWireGuardAdapter() -> WireGuardAdapter { + WireGuardAdapter( + with: self, + shouldHandleReasserting: false, + logHandler: { [weak self] logLevel, message in + self?.dispatchQueue.async { + self?.tunnelLogger.log(level: logLevel.loggerLevel, "\(message)") + } + } + ) + } + + private func createTunnelMonitor(wireGuardAdapter: WireGuardAdapter) -> TunnelMonitor { + TunnelMonitor( + eventQueue: dispatchQueue, + pinger: Pinger(replyQueue: dispatchQueue), + tunnelDeviceInfo: WgAdapterDeviceInfo(adapter: adapter), + defaultPathObserver: PacketTunnelPathObserver(packetTunnelProvider: self), + timings: TunnelMonitorTimings() + ) + } + private func startTunnelReconnectionTimer(reconnectionDelay: Duration) { dispatchPrecondition(condition: .onQueue(dispatchQueue))