blob: f83c0386df81c46e54347fd5a2359c0021b1a9d7 (
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
67
68
69
70
71
72
73
74
75
|
//
// Peer+Extension.swift
// WireGuard
//
// Created by Eric Kuck on 8/15/18.
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
//
import Foundation
extension Peer {
func validate() throws {
guard let publicKey = publicKey, !publicKey.isEmpty else {
throw PeerValidationError.emptyPublicKey
}
guard publicKey.isBase64() else {
throw PeerValidationError.invalidPublicKey
}
guard let allowedIPs = allowedIPs, !allowedIPs.isEmpty else {
throw PeerValidationError.nilAllowedIps
}
try allowedIPs.commaSeparatedToArray().forEach { address in
do {
try _ = CIDRAddress(stringRepresentation: address)
} catch {
throw PeerValidationError.invalidAllowedIPs(cause: error)
}
}
if let endpoint = endpoint {
do {
try _ = Endpoint(endpointString: endpoint)
} catch {
throw PeerValidationError.invalidEndpoint(cause: error)
}
}
guard persistentKeepalive >= 0, persistentKeepalive <= 65535 else {
throw PeerValidationError.invalidPersistedKeepAlive
}
}
func parse(attribute: Attribute) throws {
switch attribute.key {
case .allowedIPs:
allowedIPs = attribute.stringValue
case .endpoint:
endpoint = attribute.stringValue
case .persistentKeepalive:
if let keepAlive = Int32(attribute.stringValue) {
persistentKeepalive = keepAlive
}
case .presharedKey:
presharedKey = attribute.stringValue
case .publicKey:
publicKey = attribute.stringValue
default:
throw TunnelParseError.invalidLine(attribute.line)
}
}
}
enum PeerValidationError: Error {
case emptyPublicKey
case invalidPublicKey
case nilAllowedIps
case invalidAllowedIPs(cause: Error)
case invalidEndpoint(cause: Error)
case invalidPersistedKeepAlive
}
|