aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-22 01:36:33 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-22 01:36:42 +0100
commit30cf9a1d1da7feaabfb969499453e0d3b5b224f2 (patch)
treebdbb3619adbf0daa6db1d7f31dee5820a4259ac5 /WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift
parentFixes mock tunnels (diff)
downloadwireguard-apple-30cf9a1d1da7feaabfb969499453e0d3b5b224f2.tar.xz
wireguard-apple-30cf9a1d1da7feaabfb969499453e0d3b5b224f2.zip
Move model helpers to model directory
Diffstat (limited to 'WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift')
-rw-r--r--WireGuard/Shared/Model/NETunnelProviderProtocol+Extension.swift38
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)
+ }
+
+}