Commit 3de3c1bf authored by Jose Blaya's avatar Jose Blaya
Browse files

Refactor

parent 5ccbc706
......@@ -247,6 +247,12 @@
82E20B1224F652ED0065EFE3 /* AccountInfo+Kotlin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82E20B1024F652ED0065EFE3 /* AccountInfo+Kotlin.swift */; };
82E20B1624F6AA110065EFE3 /* RegionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82E20B1524F6AA110065EFE3 /* RegionData.swift */; };
82E20B1724F6AA110065EFE3 /* RegionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82E20B1524F6AA110065EFE3 /* RegionData.swift */; };
82FD5D562521F2DD00E390CB /* PIARegionClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D552521F2DD00E390CB /* PIARegionClientStateProvider.swift */; };
82FD5D572521F2DD00E390CB /* PIARegionClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D552521F2DD00E390CB /* PIARegionClientStateProvider.swift */; };
82FD5D5E2521F30300E390CB /* PIAAccountClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D5D2521F30300E390CB /* PIAAccountClientStateProvider.swift */; };
82FD5D5F2521F30300E390CB /* PIAAccountClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D5D2521F30300E390CB /* PIAAccountClientStateProvider.swift */; };
82FD5D662521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D652521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift */; };
82FD5D672521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82FD5D652521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift */; };
84125E08213D7DFF001BCC19 /* PIAColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84125E07213D7DFF001BCC19 /* PIAColors.swift */; };
84125E09213D7E04001BCC19 /* PIAColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84125E07213D7DFF001BCC19 /* PIAColors.swift */; };
84125E0B213D7E0E001BCC19 /* PIAFonts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84125E0A213D7E0E001BCC19 /* PIAFonts.swift */; };
......@@ -552,15 +558,15 @@
82183D7D25011D200033023F /* MagicLinkLoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicLinkLoginViewController.swift; sourceTree = "<group>"; };
8221921B24CEBA3800C24F1C /* NMTRules.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NMTRules.swift; sourceTree = "<group>"; };
8221921E24CEBABA00C24F1C /* NMTType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NMTType.swift; sourceTree = "<group>"; };
8221922124CEC1E400C24F1C /* Invites.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Invites.swift; sourceTree = "<group>"; };
8221922224CEC1E400C24F1C /* InvitesInformation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InvitesInformation.swift; sourceTree = "<group>"; };
8221922724CEC1ED00C24F1C /* GlossInvitesInformation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlossInvitesInformation.swift; sourceTree = "<group>"; };
822BC1CF24BF20C90041BF9A /* UIControl+Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIControl+Action.swift"; sourceTree = "<group>"; };
82C374F42514DC6D00E391EE /* EndpointManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EndpointManagerTests.swift; sourceTree = "<group>"; };
82C374F62514DC7200E391EE /* server.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = server.json; sourceTree = "<group>"; };
82C374FA2514DEC700E391EE /* EndpointManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EndpointManager.swift; sourceTree = "<group>"; };
82E20B1024F652ED0065EFE3 /* AccountInfo+Kotlin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AccountInfo+Kotlin.swift"; sourceTree = "<group>"; };
82E20B1524F6AA110065EFE3 /* RegionData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegionData.swift; sourceTree = "<group>"; };
82FD5D552521F2DD00E390CB /* PIARegionClientStateProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIARegionClientStateProvider.swift; sourceTree = "<group>"; };
82FD5D5D2521F30300E390CB /* PIAAccountClientStateProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIAAccountClientStateProvider.swift; sourceTree = "<group>"; };
82FD5D652521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIAAccountStagingClientStateProvider.swift; sourceTree = "<group>"; };
84125E07213D7DFF001BCC19 /* PIAColors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIAColors.swift; sourceTree = "<group>"; };
84125E0A213D7E0E001BCC19 /* PIAFonts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIAFonts.swift; sourceTree = "<group>"; };
841BE60C212AD0F3002EF2D1 /* ValidatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorTests.swift; sourceTree = "<group>"; };
......@@ -761,7 +767,6 @@
isa = PBXGroup;
children = (
82C374FA2514DEC700E391EE /* EndpointManager.swift */,
8221922724CEC1ED00C24F1C /* GlossInvitesInformation.swift */,
0E392DA21FE3247E0002160D /* Endpoint.swift */,
0E3D13D21F9E270A00434A48 /* GlossAccountInfo.swift */,
0E392D8E1FE2FBBE0002160D /* GlossConnectivityStatus.swift */,
......@@ -837,8 +842,6 @@
0EA7065C1F80F448001E4F88 /* WebServices */ = {
isa = PBXGroup;
children = (
8221922124CEC1E400C24F1C /* Invites.swift */,
8221922224CEC1E400C24F1C /* InvitesInformation.swift */,
0EE78AF71F818815002E4CDD /* AccountInfo.swift */,
82E20B1024F652ED0065EFE3 /* AccountInfo+Kotlin.swift */,
0E392D8B1FE2F8780002160D /* ConnectivityStatus.swift */,
......@@ -1082,6 +1085,7 @@
0EFB51281F82D4450033B81F /* Library */ = {
isa = PBXGroup;
children = (
82FD5D542521F2B600E390CB /* Providers */,
DDFCFAA621E9008E0081F235 /* Tiles */,
0EE14CF31FF0F91C008D9AC2 /* Account */,
0E2ADCD71FE072ED00BB170C /* Daemons */,
......@@ -1145,6 +1149,16 @@
path = NMT;
sourceTree = "<group>";
};
82FD5D542521F2B600E390CB /* Providers */ = {
isa = PBXGroup;
children = (
82FD5D552521F2DD00E390CB /* PIARegionClientStateProvider.swift */,
82FD5D5D2521F30300E390CB /* PIAAccountClientStateProvider.swift */,
82FD5D652521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift */,
);
path = Providers;
sourceTree = "<group>";
};
84577FB9213D8EB2006DEC3D /* Styles */ = {
isa = PBXGroup;
children = (
......@@ -1687,6 +1701,7 @@
0E7BC6FB1F96B1120035C8B2 /* LibraryCallback.swift in Sources */,
0E3D13D41F9E270A00434A48 /* GlossAccountInfo.swift in Sources */,
0E9D62D31FDEBBFD009A90CF /* GlossServer.swift in Sources */,
82FD5D672521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift in Sources */,
0E9D62DB1FDEE3FE009A90CF /* ServerProvider.swift in Sources */,
0E7BC6FA1F96B1120035C8B2 /* Notification+Core.swift in Sources */,
0EA4C4321FDDD48F0041C3D8 /* Server.swift in Sources */,
......@@ -1697,6 +1712,7 @@
0E7BC6F51F96B1120035C8B2 /* Credentials.swift in Sources */,
0E2ADCCB1FE06D7A00BB170C /* Array+Math.swift in Sources */,
DD36CB7F21CD000200FC815A /* CAGradientLayer+Image.swift in Sources */,
82FD5D5F2521F30300E390CB /* PIAAccountClientStateProvider.swift in Sources */,
82E20B1224F652ED0065EFE3 /* AccountInfo+Kotlin.swift in Sources */,
0E7BC6EE1F96B1120035C8B2 /* AccountProvider.swift in Sources */,
841BE610212AFE69002EF2D1 /* GiftCardUtil.swift in Sources */,
......@@ -1777,6 +1793,7 @@
0E392D7E1FE2E4C10002160D /* MemoryStore.swift in Sources */,
DD76292921ECDFF80092DF50 /* Usage.swift in Sources */,
0E2ADD381FE14F0000BB170C /* DefaultVPNProvider.swift in Sources */,
82FD5D572521F2DD00E390CB /* PIARegionClientStateProvider.swift in Sources */,
0E75D8D41F9E3F9F00658D1E /* UserAccount.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......@@ -1793,6 +1810,7 @@
0E392DB01FE3366B0002160D /* NSData+Compression.m in Sources */,
0E53A84D1FE5BB25000C2A18 /* VPNDaemon.swift in Sources */,
84577FBB213D8ED1006DEC3D /* StyleGuideHelpers.swift in Sources */,
82FD5D5E2521F30300E390CB /* PIAAccountClientStateProvider.swift in Sources */,
DD314991218350D1008E26E8 /* SwiftGen+SeguesStoryboards.swift in Sources */,
0EB8C0701F9CD38B005857E4 /* WelcomePageViewController.swift in Sources */,
DD0AC790218715B8009B576B /* PIAButton.swift in Sources */,
......@@ -1829,6 +1847,7 @@
0E392D8F1FE2FBBE0002160D /* GlossConnectivityStatus.swift in Sources */,
0EB8C06F1F9CD38B005857E4 /* SignupSuccessViewController.swift in Sources */,
0E492C5E1FE5F7C0007F23DF /* MockAccountProvider.swift in Sources */,
82FD5D662521F32B00E390CB /* PIAAccountStagingClientStateProvider.swift in Sources */,
0E2ADCDD1FE072ED00BB170C /* Pinger.m in Sources */,
0EB8C0721F9CD38B005857E4 /* Macros+UI.swift in Sources */,
0E3D13D01F9E26FD00434A48 /* GlossCredentials.swift in Sources */,
......@@ -1905,6 +1924,7 @@
0EF14E4B1FEAE6350007485A /* Client+Providers.swift in Sources */,
DD2683E924617F0300C65DAA /* PingTask.swift in Sources */,
84577FC1213D9A23006DEC3D /* UIImage+Color.swift in Sources */,
82FD5D562521F2DD00E390CB /* PIARegionClientStateProvider.swift in Sources */,
0E38E4101FF81722008223AB /* Notification+UI.swift in Sources */,
0E392DA91FE329AD0002160D /* PlatformVPNLog.swift in Sources */,
0EB8C0481F9CCE07005857E4 /* Macros.swift in Sources */,
......
......@@ -89,14 +89,6 @@ public protocol ServerProvider: class {
- Returns: The found `Server` object or `nil`.
*/
func find(withIdentifier identifier: String) -> Server?
/**
Returns the URL where to find a flag asset associated with a server.
- Parameter server: The `Server` to fetch the flag for.
- Returns: The `URL` of the flag asset. The asset is not guaranteed to be available.
*/
func flagURL(for server: Server) -> URL
/**
Reset the currentServers object
......
......@@ -62,9 +62,7 @@ protocol WebServices: class {
func downloadRegionsStaticData(_ callback: LibraryCallback<RegionData>?)
func flagURL(for country: String) -> URL
func taskForConnectivityCheck(_ callback: LibraryCallback<ConnectivityStatus>?) -> URLSessionDataTask
func taskForConnectivityCheck(_ callback: LibraryCallback<ConnectivityStatus>?)
func submitDebugLog(_ log: DebugLog, _ callback: SuccessLibraryCallback?)
......
......@@ -561,10 +561,9 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
}
func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?) {
let task = webServices.taskForConnectivityCheck { (_, error) in
webServices.taskForConnectivityCheck { (_, error) in
callback?(error == nil, error)
}
task.resume()
}
}
......@@ -43,8 +43,6 @@ class ConnectivityDaemon: Daemon, ConfigurationAccess, DatabaseAccess, Preferenc
private var failedConnectivityAttempts: Int
private var pendingConnectivityCheck: URLSessionDataTask?
private var wasConnected: Bool
private init() {
......@@ -57,7 +55,6 @@ class ConnectivityDaemon: Daemon, ConfigurationAccess, DatabaseAccess, Preferenc
isCheckingConnectivity = false
failedConnectivityAttempts = 0
pendingConnectivityCheck = nil
wasConnected = false
}
......@@ -128,8 +125,7 @@ class ConnectivityDaemon: Daemon, ConfigurationAccess, DatabaseAccess, Preferenc
Macros.postNotification(.PIADaemonsDidUpdateConnectivity)
isCheckingConnectivity = true
pendingConnectivityCheck?.cancel()
pendingConnectivityCheck = accessedWebServices.taskForConnectivityCheck { (connectivity, error) in
accessedWebServices.taskForConnectivityCheck { (connectivity, error) in
self.isCheckingConnectivity = false
guard let connectivity = connectivity else {
......@@ -168,7 +164,6 @@ class ConnectivityDaemon: Daemon, ConfigurationAccess, DatabaseAccess, Preferenc
Macros.postNotification(.PIADaemonsDidUpdateConnectivity)
}
pendingConnectivityCheck?.resume()
}
// MARK: Notifications
......
//
// PIAAccountClientStateProvider.swift
// PIALibrary
//
// Created by Jose Blaya on 28/09/2020.
// Copyright © 2020 Private Internet Access, Inc.
//
// This file is part of the Private Internet Access iOS Client.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import Foundation
import PIAAccount
class PIAAccountClientStateProvider : AccountClientStateProvider {
func accountEndpoints() -> [AccountEndpoint] {
let validEndpoints = EndpointManager.shared.availableEndpoints()
var clientEndpoints = [AccountEndpoint]()
for endpoint in validEndpoints.reversed() {
clientEndpoints.append(AccountEndpoint(endpoint: endpoint.host, usePinnedCertificate: endpoint.useCertificatePinning ?? false, certificateCommonName: endpoint.commonName))
}
return clientEndpoints
}
}
//
// PIAAccountStagingClientStateProvider.swift
// PIALibrary
//
// Created by Jose Blaya on 28/09/2020.
// Copyright © 2020 Private Internet Access, Inc.
//
// This file is part of the Private Internet Access iOS Client.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import Foundation
import PIAAccount
class PIAAccountStagingClientStateProvider : AccountClientStateProvider {
func accountEndpoints() -> [AccountEndpoint] {
return [
AccountEndpoint(endpoint: Client.configuration.baseUrl, usePinnedCertificate: false, certificateCommonName: nil),
]
}
}
//
// PIARegionClientStateProvider.swift
// PIALibrary
//
// Created by Jose Blaya on 28/09/2020.
// Copyright © 2020 Private Internet Access, Inc.
//
// This file is part of the Private Internet Access iOS Client.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import Foundation
import Regions
class PIARegionClientStateProvider : RegionClientStateProvider {
func regionEndpoints() -> [RegionEndpoint] {
let validEndpoints = EndpointManager.shared.availableRegionEndpoints()
var clientEndpoints = [RegionEndpoint]()
for endpoint in validEndpoints.reversed() {
clientEndpoints.append(RegionEndpoint(endpoint: endpoint.host, usePinnedCertificate: endpoint.useCertificatePinning ?? false, certificateCommonName: endpoint.commonName))
}
return clientEndpoints
}
}
......@@ -171,10 +171,6 @@ class DefaultServerProvider: ServerProvider, ConfigurationAccess, DatabaseAccess
return currentServers.first { $0.identifier == identifier }
}
func flagURL(for server: Server) -> URL {
return webServices.flagURL(for: server.country.lowercased())
}
func resetCurrentServers() {
currentServers = []
}
......@@ -185,14 +181,6 @@ class DefaultServerProvider: ServerProvider, ConfigurationAccess, DatabaseAccess
}
}
extension Server: ProvidersAccess {
/// Shortcut for `ServerProvider.flagURL(for:)` as per `Client.Providers.serverProvider`. Requires `Library` subspec.
public var flagURL: URL {
return accessedProviders.serverProvider.flagURL(for: self)
}
}
extension Server: DatabaseAccess {
/// Returns last ping response in milliseconds. Requires `Library` subspec.
......
......@@ -26,15 +26,6 @@ protocol Endpoint: ConfigurationAccess {
var url: URL { get }
}
enum ClientEndpoint: String, Endpoint {
case status
var url: URL {
return URL(string: "\(accessedConfiguration.baseUrl)/api/client/\(rawValue)")!
}
}
enum VPNEndpoint: String, Endpoint {
case servers
......
......@@ -46,10 +46,25 @@ public class EndpointManager {
var currentServers = Client.providers.serverProvider.currentServers.filter { $0.serverNetwork == .gen4 }
currentServers = currentServers.sorted(by: { $0.pingTime ?? 1000 < $1.pingTime ?? 1000 })
if let historicalServer = Client.providers.serverProvider.historicalServers.first {
availableEndpoints.append(PinningEndpoint(host: historicalServer.meta!.ip,
useCertificatePinning: true,
commonName: historicalServer.meta?.cn))
}
if currentServers.count > 2 {
availableEndpoints.append(PinningEndpoint(host: currentServers[0].meta!.ip, useCertificatePinning: true, commonName: currentServers[0].meta?.cn))
availableEndpoints.append(PinningEndpoint(host: currentServers[1].meta!.ip, useCertificatePinning: true, commonName: currentServers[1].meta?.cn))
if currentServers[0].pingTime == nil && currentServers[1].pingTime == nil {
while availableEndpoints.count < 2 {
availableEndpoints.append(PinningEndpoint(host: currentServers.randomElement()!.meta!.ip, useCertificatePinning: true, commonName: currentServers.randomElement()?.meta?.cn))
}
} else {
availableEndpoints.append(PinningEndpoint(host: currentServers[0].meta!.ip, useCertificatePinning: true, commonName: currentServers[0].meta?.cn))
if availableEndpoints.count < 2 {
availableEndpoints.append(PinningEndpoint(host: currentServers[1].meta!.ip, useCertificatePinning: true, commonName: currentServers[1].meta?.cn))
}
}
}
}
public func availableRegionEndpoints() -> [PinningEndpoint] {
......
......@@ -24,53 +24,24 @@ import Foundation
import Gloss
extension PIAWebServices {
func flagURL(for country: String) -> URL {
return URL(string: "\(accessedConfiguration.baseUrl)/images/flags/\(country)_3x.png")!
}
func taskForConnectivityCheck(_ callback: ((ConnectivityStatus?, Error?) -> Void)?) -> URLSessionDataTask {
// every status check should use a new HTTP connection (no Keep-Alive), so we
// skip using AFNetworking here and just use NSURLSession directly
let config: URLSessionConfiguration = .ephemeral
config.timeoutIntervalForRequest = Double(accessedConfiguration.connectivityTimeout) / 1000.0
config.timeoutIntervalForResource = Double(accessedConfiguration.connectivityTimeout) / 1000.0
let session = URLSession(configuration: config)
let url = ClientEndpoint.status.url
return session.dataTask(with: url, completionHandler: { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
callback?(nil, error)
return
}
guard let httpResponse = response as? HTTPURLResponse, let data = data else {
callback?(nil, ClientError.malformedResponseData)
return
}
let statusCode = httpResponse.statusCode
guard (statusCode == 200) else {
callback?(nil, ClientError.unexpectedReply)
return
}
let jsonObject: Any
do {
jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
} catch let e {
callback?(nil, e)
func taskForConnectivityCheck(_ callback: ((ConnectivityStatus?, Error?) -> Void)?) {
self.accountAPI.clientStatus { (information, error) in
DispatchQueue.main.async {
if let _ = error {
callback?(nil, ClientError.internetUnreachable)
return
}
guard let json = jsonObject as? JSON, let connectivity = GlossConnectivityStatus(json: json) else {
if let information = information {
callback?(ConnectivityStatus(ipAddress: information.ip, isVPN: information.connected), nil)
} else {
callback?(nil, ClientError.malformedResponseData)
return
}
callback?(connectivity.parsed, nil)
}
})
}
}
func submitDebugLog(_ log: DebugLog, _ callback: SuccessLibraryCallback?) {
......
......@@ -30,55 +30,22 @@ import Regions
private let log = SwiftyBeaver.self
class RegClientStateProvider : RegionClientStateProvider {
func regionEndpoints() -> [RegionEndpoint] {
let validEndpoints = EndpointManager.shared.availableRegionEndpoints()
var clientEndpoints = [RegionEndpoint]()
for endpoint in validEndpoints.reversed() {
clientEndpoints.append(RegionEndpoint(endpoint: endpoint.host, usePinnedCertificate: endpoint.useCertificatePinning ?? false, certificateCommonName: endpoint.commonName))
}
return clientEndpoints
}
}
class ClientStateProvider : AccountClientStateProvider {
func accountEndpoints() -> [AccountEndpoint] {
let validEndpoints = EndpointManager.shared.availableEndpoints()
var clientEndpoints = [AccountEndpoint]()
for endpoint in validEndpoints.reversed() {
clientEndpoints.append(AccountEndpoint(endpoint: endpoint.host, usePinnedCertificate: endpoint.useCertificatePinning ?? false, certificateCommonName: endpoint.commonName))
}
return clientEndpoints
}
}
class ClientStagingStateProvider : AccountClientStateProvider {
func accountEndpoints() -> [AccountEndpoint] {
return [
AccountEndpoint(endpoint: Client.configuration.baseUrl, usePinnedCertificate: false, certificateCommonName: "nil"),
]
}
}
class PIAWebServices: WebServices, ConfigurationAccess {
private static let serversVersion = 1002
private static let store = "apple_app_store"
private let regionsTask = RegionsTask(stateProvider: RegClientStateProvider())
private let accountAPI: IOSAccountAPI!
private let regionsTask = RegionsTask(stateProvider: PIARegionClientStateProvider())
let accountAPI: IOSAccountAPI!
init() {
if Client.environment == .staging {
self.accountAPI = AccountBuilder<IOSAccountAPI>().setPlatform(platform: .ios)
.setClientStateProvider(clientStateProvider: ClientStagingStateProvider())
.setClientStateProvider(clientStateProvider: PIAAccountStagingClientStateProvider())
.setUserAgentValue(userAgentValue: userAgent).build() as? IOSAccountAPI
} else {
self.accountAPI = AccountBuilder<IOSAccountAPI>().setPlatform(platform: .ios)
.setClientStateProvider(clientStateProvider: ClientStateProvider())
.setClientStateProvider(clientStateProvider: PIAAccountClientStateProvider())
.setUserAgentValue(userAgentValue: userAgent).build() as? IOSAccountAPI
}
}
......
......@@ -148,12 +148,7 @@ public class MockServerProvider: ServerProvider, DatabaseAccess, WebServicesCons
public func find(withIdentifier identifier: String) -> Server? {
return delegate.find(withIdentifier: identifier)
}
/// :nodoc:
public func flagURL(for server: Server) -> URL {
return delegate.flagURL(for: server)
}
public func resetCurrentServers() {
}
}
......@@ -100,8 +100,8 @@ class MockWebServices: WebServices {
return URL(fileURLWithPath: "")
}
func taskForConnectivityCheck(_ callback: ((ConnectivityStatus?, Error?) -> Void)?) -> URLSessionDataTask {
return URLSessionDataTask()
func taskForConnectivityCheck(_ callback: ((ConnectivityStatus?, Error?) -> Void)?) {
callback?(nil, nil)
}
func submitDebugLog(_ log: DebugLog, _ callback: SuccessLibraryCallback?) {
......
......@@ -422,10 +422,9 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess {
callback?(false, nil)
return
}
let task = webServices.taskForConnectivityCheck { (_, error) in
webServices.taskForConnectivityCheck { (_, error) in
callback?(error == nil, error)
}
task.resume()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment