2018-11-08 10:24:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
|
|
|
|
|
|
|
import NetworkExtension
|
|
|
|
|
|
|
|
extension NETunnelProviderProtocol {
|
2018-12-19 10:08:00 +00:00
|
|
|
convenience init?(tunnelConfiguration: TunnelConfiguration, isActivateOnDemandEnabled: Bool) {
|
2018-11-08 10:24:12 +00:00
|
|
|
assert(!tunnelConfiguration.interface.name.isEmpty)
|
|
|
|
guard let serializedTunnelConfiguration = try? JSONEncoder().encode(tunnelConfiguration) else { return nil }
|
|
|
|
|
|
|
|
self.init()
|
|
|
|
|
|
|
|
let appId = Bundle.main.bundleIdentifier!
|
|
|
|
providerBundleIdentifier = "\(appId).network-extension"
|
|
|
|
providerConfiguration = [
|
|
|
|
"tunnelConfiguration": serializedTunnelConfiguration,
|
2018-12-19 10:08:00 +00:00
|
|
|
"tunnelConfigurationVersion": 1,
|
|
|
|
"isActivateOnDemandEnabled": isActivateOnDemandEnabled
|
2018-11-08 10:24:12 +00:00
|
|
|
]
|
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
let endpoints = tunnelConfiguration.peers.compactMap {$0.endpoint}
|
2018-11-08 10:24:12 +00:00
|
|
|
if endpoints.count == 1 {
|
|
|
|
serverAddress = endpoints.first!.stringRepresentation()
|
|
|
|
} else if endpoints.isEmpty {
|
|
|
|
serverAddress = "Unspecified"
|
|
|
|
} else {
|
|
|
|
serverAddress = "Multiple endpoints"
|
|
|
|
}
|
|
|
|
username = tunnelConfiguration.interface.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func tunnelConfiguration() -> TunnelConfiguration? {
|
|
|
|
guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return nil }
|
|
|
|
return try? JSONDecoder().decode(TunnelConfiguration.self, from: serializedTunnelConfiguration)
|
|
|
|
}
|
2018-12-19 10:08:00 +00:00
|
|
|
|
|
|
|
var isActivateOnDemandEnabled: Bool {
|
|
|
|
return (providerConfiguration?["isActivateOnDemandEnabled"] as? Bool) ?? false
|
|
|
|
}
|
2018-12-19 10:32:48 +00:00
|
|
|
|
|
|
|
func hasTunnelConfiguration(tunnelConfiguration otherTunnelConfiguration: TunnelConfiguration) -> Bool {
|
|
|
|
guard let serializedThisTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return false }
|
|
|
|
guard let serializedOtherTunnelConfiguration = try? JSONEncoder().encode(otherTunnelConfiguration) else { return false }
|
|
|
|
return serializedThisTunnelConfiguration == serializedOtherTunnelConfiguration
|
|
|
|
}
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|