Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.zx2c4.com (lists.zx2c4.com [165.227.139.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2404DCA5FFF for ; Mon, 5 Oct 2026 11:15:41 +0000 (UTC) Received: by lists.zx2c4.com (OpenSMTPD) with ESMTP id f7e71310; Mon, 5 Oct 2026 07:00:08 +0000 (UTC) Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [2a00:1450:4864:20::42e]) by lists.zx2c4.com (OpenSMTPD) with ESMTPS id bd0e826a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 10 Jul 2026 13:22:56 +0000 (UTC) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-4720d22c94aso899921f8f.1 for ; Fri, 10 Jul 2026 06:22:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783689776; x=1784294576; darn=lists.zx2c4.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=e8DbLy3F3I4hbA4JWkUsvbW3rOvsY6jvkMfCBMbRMYM=; b=hslbjLudKmwApViTC2C2uTQH/reylSBeoV+C39im2aaWkxhNKwvgkMqNEbVtXiEd2S HMKa/AgrUnm1L1dnoLN5clzauoR6w5KV2M4b9nbT+pGMD9dMnqDlF1byvARj2Im8A56Q IHlhJORAPC+kgUDLoAdsXLRsMiidaBltlwghXZt3V0C7rjULwAR7+K7SFYFpTE66rRrY BZoIF3h3+46JDnDs8nsWZaf8clkSvzyaWs1qiRK4av7Hw/A7gGCh64AbqHEBRIA5/vVp 99VDinUMj+/CWAw9rS+qV5jZSnBbIg8JYAeLg+G4VGJmlUpSKq0N8LxyKoHX6M33/tIK Eqbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783689776; x=1784294576; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=e8DbLy3F3I4hbA4JWkUsvbW3rOvsY6jvkMfCBMbRMYM=; b=b1ChgenJwclANzs6P1y4bHpTwllQ/G75vAEoVqV72ozlCyGbmlj+WmFO0QUvmfdswl +BEll2G6hezdsfgQ5XiZ5yADeGUbF29iRe5Nf0yP89sbc1pyVPsW6zU3avdFz+unjXIv tLJePMf/PorlzJCNgoDFwgyyHqy/RFeQNBfXIUtUU9eaL6MQelZKrhVzXmtlWhCMBgkY GCVtdEj9yZqNlxq+q9+5FxskPEjoQXSefluV6hN5MSzAWMUw/KHuEUSsFTMezwBZTolC gJ52+DElwOAuDRV01pUr+y2xwMR0Q0Pmmha/xxtmKx0q6W5UyhTrwXGQT76kyRmgFg+U JYwQ== X-Gm-Message-State: AOJu0YyPzhL8CUuw2nQ8X+zCr+PyWw+L+awxhwipjdC8MwfN5EzzVHES yQdiVAga2yryoG8lMiu/LWsJJi8sUtTKqsK0X3DkSi8U/g3ggVotzuOE X-Gm-Gg: AfdE7cmGQirvBWi2NPLGTWEg1GdfsqTxSodEFiettGlTfenY9mu1HM2/T0U1XvLB0j1 0dPWGZ08bwCC8Iq79db5oFQMS208Yp/nyGxCZjkOL600Ky9H8PeyX+bxW31NoFxr3Yg1FvWqv6H LGNMLgCmfn8TF9djFVcRYN2TWsPbtMKfNRQLtssxHGYD5seWWdupSOEpbExokHRPz3fSkzDpMXg 8qjh7+GuwqNn9AfSW+u/KPQWSyhmgmX7/ze8ad3IWLKhTi1P7j8UCJhUBSGRIHmFvO1lNB57Uke fFJoLe+gVLCMBhFOWPqdyMQQ43iwyo6eNgK9HuWW8SCQYmHAi6Z93Zrr3Uqg507YbJ5E0J20Jvx 3g+oaQDtPGZ+IyG4Rrl1rj9FO6UO2xxspRKP0UsBejxEOCFjrp8/QVYQvU1Ub0jXAuGhYsHR7Oa p/tqyIiZQxOVxEG7hGKUkaJ69fkvz9ZcS4mmNtwN6TxuI/HMTnxQ== X-Received: by 2002:a5d:5e91:0:b0:475:f0d1:eb59 with SMTP id ffacd0b85a97d-47df07b5eb1mr13554188f8f.52.1783689775495; Fri, 10 Jul 2026 06:22:55 -0700 (PDT) Received: from localhost.localdomain ([79.118.195.86]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47aa039bcdasm62653868f8f.21.2026.07.10.06.22.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 10 Jul 2026 06:22:54 -0700 (PDT) From: Razvan Cremenescu To: "Jason A . Donenfeld" Cc: wireguard@lists.zx2c4.com, Razvan Cremenescu Subject: [PATCH wireguard-apple 7/7] Add opt-in split DNS support Date: Fri, 10 Jul 2026 16:22:39 +0300 Message-ID: <20260710132242.39814-8-razvan.cremenescu@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260710132242.39814-1-razvan.cremenescu@gmail.com> References: <20260710132242.39814-1-razvan.cremenescu@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Mon, 05 Oct 2026 06:59:44 +0000 X-BeenThere: wireguard@lists.zx2c4.com X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: Development discussion of WireGuard List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: wireguard-bounces@lists.zx2c4.com Sender: "WireGuard" When a tunnel lists DNS search domains (the non-IP entries of the wg-quick DNS= line), the tunnel's DNS servers have so far always been installed as the system-wide resolver (match domains [""]) while the search domains only served for completing short names. With multiple simultaneous tunnels this makes concurrently active tunnels fight over system DNS, and there was no way to use a tunnel's DNS only for the internal domains behind that tunnel. Add a per-tunnel Split DNS toggle, stored in the provider configuration alongside the existing keys so the wg-quick config text remains untouched and portable. When enabled and the tunnel has search domains, the DNS servers are installed with the search domains as match domains, so only queries for those domains go through the tunnel; everything else stays on the system resolver. The default remains the current match-everything behavior, so existing full-tunnel setups keep their leak-free DNS. The toggle is exposed as a checkbox in the macOS tunnel editor and as a switch in the iOS tunnel editor, and toggling it restarts an active tunnel the same way a configuration edit does. Signed-off-by: Razvan Cremenescu --- .../NETunnelProviderProtocol+Extension.swift | 26 +++++++++++++ .../Base.lproj/Localizable.strings | 6 +++ .../WireGuardApp/Tunnel/TunnelsManager.swift | 17 +++++++- .../TunnelEditTableViewController.swift | 39 ++++++++++++++++++- .../TunnelEditViewController.swift | 21 +++++++++- .../PacketTunnelSettingsGenerator.swift | 12 +++++- Sources/WireGuardKit/WireGuardAdapter.swift | 13 ++++++- .../PacketTunnelProvider.swift | 2 +- 8 files changed, 125 insertions(+), 11 deletions(-) diff --git a/Sources/Shared/Model/NETunnelProviderProtocol+Extension.swift b/Sources/Shared/Model/NETunnelProviderProtocol+Extension.swift index 0a303f4..8f56434 100644 --- a/Sources/Shared/Model/NETunnelProviderProtocol+Extension.swift +++ b/Sources/Shared/Model/NETunnelProviderProtocol+Extension.swift @@ -12,6 +12,21 @@ enum PacketTunnelProviderError: String, Error { } extension NETunnelProviderProtocol { + private static let splitDNSKey = "SplitDNS" + + /// When enabled, the tunnel's DNS servers are used only for the tunnel's + /// DNS search domains rather than for all queries on the system. + var isSplitDNSEnabled: Bool { + get { + return providerConfiguration?[Self.splitDNSKey] as? Bool ?? false + } + set { + var configuration = providerConfiguration ?? [:] + configuration[Self.splitDNSKey] = newValue + providerConfiguration = configuration + } + } + convenience init?(tunnelConfiguration: TunnelConfiguration, previouslyFrom old: NEVPNProtocol? = nil) { self.init() @@ -25,6 +40,9 @@ extension NETunnelProviderProtocol { #if os(macOS) providerConfiguration = ["UID": getuid()] #endif + if let old = old as? NETunnelProviderProtocol, old.isSplitDNSEnabled { + isSplitDNSEnabled = true + } let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint } if endpoints.count == 1 { @@ -64,6 +82,7 @@ extension NETunnelProviderProtocol { * around so that .mobileconfig files are easier. */ if let oldConfig = providerConfiguration?["WgQuickConfig"] as? String { + let wasSplitDNSEnabled = isSplitDNSEnabled #if os(macOS) providerConfiguration = ["UID": getuid()] #elseif os(iOS) @@ -71,6 +90,9 @@ extension NETunnelProviderProtocol { #else #error("Unimplemented") #endif + if wasSplitDNSEnabled { + isSplitDNSEnabled = true + } guard passwordReference == nil else { return true } wg_log(.info, message: "Migrating tunnel configuration '\(name)'") passwordReference = Keychain.makeReference(containing: oldConfig, called: name) @@ -78,7 +100,11 @@ extension NETunnelProviderProtocol { } #if os(macOS) if passwordReference != nil && providerConfiguration?["UID"] == nil && verifyConfigurationReference() { + let wasSplitDNSEnabled = isSplitDNSEnabled providerConfiguration = ["UID": getuid()] + if wasSplitDNSEnabled { + isSplitDNSEnabled = true + } return true } #elseif os(iOS) diff --git a/Sources/WireGuardApp/Base.lproj/Localizable.strings b/Sources/WireGuardApp/Base.lproj/Localizable.strings index 2e23a91..cacb316 100644 --- a/Sources/WireGuardApp/Base.lproj/Localizable.strings +++ b/Sources/WireGuardApp/Base.lproj/Localizable.strings @@ -97,6 +97,12 @@ "tunnelPeerLastHandshakeTime" = "Latest handshake"; "tunnelPeerExcludePrivateIPs" = "Exclude private IPs"; +"tunnelSectionTitleSplitDNS" = "DNS"; +"tunnelEditSplitDNS" = "Split DNS"; +"tunnelEditSplitDNSTooltip" = "Use the tunnel's DNS servers only for the tunnel's DNS search domains, instead of for all queries on the system."; +"alertSplitDNSRequiresSearchDomainsTitle" = "Split DNS requires DNS servers and search domains"; +"alertSplitDNSRequiresSearchDomainsMessage" = "To use split DNS, list both the DNS servers and the domains to route through them in the tunnel's DNS entry, e.g.: 'DNS = 10.0.0.53, corp.example.com'"; + "tunnelSectionTitleOnDemand" = "On-Demand Activation"; "tunnelOnDemandCellular" = "Cellular"; diff --git a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift index 63e1847..18829fe 100644 --- a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift +++ b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift @@ -119,7 +119,7 @@ class TunnelsManager { } } - func add(tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption = .off, completionHandler: @escaping (Result) -> Void) { + func add(tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption = .off, isSplitDNSEnabled: Bool = false, completionHandler: @escaping (Result) -> Void) { let tunnelName = tunnelConfiguration.name ?? "" if tunnelName.isEmpty { completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty)) @@ -133,6 +133,7 @@ class TunnelsManager { let tunnelProviderManager = NETunnelProviderManager() tunnelProviderManager.setTunnelConfiguration(tunnelConfiguration) + (tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.isSplitDNSEnabled = isSplitDNSEnabled tunnelProviderManager.isEnabled = true onDemandOption.apply(on: tunnelProviderManager) @@ -210,6 +211,7 @@ class TunnelsManager { func modify(tunnel: TunnelContainer, tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption, + isSplitDNSEnabled: Bool? = nil, shouldEnsureOnDemandEnabled: Bool = false, completionHandler: @escaping (TunnelsManagerError?) -> Void) { let tunnelName = tunnelConfiguration.name ?? "" @@ -224,7 +226,8 @@ class TunnelsManager { if isIntroducingOnDemandRules && tunnel.status != .inactive && tunnel.status != .deactivating { tunnel.onDeactivated = { [weak self] in self?.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, - onDemandOption: onDemandOption, shouldEnsureOnDemandEnabled: true, + onDemandOption: onDemandOption, isSplitDNSEnabled: isSplitDNSEnabled, + shouldEnsureOnDemandEnabled: true, completionHandler: completionHandler) } self.startDeactivation(of: tunnel) @@ -248,6 +251,12 @@ class TunnelsManager { tunnelProviderManager.setTunnelConfiguration(tunnelConfiguration) isTunnelConfigurationChanged = true } + if let isSplitDNSEnabled = isSplitDNSEnabled, + let proto = tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol, + proto.isSplitDNSEnabled != isSplitDNSEnabled { + proto.isSplitDNSEnabled = isSplitDNSEnabled + isTunnelConfigurationChanged = true + } tunnelProviderManager.isEnabled = true let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && shouldEnsureOnDemandEnabled @@ -637,6 +646,10 @@ class TunnelContainer: NSObject { return ActivateOnDemandOption(from: tunnelProvider) } + var isSplitDNSEnabled: Bool { + return (tunnelProvider.protocolConfiguration as? NETunnelProviderProtocol)?.isSplitDNSEnabled ?? false + } + #if os(macOS) var isTunnelAvailableToUser: Bool { return (tunnelProvider.protocolConfiguration as? NETunnelProviderProtocol)?.providerConfiguration?["UID"] as? uid_t == getuid() diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelEditTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelEditTableViewController.swift index dfb35c6..e79686f 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelEditTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelEditTableViewController.swift @@ -13,12 +13,14 @@ class TunnelEditTableViewController: UITableViewController { case interface case peer(_ peer: TunnelViewModel.PeerData) case addPeer + case splitDNS case onDemand static func == (lhs: Section, rhs: Section) -> Bool { switch (lhs, rhs) { case (.interface, .interface), (.addPeer, .addPeer), + (.splitDNS, .splitDNS), (.onDemand, .onDemand): return true case let (.peer(peerA), .peer(peerB)): @@ -53,6 +55,7 @@ class TunnelEditTableViewController: UITableViewController { let tunnel: TunnelContainer? let tunnelViewModel: TunnelViewModel var onDemandViewModel: ActivateOnDemandViewModel + private var isSplitDNSEnabled: Bool private var sections = [Section]() // Use this initializer to edit an existing tunnel. @@ -61,6 +64,7 @@ class TunnelEditTableViewController: UITableViewController { self.tunnel = tunnel tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) onDemandViewModel = ActivateOnDemandViewModel(tunnel: tunnel) + isSplitDNSEnabled = tunnel.isSplitDNSEnabled super.init(style: .grouped) loadSections() } @@ -71,6 +75,7 @@ class TunnelEditTableViewController: UITableViewController { tunnel = nil tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil) onDemandViewModel = ActivateOnDemandViewModel() + isSplitDNSEnabled = false super.init(style: .grouped) loadSections() } @@ -100,6 +105,7 @@ class TunnelEditTableViewController: UITableViewController { interfaceFieldsBySection.forEach { _ in sections.append(.interface) } tunnelViewModel.peersData.forEach { sections.append(.peer($0)) } sections.append(.addPeer) + sections.append(.splitDNS) sections.append(.onDemand) } @@ -113,10 +119,14 @@ class TunnelEditTableViewController: UITableViewController { ErrorPresenter.showErrorAlert(title: alertTitle, message: errorMessage, from: self) tableView.reloadData() // Highlight erroring fields case .saved(let tunnelConfiguration): + if isSplitDNSEnabled && (tunnelConfiguration.interface.dns.isEmpty || tunnelConfiguration.interface.dnsSearch.filter({ !$0.isEmpty }).isEmpty) { + ErrorPresenter.showErrorAlert(title: tr("alertSplitDNSRequiresSearchDomainsTitle"), message: tr("alertSplitDNSRequiresSearchDomainsMessage"), from: self) + return + } let onDemandOption = onDemandViewModel.toOnDemandOption() if let tunnel = tunnel { // We're modifying an existing tunnel - tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] error in + tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption, isSplitDNSEnabled: isSplitDNSEnabled) { [weak self] error in if let error = error { ErrorPresenter.showErrorAlert(error: error, from: self) } else { @@ -126,7 +136,7 @@ class TunnelEditTableViewController: UITableViewController { } } else { // We're adding a new tunnel - tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in + tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption, isSplitDNSEnabled: isSplitDNSEnabled) { [weak self] result in switch result { case .failure(let error): ErrorPresenter.showErrorAlert(error: error, from: self) @@ -161,6 +171,8 @@ extension TunnelEditTableViewController { return peerFieldsToShow.count case .addPeer: return 1 + case .splitDNS: + return 1 case .onDemand: if onDemandViewModel.isWiFiInterfaceEnabled { return 3 @@ -178,11 +190,22 @@ extension TunnelEditTableViewController { return tr("tunnelSectionTitlePeer") case .addPeer: return nil + case .splitDNS: + return tr("tunnelSectionTitleSplitDNS") case .onDemand: return tr("tunnelSectionTitleOnDemand") } } + override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { + switch sections[section] { + case .splitDNS: + return tr("tunnelEditSplitDNSTooltip") + default: + return nil + } + } + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch sections[indexPath.section] { case .interface: @@ -191,6 +214,8 @@ extension TunnelEditTableViewController { return peerCell(for: tableView, at: indexPath, with: peerData) case .addPeer: return addPeerCell(for: tableView, at: indexPath) + case .splitDNS: + return splitDNSCell(for: tableView, at: indexPath) case .onDemand: return onDemandCell(for: tableView, at: indexPath) } @@ -423,6 +448,16 @@ extension TunnelEditTableViewController { return cell } + private func splitDNSCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { + let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath) + cell.message = tr("tunnelEditSplitDNS") + cell.isOn = isSplitDNSEnabled + cell.onSwitchToggled = { [weak self] isOn in + self?.isSplitDNSEnabled = isOn + } + return cell + } + private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { let field = onDemandFields[indexPath.row] if indexPath.row < 2 { diff --git a/Sources/WireGuardApp/UI/macOS/ViewController/TunnelEditViewController.swift b/Sources/WireGuardApp/UI/macOS/ViewController/TunnelEditViewController.swift index 851498a..8065bdc 100644 --- a/Sources/WireGuardApp/UI/macOS/ViewController/TunnelEditViewController.swift +++ b/Sources/WireGuardApp/UI/macOS/ViewController/TunnelEditViewController.swift @@ -60,6 +60,15 @@ class TunnelEditViewController: NSViewController { return checkbox }() + let splitDNSCheckbox: NSButton = { + let checkbox = NSButton() + checkbox.title = tr("tunnelEditSplitDNS") + checkbox.toolTip = tr("tunnelEditSplitDNSTooltip") + checkbox.setButtonType(.switch) + checkbox.state = .off + return checkbox + }() + let discardButton: NSButton = { let button = NSButton() button.title = tr("macEditDiscard") @@ -106,6 +115,7 @@ class TunnelEditViewController: NSViewController { // Editing an existing tunnel let tunnelConfiguration = tunnel.tunnelConfiguration! nameRow.value = tunnel.name + splitDNSCheckbox.state = tunnel.isSplitDNSEnabled ? .on : .off textView.string = tunnelConfiguration.asWgQuickConfig() publicKeyRow.value = tunnelConfiguration.interface.privateKey.publicKey.base64Key textView.privateKeyString = tunnelConfiguration.interface.privateKey.base64Key @@ -163,6 +173,7 @@ class TunnelEditViewController: NSViewController { let buttonRowStackView = NSStackView() buttonRowStackView.setViews([discardButton, saveButton], in: .trailing) + buttonRowStackView.addView(splitDNSCheckbox, in: .leading) buttonRowStackView.addView(excludePrivateIPsCheckbox, in: .leading) buttonRowStackView.orientation = .horizontal buttonRowStackView.spacing = internalSpacing @@ -229,11 +240,17 @@ class TunnelEditViewController: NSViewController { } } + let isSplitDNSEnabled = splitDNSCheckbox.state == .on + if isSplitDNSEnabled && (tunnelConfiguration.interface.dns.isEmpty || tunnelConfiguration.interface.dnsSearch.filter({ !$0.isEmpty }).isEmpty) { + ErrorPresenter.showErrorAlert(title: tr("alertSplitDNSRequiresSearchDomainsTitle"), message: tr("alertSplitDNSRequiresSearchDomainsMessage"), from: self) + return + } + setUserInteractionEnabled(false) if let tunnel = tunnel { // We're modifying an existing tunnel - tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] error in + tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption, isSplitDNSEnabled: isSplitDNSEnabled) { [weak self] error in guard let self = self else { return } self.setUserInteractionEnabled(true) if let error = error { @@ -245,7 +262,7 @@ class TunnelEditViewController: NSViewController { } } else { // We're creating a new tunnel - self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in + self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption, isSplitDNSEnabled: isSplitDNSEnabled) { [weak self] result in guard let self = self else { return } self.setUserInteractionEnabled(true) switch result { diff --git a/Sources/WireGuardKit/PacketTunnelSettingsGenerator.swift b/Sources/WireGuardKit/PacketTunnelSettingsGenerator.swift index 3658956..36c0e06 100644 --- a/Sources/WireGuardKit/PacketTunnelSettingsGenerator.swift +++ b/Sources/WireGuardKit/PacketTunnelSettingsGenerator.swift @@ -15,10 +15,12 @@ typealias EndpointResolutionResult = Result<(Endpoint, Endpoint), DNSResolutionE class PacketTunnelSettingsGenerator { let tunnelConfiguration: TunnelConfiguration let resolvedEndpoints: [Endpoint?] + let isSplitDNSEnabled: Bool - init(tunnelConfiguration: TunnelConfiguration, resolvedEndpoints: [Endpoint?]) { + init(tunnelConfiguration: TunnelConfiguration, resolvedEndpoints: [Endpoint?], isSplitDNSEnabled: Bool = false) { self.tunnelConfiguration = tunnelConfiguration self.resolvedEndpoints = resolvedEndpoints + self.isSplitDNSEnabled = isSplitDNSEnabled } func endpointUapiConfiguration() -> (String, [EndpointResolutionResult?]) { @@ -88,7 +90,13 @@ class PacketTunnelSettingsGenerator { let dnsSettings = NEDNSSettings(servers: dnsServerStrings) dnsSettings.searchDomains = tunnelConfiguration.interface.dnsSearch if !tunnelConfiguration.interface.dns.isEmpty { - dnsSettings.matchDomains = [""] // All DNS queries must first go through the tunnel's DNS + let splitDNSMatchDomains = isSplitDNSEnabled ? tunnelConfiguration.interface.dnsSearch.filter { !$0.isEmpty } : [] + if !splitDNSMatchDomains.isEmpty { + // Use the tunnel's DNS only for the tunnel's search domains + dnsSettings.matchDomains = splitDNSMatchDomains + } else { + dnsSettings.matchDomains = [""] // All DNS queries must first go through the tunnel's DNS + } } networkSettings.dnsSettings = dnsSettings } diff --git a/Sources/WireGuardKit/WireGuardAdapter.swift b/Sources/WireGuardKit/WireGuardAdapter.swift index ba49ec7..82728e4 100644 --- a/Sources/WireGuardKit/WireGuardAdapter.swift +++ b/Sources/WireGuardKit/WireGuardAdapter.swift @@ -56,6 +56,10 @@ public class WireGuardAdapter { /// Adapter state. private var state: State = .stopped + /// Whether the tunnel's DNS servers should be used only for the tunnel's + /// DNS search domains rather than for all queries on the system. + private var isSplitDNSEnabled = false + /// Tunnel device file descriptors claimed by the adapters in this process. /// When multiple tunnels run in a single extension process, each adapter /// must attach to its own utun device rather than to the first one found. @@ -265,14 +269,18 @@ public class WireGuardAdapter { /// Start the tunnel tunnel. /// - Parameters: /// - tunnelConfiguration: tunnel configuration. + /// - isSplitDNSEnabled: use the tunnel's DNS servers only for the + /// tunnel's DNS search domains rather than for all queries. /// - completionHandler: completion handler. - public func start(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) { + public func start(tunnelConfiguration: TunnelConfiguration, isSplitDNSEnabled: Bool = false, completionHandler: @escaping (WireGuardAdapterError?) -> Void) { workQueue.async { guard case .stopped = self.state else { completionHandler(.invalidState) return } + self.isSplitDNSEnabled = isSplitDNSEnabled + let networkMonitor = NWPathMonitor() networkMonitor.pathUpdateHandler = { [weak self] path in self?.didReceivePathUpdate(path: path) @@ -486,7 +494,8 @@ public class WireGuardAdapter { private func makeSettingsGenerator(with tunnelConfiguration: TunnelConfiguration) throws -> PacketTunnelSettingsGenerator { return PacketTunnelSettingsGenerator( tunnelConfiguration: tunnelConfiguration, - resolvedEndpoints: try self.resolvePeers(for: tunnelConfiguration) + resolvedEndpoints: try self.resolvePeers(for: tunnelConfiguration), + isSplitDNSEnabled: isSplitDNSEnabled ) } diff --git a/Sources/WireGuardNetworkExtension/PacketTunnelProvider.swift b/Sources/WireGuardNetworkExtension/PacketTunnelProvider.swift index fe09026..ac8050c 100644 --- a/Sources/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/Sources/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -68,7 +68,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider { } // Start the tunnel - adapter.start(tunnelConfiguration: tunnelConfiguration) { adapterError in + adapter.start(tunnelConfiguration: tunnelConfiguration, isSplitDNSEnabled: tunnelProviderProtocol.isSplitDNSEnabled) { adapterError in guard let adapterError = adapterError else { let interfaceName = self.adapter.interfaceName ?? "unknown" -- 2.50.1 (Apple Git-155)