From 0db19f187ba63babaa6754d00b277e723839325b Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 10 Nov 2018 17:02:30 +0530 Subject: [PATCH] Model: Ensure that a TunnelConfiguration always has a valid array of peers --- WireGuard/Shared/Model/Configuration.swift | 11 +++++++++-- .../ConfigFile/WgQuickConfigFileParser.swift | 3 +-- WireGuard/WireGuard/UI/TunnelViewModel.swift | 3 +-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/WireGuard/Shared/Model/Configuration.swift b/WireGuard/Shared/Model/Configuration.swift index cbe4271..3e661e6 100644 --- a/WireGuard/Shared/Model/Configuration.swift +++ b/WireGuard/Shared/Model/Configuration.swift @@ -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(peerPublicKeysArray) + if (peerPublicKeysArray.count != peerPublicKeysSet.count) { + fatalError("Two or more peers cannot have the same public key") + } } } diff --git a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift index 863cd11..0ded15e 100644 --- a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift +++ b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift @@ -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 diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index 16da1c2..94e3e6d 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -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) } }