$ErrorActionPreference='Stop' $identity=[Security.Principal.WindowsIdentity]::GetCurrent() if(!([Security.Principal.WindowsPrincipal]::new($identity)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){throw 'Per creare la regola firewall aprire PowerShell come amministratore.'} $configPath=if($env:BPQ_CONSOLE_CONFIG){$env:BPQ_CONSOLE_CONFIG}else{Join-Path $PSScriptRoot 'console.config.json'} $config=Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json $port=if($env:BPQ_CONSOLE_PORT){[int]$env:BPQ_CONSOLE_PORT}else{[int]$config.port} if($port -lt 1024 -or $port -gt 65535){throw 'Porta pannello non valida.'} $nodeExe=if($config.nodeExe -and (Test-Path -LiteralPath $config.nodeExe)){$config.nodeExe}else{(Get-Command node.exe -ErrorAction Stop).Source} # Create the replacement before removing the previous rule, so failure keeps it intact. $rule=New-NetFirewallRule -DisplayName 'BPQ Web Console LAN' -Direction Inbound -Action Allow -Protocol TCP -LocalPort $port -RemoteAddress LocalSubnet -Program $nodeExe -Profile Private -ErrorAction Stop Get-NetFirewallRule -DisplayName 'BPQ Web Console LAN' -ErrorAction SilentlyContinue | Where-Object {$_.Name -ne $rule.Name} | Remove-NetFirewallRule Write-Host "Accesso LAN abilitato sulla porta $port (rete Privata, sottorete locale)."