VPN: Fix DNS resolution API
This commit is contained in:
parent
0f92228136
commit
69a35ec3ff
|
@ -5,19 +5,34 @@ import Network
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class DNSResolver {
|
class DNSResolver {
|
||||||
let endpoints: [Endpoint]
|
let endpoints: [Endpoint?]
|
||||||
|
|
||||||
init(endpoints: [Endpoint]) {
|
init(endpoints: [Endpoint?]) {
|
||||||
self.endpoints = endpoints
|
self.endpoints = endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolve(completionHandler: @escaping ([Endpoint?]) -> Void) {
|
func resolve(completionHandler: @escaping ([Endpoint?]?) -> Void) {
|
||||||
let endpoints = self.endpoints
|
let endpoints = self.endpoints
|
||||||
DispatchQueue.global(qos: .userInitiated).async {
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
var resolvedEndpoints = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
var resolvedEndpoints: [Endpoint?] = []
|
||||||
for (i, endpoint) in endpoints.enumerated() {
|
var isError = false
|
||||||
let resolvedEndpoint = DNSResolver.resolveSync(endpoint: endpoint)
|
for endpoint in endpoints {
|
||||||
resolvedEndpoints[i] = resolvedEndpoint
|
if let endpoint = endpoint {
|
||||||
|
if let resolvedEndpoint = DNSResolver.resolveSync(endpoint: endpoint) {
|
||||||
|
resolvedEndpoints.append(resolvedEndpoint)
|
||||||
|
} else {
|
||||||
|
isError = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolvedEndpoints.append(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isError) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completionHandler(nil)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
completionHandler(resolvedEndpoints)
|
completionHandler(resolvedEndpoints)
|
||||||
|
|
|
@ -256,13 +256,14 @@ class TunnelContainer: NSObject {
|
||||||
fileprivate func activate(completionHandler: @escaping (Bool) -> Void) {
|
fileprivate func activate(completionHandler: @escaping (Bool) -> Void) {
|
||||||
assert(status == .inactive)
|
assert(status == .inactive)
|
||||||
guard let tunnelConfiguration = tunnelConfiguration() else { fatalError() }
|
guard let tunnelConfiguration = tunnelConfiguration() else { fatalError() }
|
||||||
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
|
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
||||||
let dnsResolver = DNSResolver(endpoints: endpoints)
|
let dnsResolver = DNSResolver(endpoints: endpoints)
|
||||||
assert(self.dnsResolver == nil)
|
assert(self.dnsResolver == nil)
|
||||||
self.dnsResolver = dnsResolver
|
self.dnsResolver = dnsResolver
|
||||||
status = .resolvingEndpointDomains
|
status = .resolvingEndpointDomains
|
||||||
dnsResolver.resolve { [weak self] endpoints in
|
dnsResolver.resolve { [weak self] endpoints in
|
||||||
guard (!endpoints.contains { $0 == nil }) else {
|
guard let endpoints = endpoints else {
|
||||||
|
// TODO: Show error message
|
||||||
completionHandler(false)
|
completionHandler(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue