-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRelayConstraints.swift
79 lines (64 loc) · 2.53 KB
/
RelayConstraints.swift
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
//
// RelayConstraint.swift
// MullvadTypes
//
// Created by pronebird on 10/06/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import Foundation
public protocol ConstraintsPropagation {
var onNewConstraints: ((RelayConstraints) -> Void)? { get set }
}
public class RelayConstraintsUpdater: ConstraintsPropagation {
public var onNewConstraints: ((RelayConstraints) -> Void)?
public init(onNewConstraints: ((RelayConstraints) -> Void)? = nil) {
self.onNewConstraints = onNewConstraints
}
}
public struct RelayConstraints: Codable, Equatable, CustomDebugStringConvertible {
@available(*, deprecated, renamed: "locations")
private var location: RelayConstraint<RelayLocation> = .only(.country("se"))
// Added in 2023.3
public var port: RelayConstraint<UInt16>
public var filter: RelayConstraint<RelayFilter>
// Added in 2024.1
public var locations: RelayConstraint<RelayLocations>
public var debugDescription: String {
"RelayConstraints { locations: \(locations), port: \(port) }"
}
public init(
locations: RelayConstraint<RelayLocations> = .only(RelayLocations(locations: [.country("se")])),
port: RelayConstraint<UInt16> = .any,
filter: RelayConstraint<RelayFilter> = .any
) {
self.locations = locations
self.port = port
self.filter = filter
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Added in 2023.3
port = try container.decodeIfPresent(RelayConstraint<UInt16>.self, forKey: .port) ?? .any
filter = try container.decodeIfPresent(RelayConstraint<RelayFilter>.self, forKey: .filter) ?? .any
// Added in 2024.1
locations = try container.decodeIfPresent(RelayConstraint<RelayLocations>.self, forKey: .locations)
?? Self.migrateLocations(decoder: decoder)
?? .only(RelayLocations(locations: [.country("se")]))
}
}
extension RelayConstraints {
private static func migrateLocations(decoder: Decoder) -> RelayConstraint<RelayLocations>? {
let container = try? decoder.container(keyedBy: CodingKeys.self)
guard
let location = try? container?.decodeIfPresent(RelayConstraint<RelayLocation>.self, forKey: .location)
else {
return nil
}
switch location {
case .any:
return .any
case let .only(location):
return .only(RelayLocations(locations: [location]))
}
}
}