-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Graeme Arthur <[email protected]>
- Loading branch information
1 parent
6f36c07
commit cb3efb0
Showing
9 changed files
with
366 additions
and
8 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Sources/NetworkProtection/Models/VPNServerSelectionResolver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// VPNServerSelectionResolver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
enum VPNServerSelectionResolverError: Error { | ||
case countryNotFound | ||
case fetchingLocationsFailed(Error) | ||
} | ||
|
||
protocol VPNServerSelectionResolving { | ||
func resolvedServerSelectionMethod() async -> NetworkProtectionServerSelectionMethod | ||
} | ||
|
||
final class VPNServerSelectionResolver: VPNServerSelectionResolving { | ||
private let locationListRepository: NetworkProtectionLocationListRepository | ||
private let vpnSettings: VPNSettings | ||
|
||
init(locationListRepository: NetworkProtectionLocationListRepository, vpnSettings: VPNSettings) { | ||
self.locationListRepository = locationListRepository | ||
self.vpnSettings = vpnSettings | ||
} | ||
|
||
/// Address the case where the prefered location becomes unavailable | ||
/// We fall back to the country, if a city isn't available, | ||
/// or nearest if the country isn't available | ||
public func resolvedServerSelectionMethod() async -> NetworkProtectionServerSelectionMethod { | ||
switch currentServerSelectionMethod { | ||
case .automatic, .preferredServer, .avoidServer, .failureRecovery: | ||
return currentServerSelectionMethod | ||
case .preferredLocation(let networkProtectionSelectedLocation): | ||
do { | ||
let location = try await resolveSelectionAgainstAvailableLocations(networkProtectionSelectedLocation) | ||
return .preferredLocation(location) | ||
} catch let error as VPNServerSelectionResolverError { | ||
switch error { | ||
case .countryNotFound: | ||
return .automatic | ||
case .fetchingLocationsFailed: | ||
return currentServerSelectionMethod | ||
} | ||
} catch { | ||
return currentServerSelectionMethod | ||
} | ||
} | ||
} | ||
|
||
private func resolveSelectionAgainstAvailableLocations(_ selection: NetworkProtectionSelectedLocation) async throws -> NetworkProtectionSelectedLocation { | ||
let availableLocations: [NetworkProtectionLocation] | ||
do { | ||
availableLocations = try await locationListRepository.fetchLocationList(cachePolicy: .ignoreCache) | ||
} catch { | ||
throw VPNServerSelectionResolverError.fetchingLocationsFailed(error) | ||
} | ||
|
||
let availableCitySelections = availableLocations.flatMap { location in | ||
location.cities.map { city in NetworkProtectionSelectedLocation(country: location.country, city: city.name) } | ||
} | ||
|
||
if availableCitySelections.contains(selection) { | ||
return selection | ||
} | ||
|
||
let selectedCountry = NetworkProtectionSelectedLocation(country: selection.country) | ||
let availableCountrySelections = availableLocations.map { NetworkProtectionSelectedLocation(country: $0.country) } | ||
guard availableCountrySelections.contains(selectedCountry) else { | ||
throw VPNServerSelectionResolverError.countryNotFound | ||
} | ||
|
||
return selectedCountry | ||
} | ||
|
||
private var currentServerSelectionMethod: NetworkProtectionServerSelectionMethod { | ||
var serverSelectionMethod: NetworkProtectionServerSelectionMethod | ||
|
||
switch vpnSettings.selectedLocation { | ||
case .nearest: | ||
serverSelectionMethod = .automatic | ||
case .location(let networkProtectionSelectedLocation): | ||
serverSelectionMethod = .preferredLocation(networkProtectionSelectedLocation) | ||
} | ||
|
||
switch vpnSettings.selectedServer { | ||
case .automatic: | ||
break | ||
case .endpoint(let string): | ||
// Selecting a specific server will override locations setting | ||
// Only available in debug | ||
serverSelectionMethod = .preferredServer(serverName: string) | ||
} | ||
|
||
return serverSelectionMethod | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...NetworkProtectionTestUtils/Repositories/MockNetworkProtectionLocationListRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// MockNetworkProtectionLocationListRepository.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
@testable import NetworkProtection | ||
|
||
final class MockNetworkProtectionLocationListRepository: NetworkProtectionLocationListRepository { | ||
var stubFetchLocationList: [NetworkProtectionLocation] = [] | ||
var stubFetchLocationListError: Error? | ||
var spyIgnoreCache: Bool = false | ||
|
||
func fetchLocationList() async throws -> [NetworkProtectionLocation] { | ||
if let stubFetchLocationListError { | ||
throw stubFetchLocationListError | ||
} | ||
return stubFetchLocationList | ||
} | ||
|
||
func fetchLocationList(cachePolicy: NetworkProtectionLocationListCachePolicy) async throws -> [NetworkProtectionLocation] { | ||
switch cachePolicy { | ||
case .returnCacheElseLoad: | ||
return try await fetchLocationList() | ||
case .ignoreCache: | ||
return try await fetchLocationListIgnoringCache() | ||
} | ||
} | ||
|
||
func fetchLocationListIgnoringCache() async throws -> [NetworkProtection.NetworkProtectionLocation] { | ||
spyIgnoreCache = true | ||
return try await fetchLocationList() | ||
} | ||
} | ||
|
||
extension NetworkProtectionLocation { | ||
static func testData(country: String = "", cities: [City] = []) -> NetworkProtectionLocation { | ||
return Self(country: country, cities: cities) | ||
} | ||
} | ||
|
||
extension NetworkProtectionLocation.City { | ||
static func testData(name: String = "") -> NetworkProtectionLocation.City { | ||
Self(name: name) | ||
} | ||
} |
Oops, something went wrong.