blob: 0fad842a3407d9f0a38c59a9e2bc3bbc4d36d919 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
import Foundation
struct PeerConfiguration {
var publicKey: Data
var preSharedKey: Data? {
didSet(value) {
if let value = value {
if value.count != TunnelConfiguration.keyLength {
fatalError("Invalid preshared key")
}
}
}
}
var allowedIPs = [IPAddressRange]()
var endpoint: Endpoint?
var persistentKeepAlive: UInt16?
init(publicKey: Data) {
self.publicKey = publicKey
if publicKey.count != TunnelConfiguration.keyLength {
fatalError("Invalid public key")
}
}
}
extension PeerConfiguration: Codable {
enum CodingKeys: String, CodingKey {
case publicKey = "PublicKey"
case preSharedKey = "PreSharedKey"
case allowedIPs = "AllowedIPs"
case endpoint = "Endpoint"
case persistentKeepAlive = "PersistentKeepAlive"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
publicKey = try Data(base64Encoded: values.decode(String.self, forKey: .publicKey))!
if let base64PreSharedKey = try? values.decode(Data.self, forKey: .preSharedKey) {
preSharedKey = Data(base64Encoded: base64PreSharedKey)
} else {
preSharedKey = nil
}
allowedIPs = try values.decode([IPAddressRange].self, forKey: .allowedIPs)
endpoint = try? values.decode(Endpoint.self, forKey: .endpoint)
persistentKeepAlive = try? values.decode(UInt16.self, forKey: .persistentKeepAlive)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(publicKey.base64EncodedString(), forKey: .publicKey)
if let preSharedKey = preSharedKey {
try container.encode(preSharedKey.base64EncodedString(), forKey: .preSharedKey)
}
try container.encode(allowedIPs, forKey: .allowedIPs)
if let endpoint = endpoint {
try container.encode(endpoint, forKey: .endpoint)
}
if let persistentKeepAlive = persistentKeepAlive {
try container.encode(persistentKeepAlive, forKey: .persistentKeepAlive)
}
}
}
|