mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-08-11 18:41:18 +10:00
Adding Mullvad to DNS (#4927)
* Update dns.json * Fix Mullvad DNS review issues * Add Mullvad secondary resolvers * Register DoH before changing adapter DNS * Report DNS failures to tweak workflow * Handle non-terminating DNS assignment failures * Preserve DNS fallback and document Mullvad options --------- Co-authored-by: Chris Titus <contact@christitus.com>
This commit is contained in:
co-authored by
Chris Titus
parent
afc3e1eec2
commit
0aa4ab3a40
@@ -15,7 +15,7 @@ function Set-WinUtilDNS {
|
||||
|
||||
if($DNSProvider -eq "Default") {
|
||||
Write-WinUtilLog -Component "DNS" -Message "DNS provider is Default; no DNS changes applied."
|
||||
return
|
||||
return $true
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -29,11 +29,17 @@ function Set-WinUtilDNS {
|
||||
if($null -eq $dns) {
|
||||
Write-Warning "DNS provider $DNSProvider was not found in configuration."
|
||||
Write-WinUtilLog -Level "ERROR" -Component "DNS" -Message "DNS provider $DNSProvider was not found in configuration."
|
||||
return
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
$dohSupported = [bool](Get-Command Add-DnsClientDohServerAddress -ErrorAction SilentlyContinue)
|
||||
if ($DNSProvider -ne "DHCP" -and $dns.DohOnly -and -not $dohSupported) {
|
||||
Write-Warning "DNS provider $DNSProvider requires DNS over HTTPS, which is not supported on this system."
|
||||
Write-WinUtilLog -Level "ERROR" -Component "DNS" -Message "DNS provider $DNSProvider requires DNS over HTTPS, which is not supported on this system."
|
||||
return $false
|
||||
}
|
||||
|
||||
$dnscacheBase = "HKLM:\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters"
|
||||
|
||||
Foreach ($Adapter in $Adapters) {
|
||||
@@ -64,40 +70,59 @@ function Set-WinUtilDNS {
|
||||
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)
|
||||
$ipv4Addresses = @(@($dns.Primary, $dns.Secondary) | Where-Object { $_ })
|
||||
$ipv6Addresses = @(@($dns.Primary6, $dns.Secondary6) | Where-Object { $_ })
|
||||
|
||||
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
|
||||
try {
|
||||
$ips = @($dns.Primary, $dns.Secondary, $dns.Primary6, $dns.Secondary6) | Where-Object { $_ }
|
||||
foreach ($ip in $ips) {
|
||||
$dohTemplate = if ($dns.SecondaryDohTemplate -and @($dns.Secondary, $dns.Secondary6) -contains $ip) {
|
||||
$dns.SecondaryDohTemplate
|
||||
} else {
|
||||
$dns.DohTemplate
|
||||
}
|
||||
$existing = Get-DnsClientDohServerAddress -ServerAddress $ip -ErrorAction SilentlyContinue
|
||||
if ($existing) {
|
||||
Set-DnsClientDohServerAddress -ServerAddress $ip -DohTemplate $dohTemplate -AllowFallbackToUdp $false -AutoUpgrade $true -ErrorAction Stop
|
||||
} else {
|
||||
Write-WinUtilLog -Component "DNS" -Message "Registering DoH template for $ip."
|
||||
Add-DnsClientDohServerAddress -ServerAddress $ip -DohTemplate $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
|
||||
}
|
||||
|
||||
$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
|
||||
} catch {
|
||||
if ($dns.DohOnly) {
|
||||
throw
|
||||
}
|
||||
New-ItemProperty -Path $regPath -Name "DohFlags" -Value 1 -PropertyType QWord -Force -ErrorAction Stop | Out-Null
|
||||
|
||||
Write-Warning "DNS over HTTPS setup for provider $DNSProvider failed; continuing with plain DNS."
|
||||
Write-WinUtilLog -Level "WARN" -Component "DNS" -Message "DNS over HTTPS setup for provider $DNSProvider failed; continuing with plain DNS: $($psitem.Exception.Message)"
|
||||
}
|
||||
}
|
||||
|
||||
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 $ipv4Addresses -ErrorAction Stop
|
||||
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 $ipv6Addresses -ErrorAction Stop
|
||||
}
|
||||
}
|
||||
if ($DNSProvider -ne "DHCP" -and $dohSupported -and $dns.DohTemplate) {
|
||||
Clear-DnsClientCache
|
||||
}
|
||||
Write-WinUtilLog -Component "DNS" -Message "DNS provider change completed: $DNSProvider"
|
||||
return $true
|
||||
} catch {
|
||||
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)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user