NE: Simplify DNS resolution
This commit is contained in:
parent
6b48cb3095
commit
b02754a7bd
|
@ -4,10 +4,6 @@
|
|||
import Network
|
||||
import Foundation
|
||||
|
||||
enum DNSResolverError: Error {
|
||||
case dnsResolutionFailed(hostnames: [String])
|
||||
}
|
||||
|
||||
class DNSResolver {
|
||||
|
||||
static func isAllEndpointsAlreadyResolved(endpoints: [Endpoint?]) -> Bool {
|
||||
|
@ -20,7 +16,7 @@ class DNSResolver {
|
|||
return true
|
||||
}
|
||||
|
||||
static func resolveSync(endpoints: [Endpoint?]) throws -> [Endpoint?] {
|
||||
static func resolveSync(endpoints: [Endpoint?]) -> [Endpoint?]? {
|
||||
let dispatchGroup = DispatchGroup()
|
||||
|
||||
if isAllEndpointsAlreadyResolved(endpoints: endpoints) {
|
||||
|
@ -56,7 +52,8 @@ class DNSResolver {
|
|||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -58,11 +58,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||
errorNotifier.tunnelName = tunnelName
|
||||
|
||||
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
||||
guard let resolvedEndpoints = resolveDomainNames(endpoints: endpoints) else {
|
||||
wg_log(.error, staticMessage: "Starting tunnel failed: DNS resolution failure")
|
||||
let dnsError = PacketTunnelProviderError.dnsResolutionFailure
|
||||
errorNotifier.notify(dnsError)
|
||||
startTunnelCompletionHandler(dnsError)
|
||||
guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
|
||||
errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
|
||||
startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure)
|
||||
return
|
||||
}
|
||||
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 {
|
||||
return withStringsAsGoStrings(interfaceName, settings) { return wgTurnOn($0.0, $0.1, fileDescriptor) }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue