Model: Add activationType to tunnel configuration

We make sure existing tunnel serializations can be deserialized correctly.

We also bump up the tunnelConfigurationVersion, because the tunnel
configuration contents have changed.
This commit is contained in:
Roopesh Chander 2018-11-10 19:08:21 +05:30
parent 75474acb59
commit 049e9f0b05
3 changed files with 25 additions and 19 deletions

View File

@ -3,18 +3,18 @@
enum ActivationType {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
case useOnDemandOverWifiAndCellular
case useOnDemandOverWifiOnly
case useOnDemandOverCellularOnly
}
extension ActivationType: Codable {
// We use separate coding keys in case we might have a enum with associated values in the future
enum CodingKeys: CodingKey {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
case useOnDemandOverWifiAndCellular
case useOnDemandOverWifiOnly
case useOnDemandOverCellularOnly
}
// Decoding error
@ -28,12 +28,12 @@ extension ActivationType: Codable {
switch self {
case .activateManually:
try container.encode(true, forKey: CodingKeys.activateManually)
case .useOnDemandForAnyInternetActivity:
try container.encode(true, forKey: CodingKeys.useOnDemandForAnyInternetActivity)
case .useOnDemandOnlyOverWifi:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverWifi)
case .useOnDemandOnlyOverCellular:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverCellular)
case .useOnDemandOverWifiAndCellular:
try container.encode(true, forKey: CodingKeys.useOnDemandOverWifiAndCellular)
case .useOnDemandOverWifiOnly:
try container.encode(true, forKey: CodingKeys.useOnDemandOverWifiOnly)
case .useOnDemandOverCellularOnly:
try container.encode(true, forKey: CodingKeys.useOnDemandOverCellularOnly)
}
}
@ -46,18 +46,18 @@ extension ActivationType: Codable {
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandForAnyInternetActivity), isValid {
self = .useOnDemandForAnyInternetActivity
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOverWifiAndCellular), isValid {
self = .useOnDemandOverWifiAndCellular
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverWifi), isValid {
self = .useOnDemandOnlyOverWifi
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOverWifiOnly), isValid {
self = .useOnDemandOverWifiOnly
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverCellular), isValid {
self = .useOnDemandOnlyOverCellular
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOverCellularOnly), isValid {
self = .useOnDemandOverCellularOnly
return
}

View File

@ -7,9 +7,11 @@ import Foundation
final class TunnelConfiguration {
var interface: InterfaceConfiguration
let peers: [PeerConfiguration]
var activationType: ActivationType
init(interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
self.interface = interface
self.peers = peers
self.activationType = .activateManually
let peerPublicKeysArray = peers.map { $0.publicKey }
let peerPublicKeysSet = Set<Data>(peerPublicKeysArray)
@ -61,11 +63,15 @@ extension TunnelConfiguration: Decodable {
enum CodingKeys: CodingKey {
case interface
case peers
case activationType
}
convenience init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let interface = try values.decode(InterfaceConfiguration.self, forKey: .interface)
let peers = try values.decode([PeerConfiguration].self, forKey: .peers)
let activationType = (try? values.decode(ActivationType.self, forKey: .activationType)) ?? .activateManually
self.init(interface: interface, peers: peers)
self.activationType = activationType
}
}

View File

@ -14,7 +14,7 @@ extension NETunnelProviderProtocol {
providerBundleIdentifier = "\(appId).network-extension"
providerConfiguration = [
"tunnelConfiguration": serializedTunnelConfiguration,
"tunnelConfigurationVersion": 1
"tunnelConfigurationVersion": 2
]
let endpoints = tunnelConfiguration.peers.compactMap({$0.endpoint})