diff options
Diffstat (limited to 'WireGuard/Shared')
8 files changed, 196 insertions, 255 deletions
diff --git a/WireGuard/Shared/LegacyConfigMigration.swift b/WireGuard/Shared/LegacyConfigMigration.swift new file mode 100644 index 0000000..bd22ca4 --- /dev/null +++ b/WireGuard/Shared/LegacyConfigMigration.swift @@ -0,0 +1,196 @@ +// 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 } + guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return } + guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return } + + providerConfiguration = [Keys.wgQuickConfig.rawValue: tunnelConfigDictionary] + } + +} diff --git a/WireGuard/Shared/Model/Legacy/LegacyDNSServer.swift b/WireGuard/Shared/Model/Legacy/LegacyDNSServer.swift deleted file mode 100644 index f2afcd7..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyDNSServer.swift +++ /dev/null @@ -1,46 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation -import Network - -struct LegacyDNSServer: Codable { - let address: IPAddress - - 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 - } - - func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - try container.encode(address.rawValue) - } - - enum DecodingError: Error { - case invalidData - } -} - -extension LegacyDNSServer { - var migrated: DNSServer { - return DNSServer(address: address) - } -} - -extension Array where Element == LegacyDNSServer { - var migrated: [DNSServer] { - return map { $0.migrated } - } -} diff --git a/WireGuard/Shared/Model/Legacy/LegacyEndpoint.swift b/WireGuard/Shared/Model/Legacy/LegacyEndpoint.swift deleted file mode 100644 index 7a80be4..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyEndpoint.swift +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation -import Network - -struct LegacyEndpoint: Codable { - let host: NWEndpoint.Host - let port: NWEndpoint.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 - } - - public func encode(to encoder: Encoder) throws { - let stringRepresentation: String - switch host { - case .name(let hostname, _): - stringRepresentation = "\(hostname):\(port)" - case .ipv4(let address): - stringRepresentation = "\(address):\(port)" - case .ipv6(let address): - stringRepresentation = "[\(address)]:\(port)" - } - - var container = encoder.singleValueContainer() - try container.encode(stringRepresentation) - } - - enum DecodingError: Error { - case invalidData - } -} - -extension LegacyEndpoint { - var migrated: Endpoint { - return Endpoint(host: host, port: port) - } -} diff --git a/WireGuard/Shared/Model/Legacy/LegacyIPAddressRange.swift b/WireGuard/Shared/Model/Legacy/LegacyIPAddressRange.swift deleted file mode 100644 index ade87f2..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyIPAddressRange.swift +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation -import Network - -struct LegacyIPAddressRange: Codable { - let address: IPAddress - let networkPrefixLength: UInt8 - - 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 - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - let addressDataLength: Int - if address is IPv4Address { - addressDataLength = 4 - } else if address is IPv6Address { - addressDataLength = 16 - } else { - fatalError() - } - var data = Data(capacity: addressDataLength + 1) - data.append(address.rawValue) - data.append(networkPrefixLength) - try container.encode(data) - } - - enum DecodingError: Error { - case invalidData - } -} - -extension LegacyIPAddressRange { - var migrated: IPAddressRange { - return IPAddressRange(address: address, networkPrefixLength: networkPrefixLength) - } -} - -extension Array where Element == LegacyIPAddressRange { - var migrated: [IPAddressRange] { - return map { $0.migrated } - } -} diff --git a/WireGuard/Shared/Model/Legacy/LegacyInterfaceConfiguration.swift b/WireGuard/Shared/Model/Legacy/LegacyInterfaceConfiguration.swift deleted file mode 100644 index 680c8d7..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyInterfaceConfiguration.swift +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation - -struct LegacyInterfaceConfiguration: Codable { - let name: String - let privateKey: Data - let addresses: [LegacyIPAddressRange] - let listenPort: UInt16? - let mtu: UInt16? - let dns: [LegacyDNSServer] -} - -extension LegacyInterfaceConfiguration { - 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 - } -} diff --git a/WireGuard/Shared/Model/Legacy/LegacyPeerConfiguration.swift b/WireGuard/Shared/Model/Legacy/LegacyPeerConfiguration.swift deleted file mode 100644 index 5a61b4a..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyPeerConfiguration.swift +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation - -struct LegacyPeerConfiguration: Codable { - let publicKey: Data - let preSharedKey: Data? - let allowedIPs: [LegacyIPAddressRange] - let endpoint: LegacyEndpoint? - let persistentKeepAlive: UInt16? -} - -extension LegacyPeerConfiguration { - 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 } - } -} diff --git a/WireGuard/Shared/Model/Legacy/LegacyTunnelConfiguration.swift b/WireGuard/Shared/Model/Legacy/LegacyTunnelConfiguration.swift deleted file mode 100644 index ac369b9..0000000 --- a/WireGuard/Shared/Model/Legacy/LegacyTunnelConfiguration.swift +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright © 2018 WireGuard LLC. All Rights Reserved. - -import Foundation - -final class LegacyTunnelConfiguration: Codable { - let interface: LegacyInterfaceConfiguration - let peers: [LegacyPeerConfiguration] -} - -extension LegacyTunnelConfiguration { - var migrated: TunnelConfiguration { - return TunnelConfiguration(interface: interface.migrated, peers: peers.migrated) - } -} diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift index 7d6e412..6f4e3eb 100644 --- a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift +++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift @@ -52,24 +52,4 @@ extension NETunnelProviderProtocol { return serializedThisTunnelConfiguration == serializedOtherTunnelConfiguration } - @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 } - guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return } - guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return } - - providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ] - } - } |