VPN: If we don't have to make a DNS request, we shouldn't enter that status
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
parent
21ac338f64
commit
b7aaae7513
|
@ -16,6 +16,20 @@ 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() {
|
||||
guard let endpoint = endpoint else { continue }
|
||||
if let resolvedEndpointStringInCache = DNSResolver.cache.object(forKey: endpoint.stringRepresentation() as NSString),
|
||||
let resolvedEndpointInCache = Endpoint(from: resolvedEndpointStringInCache as String) {
|
||||
resolvedEndpoints[i] = resolvedEndpointInCache
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return resolvedEndpoints
|
||||
}
|
||||
|
||||
func resolve(completionHandler: @escaping ([Endpoint?]?) -> Void) {
|
||||
let endpoints = self.endpoints
|
||||
let dispatchGroup = self.dispatchGroup
|
||||
|
|
|
@ -261,19 +261,9 @@ class TunnelContainer: NSObject {
|
|||
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 let s = self else { return }
|
||||
assert(s.status == .resolvingEndpointDomains)
|
||||
s.dnsResolver = nil
|
||||
guard let endpoints = endpoints else {
|
||||
completionHandler(TunnelsManagerError.dnsResolutionFailed)
|
||||
s.status = .inactive
|
||||
return
|
||||
}
|
||||
s.tunnelProvider.loadFromPreferences { [weak s] (error) in
|
||||
guard let s = s else { return }
|
||||
if let endpoints = dnsResolver.resolveWithoutNetworkRequests() {
|
||||
self.tunnelProvider.loadFromPreferences { [weak self] (error) in
|
||||
guard let s = self else { return }
|
||||
s.startObservingTunnelStatus()
|
||||
let session = (s.tunnelProvider.connection as! NETunnelProviderSession)
|
||||
do {
|
||||
|
@ -285,6 +275,32 @@ class TunnelContainer: NSObject {
|
|||
completionHandler(error)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.dnsResolver = dnsResolver
|
||||
status = .resolvingEndpointDomains
|
||||
dnsResolver.resolve { [weak self] endpoints in
|
||||
guard let s = self else { return }
|
||||
assert(s.status == .resolvingEndpointDomains)
|
||||
s.dnsResolver = nil
|
||||
guard let endpoints = endpoints else {
|
||||
completionHandler(TunnelsManagerError.dnsResolutionFailed)
|
||||
s.status = .inactive
|
||||
return
|
||||
}
|
||||
s.tunnelProvider.loadFromPreferences { [weak s] (error) in
|
||||
guard let s = s else { return }
|
||||
s.startObservingTunnelStatus()
|
||||
let session = (s.tunnelProvider.connection as! NETunnelProviderSession)
|
||||
do {
|
||||
let tunnelOptions = PacketTunnelOptionsGenerator.generateOptions(
|
||||
from: tunnelConfiguration, withResolvedEndpoints: endpoints)
|
||||
try session.startTunnel(options: tunnelOptions)
|
||||
} catch (let error) {
|
||||
os_log("Failed to activate tunnel: %{public}@", log: OSLog.default, type: .debug, "\(error)")
|
||||
completionHandler(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue