VPN: If we don't have to make a DNS request, we shouldn't enter that status

This commit is contained in:
Roopesh Chander 2018-10-29 06:00:31 +05:30
parent 5b85d58b27
commit 3e1748bdd9
2 changed files with 43 additions and 13 deletions

View File

@ -16,6 +16,20 @@ class DNSResolver {
self.dispatchGroup = DispatchGroup() 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) { func resolve(completionHandler: @escaping ([Endpoint?]?) -> Void) {
let endpoints = self.endpoints let endpoints = self.endpoints
let dispatchGroup = self.dispatchGroup let dispatchGroup = self.dispatchGroup

View File

@ -261,6 +261,21 @@ class TunnelContainer: NSObject {
let endpoints = tunnelConfiguration.peers.map { $0.endpoint } let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
let dnsResolver = DNSResolver(endpoints: endpoints) let dnsResolver = DNSResolver(endpoints: endpoints)
assert(self.dnsResolver == nil) 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 self.dnsResolver = dnsResolver
status = .resolvingEndpointDomains status = .resolvingEndpointDomains
dnsResolver.resolve { [weak self] endpoints in dnsResolver.resolve { [weak self] endpoints in
@ -287,6 +302,7 @@ class TunnelContainer: NSObject {
} }
} }
} }
}
fileprivate func startDeactivation() { fileprivate func startDeactivation() {
assert(status == .active) assert(status == .active)