diff options
author | Roopesh Chander <roop@roopc.net> | 2018-11-08 16:31:42 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-11-08 17:52:11 +0530 |
commit | 8409b7e92976b6156a33e0d409ee79be4109e176 (patch) | |
tree | c63a1697693097f3c6ab91ee57b4b95c3632392b /WireGuard/WireGuardNetworkExtension/DNSResolver.swift | |
parent | DNSResolver: DNS resolution can now happen synchronously (diff) | |
download | wireguard-apple-8409b7e92976b6156a33e0d409ee79be4109e176.tar.xz wireguard-apple-8409b7e92976b6156a33e0d409ee79be4109e176.zip |
DNSResolver: Let's not cache DNS resolution results anymore
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r-- | WireGuard/WireGuardNetworkExtension/DNSResolver.swift | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift index 4181f75..d69b96d 100644 --- a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift +++ b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift @@ -12,7 +12,6 @@ class DNSResolver { let endpoints: [Endpoint?] let dispatchGroup: DispatchGroup var dispatchWorkItems: [DispatchWorkItem] - static var cache = NSCache<NSString, NSString>() init(endpoints: [Endpoint?]) { self.endpoints = endpoints @@ -20,20 +19,14 @@ class DNSResolver { self.dispatchGroup = DispatchGroup() } - func resolveWithoutNetworkRequests() -> [Endpoint?]? { - var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count) - for (i, endpoint) in self.endpoints.enumerated() { + func isAllEndpointsAlreadyResolved() -> Bool { + for endpoint in self.endpoints { guard let endpoint = endpoint else { continue } - if (endpoint.hasHostAsIPAddress()) { - resolvedEndpoints[i] = endpoint - } else if let resolvedEndpointStringInCache = DNSResolver.cache.object(forKey: endpoint.stringRepresentation() as NSString), - let resolvedEndpointInCache = Endpoint(from: resolvedEndpointStringInCache as String) { - resolvedEndpoints[i] = resolvedEndpointInCache - } else { - return nil + if (!endpoint.hasHostAsIPAddress()) { + return false } } - return resolvedEndpoints + return true } func resolveSync() throws -> [Endpoint?] { @@ -41,19 +34,18 @@ class DNSResolver { let dispatchGroup = self.dispatchGroup dispatchWorkItems = [] + if (isAllEndpointsAlreadyResolved()) { + return endpoints + } + var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count) - var isResolvedByDNSRequest: [Bool] = Array<Bool>(repeating: false, count: endpoints.count) for (i, endpoint) in self.endpoints.enumerated() { guard let endpoint = endpoint else { continue } if (endpoint.hasHostAsIPAddress()) { resolvedEndpoints[i] = endpoint - } else if let resolvedEndpointStringInCache = DNSResolver.cache.object(forKey: endpoint.stringRepresentation() as NSString), - let resolvedEndpointInCache = Endpoint(from: resolvedEndpointStringInCache as String) { - resolvedEndpoints[i] = resolvedEndpointInCache } else { let workItem = DispatchWorkItem { resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint) - isResolvedByDNSRequest[i] = true } dispatchWorkItems.append(workItem) DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem) |