diff --git a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift index 48c4589..2b4ca0b 100644 --- a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift +++ b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift @@ -23,7 +23,7 @@ class WgQuickConfigFileParser { assert(!name.isEmpty) - func collate(interfaceAttributes attributes: [String:String]) -> InterfaceConfiguration? { + func collate(interfaceAttributes attributes: [String: String]) -> InterfaceConfiguration? { // required wg fields guard let privateKeyString = attributes["PrivateKey"] else { return nil } guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == 32 else { return nil } @@ -59,7 +59,7 @@ class WgQuickConfigFileParser { return interface } - func collate(peerAttributes attributes: [String:String]) -> PeerConfiguration? { + func collate(peerAttributes attributes: [String: String]) -> PeerConfiguration? { // required wg fields guard let publicKeyString = attributes["PublicKey"] else { return nil } guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == 32 else { return nil } @@ -89,13 +89,13 @@ class WgQuickConfigFileParser { return peer } - var interfaceConfiguration: InterfaceConfiguration? = nil + var interfaceConfiguration: InterfaceConfiguration? var peerConfigurations: [PeerConfiguration] = [] let lines = text.split(separator: "\n") var parserState: ParserState = .notInASection - var attributes: [String:String] = [:] + var attributes: [String: String] = [:] for (lineIndex, line) in lines.enumerated() { var trimmedLine: String diff --git a/WireGuard/WireGuard/Model/Configuration.swift b/WireGuard/WireGuard/Model/Configuration.swift index 6586c3b..2df363b 100644 --- a/WireGuard/WireGuard/Model/Configuration.swift +++ b/WireGuard/WireGuard/Model/Configuration.swift @@ -17,8 +17,8 @@ struct InterfaceConfiguration: Codable { var name: String var privateKey: Data var addresses: [IPAddressRange] = [] - var listenPort: UInt16? = nil - var mtu: UInt16? = nil + var listenPort: UInt16? + var mtu: UInt16? var dns: [DNSServer] = [] var publicKey: Data { diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index 8a9cba4..6fa13b4 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -39,7 +39,7 @@ class TunnelViewModel { class InterfaceData { var scratchpad: [InterfaceField: String] = [:] var fieldsWithError: Set = [] - var validatedConfiguration: InterfaceConfiguration? = nil + var validatedConfiguration: InterfaceConfiguration? subscript(field: InterfaceField) -> String { get { @@ -176,7 +176,7 @@ class TunnelViewModel { var index: Int var scratchpad: [PeerField: String] = [:] var fieldsWithError: Set = [] - var validatedConfiguration: PeerConfiguration? = nil + var validatedConfiguration: PeerConfiguration? // For exclude private IPs var shouldAllowExcludePrivateIPsControl: Bool = false /* Read-only from the VC's point of view */ diff --git a/WireGuard/WireGuard/UI/iOS/AppDelegate.swift b/WireGuard/WireGuard/UI/iOS/AppDelegate.swift index 57b6191..d7d1652 100644 --- a/WireGuard/WireGuard/UI/iOS/AppDelegate.swift +++ b/WireGuard/WireGuard/UI/iOS/AppDelegate.swift @@ -26,7 +26,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { + func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool { defer { do { try FileManager.default.removeItem(at: url) diff --git a/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift b/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift index 929b6b0..ea9ece4 100644 --- a/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift @@ -6,7 +6,7 @@ import CoreData import UIKit protocol QRScanViewControllerDelegate: class { - func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController, completionHandler: (() ->Void)?) + func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController, completionHandler: (() -> Void)?) } class QRScanViewController: UIViewController { diff --git a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift index acca64d..ca8f45b 100644 --- a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift @@ -12,13 +12,13 @@ class SettingsTableViewController: UITableViewController { case exportZipArchive = "Export zip archive" } - let settingsFieldsBySection : [[SettingsFields]] = [ + let settingsFieldsBySection: [[SettingsFields]] = [ [.exportZipArchive], [.iosAppVersion, .goBackendVersion] ] let tunnelsManager: TunnelsManager? - var wireguardCaptionedImage: (view: UIView, size: CGSize)? = nil + var wireguardCaptionedImage: (view: UIView, size: CGSize)? init(tunnelsManager: TunnelsManager?) { self.tunnelsManager = tunnelsManager @@ -97,7 +97,7 @@ class SettingsTableViewController: UITableViewController { // popoverPresentationController shall be non-nil on the iPad activityVC.popoverPresentationController?.sourceView = sourceView present(activityVC, animated: true) - + } catch (let error) { showErrorAlert(title: "Unable to export", message: "There was an error exporting the tunnel configuration archive: \(String(describing: error))") } @@ -201,7 +201,7 @@ class TunnelSettingsTableViewButtonCell: UITableViewCell { get { return button.title(for: .normal) ?? "" } set(value) { button.setTitle(value, for: .normal) } } - var onTapped: (() -> Void)? = nil + var onTapped: (() -> Void)? let button: UIButton diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift index 2174602..3165553 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift @@ -62,7 +62,7 @@ class TunnelDetailTableViewController: UITableViewController { func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) { - let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { (action) in + let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { (_) in onConfirmed() } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) @@ -221,11 +221,11 @@ class TunnelDetailTableViewStatusCell: UITableViewCell { get { return statusSwitch.isUserInteractionEnabled } set(value) { statusSwitch.isUserInteractionEnabled = value } } - var onSwitchToggled: ((Bool) -> Void)? = nil + var onSwitchToggled: ((Bool) -> Void)? private var isOnSwitchToggledHandlerEnabled: Bool = true let statusSwitch: UISwitch - private var statusObservervationToken: AnyObject? = nil + private var statusObservervationToken: AnyObject? override init(style: UITableViewCellStyle, reuseIdentifier: String?) { statusSwitch = UISwitch() @@ -382,7 +382,7 @@ class TunnelDetailTableViewButtonCell: UITableViewCell { get { return button.tintColor == UIColor.red } set(value) { button.tintColor = value ? UIColor.red : buttonStandardTintColor } } - var onTapped: (() -> Void)? = nil + var onTapped: (() -> Void)? let button: UIButton var buttonStandardTintColor: UIColor diff --git a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift index 2c217a1..37613c2 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift @@ -12,7 +12,7 @@ protocol TunnelEditTableViewControllerDelegate: class { class TunnelEditTableViewController: UITableViewController { - weak var delegate: TunnelEditTableViewControllerDelegate? = nil + weak var delegate: TunnelEditTableViewControllerDelegate? let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [ [.name], @@ -388,7 +388,7 @@ extension TunnelEditTableViewController { func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) { - let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { (action) in + let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { (_) in onConfirmed() } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) @@ -440,8 +440,8 @@ class TunnelEditTableViewKeyValueCell: CopyableLabelTableViewCell { set(value) { valueTextField.keyboardType = value } } - var onValueChanged: ((String) -> Void)? = nil - var onValueBeingEdited: ((String) -> Void)? = nil + var onValueChanged: ((String) -> Void)? + var onValueBeingEdited: ((String) -> Void)? let keyLabel: UILabel let valueTextField: UITextField @@ -534,7 +534,7 @@ class TunnelEditTableViewButtonCell: UITableViewCell { get { return button.tintColor == UIColor.red } set(value) { button.tintColor = value ? UIColor.red : buttonStandardTintColor } } - var onTapped: (() -> Void)? = nil + var onTapped: (() -> Void)? let button: UIButton var buttonStandardTintColor: UIColor @@ -586,7 +586,7 @@ class TunnelEditTableViewSwitchCell: UITableViewCell { } } - var onSwitchToggled: ((Bool) -> Void)? = nil + var onSwitchToggled: ((Bool) -> Void)? let switchView: UISwitch diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index 15da060..48515e9 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -6,12 +6,12 @@ import MobileCoreServices class TunnelsListTableViewController: UIViewController { - var tunnelsManager: TunnelsManager? = nil - var onTunnelsManagerReady: ((TunnelsManager) -> Void)? = nil + var tunnelsManager: TunnelsManager? + var onTunnelsManagerReady: ((TunnelsManager) -> Void)? - var busyIndicator: UIActivityIndicatorView? = nil - var centeredAddButton: BorderedTextButton? = nil - var tableView: UITableView? = nil + var busyIndicator: UIActivityIndicatorView? + var centeredAddButton: BorderedTextButton? + var tableView: UITableView? override func viewDidLoad() { super.viewDidLoad() @@ -88,17 +88,17 @@ class TunnelsListTableViewController: UIViewController { @objc func addButtonTapped(sender: AnyObject) { if (self.tunnelsManager == nil) { return } // Do nothing until we've loaded the tunnels let alert = UIAlertController(title: "", message: "Add a new WireGuard tunnel", preferredStyle: .actionSheet) - let importFileAction = UIAlertAction(title: "Create from file or archive", style: .default) { [weak self] (action) in + let importFileAction = UIAlertAction(title: "Create from file or archive", style: .default) { [weak self] (_) in self?.presentViewControllerForFileImport() } alert.addAction(importFileAction) - let scanQRCodeAction = UIAlertAction(title: "Create from QR code", style: .default) { [weak self] (action) in + let scanQRCodeAction = UIAlertAction(title: "Create from QR code", style: .default) { [weak self] (_) in self?.presentViewControllerForScanningQRCode() } alert.addAction(scanQRCodeAction) - let createFromScratchAction = UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in + let createFromScratchAction = UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (_) in if let s = self, let tunnelsManager = s.tunnelsManager { s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil) } @@ -181,7 +181,7 @@ class TunnelsListTableViewController: UIViewController { let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) if let fileContents = try? String(contentsOf: url), let tunnelConfiguration = try? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) { - tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (tunnel, error) in + tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in if let error = error { ErrorPresenter.showErrorAlert(error: error, from: self) } @@ -203,7 +203,7 @@ class TunnelsListTableViewController: UIViewController { showErrorAlert(title: "Unable to read zip archive", message: "Unexpected error: \(String(describing: error))") return } - + for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() { let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty { @@ -218,7 +218,7 @@ class TunnelsListTableViewController: UIViewController { } guard let tunnelsManager = tunnelsManager else { return } unarchivedFiles.sort { $0.fileName < $1.fileName } - var lastFileName : String? + var lastFileName: String? var configs: [TunnelConfiguration] = [] for file in unarchivedFiles { if file.fileName == lastFileName { @@ -258,8 +258,8 @@ extension TunnelsListTableViewController: UIDocumentPickerDelegate { extension TunnelsListTableViewController: QRScanViewControllerDelegate { func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController, - completionHandler: (() ->Void)?) { - tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (tunnel, error) in + completionHandler: (() -> Void)?) { + tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in if let error = error { ErrorPresenter.showErrorAlert(error: error, from: qrScanViewController, onDismissal: completionHandler) } else { @@ -354,7 +354,7 @@ extension TunnelsListTableViewController: TunnelsManagerDelegate { func tunnelMoved(at oldIndex: Int, to newIndex: Int) { tableView?.moveRow(at: IndexPath(row: oldIndex, section: 0), to: IndexPath(row: newIndex, section: 0)) } - + func tunnelRemoved(at index: Int) { tableView?.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic) centeredAddButton?.isHidden = (tunnelsManager?.numberOfTunnels() ?? 0 > 0) @@ -377,14 +377,14 @@ class TunnelsListTableViewCell: UITableViewCell { } } } - var onSwitchToggled: ((Bool) -> Void)? = nil + var onSwitchToggled: ((Bool) -> Void)? let nameLabel: UILabel let busyIndicator: UIActivityIndicatorView let statusSwitch: UISwitch - private var statusObservervationToken: AnyObject? = nil - private var nameObservervationToken: AnyObject? = nil + private var statusObservervationToken: AnyObject? + private var nameObservervationToken: AnyObject? override init(style: UITableViewCellStyle, reuseIdentifier: String?) { nameLabel = UILabel() @@ -466,7 +466,7 @@ class BorderedTextButton: UIView { set(value) { button.setTitle(value, for: .normal) } } - var onTapped: (() -> Void)? = nil + var onTapped: (() -> Void)? init() { button = UIButton(type: .system) @@ -491,4 +491,3 @@ class BorderedTextButton: UIView { fatalError("init(coder:) has not been implemented") } } - diff --git a/WireGuard/WireGuard/VPN/DNSResolver.swift b/WireGuard/WireGuard/VPN/DNSResolver.swift index 4ceabdc..e750454 100644 --- a/WireGuard/WireGuard/VPN/DNSResolver.swift +++ b/WireGuard/WireGuard/VPN/DNSResolver.swift @@ -109,8 +109,8 @@ extension DNSResolver { // getaddrinfo succeeded let ipv4Buffer = UnsafeMutablePointer.allocate(capacity: Int(INET_ADDRSTRLEN)) let ipv6Buffer = UnsafeMutablePointer.allocate(capacity: Int(INET6_ADDRSTRLEN)) - var ipv4AddressString: String? = nil - var ipv6AddressString: String? = nil + var ipv4AddressString: String? + var ipv6AddressString: String? while (resultPointer != nil) { let result = resultPointer!.pointee resultPointer = result.ai_next diff --git a/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift b/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift index 9b9ae0a..35d4bb7 100644 --- a/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift +++ b/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift @@ -6,8 +6,8 @@ import Network class PacketTunnelOptionsGenerator { static func generateOptions(from tc: TunnelConfiguration, - withResolvedEndpoints resolvedEndpoints: [Endpoint?]) -> [String:NSObject] { - var options: [String:NSObject] = [:] + withResolvedEndpoints resolvedEndpoints: [Endpoint?]) -> [String: NSObject] { + var options: [String: NSObject] = [:] // Interface name diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index 39328a6..2ea3377 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -32,7 +32,7 @@ enum TunnelManagementError: Error { class TunnelsManager { private var tunnels: [TunnelContainer] - weak var delegate: TunnelsManagerDelegate? = nil + weak var delegate: TunnelsManagerDelegate? private var isAddingTunnel: Bool = false private var isModifyingTunnel: Bool = false @@ -58,7 +58,7 @@ class TunnelsManager { completionHandler(nil, TunnelManagementError.tunnelAlreadyExistsWithThatName) return } - + if self.tunnels.contains(where: { $0.name == tunnelName }) { completionHandler(nil, TunnelManagementError.tunnelAlreadyExistsWithThatName) return @@ -97,7 +97,7 @@ class TunnelsManager { return } let tail = tunnelConfigurations.dropFirst() - self.add(tunnelConfiguration: head) { [weak self, tail] (tunnel, error) in + self.add(tunnelConfiguration: head) { [weak self, tail] (_, error) in DispatchQueue.main.async { self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessful + (error == nil ? 1 : 0), completionHandler: completionHandler) } @@ -115,7 +115,7 @@ class TunnelsManager { let tunnelProviderManager = tunnel.tunnelProvider let isNameChanged = (tunnelName != tunnelProviderManager.localizedDescription) - var oldName: String? = nil + var oldName: String? if (isNameChanged) { if self.tunnels.contains(where: { $0.name == tunnelName }) { completionHandler(TunnelManagementError.tunnelAlreadyExistsWithThatName) @@ -238,7 +238,7 @@ class TunnelContainer: NSObject { fileprivate let tunnelProvider: NETunnelProviderManager private var statusObservationToken: AnyObject? - private var dnsResolver: DNSResolver? = nil + private var dnsResolver: DNSResolver? init(tunnel: NETunnelProviderManager) { self.name = tunnel.localizedDescription ?? "Unnamed" @@ -314,9 +314,9 @@ class TunnelContainer: NSObject { assert(resolvedEndpoints.allSatisfy { (resolvedEndpoint) in guard let resolvedEndpoint = resolvedEndpoint else { return true } switch (resolvedEndpoint.host) { - case .ipv4(_): return true - case .ipv6(_): return true - case .name(_, _): return false + case .ipv4: return true + case .ipv6: return true + case .name: return false } })