From 5a044e412945c6efc8ddb53050c3588134f42c72 Mon Sep 17 00:00:00 2001 From: Andrej Mihajlov Date: Wed, 2 Dec 2020 15:08:45 +0100 Subject: [PATCH] Linter: Fix all linter issues across the codebase Signed-off-by: Andrej Mihajlov --- Sources/Shared/Keychain.swift | 8 +++---- .../WireGuardApp/Tunnel/TunnelsManager.swift | 8 +++---- .../TunnelDetailTableViewController.swift | 4 ++-- .../TunnelsListTableViewController.swift | 6 ++--- .../WireGuardApp/ZipArchive/ZipArchive.swift | 6 ++--- .../Array+ConcurrentMap.swift | 2 +- Sources/WireGuardKitSwift/DNSResolver.swift | 14 +++++++----- .../IPAddress+AddrInfo.swift | 4 ++-- Sources/WireGuardKitSwift/PrivateKey.swift | 10 ++++----- .../WireGuardKitSwift/WireGuardAdapter.swift | 22 +++++++++++-------- WireGuard.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- 12 files changed, 48 insertions(+), 42 deletions(-) diff --git a/Sources/Shared/Keychain.swift b/Sources/Shared/Keychain.swift index 3059c57..8f40e27 100644 --- a/Sources/Shared/Keychain.swift +++ b/Sources/Shared/Keychain.swift @@ -21,18 +21,18 @@ class Keychain { static func makeReference(containing value: String, called name: String, previouslyReferencedBy oldRef: Data? = nil) -> Data? { var ret: OSStatus - guard var id = Bundle.main.bundleIdentifier else { + guard var bundleIdentifier = Bundle.main.bundleIdentifier else { wg_log(.error, staticMessage: "Unable to determine bundle identifier") return nil } - if id.hasSuffix(".network-extension") { - id.removeLast(".network-extension".count) + if bundleIdentifier.hasSuffix(".network-extension") { + bundleIdentifier.removeLast(".network-extension".count) } var items: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrLabel as String: "WireGuard Tunnel: " + name, kSecAttrAccount as String: name + ": " + UUID().uuidString, kSecAttrDescription as String: "wg-quick(8) config", - kSecAttrService as String: id, + kSecAttrService as String: bundleIdentifier, kSecValueData as String: value.data(using: .utf8) as Any, kSecReturnPersistentRef as String: true] diff --git a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift index af6a9bb..73c7f34 100644 --- a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift +++ b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift @@ -464,8 +464,8 @@ class TunnelsManager { } } - static func tunnelNameIsLessThan(_ a: String, _ b: String) -> Bool { - return a.compare(b, options: [.caseInsensitive, .diacriticInsensitive, .widthInsensitive, .numeric]) == .orderedAscending + static func tunnelNameIsLessThan(_ lhs: String, _ rhs: String) -> Bool { + return lhs.compare(rhs, options: [.caseInsensitive, .diacriticInsensitive, .widthInsensitive, .numeric]) == .orderedAscending } } @@ -701,8 +701,8 @@ class CatalinaWorkaround { } func reinstateTunnelsDeletedOutsideApp() { - let rd = reinstationDataForTunnelsDeletedOutsideApp() - reinstateTunnels(ArraySlice(rd), completionHandler: nil) + let data = reinstationDataForTunnelsDeletedOutsideApp() + reinstateTunnels(ArraySlice(data), completionHandler: nil) } private func reinstateTunnels(_ rdArray: ArraySlice, completionHandler: (() -> Void)?) { diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift index d49acbc..d3008c2 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -153,8 +153,8 @@ class TunnelDetailTableViewController: UITableViewController { } }! let firstPeerSectionIndex = interfaceSectionIndex + 1 - var interfaceFieldIsVisible = self.interfaceFieldIsVisible - var peerFieldIsVisible = self.peerFieldIsVisible + let interfaceFieldIsVisible = self.interfaceFieldIsVisible + let peerFieldIsVisible = self.peerFieldIsVisible func handleSectionFieldsModified(fields: [T], fieldIsVisible: [Bool], section: Int, changes: [T: TunnelViewModel.Changes.FieldChange]) { for (index, field) in fields.enumerated() { diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift index f442420..885e8c9 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift @@ -417,12 +417,12 @@ extension TunnelsListTableViewController: TunnelsManagerListDelegate { } extension UISplitViewController { - func showDetailViewController(_ vc: UIViewController, sender: Any?, animated: Bool) { + func showDetailViewController(_ viewController: UIViewController, sender: Any?, animated: Bool) { if animated { - showDetailViewController(vc, sender: sender) + showDetailViewController(viewController, sender: sender) } else { UIView.performWithoutAnimation { - showDetailViewController(vc, sender: sender) + showDetailViewController(viewController, sender: sender) } } } diff --git a/Sources/WireGuardApp/ZipArchive/ZipArchive.swift b/Sources/WireGuardApp/ZipArchive/ZipArchive.swift index 9c2f634..78419b3 100644 --- a/Sources/WireGuardApp/ZipArchive/ZipArchive.swift +++ b/Sources/WireGuardApp/ZipArchive/ZipArchive.swift @@ -47,7 +47,7 @@ extension ZipArchive { static func unarchive(url: URL, requiredFileExtensions: [String]) throws -> [(fileBaseName: String, contents: Data)] { var results = [(fileBaseName: String, contents: Data)]() - var requiredFileExtensionsLowercased = requiredFileExtensions.map { $0.lowercased() } + let requiredFileExtensionsLowercased = requiredFileExtensions.map { $0.lowercased() } guard let zipFile = unzOpen64(url.path) else { throw ZipArchiveError.cantOpenInputZipFile @@ -62,8 +62,8 @@ extension ZipArchive { guard unzOpenCurrentFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive } let bufferSize = 16384 // 16 KiB - var fileNameBuffer = UnsafeMutablePointer.allocate(capacity: bufferSize) - var dataBuffer = UnsafeMutablePointer.allocate(capacity: bufferSize) + let fileNameBuffer = UnsafeMutablePointer.allocate(capacity: bufferSize) + let dataBuffer = UnsafeMutablePointer.allocate(capacity: bufferSize) defer { fileNameBuffer.deallocate() diff --git a/Sources/WireGuardKitSwift/Array+ConcurrentMap.swift b/Sources/WireGuardKitSwift/Array+ConcurrentMap.swift index 8a7992a..0059cc3 100644 --- a/Sources/WireGuardKitSwift/Array+ConcurrentMap.swift +++ b/Sources/WireGuardKitSwift/Array+ConcurrentMap.swift @@ -21,7 +21,7 @@ extension Array { let execute = queue?.sync ?? { $0() } execute { - DispatchQueue.concurrentPerform(iterations: self.count) { (index) in + DispatchQueue.concurrentPerform(iterations: self.count) { index in let value = transform(self[index]) resultQueue.sync { result[index] = value diff --git a/Sources/WireGuardKitSwift/DNSResolver.swift b/Sources/WireGuardKitSwift/DNSResolver.swift index df19cb2..379c698 100644 --- a/Sources/WireGuardKitSwift/DNSResolver.swift +++ b/Sources/WireGuardKitSwift/DNSResolver.swift @@ -12,25 +12,27 @@ extension DNSResolver { private static let resolverQueue = DispatchQueue(label: "DNSResolverQueue", qos: .default, attributes: .concurrent) static func resolveSync(endpoints: [Endpoint?]) -> [Result?] { - let isAllEndpointsAlreadyResolved = endpoints.allSatisfy({ (maybeEndpoint) -> Bool in + let isAllEndpointsAlreadyResolved = endpoints.allSatisfy { maybeEndpoint -> Bool in return maybeEndpoint?.hasHostAsIPAddress() ?? true - }) + } if isAllEndpointsAlreadyResolved { - return endpoints.map { (endpoint) in + return endpoints.map { endpoint in return endpoint.map { .success($0) } } } - return endpoints.concurrentMap(queue: resolverQueue) { - (endpoint) -> Result? in + return endpoints.concurrentMap(queue: resolverQueue) { endpoint -> Result? in guard let endpoint = endpoint else { return nil } if endpoint.hasHostAsIPAddress() { return .success(endpoint) } else { return Result { try DNSResolver.resolveSync(endpoint: endpoint) } - .mapError { $0 as! DNSResolutionError } + .mapError { error -> DNSResolutionError in + // swiftlint:disable:next force_cast + return error as! DNSResolutionError + } } } } diff --git a/Sources/WireGuardKitSwift/IPAddress+AddrInfo.swift b/Sources/WireGuardKitSwift/IPAddress+AddrInfo.swift index d860077..b7e8cd5 100644 --- a/Sources/WireGuardKitSwift/IPAddress+AddrInfo.swift +++ b/Sources/WireGuardKitSwift/IPAddress+AddrInfo.swift @@ -8,7 +8,7 @@ extension IPv4Address { init?(addrInfo: addrinfo) { guard addrInfo.ai_family == AF_INET else { return nil } - let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in.self, capacity: MemoryLayout.size) { (ptr) -> Data in + let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in.self, capacity: MemoryLayout.size) { ptr -> Data in return Data(bytes: &ptr.pointee.sin_addr, count: MemoryLayout.size) } @@ -24,7 +24,7 @@ extension IPv6Address { init?(addrInfo: addrinfo) { guard addrInfo.ai_family == AF_INET6 else { return nil } - let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in6.self, capacity: MemoryLayout.size) { (ptr) -> Data in + let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in6.self, capacity: MemoryLayout.size) { ptr -> Data in return Data(bytes: &ptr.pointee.sin6_addr, count: MemoryLayout.size) } diff --git a/Sources/WireGuardKitSwift/PrivateKey.swift b/Sources/WireGuardKitSwift/PrivateKey.swift index 14c298f..545cdbc 100644 --- a/Sources/WireGuardKitSwift/PrivateKey.swift +++ b/Sources/WireGuardKitSwift/PrivateKey.swift @@ -5,7 +5,7 @@ import Foundation import WireGuardKitC /// The class describing a private key used by WireGuard. -public class PrivateKey: _BaseKey { +public class PrivateKey: BaseKey { /// Derived public key public var publicKey: PublicKey { return rawValue.withUnsafeBytes { (privateKeyBufferPointer: UnsafeRawBufferPointer) -> PublicKey in @@ -33,13 +33,13 @@ public class PrivateKey: _BaseKey { } /// The class describing a public key used by WireGuard. -public class PublicKey: _BaseKey {} +public class PublicKey: BaseKey {} /// The class describing a pre-shared key used by WireGuard. -public class PreSharedKey: _BaseKey {} +public class PreSharedKey: BaseKey {} /// The base key implementation. Should not be used directly. -public class _BaseKey: RawRepresentable, Equatable, Hashable { +public class BaseKey: RawRepresentable, Equatable, Hashable { /// Raw key representation public let rawValue: Data @@ -98,7 +98,7 @@ public class _BaseKey: RawRepresentable, Equatable, Hashable { } } - public static func == (lhs: _BaseKey, rhs: _BaseKey) -> Bool { + public static func == (lhs: BaseKey, rhs: BaseKey) -> Bool { return lhs.rawValue.withUnsafeBytes { (lhsBytes: UnsafeRawBufferPointer) -> Bool in return rhs.rawValue.withUnsafeBytes { (rhsBytes: UnsafeRawBufferPointer) -> Bool in return key_eq( diff --git a/Sources/WireGuardKitSwift/WireGuardAdapter.swift b/Sources/WireGuardKitSwift/WireGuardAdapter.swift index 2e8df6f..ef644bd 100644 --- a/Sources/WireGuardKitSwift/WireGuardAdapter.swift +++ b/Sources/WireGuardKitSwift/WireGuardAdapter.swift @@ -64,7 +64,7 @@ public class WireGuardAdapter { var buffer = [UInt8](repeating: 0, count: Int(IFNAMSIZ)) - return buffer.withUnsafeMutableBufferPointer { (mutableBufferPointer) in + return buffer.withUnsafeMutableBufferPointer { mutableBufferPointer in guard let baseAddress = mutableBufferPointer.baseAddress else { return nil } var ifnameSize = socklen_t(IFNAMSIZ) @@ -158,7 +158,7 @@ public class WireGuardAdapter { networkMonitor.start(queue: self.workQueue) self.networkMonitor = networkMonitor - self.updateNetworkSettings(tunnelConfiguration: tunnelConfiguration) { (settingsGenerator, error) in + self.updateNetworkSettings(tunnelConfiguration: tunnelConfiguration) { settingsGenerator, error in if let error = error { completionHandler(error) } else { @@ -212,7 +212,7 @@ public class WireGuardAdapter { // This will broadcast the `NEVPNStatusDidChange` notification to the GUI process. self.packetTunnelProvider?.reasserting = true - self.updateNetworkSettings(tunnelConfiguration: tunnelConfiguration) { (settingsGenerator, error) in + self.updateNetworkSettings(tunnelConfiguration: tunnelConfiguration) { settingsGenerator, error in if let error = error { completionHandler(error) } else { @@ -230,7 +230,7 @@ public class WireGuardAdapter { /// Setup WireGuard log handler. private func setupLogHandler() { let context = Unmanaged.passUnretained(self).toOpaque() - wgSetLogger(context) { (context, logLevel, message) in + wgSetLogger(context) { context, logLevel, message in guard let context = context, let message = message else { return } let unretainedSelf = Unmanaged.fromOpaque(context) @@ -251,7 +251,10 @@ public class WireGuardAdapter { let resolvedEndpoints: [Endpoint?] let resolvePeersResult = Result { try self.resolvePeers(for: tunnelConfiguration) } - .mapError { $0 as! WireGuardAdapterError } + .mapError { error -> WireGuardAdapterError in + // swiftlint:disable:next force_cast + return error as! WireGuardAdapterError + } switch resolvePeersResult { case .success(let endpoints): @@ -271,10 +274,10 @@ public class WireGuardAdapter { condition.lock() defer { condition.unlock() } - self.packetTunnelProvider?.setTunnelNetworkSettings(networkSettings, completionHandler: { (error) in + self.packetTunnelProvider?.setTunnelNetworkSettings(networkSettings) { error in systemError = error condition.signal() - }) + } // Packet tunnel's `setTunnelNetworkSettings` times out in certain // scenarios & never calls the given callback. @@ -301,7 +304,7 @@ public class WireGuardAdapter { private func resolvePeers(for tunnelConfiguration: TunnelConfiguration) throws -> [Endpoint?] { let endpoints = tunnelConfiguration.peers.map { $0.endpoint } let resolutionResults = DNSResolver.resolveSync(endpoints: endpoints) - let resolutionErrors = resolutionResults.compactMap { (result) -> DNSResolutionError? in + let resolutionErrors = resolutionResults.compactMap { result -> DNSResolutionError? in if case .failure(let error) = result { return error } else { @@ -313,7 +316,8 @@ public class WireGuardAdapter { throw WireGuardAdapterError.dnsResolution(resolutionErrors) } - let resolvedEndpoints = resolutionResults.map { (result) -> Endpoint? in + let resolvedEndpoints = resolutionResults.map { result -> Endpoint? in + // swiftlint:disable:next force_try return try! result?.get() } diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index c6e9f70..c9b51a5 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -1746,7 +1746,7 @@ isa = XCRemoteSwiftPackageReference; repositoryURL = "https://git.zx2c4.com/wireguard-apple"; requirement = { - branch = master; + branch = "am/wg-adapter-rebased"; kind = branch; }; }; diff --git a/WireGuard.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/WireGuard.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 01be9af..4aeec04 100644 --- a/WireGuard.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/WireGuard.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "package": "WireGuardKit", "repositoryURL": "https://git.zx2c4.com/wireguard-apple", "state": { - "branch": "master", - "revision": "737f847c0db36429f7136fb514409c22e13a70ee", + "branch": "am/wg-adapter-rebased", + "revision": "e52f83b915a985a945dff622e5f48fa52e96f045", "version": null } }