NE: Simplify DNS resolution

This commit is contained in:
Roopesh Chander 2018-12-21 19:23:16 +05:30
parent 6b48cb3095
commit b02754a7bd
2 changed files with 6 additions and 23 deletions

View File

@ -4,10 +4,6 @@
import Network import Network
import Foundation import Foundation
enum DNSResolverError: Error {
case dnsResolutionFailed(hostnames: [String])
}
class DNSResolver { class DNSResolver {
static func isAllEndpointsAlreadyResolved(endpoints: [Endpoint?]) -> Bool { static func isAllEndpointsAlreadyResolved(endpoints: [Endpoint?]) -> Bool {
@ -20,7 +16,7 @@ class DNSResolver {
return true return true
} }
static func resolveSync(endpoints: [Endpoint?]) throws -> [Endpoint?] { static func resolveSync(endpoints: [Endpoint?]) -> [Endpoint?]? {
let dispatchGroup = DispatchGroup() let dispatchGroup = DispatchGroup()
if isAllEndpointsAlreadyResolved(endpoints: endpoints) { if isAllEndpointsAlreadyResolved(endpoints: endpoints) {
@ -56,7 +52,8 @@ class DNSResolver {
} }
} }
if !hostnamesWithDnsResolutionFailure.isEmpty { if !hostnamesWithDnsResolutionFailure.isEmpty {
throw DNSResolverError.dnsResolutionFailed(hostnames: hostnamesWithDnsResolutionFailure) wg_log(.error, message: "DNS resolution failed for the following hostnames: \(hostnamesWithDnsResolutionFailure.joined(separator: ", "))")
return nil
} }
return resolvedEndpoints return resolvedEndpoints
} }

View File

@ -58,11 +58,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
errorNotifier.tunnelName = tunnelName errorNotifier.tunnelName = tunnelName
let endpoints = tunnelConfiguration.peers.map { $0.endpoint } let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
guard let resolvedEndpoints = resolveDomainNames(endpoints: endpoints) else { guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
wg_log(.error, staticMessage: "Starting tunnel failed: DNS resolution failure") errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
let dnsError = PacketTunnelProviderError.dnsResolutionFailure startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure)
errorNotifier.notify(dnsError)
startTunnelCompletionHandler(dnsError)
return return
} }
assert(endpoints.count == resolvedEndpoints.count) assert(endpoints.count == resolvedEndpoints.count)
@ -167,18 +165,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} }
} }
private func resolveDomainNames(endpoints: [Endpoint?]) -> [Endpoint?]? {
do {
return try DNSResolver.resolveSync(endpoints: endpoints)
} catch DNSResolverError.dnsResolutionFailed(let hostnames) {
wg_log(.error, message: "DNS resolution failed for the following hostnames: \(hostnames.joined(separator: ", "))")
} catch {
// There can be no other errors from DNSResolver.resolveSync()
fatalError()
}
return nil
}
private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 { private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 {
return withStringsAsGoStrings(interfaceName, settings) { return wgTurnOn($0.0, $0.1, fileDescriptor) } return withStringsAsGoStrings(interfaceName, settings) { return wgTurnOn($0.0, $0.1, fileDescriptor) }
} }