From 0340641c4c8d7be93e8f85be9827dec68711ab9a Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 25 May 2019 18:02:16 +0200 Subject: NetworkExtension: apparently the extension process is scoped properly anyway Signed-off-by: Jason A. Donenfeld --- .../PacketTunnelProvider.swift | 21 ++++++++++---------- .../PacketTunnelSettingsGenerator.swift | 23 ---------------------- 2 files changed, 10 insertions(+), 34 deletions(-) (limited to 'WireGuard') diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift index 9aa466f..46d5c33 100644 --- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -9,13 +9,17 @@ import os.log class PacketTunnelProvider: NEPacketTunnelProvider { private var handle: Int32? + #if os(iOS) private var networkMonitor: NWPathMonitor? + #endif private var ifname: String? private var packetTunnelSettingsGenerator: PacketTunnelSettingsGenerator? + #if os(iOS) deinit { networkMonitor?.cancel() } + #endif override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) { let activationAttemptId = options?["activationAttemptId"] as? String @@ -51,9 +55,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider { errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings) startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings) } else { + #if os(iOS) self.networkMonitor = NWPathMonitor() self.networkMonitor!.pathUpdateHandler = self.pathUpdate self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor")) + #endif let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1 if fileDescriptor < 0 { @@ -84,8 +90,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider { } override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { + #if os(iOS) networkMonitor?.cancel() networkMonitor = nil + #endif ErrorNotifier.removeLastErrorFile() @@ -140,23 +148,14 @@ class PacketTunnelProvider: NEPacketTunnelProvider { } } + #if os(iOS) private func pathUpdate(path: Network.NWPath) { guard let handle = handle else { return } - wg_log(.debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)") - #if os(iOS) if let packetTunnelSettingsGenerator = packetTunnelSettingsGenerator { _ = packetTunnelSettingsGenerator.endpointUapiConfiguration().withGoString { return wgSetConfig(handle, $0) } } - #elseif os(macOS) - var interfaces = path.availableInterfaces - if let ifname = ifname { - interfaces = interfaces.filter { $0.name != ifname } - } - if let ifscope = interfaces.first?.index { - wgBindInterfaceScope(handle, Int32(ifscope)) - } - #endif } + #endif } extension String { diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift index cc491af..a4ff7dd 100644 --- a/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift +++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift @@ -97,16 +97,13 @@ class PacketTunnelSettingsGenerator { let (ipv4Routes, ipv6Routes) = routes() let (ipv4IncludedRoutes, ipv6IncludedRoutes) = includedRoutes() - let (ipv4ExcludedRoutes, ipv6ExcludedRoutes) = excludedRoutes() let ipv4Settings = NEIPv4Settings(addresses: ipv4Routes.map { $0.destinationAddress }, subnetMasks: ipv4Routes.map { $0.destinationSubnetMask }) ipv4Settings.includedRoutes = ipv4IncludedRoutes - ipv4Settings.excludedRoutes = ipv4ExcludedRoutes networkSettings.ipv4Settings = ipv4Settings let ipv6Settings = NEIPv6Settings(addresses: ipv6Routes.map { $0.destinationAddress }, networkPrefixLengths: ipv6Routes.map { $0.destinationNetworkPrefixLength }) ipv6Settings.includedRoutes = ipv6IncludedRoutes - ipv6Settings.excludedRoutes = ipv6ExcludedRoutes networkSettings.ipv6Settings = ipv6Settings return networkSettings @@ -156,24 +153,4 @@ class PacketTunnelSettingsGenerator { } return (ipv4IncludedRoutes, ipv6IncludedRoutes) } - private func excludedRoutes() -> ([NEIPv4Route]?, [NEIPv6Route]?) { - #if os(macOS) - return (nil, nil) - #elseif os(iOS) - var ipv4ExcludedRoutes = [NEIPv4Route]() - var ipv6ExcludedRoutes = [NEIPv6Route]() - for endpoint in resolvedEndpoints { - guard let host = endpoint?.host else { continue } - switch host { - case .ipv4(let v4): - ipv4ExcludedRoutes.append(NEIPv4Route(destinationAddress: "\(v4)", subnetMask: "255.255.255.255")) - case .ipv6(let v6): - ipv6ExcludedRoutes.append(NEIPv6Route(destinationAddress: "\(v6)", networkPrefixLength: 128)) - default: - continue - } - } - return (ipv4ExcludedRoutes, ipv6ExcludedRoutes) - #endif - } } -- cgit v1.2.3-59-g8ed1b