aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-08 15:54:12 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-08 17:52:01 +0530
commite66cf5264ad6980dae3f990b232c2f62c43608db (patch)
tree835a8c676e901eebdeafc422bb29acc9d41925f3 /WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
parentMove logic to extension: Refactor PacketTunnelOptionsGenerator into a PacketTunnelSettingsGenerator (diff)
downloadwireguard-apple-e66cf5264ad6980dae3f990b232c2f62c43608db.tar.xz
wireguard-apple-e66cf5264ad6980dae3f990b232c2f62c43608db.zip
Move logic to extension: NETunnelProviderProtocol extension code should be shared
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared/NETunnelProviderProtocol+Extension.swift')
-rw-r--r--WireGuard/Shared/NETunnelProviderProtocol+Extension.swift35
1 files changed, 35 insertions, 0 deletions
diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
new file mode 100644
index 0000000..ec8b294
--- /dev/null
+++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import NetworkExtension
+
+extension NETunnelProviderProtocol {
+ convenience init?(tunnelConfiguration: TunnelConfiguration) {
+ assert(!tunnelConfiguration.interface.name.isEmpty)
+ guard let serializedTunnelConfiguration = try? JSONEncoder().encode(tunnelConfiguration) else { return nil }
+
+ self.init()
+
+ let appId = Bundle.main.bundleIdentifier!
+ providerBundleIdentifier = "\(appId).network-extension"
+ providerConfiguration = [
+ "tunnelConfiguration": serializedTunnelConfiguration,
+ "tunnelConfigurationVersion": 1
+ ]
+
+ let endpoints = tunnelConfiguration.peers.compactMap({$0.endpoint})
+ if endpoints.count == 1 {
+ serverAddress = endpoints.first!.stringRepresentation()
+ } else if endpoints.isEmpty {
+ serverAddress = "Unspecified"
+ } else {
+ serverAddress = "Multiple endpoints"
+ }
+ username = tunnelConfiguration.interface.name
+ }
+
+ func tunnelConfiguration() -> TunnelConfiguration? {
+ guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return nil }
+ return try? JSONDecoder().decode(TunnelConfiguration.self, from: serializedTunnelConfiguration)
+ }
+}