j00*_*057 4 windows dns split-tunnel wireguard
我想在 Windows 上使用 Wireguard 实现“拆分 DNS”,其中特定域的 DNS 请求由可通过 Wireguard 隧道访问的特定 DNS 服务器解析,而其他 DNS 请求由普通 DNS 服务器解析。
我该如何在 Windows 上执行此操作?在 Linux 上,我将使用自定义调用来resolvconf
代替使用DNS=
for 选项wg-quick
(请参阅wg-quick(8))。
您可以使用Windows的NRPT(网络策略响应表)将特定区域中的域名路由到特定的DNS服务器。NRPT 还有许多我们将用于此用例的其他功能。
为此,我们将使用“过程:使用 Powershell 配置 NRPT”,利用一些简单的 Powershell 脚本以及Powershell dnsclient 参考Add-DnsClientNrptRule
中记录的、Get-DnsClientNrptRule
和Remove-DnsClientNrptRule
cmdlet 。
因此,让我们为名为example-tunnel
(隧道名称并不重要)的隧道配置 NRPT,我们希望将所有 DNS 名称路由example.com
到侦听以下位置的 DNS 服务器172.16.2.53
:
[Interface]
PrivateKey = KGtxx2By12kE/Ru0qkhM/41H0Lu2JzvCSB8dM61MIX0=
Address = 172.16.1.2/32
PostUp = powershell.exe -Command "& { Add-DnsClientNrptRule -Comment 'wg-example-tunnel' -Namespace '.example.com' -NameServers 172.16.2.53 }"
PostDown = powershell.exe -Command "& { Get-DnsClientNrptRule | where Comment -eq 'wg-example-tunnel' | foreach { Remove-DnsClientNrptRule -Name $_.Name -Force } }"
[Peer]
PublicKey = +ZDFdUwa6NZwI8YVQewnl1bBi1D2qKor8/JPLwj6m0=
Endpoint = 203.0.113.234:51820
AllowedIPs = 172.16.1.0/24, 172.16.2.0/24
Run Code Online (Sandbox Code Playgroud)
启用隧道后,应该有一个 NRPT 规则,应如下所示:
PS C:\Users\joost> Get-DnsClientNrptRule | fl -Property Name,Namespace,NameServers,Comment
Name : {6105290F-37C2-439C-A25E-F62A8DCE22AC}
Namespace : {.example.com}
NameServers : 172.16.2.53
Comment : wg-example-tunnel
Run Code Online (Sandbox Code Playgroud)
所以这是PostUp=
命令:
Add-DnsClientNrptRule -Comment 'wg-example-tunnel' -Namespace '.example.com' -NameServers 172.16.2.53
Run Code Online (Sandbox Code Playgroud)
它使用管理注释设置路由规则,以便我们稍后找到并删除它。
这是PostDown=
命令:
Get-DnsClientNrptRule `
| where Comment -eq 'wg-example-tunnel' `
| foreach { Remove-DnsClientNrptRule -Name $_.Name -Force }
Run Code Online (Sandbox Code Playgroud)
这是一个枚举所有 NRPT 规则、将其过滤到与命令中的管理注释相匹配的规则的管道PostUp=
,然后将其删除。
如果 Wireguard 阻止脚本执行,请创建一个注册表项以启用危险脚本执行。
if (-not (Test-Path -Path HKLM:\SOFTWARE\Wireguard)) {
New-Item -Path HKLM:\SOFTWARE\Wireguard -ItemType Directory -ErrorAction Stop
}
Set-ItemProperty -Path HKLM:\SOFTWARE\Wireguard -Name DangerousScriptExecution -Type DWord -Value 1
Get-ItemProperty -Path HKLM:\SOFTWARE\Wireguard
Run Code Online (Sandbox Code Playgroud)
在启用此功能之前,请务必阅读 Wireguard-Windows 的admin-registry.md文档中的安全警告!