wireguard-apple/WireGuard/Shared/Model/Configuration.swift

58 lines
1.7 KiB
Swift
Raw Normal View History

2018-10-24 01:37:28 +00:00
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
2018-10-13 12:14:46 +00:00
import Foundation
@available(OSX 10.14, iOS 12.0, *)
2018-10-13 12:14:46 +00:00
class TunnelConfiguration: Codable {
var interface: InterfaceConfiguration
let peers: [PeerConfiguration]
init(interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
2018-10-13 12:14:46 +00:00
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")
}
2018-10-13 12:14:46 +00:00
}
}
@available(OSX 10.14, iOS 12.0, *)
struct InterfaceConfiguration: Codable {
var name: String
2018-10-13 12:14:46 +00:00
var privateKey: Data
var addresses: [IPAddressRange] = []
2018-11-03 18:35:25 +00:00
var listenPort: UInt16?
var mtu: UInt16?
var dns: [DNSServer] = []
init(name: String, privateKey: Data) {
self.name = name
2018-10-13 12:14:46 +00:00
self.privateKey = privateKey
if (name.isEmpty) { fatalError("Empty name") }
if (privateKey.count != 32) { fatalError("Invalid private key") }
2018-10-13 12:14:46 +00:00
}
}
@available(OSX 10.14, iOS 12.0, *)
struct PeerConfiguration: Codable {
2018-10-13 12:14:46 +00:00
var publicKey: Data
var preSharedKey: Data? {
didSet(value) {
if let value = value {
2018-11-01 17:59:58 +00:00
if (value.count != 32) { fatalError("Invalid preshared key") }
}
}
}
var allowedIPs: [IPAddressRange] = []
var endpoint: Endpoint?
var persistentKeepAlive: UInt16?
2018-10-13 12:14:46 +00:00
init(publicKey: Data) {
self.publicKey = publicKey
if (publicKey.count != 32) { fatalError("Invalid public key") }
2018-10-13 12:14:46 +00:00
}
}