From d38a81c3016fc24f1c46efb585ae9be8c8b358f0 Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Sun, 12 Aug 2018 21:37:56 +0200 Subject: Add back addresses. Signed-off-by: Jason A. Donenfeld --- Shared/ProviderConfigurationKeys.swift | 13 +++++ WireGuard.xcodeproj/project.pbxproj | 18 ++++++- WireGuard/Base.lproj/Main.storyboard | 62 +++++++++++++++------- .../Models/Interface+CoreDataProperties.swift | 1 + WireGuard/Models/Tunnel+Extension.swift | 14 +++-- .../WireGuard.xcdatamodel/contents | 3 +- .../TunnelConfigurationTableViewController.swift | 4 ++ .../PacketTunnelProvider.swift | 9 +++- 8 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 Shared/ProviderConfigurationKeys.swift diff --git a/Shared/ProviderConfigurationKeys.swift b/Shared/ProviderConfigurationKeys.swift new file mode 100644 index 0000000..401cafe --- /dev/null +++ b/Shared/ProviderConfigurationKeys.swift @@ -0,0 +1,13 @@ +// +// ProviderConfigurationKeys.swift +// WireGuard +// +// Created by Jeroen Leenarts on 12-08-18. +// Copyright © 2018 WireGuard. All rights reserved. +// + +import Foundation + +enum PCKeys: String { + case title, tunnelIdentifier, endpoints, dns, addresses, mtu, settings +} diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index d01f8c5..37cb3ca 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -27,10 +27,12 @@ 4A4BAD2120B6026900F12B28 /* Peer+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A4BAD1D20B6026900F12B28 /* Peer+CoreDataClass.swift */; }; 4A4BAD2220B6026900F12B28 /* Interface+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A4BAD1E20B6026900F12B28 /* Interface+CoreDataProperties.swift */; }; 4A4BAD2320B6026900F12B28 /* Interface+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A4BAD1F20B6026900F12B28 /* Interface+CoreDataClass.swift */; }; - 4A61D82920D98CE2006C7A76 /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A61D82820D98CE2006C7A76 /* PacketTunnelProvider.swift */; }; 4A61D82E20D98CE2006C7A76 /* WireGuardNetworkExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 4A61D82620D98CE1006C7A76 /* WireGuardNetworkExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 4A61D83520D98D25006C7A76 /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A61D83420D98D25006C7A76 /* NetworkExtension.framework */; }; 4A8AABD820B6A79100B6D8C1 /* UITableView+WireGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */; }; + 4AC086832120B9F900CEE5ED /* ProviderConfigurationKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC086822120B9F900CEE5ED /* ProviderConfigurationKeys.swift */; }; + 4AC086852120BCB500CEE5ED /* ProviderConfigurationKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC086822120B9F900CEE5ED /* ProviderConfigurationKeys.swift */; }; + 4AC086862120BD5800CEE5ED /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A61D82820D98CE2006C7A76 /* PacketTunnelProvider.swift */; }; 4AC5462E2116306F00749D21 /* Tunnel+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC5462D2116306F00749D21 /* Tunnel+Extension.swift */; }; 4AC5462F211643DE00749D21 /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A61D83420D98D25006C7A76 /* NetworkExtension.framework */; }; 4AD095C820DC4190000E9CF5 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AD0900120DC4171000E9CF5 /* libwg-go.a */; }; @@ -103,6 +105,7 @@ 4A61D83320D98D07006C7A76 /* WireGuard.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuard.entitlements; sourceTree = ""; }; 4A61D83420D98D25006C7A76 /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = System/Library/Frameworks/NetworkExtension.framework; sourceTree = SDKROOT; }; 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+WireGuard.swift"; sourceTree = ""; }; + 4AC086822120B9F900CEE5ED /* ProviderConfigurationKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderConfigurationKeys.swift; sourceTree = ""; }; 4AC5462D2116306F00749D21 /* Tunnel+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tunnel+Extension.swift"; sourceTree = ""; }; 4AD0900120DC4171000E9CF5 /* libwg-go.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = "libwg-go.a"; sourceTree = ""; }; 4AD0900720DC4171000E9CF5 /* wireguard.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = wireguard.h; sourceTree = ""; }; @@ -146,6 +149,7 @@ 4A4BACD920B5F1BF00F12B28 = { isa = PBXGroup; children = ( + 4AC086812120B9E600CEE5ED /* Shared */, 4AD08FFE20DC4171000E9CF5 /* wireguard-go-bridge */, 4A4BAD0720B5F4BC00F12B28 /* Resources */, 4A4BACE420B5F1BF00F12B28 /* WireGuard */, @@ -251,6 +255,14 @@ path = WireGuardNetworkExtension; sourceTree = ""; }; + 4AC086812120B9E600CEE5ED /* Shared */ = { + isa = PBXGroup; + children = ( + 4AC086822120B9F900CEE5ED /* ProviderConfigurationKeys.swift */, + ); + path = Shared; + sourceTree = ""; + }; 4AD08FFE20DC4171000E9CF5 /* wireguard-go-bridge */ = { isa = PBXGroup; children = ( @@ -533,6 +545,7 @@ 4A4BA6D820B73CBA00223AB8 /* TunnelConfigurationTableViewController.swift in Sources */, 4A4BAD2020B6026900F12B28 /* Peer+CoreDataProperties.swift in Sources */, 4A4BAD2320B6026900F12B28 /* Interface+CoreDataClass.swift in Sources */, + 4AC086832120B9F900CEE5ED /* ProviderConfigurationKeys.swift in Sources */, 4A8AABD820B6A79100B6D8C1 /* UITableView+WireGuard.swift in Sources */, 4A4BAD2120B6026900F12B28 /* Peer+CoreDataClass.swift in Sources */, 4A4BAD1B20B5F8FF00F12B28 /* Tunnel+CoreDataProperties.swift in Sources */, @@ -553,7 +566,8 @@ buildActionMask = 2147483647; files = ( 4AD095CC20DC42CD000E9CF5 /* WireGuardGoWrapper.m in Sources */, - 4A61D82920D98CE2006C7A76 /* PacketTunnelProvider.swift in Sources */, + 4AC086852120BCB500CEE5ED /* ProviderConfigurationKeys.swift in Sources */, + 4AC086862120BD5800CEE5ED /* PacketTunnelProvider.swift in Sources */, 4AEAC32920F14B3B007B67AB /* Log.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/WireGuard/Base.lproj/Main.storyboard b/WireGuard/Base.lproj/Main.storyboard index 1b486a8..013b7b3 100644 --- a/WireGuard/Base.lproj/Main.storyboard +++ b/WireGuard/Base.lproj/Main.storyboard @@ -87,24 +87,24 @@ - - + + - + - + - + - + @@ -125,7 +125,7 @@ - + - + - + @@ -147,7 +147,7 @@ @@ -155,7 +155,7 @@ - + - + @@ -175,11 +175,32 @@ + + + + + + + + + + + + + + + + - + - + - + @@ -200,7 +221,7 @@ - + - + @@ -237,6 +258,7 @@ + @@ -245,7 +267,7 @@ - + @@ -270,7 +292,7 @@ - + diff --git a/WireGuard/Models/Interface+CoreDataProperties.swift b/WireGuard/Models/Interface+CoreDataProperties.swift index 7c426b1..d52901f 100644 --- a/WireGuard/Models/Interface+CoreDataProperties.swift +++ b/WireGuard/Models/Interface+CoreDataProperties.swift @@ -16,6 +16,7 @@ extension Interface { return NSFetchRequest(entityName: "Interface") } + @NSManaged public var addresses: String? @NSManaged public var listenPort: Int16 @NSManaged public var privateKey: String? @NSManaged public var mtu: Int32 diff --git a/WireGuard/Models/Tunnel+Extension.swift b/WireGuard/Models/Tunnel+Extension.swift index cd1f015..6d50fb2 100644 --- a/WireGuard/Models/Tunnel+Extension.swift +++ b/WireGuard/Models/Tunnel+Extension.swift @@ -12,8 +12,15 @@ extension Tunnel { public func generateProviderConfiguration() -> [String: Any] { var providerConfiguration = [String: Any]() - providerConfiguration["title"] = self.title - providerConfiguration["tunnelIdentifier"] = self.tunnelIdentifier + providerConfiguration[PCKeys.title.rawValue] = self.title + providerConfiguration[PCKeys.tunnelIdentifier.rawValue] = self.tunnelIdentifier + providerConfiguration[PCKeys.endpoints.rawValue] = peers?.array.compactMap {($0 as? Peer)?.endpoint}.joined(separator: ", ") + providerConfiguration[PCKeys.dns.rawValue] = interface?.dns + providerConfiguration[PCKeys.addresses.rawValue] = interface?.addresses + if let mtu = interface?.mtu { + providerConfiguration[PCKeys.mtu.rawValue] = NSNumber(value: mtu) + } + var settingsString = "replace_peers=true\n" if let interface = interface { settingsString += generateInterfaceProviderConfiguration(interface) @@ -40,9 +47,6 @@ extension Tunnel { if interface.listenPort > 0 { settingsString += "listen_port=\(interface.listenPort)\n" } - if let dns = interface.dns { - settingsString += "dns=\(dns)\n" - } if interface.mtu > 0 { settingsString += "mtu=\(interface.mtu)\n" } diff --git a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents index d888fa1..398ed39 100644 --- a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents +++ b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents @@ -1,6 +1,7 @@ + @@ -22,7 +23,7 @@ - + diff --git a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift index f761495..85d3581 100644 --- a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift +++ b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift @@ -137,6 +137,7 @@ class InterfaceTableViewCell: UITableViewCell { var model: Interface! { didSet { nameField.text = model.tunnel?.title + addressesField.text = model.addresses privateKeyField.text = model.privateKey listenPortField.text = String(model.listenPort) dnsField.text = model.dns @@ -145,6 +146,7 @@ class InterfaceTableViewCell: UITableViewCell { } @IBOutlet weak var nameField: UITextField! + @IBOutlet weak var addressesField: UITextField! @IBOutlet weak var privateKeyField: UITextField! @IBOutlet weak var publicKeyField: UITextField! @IBOutlet weak var listenPortField: UITextField! @@ -161,6 +163,8 @@ extension InterfaceTableViewCell: UITextFieldDelegate { model.tunnel?.title = string } else if sender == privateKeyField { model.privateKey = string + } else if sender == addressesField { + model.addresses = string } else if sender == listenPortField { if let string = string, let port = Int16(string) { model.listenPort = port diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index a6891fa..3443e98 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -28,8 +28,13 @@ class PacketTunnelProvider: NEPacketTunnelProvider { os_log("Starting tunnel", log: Log.general, type: .info) let config = self.protocolConfiguration as! NETunnelProviderProtocol // swiftlint:disable:this force_cast - let interfaceName = config.providerConfiguration!["title"]! as! String // swiftlint:disable:this force_cast - let settings = config.providerConfiguration!["settings"]! as! String // swiftlint:disable:this force_cast + let interfaceName = config.providerConfiguration![PCKeys.title.rawValue]! as! String // swiftlint:disable:this force_cast + let mtu = config.providerConfiguration![PCKeys.mtu.rawValue] as? NSNumber + let settings = config.providerConfiguration![PCKeys.settings.rawValue]! as! String // swiftlint:disable:this force_cast + let endpoints = config.providerConfiguration?[PCKeys.endpoints.rawValue] as? String ?? "" + let addresses = (config.providerConfiguration?[PCKeys.addresses.rawValue] as? String ?? "").split(separator: ",") + + settings.split(separator: "\n").forEach {os_log("Tunnel config: %{public}s", log: Log.general, type: .info, String($0))} if wireGuardWrapper.turnOn(withInterfaceName: interfaceName, settingsString: settings) { // Success -- cgit v1.2.3-59-g8ed1b