Skip to content

Commit 592105d

Browse files
author
Jon Petersson
committed
RelaySelector cannot resolve locations under certain constraints
1 parent 6dce18b commit 592105d

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

ios/MullvadREST/Relay/RelaySelector.swift

+23-22
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ public enum RelaySelector {
140140
_ constraints: RelayConstraints,
141141
relays: [RelayWithLocation<T>]
142142
) -> [RelayWithLocation<T>] {
143+
// Filter on active status, filter, and location.
143144
let filteredRelays = relays.filter { relayWithLocation -> Bool in
145+
guard relayWithLocation.relay.active else {
146+
return false
147+
}
148+
144149
switch constraints.filter {
145150
case .any:
146151
break
@@ -150,42 +155,38 @@ public enum RelaySelector {
150155
}
151156
}
152157

153-
switch constraints.locations {
158+
return switch constraints.locations {
154159
case .any:
155-
return true
160+
true
156161
case let .only(relayConstraint):
157162
// At least one location must match the relay under test.
158-
return relayConstraint.locations.contains { location in
163+
relayConstraint.locations.contains { location in
159164
relayWithLocation.matches(location: location)
160165
}
161166
}
162-
}.filter { relayWithLocation -> Bool in
163-
relayWithLocation.relay.active
164167
}
165168

166-
filteredRelays.filter { relayWithLocation in
167-
switch constraints.locations {
169+
// Filter on country inclusion.
170+
let includeInCountryFilteredRelays = filteredRelays.filter { relayWithLocation in
171+
return switch constraints.locations {
168172
case .any:
169-
return true
173+
true
170174
case let .only(relayConstraint):
171-
// At least one location must match the relay under test.
172-
return relayConstraint.locations.contains { location in
173-
relayWithLocation.matches(location: location)
175+
relayConstraint.locations.contains { location in
176+
if case .country = location {
177+
return relayWithLocation.relay.includeInCountry
178+
}
179+
return false
174180
}
175181
}
176-
if case let .country(countryCode) = constraints.locations. {
177-
178-
}
179-
return true
180182
}
181183

182-
// filteredRelays.filter { relayWithLocation in
183-
// if case let .country(countryCode) = relayWithLocation {
184-
//
185-
// }
186-
// }
187-
188-
return relayWithLocations
184+
// If no relays should be included in the matched country, instead accept all.
185+
if includeInCountryFilteredRelays.isEmpty {
186+
return filteredRelays
187+
} else {
188+
return includeInCountryFilteredRelays
189+
}
189190
}
190191

191192
/// Produce a port that is either user provided or randomly selected, satisfying the given constraints.

0 commit comments

Comments
 (0)