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:
Roopesh Chander 2018-10-29 06:00:31 +05:30
parent 21ac338f64
commit b7aaae7513
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,19 +261,9 @@ 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)
self.dnsResolver = dnsResolver if let endpoints = dnsResolver.resolveWithoutNetworkRequests() {
status = .resolvingEndpointDomains self.tunnelProvider.loadFromPreferences { [weak self] (error) in
dnsResolver.resolve { [weak self] endpoints in guard let s = self else { return }
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() s.startObservingTunnelStatus()
let session = (s.tunnelProvider.connection as! NETunnelProviderSession) let session = (s.tunnelProvider.connection as! NETunnelProviderSession)
do { do {
@ -285,6 +275,32 @@ class TunnelContainer: NSObject {
completionHandler(error) 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)
}
}
}
} }
} }