Make sure that the tunnel and path monitor run on the same serial queue
Signed-off-by: Andrej Mihajlov <and@mullvad.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
e9bd6e576f
commit
3646430528
|
@ -8,6 +8,8 @@ import os.log
|
||||||
|
|
||||||
class PacketTunnelProvider: NEPacketTunnelProvider {
|
class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
|
|
||||||
|
private let dispatchQueue = DispatchQueue(label: "PacketTunnel")
|
||||||
|
|
||||||
private var handle: Int32?
|
private var handle: Int32?
|
||||||
private var networkMonitor: NWPathMonitor?
|
private var networkMonitor: NWPathMonitor?
|
||||||
private var ifname: String?
|
private var ifname: String?
|
||||||
|
@ -18,105 +20,115 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
||||||
let activationAttemptId = options?["activationAttemptId"] as? String
|
dispatchQueue.async {
|
||||||
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId)
|
let activationAttemptId = options?["activationAttemptId"] as? String
|
||||||
|
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId)
|
||||||
|
|
||||||
guard let tunnelProviderProtocol = protocolConfiguration as? NETunnelProviderProtocol,
|
guard let tunnelProviderProtocol = self.protocolConfiguration as? NETunnelProviderProtocol,
|
||||||
let tunnelConfiguration = tunnelProviderProtocol.asTunnelConfiguration() else {
|
let tunnelConfiguration = tunnelProviderProtocol.asTunnelConfiguration() else {
|
||||||
errorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
|
errorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
|
||||||
startTunnelCompletionHandler(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
|
startTunnelCompletionHandler(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.configureLogger()
|
||||||
|
#if os(macOS)
|
||||||
|
wgEnableRoaming(true)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
wg_log(.info, message: "Starting tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
|
||||||
|
|
||||||
|
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
||||||
|
guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
|
||||||
|
errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
|
||||||
|
startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
assert(endpoints.count == resolvedEndpoints.count)
|
||||||
|
|
||||||
configureLogger()
|
self.packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
|
||||||
#if os(macOS)
|
|
||||||
wgEnableRoaming(true)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
wg_log(.info, message: "Starting tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
|
self.setTunnelNetworkSettings(self.packetTunnelSettingsGenerator!.generateNetworkSettings()) { error in
|
||||||
|
self.dispatchQueue.async {
|
||||||
|
if let error = error {
|
||||||
|
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
|
||||||
|
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
||||||
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
||||||
|
} else {
|
||||||
|
self.networkMonitor = NWPathMonitor()
|
||||||
|
self.networkMonitor!.pathUpdateHandler = { [weak self] path in
|
||||||
|
self?.pathUpdate(path: path)
|
||||||
|
}
|
||||||
|
self.networkMonitor!.start(queue: self.dispatchQueue)
|
||||||
|
|
||||||
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
|
||||||
guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
|
if fileDescriptor < 0 {
|
||||||
errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
|
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
|
||||||
startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure)
|
errorNotifier.notify(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
||||||
return
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
||||||
}
|
return
|
||||||
assert(endpoints.count == resolvedEndpoints.count)
|
}
|
||||||
|
|
||||||
packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
|
self.ifname = Self.getInterfaceName(fileDescriptor: fileDescriptor)
|
||||||
|
wg_log(.info, message: "Tunnel interface is \(self.ifname ?? "unknown")")
|
||||||
|
|
||||||
setTunnelNetworkSettings(packetTunnelSettingsGenerator!.generateNetworkSettings()) { error in
|
let handle = self.packetTunnelSettingsGenerator!.uapiConfiguration()
|
||||||
if let error = error {
|
.withCString { return wgTurnOn($0, fileDescriptor) }
|
||||||
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
|
if handle < 0 {
|
||||||
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
wg_log(.error, message: "Starting tunnel failed with wgTurnOn returning \(handle)")
|
||||||
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
errorNotifier.notify(PacketTunnelProviderError.couldNotStartBackend)
|
||||||
} else {
|
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartBackend)
|
||||||
self.networkMonitor = NWPathMonitor()
|
return
|
||||||
self.networkMonitor!.pathUpdateHandler = { [weak self] path in
|
}
|
||||||
self?.pathUpdate(path: path)
|
self.handle = handle
|
||||||
|
startTunnelCompletionHandler(nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
|
||||||
|
|
||||||
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
|
|
||||||
if fileDescriptor < 0 {
|
|
||||||
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
|
|
||||||
errorNotifier.notify(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
|
||||||
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
self.ifname = Self.getInterfaceName(fileDescriptor: fileDescriptor)
|
|
||||||
wg_log(.info, message: "Tunnel interface is \(self.ifname ?? "unknown")")
|
|
||||||
|
|
||||||
let handle = self.packetTunnelSettingsGenerator!.uapiConfiguration()
|
|
||||||
.withCString { return wgTurnOn($0, fileDescriptor) }
|
|
||||||
if handle < 0 {
|
|
||||||
wg_log(.error, message: "Starting tunnel failed with wgTurnOn returning \(handle)")
|
|
||||||
errorNotifier.notify(PacketTunnelProviderError.couldNotStartBackend)
|
|
||||||
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartBackend)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
self.handle = handle
|
|
||||||
startTunnelCompletionHandler(nil)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
||||||
networkMonitor?.cancel()
|
dispatchQueue.async {
|
||||||
networkMonitor = nil
|
self.networkMonitor?.cancel()
|
||||||
|
self.networkMonitor = nil
|
||||||
|
|
||||||
ErrorNotifier.removeLastErrorFile()
|
ErrorNotifier.removeLastErrorFile()
|
||||||
|
|
||||||
wg_log(.info, staticMessage: "Stopping tunnel")
|
wg_log(.info, staticMessage: "Stopping tunnel")
|
||||||
if let handle = handle {
|
if let handle = self.handle {
|
||||||
wgTurnOff(handle)
|
wgTurnOff(handle)
|
||||||
|
}
|
||||||
|
completionHandler()
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
// HACK: This is a filthy hack to work around Apple bug 32073323 (dup'd by us as 47526107).
|
||||||
|
// Remove it when they finally fix this upstream and the fix has been rolled out to
|
||||||
|
// sufficient quantities of users.
|
||||||
|
exit(0)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
completionHandler()
|
|
||||||
|
|
||||||
#if os(macOS)
|
|
||||||
// HACK: This is a filthy hack to work around Apple bug 32073323 (dup'd by us as 47526107).
|
|
||||||
// Remove it when they finally fix this upstream and the fix has been rolled out to
|
|
||||||
// sufficient quantities of users.
|
|
||||||
exit(0)
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
||||||
guard let completionHandler = completionHandler else { return }
|
dispatchQueue.async {
|
||||||
guard let handle = handle else {
|
guard let completionHandler = completionHandler else { return }
|
||||||
completionHandler(nil)
|
guard let handle = self.handle else {
|
||||||
return
|
|
||||||
}
|
|
||||||
if messageData.count == 1 && messageData[0] == 0 {
|
|
||||||
guard let settings = wgGetConfig(handle) else {
|
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
completionHandler(String(cString: settings).data(using: .utf8)!)
|
|
||||||
free(settings)
|
if messageData.count == 1 && messageData[0] == 0 {
|
||||||
} else {
|
if let settings = wgGetConfig(handle) {
|
||||||
completionHandler(nil)
|
let data = String(cString: settings).data(using: .utf8)!
|
||||||
|
completionHandler(data)
|
||||||
|
free(settings)
|
||||||
|
} else {
|
||||||
|
completionHandler(nil)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
completionHandler(nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue