From 7b0a9076380bbcdc2f83d6da63ca3800b5d68412 Mon Sep 17 00:00:00 2001 From: samosvalishe Date: Tue, 26 May 2026 01:02:30 +0300 Subject: [PATCH] =?UTF-8?q?fix(routes):=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D1=83=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BC=D0=B0=D1=80=D1=88=D1=80=D1=83=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B2=20routes.ps1=20=D0=BD=D0=B0=20Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Два Windows-specific бага мешали ставить /32-маршруты к TURN-реле: - log.Printf пишет в stderr, а usage гонит `2>&1 | routes.ps1`. В Windows PowerShell 5.1 `2>&1` на нативном exe оборачивает каждую stderr-строку в ErrorRecord (нет метода .Trim()) — `$_.Trim()` бросал исключение на каждой строке, ни одна не матчилась. Заменено на $_.Trim(), который коэрсит и String, и ErrorRecord в текст строки лога. - New-NetRoute требует -InterfaceIndex (параметр mandatory); без него в non-interactive пайпе cmdlet падал. InterfaceIndex берётся из того же default-route, что и NextHop. --- scripts/routes.ps1 | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/scripts/routes.ps1 b/scripts/routes.ps1 index 0b2012b..f8acc91 100644 --- a/scripts/routes.ps1 +++ b/scripts/routes.ps1 @@ -1,20 +1,26 @@ # Usage: .\client.exe -debug ... 2>&1 | .\scripts\routes.ps1 # Requires Administrator privileges. -$gateway = Get-NetRoute ` +$defaultRoute = Get-NetRoute ` -DestinationPrefix "0.0.0.0/0" ` + -ErrorAction SilentlyContinue ` | Sort-Object RouteMetric ` - | Select-Object -First 1 -ExpandProperty NextHop + | Select-Object -First 1 -if (-not $gateway) { - Write-Error "Could not determine default gateway" +$gateway = $defaultRoute.NextHop +$ifIndex = $defaultRoute.InterfaceIndex + +if (-not $gateway -or -not $ifIndex) { + Write-Error "Could not determine default gateway / interface" exit 1 } -Write-Host "Default gateway: $gateway" +Write-Host "Default gateway: $gateway (ifIndex $ifIndex)" $input | ForEach-Object { - $line = $_.Trim() + # 2>&1 на нативном exe оборачивает stderr-строки в ErrorRecord (нет .Trim()). + # "$_" приводит и String, и ErrorRecord к тексту строки лога. + $line = "$_".Trim() if ($line -eq "") { return } $prefix = $null @@ -51,6 +57,7 @@ $input | ForEach-Object { New-NetRoute ` -DestinationPrefix $prefix ` -NextHop $gateway ` + -InterfaceIndex $ifIndex ` -PolicyStore ActiveStore ` -ErrorAction Stop | Out-Null }