aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Models/Interface+Extension.swift
blob: 5dbeb5a7a56f80e809956eb20a72b355d8f27c26 (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
//
//  Interface+Extension.swift
//  WireGuard
//
//  Created by Eric Kuck on 8/15/18.
//  Copyright © 2018 WireGuard LLC. All rights reserved.
//

import Foundation

extension Interface {

    func validate() throws {
        guard let privateKey = privateKey, !privateKey.isEmpty else {
            throw InterfaceValidationError.emptyPrivateKey
        }

        guard privateKey.isBase64() else {
            throw InterfaceValidationError.invalidPrivateKey
        }

        try addresses?.commaSeparatedToArray().forEach { address in
            do {
                try _ = CIDRAddress(stringRepresentation: address)
            } catch {
                throw InterfaceValidationError.invalidAddress(cause: error)
            }
        }

        try dns?.commaSeparatedToArray().forEach { address in
            do {
                try _ = Endpoint(endpointString: address, needsPort: false)
            } catch {
                throw InterfaceValidationError.invalidDNSServer(cause: error)
            }
        }
    }

    func parse(attribute: Attribute) throws {
        switch attribute.key {
        case .address:
            addresses = attribute.stringValue
        case .dns:
            dns = attribute.stringValue
        case .listenPort:
            if let port = Int16(attribute.stringValue) {
                listenPort = port
            }
        case .mtu:
            if let mtu = Int32(attribute.stringValue) {
                self.mtu = mtu
            }
        case .privateKey:
            privateKey = attribute.stringValue
        default:
            throw TunnelParseError.invalidLine(attribute.line)
        }
    }

    func export() -> String {
        var exportString = "[Interface]\n"
        if let privateKey = privateKey {
            exportString.append("PrivateKey=\(privateKey)\n")
        }
        if let addresses = addresses {
            exportString.append("Address=\(addresses)\n")
        }
        if let dns = dns {
            exportString.append("DNS=\(dns)\n")
        }
        if mtu > 0 {
            exportString.append("MTU=\(mtu)\n")
        }
        if listenPort > 0 {
            exportString.append("ListenPort=\(listenPort)\n")
        }

        exportString.append("\n")

        return exportString
    }

}

enum InterfaceValidationError: Error {
    case emptyPrivateKey
    case invalidPrivateKey
    case invalidAddress(cause: Error)
    case invalidDNSServer(cause: Error)
}