Model: Ensure that a TunnelConfiguration always has a valid array of peers

This commit is contained in:
Roopesh Chander 2018-11-10 17:02:30 +05:30
parent e974b7df23
commit 0db19f187b
3 changed files with 11 additions and 6 deletions

View File

@ -6,9 +6,16 @@ import Foundation
@available(OSX 10.14, iOS 12.0, *)
class TunnelConfiguration: Codable {
var interface: InterfaceConfiguration
var peers: [PeerConfiguration] = []
init(interface: InterfaceConfiguration) {
let peers: [PeerConfiguration]
init(interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
self.interface = interface
self.peers = peers
let peerPublicKeysArray = peers.map { $0.publicKey }
let peerPublicKeysSet = Set<Data>(peerPublicKeysArray)
if (peerPublicKeysArray.count != peerPublicKeysSet.count) {
fatalError("Two or more peers cannot have the same public key")
}
}
}

View File

@ -157,8 +157,7 @@ class WgQuickConfigFileParser {
}
if let interfaceConfiguration = interfaceConfiguration {
let tunnelConfiguration = TunnelConfiguration(interface: interfaceConfiguration)
tunnelConfiguration.peers = peerConfigurations
let tunnelConfiguration = TunnelConfiguration(interface: interfaceConfiguration, peers: peerConfigurations)
return tunnelConfiguration
} else {
throw ParseError.noInterface

View File

@ -441,8 +441,7 @@ class TunnelViewModel {
return .error("Two or more peers cannot have the same public key")
}
let tunnelConfiguration = TunnelConfiguration(interface: interfaceConfiguration)
tunnelConfiguration.peers = peerConfigurations
let tunnelConfiguration = TunnelConfiguration(interface: interfaceConfiguration, peers: peerConfigurations)
return .saved(tunnelConfiguration)
}
}