From 18d82d07af9b8b40aff2cad0acb9f249f1dbf6ca Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Wed, 26 Sep 2018 11:26:52 +0200 Subject: [PATCH] Use message to extension to obtain version info. Signed-off-by: Jason A. Donenfeld --- Shared/ExtensionMessage.swift | 25 +++++++++++++++++++ WireGuard.xcodeproj/project.pbxproj | 10 +++++--- ...+SettingsTableViewControllerDelegate.swift | 15 +++++++++++ .../SetttingsTableViewController.swift | 24 +++++++++++++++--- .../PacketTunnelProvider.swift | 13 +++++----- 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 Shared/ExtensionMessage.swift diff --git a/Shared/ExtensionMessage.swift b/Shared/ExtensionMessage.swift new file mode 100644 index 0000000..beba65f --- /dev/null +++ b/Shared/ExtensionMessage.swift @@ -0,0 +1,25 @@ +// +// Copyright © 2018 WireGuard LLC. All rights reserved. +// + +import Foundation + +public class ExtensionMessage: Equatable { + + public static let requestVersion = ExtensionMessage(0xff) + + public let data: Data + + private init(_ byte: UInt8) { + data = Data(bytes: [byte]) + } + + init(_ data: Data) { + self.data = data + } + + // MARK: Equatable + public static func ==(lhs: ExtensionMessage, rhs: ExtensionMessage) -> Bool { + return (lhs.data == rhs.data) + } +} diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index 55956de..19be835 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -10,7 +10,6 @@ 48CF751B34E9703133F1B1AF /* Pods_WireGuard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 861983CAE8FDC13BC83E7E04 /* Pods_WireGuard.framework */; }; 4A430E802139DC8F0078172C /* icon_20pt@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4A430E7F2139DC8F0078172C /* icon_20pt@3x.png */; }; 4A430E842139DCFB0078172C /* icon_60pt@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4A430E832139DCFB0078172C /* icon_60pt@3x.png */; }; - 4A4349752151865E00EF92F8 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AD0900120DC4171000E9CF5 /* libwg-go.a */; }; 4A4351592124956200261999 /* Validators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A4351582124956200261999 /* Validators.swift */; }; 4A43515A2124956200261999 /* Validators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A4351582124956200261999 /* Validators.swift */; }; 4A43515C21249E5700261999 /* ValidatorsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A43515B21249E5700261999 /* ValidatorsTests.swift */; }; @@ -39,6 +38,8 @@ 4A8A229A215B782E00736141 /* AppCoordinator+TunnelConfigurationTableViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8A2299215B782D00736141 /* AppCoordinator+TunnelConfigurationTableViewControllerDelegate.swift */; }; 4A8A229C215B787E00736141 /* AppCoordinator+TunnelsTableViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8A229B215B787E00736141 /* AppCoordinator+TunnelsTableViewControllerDelegate.swift */; }; 4A8A229E215B793C00736141 /* AppCoordinator+TunnelInfoTableViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8A229D215B793C00736141 /* AppCoordinator+TunnelInfoTableViewControllerDelegate.swift */; }; + 4A8A22A0215B821A00736141 /* ExtensionMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8A229F215B821A00736141 /* ExtensionMessage.swift */; }; + 4A8A22A1215B823100736141 /* ExtensionMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8A229F215B821A00736141 /* ExtensionMessage.swift */; }; 4A8AABD820B6A79100B6D8C1 /* UITableView+WireGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */; }; 4AADEA2B212616F7008C24FD /* String+Arrays.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FA1D50F2124D80C00DBA2E6 /* String+Arrays.swift */; }; 4ABF718E214D8B0300A1E0BF /* TunnelInfoTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ABF718D214D8B0300A1E0BF /* TunnelInfoTableViewController.swift */; }; @@ -133,6 +134,7 @@ 4A8A2299215B782D00736141 /* AppCoordinator+TunnelConfigurationTableViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppCoordinator+TunnelConfigurationTableViewControllerDelegate.swift"; sourceTree = ""; }; 4A8A229B215B787E00736141 /* AppCoordinator+TunnelsTableViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppCoordinator+TunnelsTableViewControllerDelegate.swift"; sourceTree = ""; }; 4A8A229D215B793C00736141 /* AppCoordinator+TunnelInfoTableViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppCoordinator+TunnelInfoTableViewControllerDelegate.swift"; sourceTree = ""; }; + 4A8A229F215B821A00736141 /* ExtensionMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionMessage.swift; sourceTree = ""; }; 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+WireGuard.swift"; sourceTree = ""; }; 4ABF718D214D8B0300A1E0BF /* TunnelInfoTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelInfoTableViewController.swift; sourceTree = ""; }; 4ABFFE9D212D399F00107136 /* WireGuard-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuard-Bridging-Header.h"; sourceTree = ""; }; @@ -162,7 +164,6 @@ buildActionMask = 2147483647; files = ( 4ABFFEA3212D3C8300107136 /* Security.framework in Frameworks */, - 4A4349752151865E00EF92F8 /* libwg-go.a in Frameworks */, 48CF751B34E9703133F1B1AF /* Pods_WireGuard.framework in Frameworks */, 4A61D83520D98D25006C7A76 /* NetworkExtension.framework in Frameworks */, ); @@ -325,6 +326,7 @@ children = ( 4AC086822120B9F900CEE5ED /* ProviderConfigurationKeys.swift */, 4A4351582124956200261999 /* Validators.swift */, + 4A8A229F215B821A00736141 /* ExtensionMessage.swift */, ); path = Shared; sourceTree = ""; @@ -601,6 +603,7 @@ buildActionMask = 2147483647; files = ( 4A8A229A215B782E00736141 /* AppCoordinator+TunnelConfigurationTableViewControllerDelegate.swift in Sources */, + 4A8A22A0215B821A00736141 /* ExtensionMessage.swift in Sources */, 4A4BAD0C20B5F6AA00F12B28 /* AppCoordinator.swift in Sources */, 4A4BAD2220B6026900F12B28 /* Interface+CoreDataProperties.swift in Sources */, 4A8A2298215B780600736141 /* AppCoordinator+SettingsTableViewControllerDelegate.swift in Sources */, @@ -653,6 +656,7 @@ 4A43515A2124956200261999 /* Validators.swift in Sources */, 4AADEA2B212616F7008C24FD /* String+Arrays.swift in Sources */, 4AEAC32920F14B3B007B67AB /* Log.swift in Sources */, + 4A8A22A1215B823100736141 /* ExtensionMessage.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -816,7 +820,6 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Manual; DEVELOPMENT_TEAM = L82V4Y2P3C; - ENABLE_BITCODE = NO; INFOPLIST_FILE = WireGuard/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 10.3; LD_RUNPATH_SEARCH_PATHS = ( @@ -848,7 +851,6 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Manual; DEVELOPMENT_TEAM = L82V4Y2P3C; - ENABLE_BITCODE = NO; INFOPLIST_FILE = WireGuard/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 10.3; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift b/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift index 0e09386..5784f5d 100644 --- a/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift +++ b/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift @@ -11,6 +11,21 @@ enum GoVersionCoordinatorError: Error { } extension AppCoordinator: SettingsTableViewControllerDelegate { + func goVersionInformation() -> Promise { + return Promise(resolver: { (resolver) in + guard let session = self.providerManagers?.first?.connection as? NETunnelProviderSession else { + resolver.reject(GoVersionCoordinatorError.noSession) + return + } + try session.sendProviderMessage(ExtensionMessage.requestVersion.data, responseHandler: { (data) in + guard let responseString = String(data: data!, encoding: .utf8) else { + return + } + resolver.fulfill(responseString) + }) + }) + } + func exportTunnels(settingsTableViewController: SettingsTableViewController, sourceView: UIView) { self.exportConfigs(sourceView: sourceView) } diff --git a/WireGuard/ViewControllers/SetttingsTableViewController.swift b/WireGuard/ViewControllers/SetttingsTableViewController.swift index a009403..0421815 100644 --- a/WireGuard/ViewControllers/SetttingsTableViewController.swift +++ b/WireGuard/ViewControllers/SetttingsTableViewController.swift @@ -3,9 +3,15 @@ // import UIKit +import PromiseKit + +enum GoVersionError: Error { + case noDelegate +} protocol SettingsTableViewControllerDelegate: class { func exportTunnels(settingsTableViewController: SettingsTableViewController, sourceView: UIView) + func goVersionInformation() -> Promise } class SettingsTableViewController: UITableViewController { @@ -21,14 +27,24 @@ class SettingsTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() versionInfoLabel.text = versionInformation - goVersionInfoLabel.text = goVersionInformation + _ = firstly { () -> Promise in + self.goVersionInfoLabel.text = NSLocalizedString("Loading...", comment: "") + return goVersionInformation() + }.then { (goVersion: String) -> Guarantee in + if let label = self.goVersionInfoLabel { + label.text = goVersion + } + return Guarantee.value(()) + }.recover({ (_) in + self.goVersionInfoLabel.text = NSLocalizedString("Unknown", comment: "") + }) } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { switch cell { case versionInfoCell, goVersionInfoCell: - UIPasteboard.general.string = ["WireGuard for iOS:", versionInformation, "Go userspace backend:", goVersionInformation].joined(separator: "\n") + UIPasteboard.general.string = ["WireGuard for iOS:", versionInformation, "Go userspace backend:", goVersionInfoLabel.text ?? ""].joined(separator: "\n") showCopyConfirmation() case exportCell: delegate?.exportTunnels(settingsTableViewController: self, sourceView: exportCell) @@ -49,8 +65,8 @@ class SettingsTableViewController: UITableViewController { return versionElements.joined(separator: " ") } - var goVersionInformation: String { - return wgVersion().flatMap { String(cString: $0) } ?? "" + func goVersionInformation() -> Promise { + return self.delegate?.goVersionInformation() ?? Promise(error: GoVersionError.noDelegate) } private func showCopyConfirmation() { diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index a6ef5c3..e0536b2 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -110,14 +110,15 @@ class PacketTunnelProvider: NEPacketTunnelProvider { /// Handle IPC messages from the app. override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { - guard let messageString = NSString(data: messageData, encoding: String.Encoding.utf8.rawValue) else { - completionHandler?(nil) - return + let responseData: Data? + + switch ExtensionMessage(messageData) { + case ExtensionMessage.requestVersion: + responseData = (wgVersion().flatMap { String(cString: $0) } ?? "").data(using: .utf8) + default: + responseData = nil } - os_log("Got a message from the app: %s", log: Log.general, type: .info, messageString) - - let responseData = "Hello app".data(using: String.Encoding.utf8) completionHandler?(responseData) }