Model: Make it impossible to create invalid interface / peer configuration instances

This commit is contained in:
Roopesh Chander 2018-10-20 16:05:25 +05:30
parent 255b579f4d
commit 3e72dcade8
1 changed files with 12 additions and 1 deletions

View File

@ -25,20 +25,31 @@ class InterfaceConfiguration: Codable {
var listenPort: UInt64? = nil var listenPort: UInt64? = nil
var mtu: UInt64? = nil var mtu: UInt64? = nil
var dns: String? = nil var dns: String? = nil
init(name: String, privateKey: Data) { init(name: String, privateKey: Data) {
self.name = name self.name = name
self.privateKey = privateKey self.privateKey = privateKey
if (name.isEmpty) { fatalError("Empty name") }
if (privateKey.count != 32) { fatalError("Invalid private key") }
} }
} }
@available(OSX 10.14, iOS 12.0, *) @available(OSX 10.14, iOS 12.0, *)
class PeerConfiguration: Codable { class PeerConfiguration: Codable {
var publicKey: Data 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 allowedIPs: [IPAddressRange] = []
var endpoint: Endpoint? var endpoint: Endpoint?
var persistentKeepAlive: UInt64? var persistentKeepAlive: UInt64?
init(publicKey: Data) { init(publicKey: Data) {
self.publicKey = publicKey self.publicKey = publicKey
if (publicKey.count != 32) { fatalError("Invalid public key") }
} }
} }