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,17 +20,18 @@ 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) {
|
||||||
|
dispatchQueue.async {
|
||||||
let activationAttemptId = options?["activationAttemptId"] as? String
|
let activationAttemptId = options?["activationAttemptId"] as? String
|
||||||
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
configureLogger()
|
self.configureLogger()
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
wgEnableRoaming(true)
|
wgEnableRoaming(true)
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,9 +46,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
}
|
}
|
||||||
assert(endpoints.count == resolvedEndpoints.count)
|
assert(endpoints.count == resolvedEndpoints.count)
|
||||||
|
|
||||||
packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
|
self.packetTunnelSettingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
|
||||||
|
|
||||||
setTunnelNetworkSettings(packetTunnelSettingsGenerator!.generateNetworkSettings()) { error in
|
self.setTunnelNetworkSettings(self.packetTunnelSettingsGenerator!.generateNetworkSettings()) { error in
|
||||||
|
self.dispatchQueue.async {
|
||||||
if let error = error {
|
if let error = error {
|
||||||
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
|
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
|
||||||
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
|
||||||
|
@ -55,7 +59,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
self.networkMonitor!.pathUpdateHandler = { [weak self] path in
|
self.networkMonitor!.pathUpdateHandler = { [weak self] path in
|
||||||
self?.pathUpdate(path: path)
|
self?.pathUpdate(path: path)
|
||||||
}
|
}
|
||||||
self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
self.networkMonitor!.start(queue: self.dispatchQueue)
|
||||||
|
|
||||||
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
|
let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1
|
||||||
if fileDescriptor < 0 {
|
if fileDescriptor < 0 {
|
||||||
|
@ -81,15 +85,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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()
|
completionHandler()
|
||||||
|
@ -101,23 +108,28 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
exit(0)
|
exit(0)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
||||||
|
dispatchQueue.async {
|
||||||
guard let completionHandler = completionHandler else { return }
|
guard let completionHandler = completionHandler else { return }
|
||||||
guard let handle = handle else {
|
guard let handle = self.handle else {
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if messageData.count == 1 && messageData[0] == 0 {
|
if messageData.count == 1 && messageData[0] == 0 {
|
||||||
guard let settings = wgGetConfig(handle) else {
|
if let settings = wgGetConfig(handle) {
|
||||||
completionHandler(nil)
|
let data = String(cString: settings).data(using: .utf8)!
|
||||||
return
|
completionHandler(data)
|
||||||
}
|
|
||||||
completionHandler(String(cString: settings).data(using: .utf8)!)
|
|
||||||
free(settings)
|
free(settings)
|
||||||
} else {
|
} else {
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
completionHandler(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class func getInterfaceName(fileDescriptor: Int32) -> String? {
|
private class func getInterfaceName(fileDescriptor: Int32) -> String? {
|
||||||
|
|
Loading…
Reference in New Issue