VPN: If we don't have to make a DNS request, we shouldn't enter that status
This commit is contained in:
parent
5b85d58b27
commit
3e1748bdd9
|
@ -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,6 +261,21 @@ class TunnelContainer: NSObject {
|
|||
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
||||
let dnsResolver = DNSResolver(endpoints: endpoints)
|
||||
assert(self.dnsResolver == nil)
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.dnsResolver = dnsResolver
|
||||
status = .resolvingEndpointDomains
|
||||
dnsResolver.resolve { [weak self] endpoints in
|
||||
|
@ -287,6 +302,7 @@ class TunnelContainer: NSObject {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func startDeactivation() {
|
||||
assert(status == .active)
|
||||
|
|
Loading…
Reference in New Issue