VPN: Fix DNS resolution API

This commit is contained in:
Roopesh Chander 2018-10-27 04:41:05 +05:30
parent 0f92228136
commit 69a35ec3ff
2 changed files with 25 additions and 9 deletions

View File

@ -5,19 +5,34 @@ import Network
import Foundation
class DNSResolver {
let endpoints: [Endpoint]
let endpoints: [Endpoint?]
init(endpoints: [Endpoint]) {
init(endpoints: [Endpoint?]) {
self.endpoints = endpoints
}
func resolve(completionHandler: @escaping ([Endpoint?]) -> Void) {
func resolve(completionHandler: @escaping ([Endpoint?]?) -> Void) {
let endpoints = self.endpoints
DispatchQueue.global(qos: .userInitiated).async {
var resolvedEndpoints = Array<Endpoint?>(repeating: nil, count: endpoints.count)
for (i, endpoint) in endpoints.enumerated() {
let resolvedEndpoint = DNSResolver.resolveSync(endpoint: endpoint)
resolvedEndpoints[i] = resolvedEndpoint
var resolvedEndpoints: [Endpoint?] = []
var isError = false
for endpoint in endpoints {
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 {
completionHandler(resolvedEndpoints)

View File

@ -256,13 +256,14 @@ class TunnelContainer: NSObject {
fileprivate func activate(completionHandler: @escaping (Bool) -> Void) {
assert(status == .inactive)
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)
assert(self.dnsResolver == nil)
self.dnsResolver = dnsResolver
status = .resolvingEndpointDomains
dnsResolver.resolve { [weak self] endpoints in
guard (!endpoints.contains { $0 == nil }) else {
guard let endpoints = endpoints else {
// TODO: Show error message
completionHandler(false)
return
}