mirror of
https://github.com/passepartoutvpn/wireguard-apple.git
synced 2025-02-08 08:52:02 +00:00
We make sure existing tunnel serializations can be deserialized correctly. We also bump up the tunnelConfigurationVersion, because the tunnel configuration contents have changed.
36 lines
1.3 KiB
Swift
36 lines
1.3 KiB
Swift
// SPDX-License-Identifier: MIT
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
|
|
|
import NetworkExtension
|
|
|
|
extension NETunnelProviderProtocol {
|
|
convenience init?(tunnelConfiguration: TunnelConfiguration) {
|
|
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,
|
|
"tunnelConfigurationVersion": 2
|
|
]
|
|
|
|
let endpoints = tunnelConfiguration.peers.compactMap({$0.endpoint})
|
|
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)
|
|
}
|
|
}
|