Commit 5ccbc706 authored by Jose Blaya's avatar Jose Blaya
Browse files

Use meta IP

parent a422842c
......@@ -142,6 +142,9 @@ public class Server: Hashable {
/// - Seealso: `Macros.ping(...)`
public let pingAddress: Address?
/// The meta IP.
public let meta: ServerAddressIP?
var isAutomatic: Bool
/// :nodoc:
......@@ -160,6 +163,7 @@ public class Server: Hashable {
responseTime: Int? = 0,
serverNetwork: ServersNetwork? = .legacy,
geo: Bool = false,
meta: ServerAddressIP? = nil,
regionIdentifier: String) {
self.serial = serial
......@@ -177,6 +181,7 @@ public class Server: Hashable {
self.wireGuardAddressesForUDP = wireGuardAddressesForUDP
self.iKEv2AddressesForUDP = iKEv2AddressesForUDP
self.meta = meta
self.pingAddress = pingAddress
self.serverNetwork = serverNetwork
......
......@@ -46,11 +46,3 @@ enum VPNEndpoint: String, Endpoint {
}
enum ServerEndpoint: String, Endpoint {
case gen4
var url: URL {
return URL(string: "https://serverlist.piaservers.net/vpninfo/servers/new")!
}
}
......@@ -38,9 +38,41 @@ public class EndpointManager {
private let internalUrl = "10.0.0.1"
private let proxy = "piaproxy.net"
private let pia = "www.privateinternetaccess.com"
private let region = "serverlist.piaservers.net"
public static let shared = EndpointManager()
private func availableMetaEndpoints(_ availableEndpoints: inout [PinningEndpoint]) {
var currentServers = Client.providers.serverProvider.currentServers.filter { $0.serverNetwork == .gen4 }
currentServers = currentServers.sorted(by: { $0.pingTime ?? 1000 < $1.pingTime ?? 1000 })
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))
}
}
public func availableRegionEndpoints() -> [PinningEndpoint] {
if Client.configuration.currentServerNetwork() == .gen4 {
if Client.providers.vpnProvider.isVPNConnected {
return [PinningEndpoint(host: internalUrl),
PinningEndpoint(host: region)]
}
var availableEndpoints = [PinningEndpoint]()
availableMetaEndpoints(&availableEndpoints)
availableEndpoints.append(PinningEndpoint(host: region))
return availableEndpoints
} else {
return [PinningEndpoint(host: region)]
}
}
public func availableEndpoints() -> [PinningEndpoint] {
if Client.configuration.currentServerNetwork() == .gen4 {
......@@ -52,14 +84,8 @@ public class EndpointManager {
}
var availableEndpoints = [PinningEndpoint]()
var currentServers = Client.providers.serverProvider.currentServers.filter { $0.serverNetwork == .gen4 }
currentServers = currentServers.sorted(by: { $0.pingTime ?? 1000 < $1.pingTime ?? 1000 })
availableMetaEndpoints(&availableEndpoints)
if currentServers.count > 2 {
availableEndpoints.append(PinningEndpoint(host: currentServers[0].bestAddressForWireGuard()!.ip, useCertificatePinning: true, commonName: currentServers[0].bestAddressForWireGuard()?.cn))
availableEndpoints.append(PinningEndpoint(host: currentServers[1].bestAddressForWireGuard()!.ip, useCertificatePinning: true, commonName: currentServers[1].bestAddressForWireGuard()?.cn))
}
availableEndpoints.append(PinningEndpoint(host: pia))
availableEndpoints.append(PinningEndpoint(host: proxy))
......
......@@ -48,6 +48,18 @@ class GlossServer: GlossParser {
if Client.configuration.serverNetwork == .gen4 {
var meta: Server.ServerAddressIP?
if let metaServer: [String: Any] = "servers" <~~ json {
if let addressArray: [JSON] = "meta" <~~ metaServer {
for address in addressArray {
if let ip: String = "ip" <~~ address,
let cn: String = "cn" <~~ address {
meta = Server.ServerAddressIP(ip: ip, cn: cn)
}
}
}
}
guard let regionIdentifier: String = "id" <~~ json else {
return nil
}
......@@ -119,6 +131,7 @@ class GlossServer: GlossParser {
responseTime: 0,
serverNetwork: internalServerNetwork ?? .gen4,
geo: geo,
meta: meta,
regionIdentifier: regionIdentifier
)
......
......@@ -33,7 +33,7 @@ private let log = SwiftyBeaver.self
class RegClientStateProvider : RegionClientStateProvider {
func regionEndpoints() -> [RegionEndpoint] {
let validEndpoints = EndpointManager.shared.availableEndpoints()
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))
......
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