2018-11-08 10:24:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
|
|
|
|
|
|
|
import NetworkExtension
|
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
private var tunnelNameKey: Void?
|
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
extension NETunnelProviderProtocol {
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
enum Keys: String {
|
2018-12-21 21:16:09 +00:00
|
|
|
case wgQuickConfig = "WgQuickConfig"
|
2018-12-21 04:52:45 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 17:50:32 +00:00
|
|
|
convenience init?(tunnelConfiguration: TunnelConfiguration) {
|
2018-11-08 10:24:12 +00:00
|
|
|
self.init()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
let appId = Bundle.main.bundleIdentifier!
|
|
|
|
providerBundleIdentifier = "\(appId).network-extension"
|
2018-12-21 21:16:09 +00:00
|
|
|
providerConfiguration = [Keys.wgQuickConfig.rawValue: tunnelConfiguration.asWgQuickConfig()]
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
|
2018-11-08 10:24:12 +00:00
|
|
|
if endpoints.count == 1 {
|
2018-12-21 04:52:45 +00:00
|
|
|
serverAddress = endpoints[0].stringRepresentation
|
2018-11-08 10:24:12 +00:00
|
|
|
} else if endpoints.isEmpty {
|
|
|
|
serverAddress = "Unspecified"
|
|
|
|
} else {
|
|
|
|
serverAddress = "Multiple endpoints"
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
//TODO(roopc): Why are we doing this? Just for kicks? Is it useful? Seems needless.
|
|
|
|
username = tunnelConfiguration.name
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
func asTunnelConfiguration(called name: String? = nil) -> TunnelConfiguration? {
|
2018-12-21 21:16:09 +00:00
|
|
|
migrateConfigurationIfNeeded()
|
|
|
|
guard let serializedConfig = providerConfiguration?[Keys.wgQuickConfig.rawValue] as? String else { return nil }
|
2018-12-21 23:28:18 +00:00
|
|
|
return try? TunnelConfiguration(fromWgQuickConfig: serializedConfig, called: name)
|
2018-12-19 10:32:48 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|