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 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 17:50:32 +00:00
|
|
|
convenience init?(tunnelConfiguration: TunnelConfiguration) {
|
2018-11-08 10:24:12 +00:00
|
|
|
self.init()
|
2018-12-21 21:16:09 +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 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 21:16:09 +00:00
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
username = tunnelConfiguration.interface.name
|
|
|
|
}
|
2018-12-21 21:16:09 +00:00
|
|
|
|
|
|
|
func tunnelConfiguration(name: String?) -> TunnelConfiguration? {
|
|
|
|
migrateConfigurationIfNeeded()
|
|
|
|
guard let serializedConfig = providerConfiguration?[Keys.wgQuickConfig.rawValue] as? String else { return nil }
|
|
|
|
return try? TunnelConfiguration(serializedConfig, name: name)
|
2018-12-19 10:32:48 +00:00
|
|
|
}
|
2018-12-21 04:52:45 +00:00
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|