@echo on
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo [ERROR] Must run as Administrator.
  pause
  exit /b 1
)
cd /d "%~dp0"

echo ================================================
echo   Capture current DNS, then point Windows at 127.0.0.1
echo   Corporate/internal names keep working via the
echo   captured upstream.
echo ================================================
echo.

echo Capturing current DNS servers to upstream-dns.txt ...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$srv = Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses } | Select-Object -Expand ServerAddresses -Unique | Where-Object { $_ -ne '127.0.0.1' -and $_ -ne '::1' }; $srv | Set-Content -Path 'upstream-dns.txt' -Encoding ASCII"
type upstream-dns.txt
echo.

echo Setting DNS to 127.0.0.1 on every UP physical adapter...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$adapters = Get-NetAdapter -Physical | Where-Object { $_.Status -eq 'Up' }; if (-not $adapters) { Write-Host '[FATAL] No active physical adapter.'; exit 1 }; foreach ($a in $adapters) { Write-Host ('Setting DNS on: ' + $a.Name + '  (ifIndex=' + $a.ifIndex + ')'); Set-DnsClientServerAddress -InterfaceIndex $a.ifIndex -ServerAddresses '127.0.0.1' -ErrorAction Stop }"
if %errorlevel% neq 0 (
  echo [FATAL] Set-DNS failed.
  pause
  exit /b 2
)

echo.
echo Flushing DNS cache...
ipconfig /flushdns
echo.

echo Current DNS servers:
netsh interface ipv4 show dnsservers
echo.
echo ================================================
echo   Done. Now RESTART 1-run-proxy.bat so it picks
echo   up the captured upstream.
echo   Test in EDGE or CHROME. NOT Firefox.
echo   Revert:  3-reset-dns.bat
echo ================================================
pause
