wireguard-apple/WireGuard/Shared/Model/NETunnelProviderProtocol+Ex...

44 lines
1.4 KiB
Swift
Raw Normal View History

// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
import NetworkExtension
enum PacketTunnelProviderError: String, Error {
case savedProtocolConfigurationIsInvalid
case dnsResolutionFailure
case couldNotStartBackend
case couldNotDetermineFileDescriptor
case couldNotSetNetworkSettings
}
extension NETunnelProviderProtocol {
2018-12-21 22:34:56 +00:00
2018-12-21 04:52:45 +00:00
enum Keys: String {
case wgQuickConfig = "WgQuickConfig"
2018-12-21 04:52:45 +00:00
}
2018-12-21 22:34:56 +00:00
convenience init?(tunnelConfiguration: TunnelConfiguration) {
self.init()
2018-12-21 22:34:56 +00:00
let appId = Bundle.main.bundleIdentifier!
providerBundleIdentifier = "\(appId).network-extension"
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 }
if endpoints.count == 1 {
2018-12-21 04:52:45 +00:00
serverAddress = endpoints[0].stringRepresentation
} 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
func asTunnelConfiguration(called name: String? = nil) -> TunnelConfiguration? {
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-21 22:34:56 +00:00
}