2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
2018-10-23 09:53:18 +00:00
|
|
|
|
2018-12-22 11:38:55 +00:00
|
|
|
import Foundation
|
2018-10-23 09:53:18 +00:00
|
|
|
|
|
|
|
class TunnelViewModel {
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
enum InterfaceField: CaseIterable {
|
2018-12-18 11:00:16 +00:00
|
|
|
case name
|
|
|
|
case privateKey
|
|
|
|
case publicKey
|
|
|
|
case generateKeyPair
|
|
|
|
case addresses
|
|
|
|
case listenPort
|
|
|
|
case mtu
|
|
|
|
case dns
|
2019-03-16 10:55:17 +00:00
|
|
|
case status
|
2019-03-17 11:08:07 +00:00
|
|
|
case toggleStatus
|
2018-12-18 11:00:16 +00:00
|
|
|
|
|
|
|
var localizedUIString: String {
|
|
|
|
switch self {
|
|
|
|
case .name: return tr("tunnelInterfaceName")
|
|
|
|
case .privateKey: return tr("tunnelInterfacePrivateKey")
|
|
|
|
case .publicKey: return tr("tunnelInterfacePublicKey")
|
|
|
|
case .generateKeyPair: return tr("tunnelInterfaceGenerateKeypair")
|
|
|
|
case .addresses: return tr("tunnelInterfaceAddresses")
|
|
|
|
case .listenPort: return tr("tunnelInterfaceListenPort")
|
|
|
|
case .mtu: return tr("tunnelInterfaceMTU")
|
|
|
|
case .dns: return tr("tunnelInterfaceDNS")
|
2019-03-16 10:55:17 +00:00
|
|
|
case .status: return tr("tunnelInterfaceStatus")
|
2019-03-17 11:08:07 +00:00
|
|
|
case .toggleStatus: return ""
|
2018-12-18 11:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 09:55:30 +00:00
|
|
|
static let interfaceFieldsWithControl: Set<InterfaceField> = [
|
2018-10-31 00:00:27 +00:00
|
|
|
.generateKeyPair
|
2018-10-24 09:55:30 +00:00
|
|
|
]
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
enum PeerField: CaseIterable {
|
2018-12-18 11:00:16 +00:00
|
|
|
case publicKey
|
|
|
|
case preSharedKey
|
|
|
|
case endpoint
|
|
|
|
case persistentKeepAlive
|
|
|
|
case allowedIPs
|
2019-01-23 23:00:46 +00:00
|
|
|
case rxBytes
|
|
|
|
case txBytes
|
|
|
|
case lastHandshakeTime
|
2018-12-18 11:00:16 +00:00
|
|
|
case excludePrivateIPs
|
|
|
|
case deletePeer
|
|
|
|
|
|
|
|
var localizedUIString: String {
|
|
|
|
switch self {
|
|
|
|
case .publicKey: return tr("tunnelPeerPublicKey")
|
|
|
|
case .preSharedKey: return tr("tunnelPeerPreSharedKey")
|
|
|
|
case .endpoint: return tr("tunnelPeerEndpoint")
|
|
|
|
case .persistentKeepAlive: return tr("tunnelPeerPersistentKeepalive")
|
|
|
|
case .allowedIPs: return tr("tunnelPeerAllowedIPs")
|
2019-01-23 23:00:46 +00:00
|
|
|
case .rxBytes: return tr("tunnelPeerRxBytes")
|
|
|
|
case .txBytes: return tr("tunnelPeerTxBytes")
|
|
|
|
case .lastHandshakeTime: return tr("tunnelPeerLastHandshakeTime")
|
2018-12-18 11:00:16 +00:00
|
|
|
case .excludePrivateIPs: return tr("tunnelPeerExcludePrivateIPs")
|
|
|
|
case .deletePeer: return tr("deletePeerButtonTitle")
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 09:55:30 +00:00
|
|
|
static let peerFieldsWithControl: Set<PeerField> = [
|
|
|
|
.excludePrivateIPs, .deletePeer
|
|
|
|
]
|
|
|
|
|
2018-10-24 08:41:34 +00:00
|
|
|
static let keyLengthInBase64 = 44
|
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
struct Changes {
|
|
|
|
enum FieldChange: Equatable {
|
2019-02-01 11:36:42 +00:00
|
|
|
case added
|
|
|
|
case removed
|
2019-02-09 21:08:23 +00:00
|
|
|
case modified(newValue: String)
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
2019-02-08 16:05:58 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
var interfaceChanges: [InterfaceField: FieldChange]
|
|
|
|
var peerChanges: [(peerIndex: Int, changes: [PeerField: FieldChange])]
|
|
|
|
var peersRemovedIndices: [Int]
|
|
|
|
var peersInsertedIndices: [Int]
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
class InterfaceData {
|
2018-12-13 03:09:52 +00:00
|
|
|
var scratchpad = [InterfaceField: String]()
|
|
|
|
var fieldsWithError = Set<InterfaceField>()
|
2018-11-03 18:35:25 +00:00
|
|
|
var validatedConfiguration: InterfaceConfiguration?
|
2018-12-21 23:28:18 +00:00
|
|
|
var validatedName: String?
|
2018-10-23 09:53:18 +00:00
|
|
|
|
2018-10-23 12:18:07 +00:00
|
|
|
subscript(field: InterfaceField) -> String {
|
2018-10-23 09:53:18 +00:00
|
|
|
get {
|
2018-12-12 18:28:27 +00:00
|
|
|
if scratchpad.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
return scratchpad[field] ?? ""
|
|
|
|
}
|
|
|
|
set(stringValue) {
|
2018-12-12 18:28:27 +00:00
|
|
|
if scratchpad.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
validatedConfiguration = nil
|
2018-12-21 23:28:18 +00:00
|
|
|
validatedName = nil
|
2018-12-12 18:28:27 +00:00
|
|
|
if stringValue.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
scratchpad.removeValue(forKey: field)
|
|
|
|
} else {
|
|
|
|
scratchpad[field] = stringValue
|
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
if field == .privateKey {
|
2020-11-26 16:23:50 +00:00
|
|
|
if stringValue.count == TunnelViewModel.keyLengthInBase64,
|
|
|
|
let privateKey = PrivateKey(base64Key: stringValue) {
|
|
|
|
scratchpad[.publicKey] = privateKey.publicKey.base64Key
|
2018-10-24 08:41:34 +00:00
|
|
|
} else {
|
|
|
|
scratchpad.removeValue(forKey: .publicKey)
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateScratchpad() {
|
|
|
|
guard let config = validatedConfiguration else { return }
|
2018-12-21 23:28:18 +00:00
|
|
|
guard let name = validatedName else { return }
|
2019-02-01 11:36:42 +00:00
|
|
|
scratchpad = TunnelViewModel.InterfaceData.createScratchPad(from: config, name: name)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func createScratchPad(from config: InterfaceConfiguration, name: String) -> [InterfaceField: String] {
|
|
|
|
var scratchpad = [InterfaceField: String]()
|
2018-12-21 23:28:18 +00:00
|
|
|
scratchpad[.name] = name
|
2020-11-26 16:23:50 +00:00
|
|
|
scratchpad[.privateKey] = config.privateKey.base64Key
|
|
|
|
scratchpad[.publicKey] = config.privateKey.publicKey.base64Key
|
2018-12-12 18:28:27 +00:00
|
|
|
if !config.addresses.isEmpty {
|
2018-12-21 04:52:45 +00:00
|
|
|
scratchpad[.addresses] = config.addresses.map { $0.stringRepresentation }.joined(separator: ", ")
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
if let listenPort = config.listenPort {
|
|
|
|
scratchpad[.listenPort] = String(listenPort)
|
|
|
|
}
|
|
|
|
if let mtu = config.mtu {
|
|
|
|
scratchpad[.mtu] = String(mtu)
|
|
|
|
}
|
2020-12-15 12:49:21 +00:00
|
|
|
if !config.dns.isEmpty || !config.dnsSearch.isEmpty {
|
|
|
|
var dns = config.dns.map { $0.stringRepresentation }
|
|
|
|
dns.append(contentsOf: config.dnsSearch)
|
|
|
|
scratchpad[.dns] = dns.joined(separator: ", ")
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
return scratchpad
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
func save() -> SaveResult<(String, InterfaceConfiguration)> {
|
|
|
|
if let config = validatedConfiguration, let name = validatedName {
|
|
|
|
return .saved((name, config))
|
2018-11-05 17:25:09 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
fieldsWithError.removeAll()
|
2018-11-03 10:15:29 +00:00
|
|
|
guard let name = scratchpad[.name]?.trimmingCharacters(in: .whitespacesAndNewlines), (!name.isEmpty) else {
|
2018-10-23 09:53:18 +00:00
|
|
|
fieldsWithError.insert(.name)
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidInterfaceMessageNameRequired"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
guard let privateKeyString = scratchpad[.privateKey] else {
|
|
|
|
fieldsWithError.insert(.privateKey)
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidInterfaceMessagePrivateKeyRequired"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2020-11-26 16:23:50 +00:00
|
|
|
guard let privateKey = PrivateKey(base64Key: privateKeyString) else {
|
2018-10-23 09:53:18 +00:00
|
|
|
fieldsWithError.insert(.privateKey)
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidInterfaceMessagePrivateKeyInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2018-12-21 23:28:18 +00:00
|
|
|
var config = InterfaceConfiguration(privateKey: privateKey)
|
2018-12-13 03:09:52 +00:00
|
|
|
var errorMessages = [String]()
|
2018-10-23 09:53:18 +00:00
|
|
|
if let addressesString = scratchpad[.addresses] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var addresses = [IPAddressRange]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for addressString in addressesString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
|
|
|
if let address = IPAddressRange(from: addressString) {
|
2018-10-23 09:53:18 +00:00
|
|
|
addresses.append(address)
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.addresses)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidInterfaceMessageAddressInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
config.addresses = addresses
|
|
|
|
}
|
|
|
|
if let listenPortString = scratchpad[.listenPort] {
|
2018-10-23 10:21:19 +00:00
|
|
|
if let listenPort = UInt16(listenPortString) {
|
2018-10-23 09:53:18 +00:00
|
|
|
config.listenPort = listenPort
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.listenPort)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidInterfaceMessageListenPortInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let mtuString = scratchpad[.mtu] {
|
2018-11-01 17:59:58 +00:00
|
|
|
if let mtu = UInt16(mtuString), mtu >= 576 {
|
2018-10-23 09:53:18 +00:00
|
|
|
config.mtu = mtu
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.mtu)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidInterfaceMessageMTUInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let dnsString = scratchpad[.dns] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var dnsServers = [DNSServer]()
|
2020-12-15 12:49:21 +00:00
|
|
|
var dnsSearch = [String]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for dnsServerString in dnsString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
|
|
|
if let dnsServer = DNSServer(from: dnsServerString) {
|
2018-10-23 10:58:24 +00:00
|
|
|
dnsServers.append(dnsServer)
|
|
|
|
} else {
|
2020-12-15 12:49:21 +00:00
|
|
|
dnsSearch.append(dnsServerString)
|
2018-10-23 10:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
config.dns = dnsServers
|
2020-12-15 12:49:21 +00:00
|
|
|
config.dnsSearch = dnsSearch
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
guard errorMessages.isEmpty else { return .error(errorMessages.first!) }
|
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
validatedConfiguration = config
|
2018-12-21 23:28:18 +00:00
|
|
|
validatedName = name
|
|
|
|
return .saved((name, config))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2018-10-24 09:55:30 +00:00
|
|
|
|
|
|
|
func filterFieldsWithValueOrControl(interfaceFields: [InterfaceField]) -> [InterfaceField] {
|
2018-12-12 21:33:14 +00:00
|
|
|
return interfaceFields.filter { field in
|
2018-12-12 18:28:27 +00:00
|
|
|
if TunnelViewModel.interfaceFieldsWithControl.contains(field) {
|
2018-10-24 09:55:30 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-12-20 16:46:26 +00:00
|
|
|
return !self[field].isEmpty
|
2018-10-24 09:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
func applyConfiguration(other: InterfaceConfiguration, otherName: String) -> [InterfaceField: Changes.FieldChange] {
|
2019-02-01 11:36:42 +00:00
|
|
|
if scratchpad.isEmpty {
|
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
let otherScratchPad = InterfaceData.createScratchPad(from: other, name: otherName)
|
2019-02-09 21:08:23 +00:00
|
|
|
var changes = [InterfaceField: Changes.FieldChange]()
|
2019-02-01 11:36:42 +00:00
|
|
|
for field in InterfaceField.allCases {
|
|
|
|
switch (scratchpad[field] ?? "", otherScratchPad[field] ?? "") {
|
|
|
|
case ("", ""):
|
|
|
|
break
|
|
|
|
case ("", _):
|
|
|
|
changes[field] = .added
|
|
|
|
case (_, ""):
|
|
|
|
changes[field] = .removed
|
|
|
|
case (let this, let other):
|
|
|
|
if this != other {
|
2019-02-09 21:08:23 +00:00
|
|
|
changes[field] = .modified(newValue: other)
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scratchpad = otherScratchPad
|
2019-02-09 21:08:23 +00:00
|
|
|
return changes
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class PeerData {
|
|
|
|
var index: Int
|
2018-12-13 03:09:52 +00:00
|
|
|
var scratchpad = [PeerField: String]()
|
|
|
|
var fieldsWithError = Set<PeerField>()
|
2018-11-03 18:35:25 +00:00
|
|
|
var validatedConfiguration: PeerConfiguration?
|
2020-11-26 16:23:50 +00:00
|
|
|
var publicKey: PublicKey? {
|
2019-02-01 11:36:42 +00:00
|
|
|
if let validatedConfiguration = validatedConfiguration {
|
|
|
|
return validatedConfiguration.publicKey
|
|
|
|
}
|
|
|
|
if let scratchPadPublicKey = scratchpad[.publicKey] {
|
2020-11-26 16:23:50 +00:00
|
|
|
return PublicKey(base64Key: scratchPadPublicKey)
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
private(set) var shouldAllowExcludePrivateIPsControl = false
|
2018-12-22 00:55:42 +00:00
|
|
|
private(set) var shouldStronglyRecommendDNS = false
|
2018-12-13 03:09:52 +00:00
|
|
|
private(set) var excludePrivateIPsValue = false
|
|
|
|
fileprivate var numberOfPeers = 0
|
2018-10-29 09:38:26 +00:00
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
init(index: Int) {
|
|
|
|
self.index = index
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:18:07 +00:00
|
|
|
subscript(field: PeerField) -> String {
|
2018-10-23 09:53:18 +00:00
|
|
|
get {
|
2018-12-12 18:28:27 +00:00
|
|
|
if scratchpad.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
return scratchpad[field] ?? ""
|
|
|
|
}
|
|
|
|
set(stringValue) {
|
2018-12-12 18:28:27 +00:00
|
|
|
if scratchpad.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
validatedConfiguration = nil
|
2018-12-12 18:28:27 +00:00
|
|
|
if stringValue.isEmpty {
|
2018-10-23 09:53:18 +00:00
|
|
|
scratchpad.removeValue(forKey: field)
|
|
|
|
} else {
|
|
|
|
scratchpad[field] = stringValue
|
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
if field == .allowedIPs {
|
2018-10-29 09:38:26 +00:00
|
|
|
updateExcludePrivateIPsFieldState()
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateScratchpad() {
|
|
|
|
guard let config = validatedConfiguration else { return }
|
2019-02-01 11:36:42 +00:00
|
|
|
scratchpad = TunnelViewModel.PeerData.createScratchPad(from: config)
|
|
|
|
updateExcludePrivateIPsFieldState()
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func createScratchPad(from config: PeerConfiguration) -> [PeerField: String] {
|
|
|
|
var scratchpad = [PeerField: String]()
|
2020-11-26 16:23:50 +00:00
|
|
|
scratchpad[.publicKey] = config.publicKey.base64Key
|
|
|
|
if let preSharedKey = config.preSharedKey?.base64Key {
|
2019-02-07 23:44:14 +00:00
|
|
|
scratchpad[.preSharedKey] = preSharedKey
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
if !config.allowedIPs.isEmpty {
|
2018-12-21 04:52:45 +00:00
|
|
|
scratchpad[.allowedIPs] = config.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ")
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
if let endpoint = config.endpoint {
|
2018-12-21 04:52:45 +00:00
|
|
|
scratchpad[.endpoint] = endpoint.stringRepresentation
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
if let persistentKeepAlive = config.persistentKeepAlive {
|
2019-01-31 12:47:46 +00:00
|
|
|
scratchpad[.persistentKeepAlive] = String(persistentKeepAlive)
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2019-01-23 23:00:46 +00:00
|
|
|
if let rxBytes = config.rxBytes {
|
2019-01-28 10:52:20 +00:00
|
|
|
scratchpad[.rxBytes] = prettyBytes(rxBytes)
|
2019-01-23 23:00:46 +00:00
|
|
|
}
|
|
|
|
if let txBytes = config.txBytes {
|
2019-01-28 10:52:20 +00:00
|
|
|
scratchpad[.txBytes] = prettyBytes(txBytes)
|
2019-01-23 23:00:46 +00:00
|
|
|
}
|
|
|
|
if let lastHandshakeTime = config.lastHandshakeTime {
|
2019-01-28 10:52:20 +00:00
|
|
|
scratchpad[.lastHandshakeTime] = prettyTimeAgo(timestamp: lastHandshakeTime)
|
2019-01-23 23:00:46 +00:00
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
return scratchpad
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func save() -> SaveResult<PeerConfiguration> {
|
2018-11-05 17:25:09 +00:00
|
|
|
if let validatedConfiguration = validatedConfiguration {
|
|
|
|
return .saved(validatedConfiguration)
|
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
fieldsWithError.removeAll()
|
|
|
|
guard let publicKeyString = scratchpad[.publicKey] else {
|
|
|
|
fieldsWithError.insert(.publicKey)
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidPeerMessagePublicKeyRequired"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2020-11-26 16:23:50 +00:00
|
|
|
guard let publicKey = PublicKey(base64Key: publicKeyString) else {
|
2018-10-23 09:53:18 +00:00
|
|
|
fieldsWithError.insert(.publicKey)
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidPeerMessagePublicKeyInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
var config = PeerConfiguration(publicKey: publicKey)
|
2018-12-13 03:09:52 +00:00
|
|
|
var errorMessages = [String]()
|
2018-10-23 09:53:18 +00:00
|
|
|
if let preSharedKeyString = scratchpad[.preSharedKey] {
|
2020-11-26 16:23:50 +00:00
|
|
|
if let preSharedKey = PreSharedKey(base64Key: preSharedKeyString) {
|
2018-10-23 09:53:18 +00:00
|
|
|
config.preSharedKey = preSharedKey
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.preSharedKey)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidPeerMessagePreSharedKeyInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let allowedIPsString = scratchpad[.allowedIPs] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var allowedIPs = [IPAddressRange]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for allowedIPString in allowedIPsString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
|
|
|
if let allowedIP = IPAddressRange(from: allowedIPString) {
|
2018-10-23 09:53:18 +00:00
|
|
|
allowedIPs.append(allowedIP)
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.allowedIPs)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidPeerMessageAllowedIPsInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
config.allowedIPs = allowedIPs
|
|
|
|
}
|
|
|
|
if let endpointString = scratchpad[.endpoint] {
|
|
|
|
if let endpoint = Endpoint(from: endpointString) {
|
|
|
|
config.endpoint = endpoint
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.endpoint)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidPeerMessageEndpointInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let persistentKeepAliveString = scratchpad[.persistentKeepAlive] {
|
2018-10-23 10:21:19 +00:00
|
|
|
if let persistentKeepAlive = UInt16(persistentKeepAliveString) {
|
2018-10-23 09:53:18 +00:00
|
|
|
config.persistentKeepAlive = persistentKeepAlive
|
|
|
|
} else {
|
|
|
|
fieldsWithError.insert(.persistentKeepAlive)
|
2018-12-18 11:00:16 +00:00
|
|
|
errorMessages.append(tr("alertInvalidPeerMessagePersistentKeepaliveInvalid"))
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
guard errorMessages.isEmpty else { return .error(errorMessages.first!) }
|
2018-12-12 21:33:14 +00:00
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
validatedConfiguration = config
|
|
|
|
return .saved(config)
|
|
|
|
}
|
2018-10-24 09:55:30 +00:00
|
|
|
|
|
|
|
func filterFieldsWithValueOrControl(peerFields: [PeerField]) -> [PeerField] {
|
2018-12-12 21:33:14 +00:00
|
|
|
return peerFields.filter { field in
|
2018-12-12 18:28:27 +00:00
|
|
|
if TunnelViewModel.peerFieldsWithControl.contains(field) {
|
2018-10-24 09:55:30 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return (!self[field].isEmpty)
|
|
|
|
}
|
|
|
|
}
|
2018-10-29 09:38:26 +00:00
|
|
|
|
|
|
|
static let ipv4DefaultRouteString = "0.0.0.0/0"
|
|
|
|
static let ipv4DefaultRouteModRFC1918String = [ // Set of all non-private IPv4 IPs
|
2020-12-18 11:42:28 +00:00
|
|
|
"1.0.0.0/8", "2.0.0.0/8", "3.0.0.0/8", "4.0.0.0/6", "8.0.0.0/7", "11.0.0.0/8",
|
|
|
|
"12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3", "64.0.0.0/2", "128.0.0.0/3",
|
|
|
|
"160.0.0.0/5", "168.0.0.0/6", "172.0.0.0/12", "172.32.0.0/11", "172.64.0.0/10",
|
|
|
|
"172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7", "176.0.0.0/4", "192.0.0.0/9",
|
|
|
|
"192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16", "192.170.0.0/15",
|
|
|
|
"192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8",
|
|
|
|
"194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4"
|
2018-10-29 09:38:26 +00:00
|
|
|
]
|
|
|
|
|
2019-02-15 10:50:04 +00:00
|
|
|
static func excludePrivateIPsFieldStates(isSinglePeer: Bool, allowedIPs: Set<String>) -> (shouldAllowExcludePrivateIPsControl: Bool, excludePrivateIPsValue: Bool) {
|
|
|
|
guard isSinglePeer else {
|
|
|
|
return (shouldAllowExcludePrivateIPsControl: false, excludePrivateIPsValue: false)
|
|
|
|
}
|
|
|
|
let allowedIPStrings = Set<String>(allowedIPs)
|
|
|
|
if allowedIPStrings.contains(TunnelViewModel.PeerData.ipv4DefaultRouteString) {
|
|
|
|
return (shouldAllowExcludePrivateIPsControl: true, excludePrivateIPsValue: false)
|
|
|
|
} else if allowedIPStrings.isSuperset(of: TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String) {
|
|
|
|
return (shouldAllowExcludePrivateIPsControl: true, excludePrivateIPsValue: true)
|
|
|
|
} else {
|
|
|
|
return (shouldAllowExcludePrivateIPsControl: false, excludePrivateIPsValue: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 09:38:26 +00:00
|
|
|
func updateExcludePrivateIPsFieldState() {
|
2018-12-22 00:55:42 +00:00
|
|
|
if scratchpad.isEmpty {
|
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
let allowedIPStrings = Set<String>(scratchpad[.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines))
|
2019-02-15 10:50:04 +00:00
|
|
|
(shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: numberOfPeers == 1, allowedIPs: allowedIPStrings)
|
2018-12-22 00:55:42 +00:00
|
|
|
shouldStronglyRecommendDNS = allowedIPStrings.contains(TunnelViewModel.PeerData.ipv4DefaultRouteString) || allowedIPStrings.isSuperset(of: TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String)
|
2019-02-15 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:27:31 +00:00
|
|
|
static func normalizedIPAddressRangeStrings(_ list: [String]) -> [String] {
|
|
|
|
return list.compactMap { IPAddressRange(from: $0) }.map { $0.stringRepresentation }
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:47:23 +00:00
|
|
|
static func modifiedAllowedIPs(currentAllowedIPs: [String], excludePrivateIPs: Bool, dnsServers: [String], oldDNSServers: [String]?) -> [String] {
|
2019-02-16 14:27:31 +00:00
|
|
|
let normalizedDNSServers = normalizedIPAddressRangeStrings(dnsServers)
|
2019-02-21 12:47:23 +00:00
|
|
|
let normalizedOldDNSServers = oldDNSServers == nil ? normalizedDNSServers : normalizedIPAddressRangeStrings(oldDNSServers!)
|
2019-02-16 14:27:31 +00:00
|
|
|
let ipv6Addresses = normalizedIPAddressRangeStrings(currentAllowedIPs.filter { $0.contains(":") })
|
2019-02-15 10:50:04 +00:00
|
|
|
if excludePrivateIPs {
|
2019-02-21 12:26:52 +00:00
|
|
|
return ipv6Addresses + TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String + normalizedDNSServers
|
2018-10-29 09:38:26 +00:00
|
|
|
} else {
|
2019-02-21 12:47:23 +00:00
|
|
|
return ipv6Addresses.filter { !normalizedOldDNSServers.contains($0) } + [TunnelViewModel.PeerData.ipv4DefaultRouteString]
|
2018-10-29 09:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:47:23 +00:00
|
|
|
func excludePrivateIPsValueChanged(isOn: Bool, dnsServers: String, oldDNSServers: String? = nil) {
|
2018-12-20 16:46:26 +00:00
|
|
|
let allowedIPStrings = scratchpad[.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines)
|
|
|
|
let dnsServerStrings = dnsServers.splitToArray(trimmingCharacters: .whitespacesAndNewlines)
|
2019-02-21 12:47:23 +00:00
|
|
|
let oldDNSServerStrings = oldDNSServers?.splitToArray(trimmingCharacters: .whitespacesAndNewlines)
|
|
|
|
let modifiedAllowedIPStrings = TunnelViewModel.PeerData.modifiedAllowedIPs(currentAllowedIPs: allowedIPStrings, excludePrivateIPs: isOn, dnsServers: dnsServerStrings, oldDNSServers: oldDNSServerStrings)
|
2018-10-29 09:38:26 +00:00
|
|
|
scratchpad[.allowedIPs] = modifiedAllowedIPStrings.joined(separator: ", ")
|
2018-12-22 19:01:20 +00:00
|
|
|
validatedConfiguration = nil
|
2018-10-29 09:38:26 +00:00
|
|
|
excludePrivateIPsValue = isOn
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
func applyConfiguration(other: PeerConfiguration) -> [PeerField: Changes.FieldChange] {
|
2019-02-01 11:36:42 +00:00
|
|
|
if scratchpad.isEmpty {
|
|
|
|
populateScratchpad()
|
|
|
|
}
|
|
|
|
let otherScratchPad = PeerData.createScratchPad(from: other)
|
2019-02-09 21:08:23 +00:00
|
|
|
var changes = [PeerField: Changes.FieldChange]()
|
2019-02-01 11:36:42 +00:00
|
|
|
for field in PeerField.allCases {
|
|
|
|
switch (scratchpad[field] ?? "", otherScratchPad[field] ?? "") {
|
|
|
|
case ("", ""):
|
|
|
|
break
|
|
|
|
case ("", _):
|
|
|
|
changes[field] = .added
|
|
|
|
case (_, ""):
|
|
|
|
changes[field] = .removed
|
|
|
|
case (let this, let other):
|
|
|
|
if this != other {
|
2019-02-09 21:08:23 +00:00
|
|
|
changes[field] = .modified(newValue: other)
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scratchpad = otherScratchPad
|
2019-02-09 21:08:23 +00:00
|
|
|
return changes
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum SaveResult<Configuration> {
|
|
|
|
case saved(Configuration)
|
2018-12-18 11:00:16 +00:00
|
|
|
case error(String)
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
private(set) var interfaceData: InterfaceData
|
|
|
|
private(set) var peersData: [PeerData]
|
2018-10-23 09:53:18 +00:00
|
|
|
|
|
|
|
init(tunnelConfiguration: TunnelConfiguration?) {
|
2018-12-20 17:22:37 +00:00
|
|
|
let interfaceData = InterfaceData()
|
2018-12-13 03:09:52 +00:00
|
|
|
var peersData = [PeerData]()
|
2018-10-23 09:53:18 +00:00
|
|
|
if let tunnelConfiguration = tunnelConfiguration {
|
|
|
|
interfaceData.validatedConfiguration = tunnelConfiguration.interface
|
2018-12-21 23:28:18 +00:00
|
|
|
interfaceData.validatedName = tunnelConfiguration.name
|
2018-12-12 17:40:57 +00:00
|
|
|
for (index, peerConfiguration) in tunnelConfiguration.peers.enumerated() {
|
|
|
|
let peerData = PeerData(index: index)
|
2018-10-23 09:53:18 +00:00
|
|
|
peerData.validatedConfiguration = peerConfiguration
|
|
|
|
peersData.append(peerData)
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 06:35:40 +00:00
|
|
|
let numberOfPeers = peersData.count
|
|
|
|
for peerData in peersData {
|
|
|
|
peerData.numberOfPeers = numberOfPeers
|
2018-11-02 07:39:45 +00:00
|
|
|
peerData.updateExcludePrivateIPsFieldState()
|
2018-11-02 06:35:40 +00:00
|
|
|
}
|
2018-11-02 07:39:45 +00:00
|
|
|
self.interfaceData = interfaceData
|
|
|
|
self.peersData = peersData
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendEmptyPeer() {
|
|
|
|
let peer = PeerData(index: peersData.count)
|
|
|
|
peersData.append(peer)
|
2018-12-12 17:40:57 +00:00
|
|
|
for peer in peersData {
|
|
|
|
peer.numberOfPeers = peersData.count
|
|
|
|
peer.updateExcludePrivateIPsFieldState()
|
2018-10-29 09:38:26 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deletePeer(peer: PeerData) {
|
|
|
|
let removedPeer = peersData.remove(at: peer.index)
|
|
|
|
assert(removedPeer.index == peer.index)
|
2018-12-12 17:40:57 +00:00
|
|
|
for peer in peersData[peer.index ..< peersData.count] {
|
|
|
|
assert(peer.index > 0)
|
|
|
|
peer.index -= 1
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
for peer in peersData {
|
|
|
|
peer.numberOfPeers = peersData.count
|
|
|
|
peer.updateExcludePrivateIPsFieldState()
|
2018-10-29 09:38:26 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:26:52 +00:00
|
|
|
func updateDNSServersInAllowedIPsIfRequired(oldDNSServers: String, newDNSServers: String) -> Bool {
|
|
|
|
guard peersData.count == 1, let firstPeer = peersData.first else { return false }
|
|
|
|
guard firstPeer.shouldAllowExcludePrivateIPsControl && firstPeer.excludePrivateIPsValue else { return false }
|
|
|
|
let allowedIPStrings = firstPeer[.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines)
|
|
|
|
let oldDNSServerStrings = TunnelViewModel.PeerData.normalizedIPAddressRangeStrings(oldDNSServers.splitToArray(trimmingCharacters: .whitespacesAndNewlines))
|
|
|
|
let newDNSServerStrings = TunnelViewModel.PeerData.normalizedIPAddressRangeStrings(newDNSServers.splitToArray(trimmingCharacters: .whitespacesAndNewlines))
|
|
|
|
let updatedAllowedIPStrings = allowedIPStrings.filter { !oldDNSServerStrings.contains($0) } + newDNSServerStrings
|
|
|
|
firstPeer[.allowedIPs] = updatedAllowedIPStrings.joined(separator: ", ")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
func save() -> SaveResult<TunnelConfiguration> {
|
|
|
|
let interfaceSaveResult = interfaceData.save()
|
2018-12-22 19:01:20 +00:00
|
|
|
let peerSaveResults = peersData.map { $0.save() } // Save all, to help mark erroring fields in red
|
2018-12-12 18:28:27 +00:00
|
|
|
switch interfaceSaveResult {
|
2018-10-23 09:53:18 +00:00
|
|
|
case .error(let errorMessage):
|
|
|
|
return .error(errorMessage)
|
|
|
|
case .saved(let interfaceConfiguration):
|
2018-12-13 03:09:52 +00:00
|
|
|
var peerConfigurations = [PeerConfiguration]()
|
2018-10-23 09:53:18 +00:00
|
|
|
peerConfigurations.reserveCapacity(peerSaveResults.count)
|
|
|
|
for peerSaveResult in peerSaveResults {
|
2018-12-12 18:28:27 +00:00
|
|
|
switch peerSaveResult {
|
2018-10-23 09:53:18 +00:00
|
|
|
case .error(let errorMessage):
|
|
|
|
return .error(errorMessage)
|
|
|
|
case .saved(let peerConfiguration):
|
|
|
|
peerConfigurations.append(peerConfiguration)
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 02:47:19 +00:00
|
|
|
|
|
|
|
let peerPublicKeysArray = peerConfigurations.map { $0.publicKey }
|
2020-11-26 16:23:50 +00:00
|
|
|
let peerPublicKeysSet = Set<PublicKey>(peerPublicKeysArray)
|
2018-12-12 18:28:27 +00:00
|
|
|
if peerPublicKeysArray.count != peerPublicKeysSet.count {
|
2018-12-18 11:00:16 +00:00
|
|
|
return .error(tr("alertInvalidPeerMessagePublicKeyDuplicated"))
|
2018-11-06 02:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
let tunnelConfiguration = TunnelConfiguration(name: interfaceConfiguration.0, interface: interfaceConfiguration.1, peers: peerConfigurations)
|
2018-10-23 09:53:18 +00:00
|
|
|
return .saved(tunnelConfiguration)
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-16 12:55:17 +00:00
|
|
|
func asWgQuickConfig() -> String? {
|
|
|
|
let saveResult = save()
|
|
|
|
if case .saved(let tunnelConfiguration) = saveResult {
|
|
|
|
return tunnelConfiguration.asWgQuickConfig()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
@discardableResult
|
|
|
|
func applyConfiguration(other: TunnelConfiguration) -> Changes {
|
2019-02-01 11:36:42 +00:00
|
|
|
// Replaces current data with data from other TunnelConfiguration, ignoring any changes in peer ordering.
|
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
let interfaceChanges = interfaceData.applyConfiguration(other: other.interface, otherName: other.name ?? "")
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
var peerChanges = [(peerIndex: Int, changes: [PeerField: Changes.FieldChange])]()
|
2019-02-01 11:36:42 +00:00
|
|
|
for otherPeer in other.peers {
|
|
|
|
if let peersDataIndex = peersData.firstIndex(where: { $0.publicKey == otherPeer.publicKey }) {
|
|
|
|
let peerData = peersData[peersDataIndex]
|
2019-02-09 21:08:23 +00:00
|
|
|
let changes = peerData.applyConfiguration(other: otherPeer)
|
|
|
|
if !changes.isEmpty {
|
|
|
|
peerChanges.append((peerIndex: peersDataIndex, changes: changes))
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var removedPeerIndices = [Int]()
|
|
|
|
for (index, peerData) in peersData.enumerated().reversed() {
|
|
|
|
if let peerPublicKey = peerData.publicKey, !other.peers.contains(where: { $0.publicKey == peerPublicKey}) {
|
|
|
|
removedPeerIndices.append(index)
|
|
|
|
peersData.remove(at: index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addedPeerIndices = [Int]()
|
|
|
|
for otherPeer in other.peers {
|
|
|
|
if !peersData.contains(where: { $0.publicKey == otherPeer.publicKey }) {
|
|
|
|
addedPeerIndices.append(peersData.count)
|
|
|
|
let peerData = PeerData(index: peersData.count)
|
|
|
|
peerData.validatedConfiguration = otherPeer
|
|
|
|
peersData.append(peerData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index, peer) in peersData.enumerated() {
|
|
|
|
peer.index = index
|
|
|
|
peer.numberOfPeers = peersData.count
|
|
|
|
peer.updateExcludePrivateIPsFieldState()
|
|
|
|
}
|
2019-02-09 21:08:23 +00:00
|
|
|
|
|
|
|
return Changes(interfaceChanges: interfaceChanges, peerChanges: peerChanges, peersRemovedIndices: removedPeerIndices, peersInsertedIndices: addedPeerIndices)
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
2018-10-23 09:53:18 +00:00
|
|
|
}
|
2018-11-12 10:22:28 +00:00
|
|
|
|
2019-01-28 10:52:20 +00:00
|
|
|
private func prettyBytes(_ bytes: UInt64) -> String {
|
|
|
|
switch bytes {
|
|
|
|
case 0..<1024:
|
|
|
|
return "\(bytes) B"
|
|
|
|
case 1024 ..< (1024 * 1024):
|
|
|
|
return String(format: "%.2f", Double(bytes) / 1024) + " KiB"
|
|
|
|
case 1024 ..< (1024 * 1024 * 1024):
|
|
|
|
return String(format: "%.2f", Double(bytes) / (1024 * 1024)) + " MiB"
|
|
|
|
case 1024 ..< (1024 * 1024 * 1024 * 1024):
|
|
|
|
return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024)) + " GiB"
|
|
|
|
default:
|
|
|
|
return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024 * 1024)) + " TiB"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func prettyTimeAgo(timestamp: Date) -> String {
|
|
|
|
let now = Date()
|
|
|
|
let timeInterval = Int64(now.timeIntervalSince(timestamp))
|
|
|
|
switch timeInterval {
|
|
|
|
case ..<0: return tr("tunnelHandshakeTimestampSystemClockBackward")
|
|
|
|
case 0: return tr("tunnelHandshakeTimestampNow")
|
|
|
|
default:
|
|
|
|
return tr(format: "tunnelHandshakeTimestampAgo (%@)", prettyTime(secondsLeft: timeInterval))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func prettyTime(secondsLeft: Int64) -> String {
|
|
|
|
var left = secondsLeft
|
|
|
|
var timeStrings = [String]()
|
|
|
|
let years = left / (365 * 24 * 60 * 60)
|
|
|
|
left = left % (365 * 24 * 60 * 60)
|
|
|
|
let days = left / (24 * 60 * 60)
|
|
|
|
left = left % (24 * 60 * 60)
|
|
|
|
let hours = left / (60 * 60)
|
|
|
|
left = left % (60 * 60)
|
|
|
|
let minutes = left / 60
|
|
|
|
let seconds = left % 60
|
2019-02-07 22:44:10 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
if years > 0 {
|
|
|
|
return years == 1 ? tr(format: "tunnelHandshakeTimestampYear (%d)", years) : tr(format: "tunnelHandshakeTimestampYears (%d)", years)
|
|
|
|
}
|
|
|
|
if days > 0 {
|
|
|
|
return days == 1 ? tr(format: "tunnelHandshakeTimestampDay (%d)", days) : tr(format: "tunnelHandshakeTimestampDays (%d)", days)
|
|
|
|
}
|
|
|
|
if hours > 0 {
|
|
|
|
let hhmmss = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
|
|
|
|
return tr(format: "tunnelHandshakeTimestampHours hh:mm:ss (%@)", hhmmss)
|
|
|
|
}
|
|
|
|
if minutes > 0 {
|
|
|
|
let mmss = String(format: "%02d:%02d", minutes, seconds)
|
|
|
|
return tr(format: "tunnelHandshakeTimestampMinutes mm:ss (%@)", mmss)
|
|
|
|
}
|
|
|
|
return seconds == 1 ? tr(format: "tunnelHandshakeTimestampSecond (%d)", seconds) : tr(format: "tunnelHandshakeTimestampSeconds (%d)", seconds)
|
|
|
|
#elseif os(macOS)
|
2019-01-28 10:52:20 +00:00
|
|
|
if years > 0 {
|
|
|
|
timeStrings.append(years == 1 ? tr(format: "tunnelHandshakeTimestampYear (%d)", years) : tr(format: "tunnelHandshakeTimestampYears (%d)", years))
|
|
|
|
}
|
|
|
|
if days > 0 {
|
|
|
|
timeStrings.append(days == 1 ? tr(format: "tunnelHandshakeTimestampDay (%d)", days) : tr(format: "tunnelHandshakeTimestampDays (%d)", days))
|
|
|
|
}
|
|
|
|
if hours > 0 {
|
|
|
|
timeStrings.append(hours == 1 ? tr(format: "tunnelHandshakeTimestampHour (%d)", hours) : tr(format: "tunnelHandshakeTimestampHours (%d)", hours))
|
|
|
|
}
|
|
|
|
if minutes > 0 {
|
|
|
|
timeStrings.append(minutes == 1 ? tr(format: "tunnelHandshakeTimestampMinute (%d)", minutes) : tr(format: "tunnelHandshakeTimestampMinutes (%d)", minutes))
|
|
|
|
}
|
|
|
|
if seconds > 0 {
|
|
|
|
timeStrings.append(seconds == 1 ? tr(format: "tunnelHandshakeTimestampSecond (%d)", seconds) : tr(format: "tunnelHandshakeTimestampSeconds (%d)", seconds))
|
|
|
|
}
|
|
|
|
return timeStrings.joined(separator: ", ")
|
2019-02-07 22:44:10 +00:00
|
|
|
#endif
|
2019-01-28 10:52:20 +00:00
|
|
|
}
|