From 049e9f0b05250db97216bd0cf7233d7a0f0a5b0f Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 10 Nov 2018 19:08:21 +0530 Subject: [PATCH] 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. --- WireGuard/Shared/Model/ActivationType.swift | 36 +++++++++---------- WireGuard/Shared/Model/Configuration.swift | 6 ++++ .../NETunnelProviderProtocol+Extension.swift | 2 +- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/WireGuard/Shared/Model/ActivationType.swift b/WireGuard/Shared/Model/ActivationType.swift index 56b0e6c..ea5927d 100644 --- a/WireGuard/Shared/Model/ActivationType.swift +++ b/WireGuard/Shared/Model/ActivationType.swift @@ -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 } diff --git a/WireGuard/Shared/Model/Configuration.swift b/WireGuard/Shared/Model/Configuration.swift index 45c29c1..f6598bb 100644 --- a/WireGuard/Shared/Model/Configuration.swift +++ b/WireGuard/Shared/Model/Configuration.swift @@ -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(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 } } diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift index ec8b294..591b0eb 100644 --- a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift +++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift @@ -14,7 +14,7 @@ extension NETunnelProviderProtocol { providerBundleIdentifier = "\(appId).network-extension" providerConfiguration = [ "tunnelConfiguration": serializedTunnelConfiguration, - "tunnelConfigurationVersion": 1 + "tunnelConfigurationVersion": 2 ] let endpoints = tunnelConfiguration.peers.compactMap({$0.endpoint})