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

View File

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

View File

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