aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Models/Peer+Extension.swift
blob: 05d8c7724203c4c1fb2980914c56dbb4cdfbf389 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  Peer+Extension.swift
//  WireGuard
//
//  Created by Eric Kuck on 8/15/18.
//  Copyright © 2018 WireGuard LLC. 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)
        }
    }

    func export() -> String {
        var exportString = "[Peer]\n"
        if let publicKey = publicKey {
            exportString.append("PublicKey=\(publicKey)\n")
        }
        if let presharedKey = presharedKey {
            exportString.append("PresharedKey=\(presharedKey)\n")
        }
        if let allowedIPs = allowedIPs {
            exportString.append("AllowedIPs=\(allowedIPs)\n")
        }
        if let endpoint = endpoint {
            exportString.append("Endpoint=\(endpoint)\n")
        }
        if persistentKeepalive > 0 {
            exportString.append("PersistentKeepalive=\(persistentKeepalive)\n")
        }

        exportString.append("\n")

        return exportString
    }

}

enum PeerValidationError: Error {
    case emptyPublicKey
    case invalidPublicKey
    case nilAllowedIps
    case invalidAllowedIPs(cause: Error)
    case invalidEndpoint(cause: Error)
    case invalidPersistedKeepAlive
}