Skip to content

Commit

Permalink
Adjust relay selector to support custom lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson committed Feb 7, 2024
1 parent 0d4ee24 commit 5aac813
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
32 changes: 18 additions & 14 deletions ios/MullvadREST/Relay/RelaySelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,28 @@ public enum RelaySelector {
}
}

switch constraints.location {
switch constraints.locations {
case .any:
return true
case let .only(relayConstraint):
switch relayConstraint {
case let .country(countryCode):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.relay.includeInCountry

case let .city(countryCode, cityCode):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.serverLocation.cityCode == cityCode

case let .hostname(countryCode, cityCode, hostname):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.serverLocation.cityCode == cityCode &&
relayWithLocation.relay.hostname == hostname
for location in relayConstraint.locations {
switch location {
case let .country(countryCode):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.relay.includeInCountry

case let .city(countryCode, cityCode):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.serverLocation.cityCode == cityCode

case let .hostname(countryCode, cityCode, hostname):
return relayWithLocation.serverLocation.countryCode == countryCode &&
relayWithLocation.serverLocation.cityCode == cityCode &&
relayWithLocation.relay.hostname == hostname
}
}

return false
}
}.filter { relayWithLocation -> Bool in
relayWithLocation.relay.active
Expand Down
21 changes: 16 additions & 5 deletions ios/MullvadTypes/RelayConstraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,41 @@ public class RelayConstraintsUpdater: ConstraintsPropagation {
}
}

public struct RelayLocationPersistent: Codable, Equatable {
public let locations: [RelayLocation]
public let customListId: UUID?

public init(locations: [RelayLocation], customListId: UUID?) {
self.locations = locations
self.customListId = customListId
}
}

public struct RelayConstraints: Codable, Equatable, CustomDebugStringConvertible {
public var location: RelayConstraint<RelayLocation>
public var locations: RelayConstraint<RelayLocationPersistent>

// Added in 2023.3
public var port: RelayConstraint<UInt16>
public var filter: RelayConstraint<RelayFilter>

public var debugDescription: String {
"RelayConstraints { location: \(location), port: \(port) }"
"RelayConstraints { locations: \(locations), port: \(port) }"
}

public init(
location: RelayConstraint<RelayLocation> = .only(.country("se")),
locations: RelayConstraint<RelayLocationPersistent> =
.only(RelayLocationPersistent(locations: [.country("se")], customListId: nil)),
port: RelayConstraint<UInt16> = .any,
filter: RelayConstraint<RelayFilter> = .any
) {
self.location = location
self.locations = locations
self.port = port
self.filter = filter
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
location = try container.decode(RelayConstraint<RelayLocation>.self, forKey: .location)
locations = try container.decode(RelayConstraint<RelayLocationPersistent>.self, forKey: .locations)

// Added in 2023.3
port = try container.decodeIfPresent(RelayConstraint<UInt16>.self, forKey: .port) ?? .any
Expand Down

0 comments on commit 5aac813

Please sign in to comment.