aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2018-12-21 15:16:09 -0600
committerEric Kuck <eric@bluelinelabs.com>2018-12-21 16:32:08 -0600
commit1fecd8eb6c5c2327d4bfd5044876be12876fc7e5 (patch)
tree45c7f4c552eb01bd15c54e58615c9122acc99e83 /WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
parentDo not require NetworkExtension to know its own name (diff)
downloadwireguard-apple-1fecd8eb6c5c2327d4bfd5044876be12876fc7e5.tar.xz
wireguard-apple-1fecd8eb6c5c2327d4bfd5044876be12876fc7e5.zip
providerConfiguration is now a WgQuickConfig
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to '')
-rw-r--r--WireGuard/Shared/NETunnelProviderProtocol+Extension.swift40
1 files changed, 12 insertions, 28 deletions
diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
index 6f4e3eb0..4f3e1224 100644
--- a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
+++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
@@ -3,38 +3,21 @@
import NetworkExtension
+private var tunnelNameKey: Void?
+
extension NETunnelProviderProtocol {
enum Keys: String {
- case wgQuickConfig = "WgQuickConfigV1"
- }
-
- var tunnelConfiguration: TunnelConfiguration? {
- migrateConfigurationIfNeeded()
-
- let tunnelConfigurationData: Data?
- if let configurationDictionary = providerConfiguration?[Keys.wgQuickConfig.rawValue] {
- tunnelConfigurationData = try? JSONSerialization.data(withJSONObject: configurationDictionary, options: [])
- } else {
- tunnelConfigurationData = nil
- }
-
- guard tunnelConfigurationData != nil else { return nil }
- return try? JSONDecoder().decode(TunnelConfiguration.self, from: tunnelConfigurationData!)
+ case wgQuickConfig = "WgQuickConfig"
}
convenience init?(tunnelConfiguration: TunnelConfiguration) {
- assert(!tunnelConfiguration.interface.name.isEmpty)
-
- guard let tunnelConfigData = try? JSONEncoder().encode(tunnelConfiguration) else { return nil }
- guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return nil }
-
self.init()
-
+
let appId = Bundle.main.bundleIdentifier!
providerBundleIdentifier = "\(appId).network-extension"
- providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ]
-
+ providerConfiguration = [Keys.wgQuickConfig.rawValue: tunnelConfiguration.asWgQuickConfig()]
+
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
if endpoints.count == 1 {
serverAddress = endpoints[0].stringRepresentation
@@ -43,13 +26,14 @@ extension NETunnelProviderProtocol {
} else {
serverAddress = "Multiple endpoints"
}
+
username = tunnelConfiguration.interface.name
}
-
- func hasTunnelConfiguration(tunnelConfiguration otherTunnelConfiguration: TunnelConfiguration) -> Bool {
- guard let serializedThisTunnelConfiguration = try? JSONEncoder().encode(tunnelConfiguration) else { return false }
- guard let serializedOtherTunnelConfiguration = try? JSONEncoder().encode(otherTunnelConfiguration) else { return false }
- return serializedThisTunnelConfiguration == serializedOtherTunnelConfiguration
+
+ func tunnelConfiguration(name: String?) -> TunnelConfiguration? {
+ migrateConfigurationIfNeeded()
+ guard let serializedConfig = providerConfiguration?[Keys.wgQuickConfig.rawValue] as? String else { return nil }
+ return try? TunnelConfiguration(serializedConfig, name: name)
}
}