diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-22 01:36:33 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-22 01:36:42 +0100 |
commit | aede9f6e450d25a0a9cbeeea604e08ba76246869 (patch) | |
tree | bdbb3619adbf0daa6db1d7f31dee5820a4259ac5 /WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift | |
parent | Fixes mock tunnels (diff) | |
download | wireguard-apple-aede9f6e450d25a0a9cbeeea604e08ba76246869.tar.xz wireguard-apple-aede9f6e450d25a0a9cbeeea604e08ba76246869.zip |
Move model helpers to model directory
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift')
-rw-r--r-- | WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift new file mode 100644 index 0000000..98634fd --- /dev/null +++ b/WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import NetworkExtension + +extension NETunnelProviderProtocol { + + enum Keys: String { + case wgQuickConfig = "WgQuickConfig" + } + + convenience init?(tunnelConfiguration: TunnelConfiguration) { + self.init() + + let appId = Bundle.main.bundleIdentifier! + providerBundleIdentifier = "\(appId).network-extension" + providerConfiguration = [Keys.wgQuickConfig.rawValue: tunnelConfiguration.asWgQuickConfig()] + + let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint } + if endpoints.count == 1 { + serverAddress = endpoints[0].stringRepresentation + } else if endpoints.isEmpty { + serverAddress = "Unspecified" + } else { + serverAddress = "Multiple endpoints" + } + + //TODO(roopc): Why are we doing this? Just for kicks? Is it useful? Seems needless. + username = tunnelConfiguration.name + } + + func asTunnelConfiguration(called name: String? = nil) -> TunnelConfiguration? { + migrateConfigurationIfNeeded() + guard let serializedConfig = providerConfiguration?[Keys.wgQuickConfig.rawValue] as? String else { return nil } + return try? TunnelConfiguration(fromWgQuickConfig: serializedConfig, called: name) + } + +} |