mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-08-09 09:31:15 +10:00
Add DNS-over-HTTPS (DoH) support (#4856)
* feat: Add DoH templates for DNS providers in configuration * feat: Implement DoH support for DNS configuration and management * feat(pester): enhance DNS tests to include DoH server address handling and template application * fix: address codex feedback * fix: update existing DoH server entries * fix: enable DoH auto-upgrade * fix: handle DNS setup failures * fix: remove DoH state on DHCP reset
This commit is contained in:
@@ -33,23 +33,71 @@ function Set-WinUtilDNS {
|
||||
}
|
||||
}
|
||||
|
||||
$dohSupported = [bool](Get-Command Add-DnsClientDohServerAddress -ErrorAction SilentlyContinue)
|
||||
$dnscacheBase = "HKLM:\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters"
|
||||
|
||||
Foreach ($Adapter in $Adapters) {
|
||||
$interfaceParams = "$dnscacheBase\$($Adapter.InterfaceGuid)"
|
||||
|
||||
if($DNSProvider -eq "DHCP") {
|
||||
Write-WinUtilLog -Component "DNS" -Message "Resetting DNS to DHCP on adapter $($Adapter.Name) (ifIndex: $($Adapter.ifIndex))."
|
||||
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ResetServerAddresses
|
||||
netsh interface ip set dnsservers name="$($Adapter.Name)" source=dhcp
|
||||
netsh interface ipv6 set dnsservers name="$($Adapter.Name)" source=dhcp
|
||||
|
||||
$dohInterfaceSettings = "$interfaceParams\DohInterfaceSettings"
|
||||
if (Test-Path $dohInterfaceSettings) {
|
||||
if ($dohSupported) {
|
||||
$dohServerAddresses = @(
|
||||
Get-ChildItem -Path "$dohInterfaceSettings\Doh" -ErrorAction SilentlyContinue
|
||||
Get-ChildItem -Path "$dohInterfaceSettings\Doh6" -ErrorAction SilentlyContinue
|
||||
) | Select-Object -ExpandProperty PSChildName -Unique
|
||||
|
||||
foreach ($ip in $dohServerAddresses) {
|
||||
if (Get-DnsClientDohServerAddress -ServerAddress $ip -ErrorAction SilentlyContinue) {
|
||||
Write-WinUtilLog -Component "DNS" -Message "Removing DoH registration for $ip."
|
||||
Remove-DnsClientDohServerAddress -ServerAddress $ip -Confirm:$false -ErrorAction Stop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Remove-Item -Path $dohInterfaceSettings -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
} else {
|
||||
Write-WinUtilLog -Component "DNS" -Message "Setting IPv4 DNS on adapter $($Adapter.Name) (ifIndex: $($Adapter.ifIndex)) to $($dns.Primary), $($dns.Secondary)."
|
||||
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ServerAddresses ($dns.Primary, $dns.Secondary)
|
||||
Write-WinUtilLog -Component "DNS" -Message "Setting IPv6 DNS on adapter $($Adapter.Name) (ifIndex: $($Adapter.ifIndex)) to $($dns.Primary6), $($dns.Secondary6)."
|
||||
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ServerAddresses ($dns.Primary6, $dns.Secondary6)
|
||||
|
||||
if ($dohSupported -and $dns.DohTemplate) {
|
||||
$ips = @($dns.Primary, $dns.Secondary, $dns.Primary6, $dns.Secondary6) | Where-Object { $_ }
|
||||
foreach ($ip in $ips) {
|
||||
$existing = Get-DnsClientDohServerAddress -ServerAddress $ip -ErrorAction SilentlyContinue
|
||||
if ($existing) {
|
||||
Set-DnsClientDohServerAddress -ServerAddress $ip -DohTemplate $dns.DohTemplate -AllowFallbackToUdp $false -AutoUpgrade $true -ErrorAction Stop
|
||||
} else {
|
||||
Write-WinUtilLog -Component "DNS" -Message "Registering DoH template for $ip."
|
||||
Add-DnsClientDohServerAddress -ServerAddress $ip -DohTemplate $dns.DohTemplate -AllowFallbackToUdp $false -AutoUpgrade $true -ErrorAction Stop
|
||||
}
|
||||
|
||||
$leaf = if ($ip.Contains(':')) { 'Doh6' } else { 'Doh' }
|
||||
$regPath = "$interfaceParams\DohInterfaceSettings\$leaf\$ip"
|
||||
|
||||
if (-not (Test-Path $regPath)) {
|
||||
New-Item -Path $regPath -Force -ErrorAction Stop | Out-Null
|
||||
}
|
||||
New-ItemProperty -Path $regPath -Name "DohFlags" -Value 1 -PropertyType QWord -Force -ErrorAction Stop | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($DNSProvider -ne "DHCP" -and $dohSupported -and $dns.DohTemplate) {
|
||||
Clear-DnsClientCache
|
||||
}
|
||||
Write-WinUtilLog -Component "DNS" -Message "DNS provider change completed: $DNSProvider"
|
||||
} catch {
|
||||
Write-Warning "Unable to set DNS Provider due to an unhandled exception."
|
||||
Write-Warning $psitem.Exception.StackTrace
|
||||
Write-WinUtilLog -Level "ERROR" -Component "DNS" -Message "Unable to set DNS provider $DNSProvider`: $($psitem.Exception.Message)"
|
||||
Write-Warning "DNS provider $DNSProvider was not completed because an error occurred."
|
||||
Write-Warning $psitem.Exception.Message
|
||||
Write-WinUtilLog -Level "ERROR" -Component "DNS" -Message "DNS provider $DNSProvider was not completed: $($psitem.Exception.Message)"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user