aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/LegacyConfigMigration.swift
blob: e7588a28fd9dab7b04d8e24e952b14892d1ee14e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.

import Foundation
import Network
import NetworkExtension

protocol LegacyModel: Decodable {
    associatedtype Model
    
    var migrated: Model { get }
}

struct LegacyDNSServer: LegacyModel {
    let address: IPAddress
    
    var migrated: DNSServer {
        return DNSServer(address: address)
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        var data = try container.decode(Data.self)
        let ipAddressFromData: IPAddress? = {
            switch data.count {
            case 4: return IPv4Address(data)
            case 16: return IPv6Address(data)
            default: return nil
            }
        }()
        guard let ipAddress = ipAddressFromData else {
            throw DecodingError.invalidData
        }
        address = ipAddress
    }
    
    enum DecodingError: Error {
        case invalidData
    }
}

extension Array where Element == LegacyDNSServer {
    var migrated: [DNSServer] {
        return map { $0.migrated }
    }
}

struct LegacyEndpoint: LegacyModel {
    let host: Network.NWEndpoint.Host
    let port: Network.NWEndpoint.Port
    
    var migrated: Endpoint {
        return Endpoint(host: host, port: port)
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let endpointString = try container.decode(String.self)
        guard !endpointString.isEmpty else { throw DecodingError.invalidData }
        let startOfPort: String.Index
        let hostString: String
        if endpointString.first! == "[" {
            // Look for IPv6-style endpoint, like [::1]:80
            let startOfHost = endpointString.index(after: endpointString.startIndex)
            guard let endOfHost = endpointString.dropFirst().firstIndex(of: "]") else { throw DecodingError.invalidData }
            let afterEndOfHost = endpointString.index(after: endOfHost)
            guard endpointString[afterEndOfHost] == ":" else { throw DecodingError.invalidData }
            startOfPort = endpointString.index(after: afterEndOfHost)
            hostString = String(endpointString[startOfHost ..< endOfHost])
        } else {
            // Look for an IPv4-style endpoint, like 127.0.0.1:80
            guard let endOfHost = endpointString.firstIndex(of: ":") else { throw DecodingError.invalidData }
            startOfPort = endpointString.index(after: endOfHost)
            hostString = String(endpointString[endpointString.startIndex ..< endOfHost])
        }
        guard let endpointPort = NWEndpoint.Port(String(endpointString[startOfPort ..< endpointString.endIndex])) else { throw DecodingError.invalidData }
        let invalidCharacterIndex = hostString.unicodeScalars.firstIndex { char in
            return !CharacterSet.urlHostAllowed.contains(char)
        }
        guard invalidCharacterIndex == nil else { throw DecodingError.invalidData }
        host = NWEndpoint.Host(hostString)
        port = endpointPort
    }
    
    enum DecodingError: Error {
        case invalidData
    }
}

struct LegacyInterfaceConfiguration: LegacyModel {
    let name: String
    let privateKey: Data
    let addresses: [LegacyIPAddressRange]
    let listenPort: UInt16?
    let mtu: UInt16?
    let dns: [LegacyDNSServer]
    
    var migrated: InterfaceConfiguration {
        var interface = InterfaceConfiguration(name: name, privateKey: privateKey)
        interface.addresses = addresses.migrated
        interface.listenPort = listenPort
        interface.mtu = mtu
        interface.dns = dns.migrated
        return interface
    }
}

struct LegacyIPAddressRange: LegacyModel {
    let address: IPAddress
    let networkPrefixLength: UInt8
    
    var migrated: IPAddressRange {
        return IPAddressRange(address: address, networkPrefixLength: networkPrefixLength)
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        var data = try container.decode(Data.self)
        networkPrefixLength = data.removeLast()
        let ipAddressFromData: IPAddress? = {
            switch data.count {
            case 4: return IPv4Address(data)
            case 16: return IPv6Address(data)
            default: return nil
            }
        }()
        guard let ipAddress = ipAddressFromData else { throw DecodingError.invalidData }
        address = ipAddress
    }
    
    enum DecodingError: Error {
        case invalidData
    }
}

extension Array where Element == LegacyIPAddressRange {
    var migrated: [IPAddressRange] {
        return map { $0.migrated }
    }
}

struct LegacyPeerConfiguration: LegacyModel {
    let publicKey: Data
    let preSharedKey: Data?
    let allowedIPs: [LegacyIPAddressRange]
    let endpoint: LegacyEndpoint?
    let persistentKeepAlive: UInt16?
    
    var migrated: PeerConfiguration {
        var configuration = PeerConfiguration(publicKey: publicKey)
        configuration.preSharedKey = preSharedKey
        configuration.allowedIPs = allowedIPs.migrated
        configuration.endpoint = endpoint?.migrated
        configuration.persistentKeepAlive = persistentKeepAlive
        return configuration
    }
}

extension Array where Element == LegacyPeerConfiguration {
    var migrated: [PeerConfiguration] {
        return map { $0.migrated }
    }
}

final class LegacyTunnelConfiguration: LegacyModel {
    let interface: LegacyInterfaceConfiguration
    let peers: [LegacyPeerConfiguration]
    
    var migrated: TunnelConfiguration {
        return TunnelConfiguration(interface: interface.migrated, peers: peers.migrated)
    }
}

extension NETunnelProviderProtocol {
    
    @discardableResult
    func migrateConfigurationIfNeeded() -> Bool {
        guard let configurationVersion = providerConfiguration?["tunnelConfigurationVersion"] as? Int else { return false }
        if configurationVersion == 1 {
            migrateFromConfigurationV1()
        } else {
            fatalError("No migration from configuration version \(configurationVersion) exists.")
        }
        return true
    }
    
    private func migrateFromConfigurationV1() {
        guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return }
        guard let configuration = try? JSONDecoder().decode(LegacyTunnelConfiguration.self, from: serializedTunnelConfiguration) else { return }        
        providerConfiguration = [Keys.wgQuickConfig.rawValue: configuration.migrated.asWgQuickConfig()]
    }
    
}