diff options
author | Roopesh Chander <roop@roopc.net> | 2018-10-20 16:05:25 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-10-27 15:13:01 +0530 |
commit | 89214e7db9e73f94ad2227607337713f00c0d740 (patch) | |
tree | a33ea2d65a063aa8eb17a00c791ce18efec89676 /WireGuard/WireGuard/Model | |
parent | Use Endpoint in the Configuration model (diff) | |
download | wireguard-apple-89214e7db9e73f94ad2227607337713f00c0d740.tar.xz wireguard-apple-89214e7db9e73f94ad2227607337713f00c0d740.zip |
Model: Make it impossible to create invalid interface / peer configuration instances
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/Model')
-rw-r--r-- | WireGuard/WireGuard/Model/Configuration.swift | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/WireGuard/WireGuard/Model/Configuration.swift b/WireGuard/WireGuard/Model/Configuration.swift index 79018ca..152d30a 100644 --- a/WireGuard/WireGuard/Model/Configuration.swift +++ b/WireGuard/WireGuard/Model/Configuration.swift @@ -25,20 +25,31 @@ class InterfaceConfiguration: Codable { var listenPort: UInt64? = nil var mtu: UInt64? = nil var dns: String? = nil + init(name: String, privateKey: Data) { self.name = name self.privateKey = privateKey + if (name.isEmpty) { fatalError("Empty name") } + if (privateKey.count != 32) { fatalError("Invalid private key") } } } @available(OSX 10.14, iOS 12.0, *) class PeerConfiguration: Codable { var publicKey: Data - var preSharedKey: Data? + var preSharedKey: Data? { + didSet(value) { + if let value = value { + if (value.count != 32) { fatalError("Invalid pre-shared key") } + } + } + } var allowedIPs: [IPAddressRange] = [] var endpoint: Endpoint? var persistentKeepAlive: UInt64? + init(publicKey: Data) { self.publicKey = publicKey + if (publicKey.count != 32) { fatalError("Invalid public key") } } } |