# Infraback CLI ToolKit - menu principal # # Lancement (unique methode prise en charge), dans une console PowerShell : # irm toolkit.infraback.com | iex # # Rien n'est installe ni ecrit sur le poste (hors exports demandes) : ce menu, le moteur d'affichage commun # (common/ib-tui.ps1) et chaque outil (tools//.ps1) sont telecharges et executes en memoire. # Tout le code s'execute dans le bloc & { } ci-dessous : a la sortie, aucune variable ni fonction ne reste # dans la session de l'utilisateur, et l'encodage / les couleurs / le curseur de la console sont restaures. # Ne jamais utiliser 'exit' ici : la commande fermerait la console de l'utilisateur. & { Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' # la barre de progression de irm ralentit fortement Windows PowerShell 5.1 if ($PSVersionTable.PSVersion.Major -lt 5 -or ($PSVersionTable.PSVersion.Major -eq 5 -and $PSVersionTable.PSVersion.Minor -lt 1)) { Write-Host 'Infraback CLI ToolKit requires PowerShell 5.1 or later.' -ForegroundColor Red return } if ($Host.Name -ne 'ConsoleHost') { Write-Host 'Infraback CLI ToolKit needs a real console (PowerShell / Windows Terminal), not the ISE.' -ForegroundColor Red return } # TLS 1.2 : pas toujours actif par defaut sous Windows PowerShell 5.1 try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch {} # Contexte transmis aux outils : adresse du site et donnees gardees en memoire pour la session $IB = @{ Base = 'https://toolkit.infraback.com' Cache = @{} } # Outils proposes dans le menu (Path = chemin sur le site) $Tools = @( @{ Label = 'IP Scanner'; Path = 'tools/ipscanner/ipscanner.ps1' } ) function Get-IBText([string]$Path) { $key = "file:$Path" if (-not $IB.Cache.ContainsKey($key)) { $IB.Cache[$key] = [string](Invoke-RestMethod -Uri "$($IB.Base)/$Path" -UseBasicParsing -TimeoutSec 20) } return $IB.Cache[$key] } # Moteur d'affichage commun : charge dans cette portee, donc visible des outils (portees enfants) try { $common = Get-IBText 'common/ib-tui.ps1' } catch { Write-Host "Unable to download Infraback CLI ToolKit from $($IB.Base): $($_.Exception.Message)" -ForegroundColor Red return } . ([scriptblock]::Create($common)) #region ---------- Menu ---------- $M = @{ Sel = 0; Msg = '' } $MenuRender = { param([int]$w, [int]$h) $L = New-TuiFrame $w $h 'Infraback CLI ToolKit' (Get-TuiHelp) $texts = @(for ($i = 0; $i -lt $Tools.Count; $i++) { '{0}. {1}' -f ($i + 1), $Tools[$i].Label }) + 'x Exit' $rows = @(for ($i = 0; $i -lt $Tools.Count; $i++) { 5 + $i }) + (6 + $Tools.Count) Add-TuiItems $L $rows $texts $M.Sel if ($M.Msg) { $L[8 + $Tools.Count] = @(Seg $M.Msg 'Red') } return , $L } $MenuKey = { param($k) $n = $Tools.Count + 1 switch ($k.Key) { 'UpArrow' { $M.Sel = ($M.Sel - 1 + $n) % $n } 'DownArrow' { $M.Sel = ($M.Sel + 1) % $n } 'Escape' { Stop-TuiLoop 'quit' } 'Enter' { if ($M.Sel -ge $Tools.Count) { Stop-TuiLoop 'quit' } else { Stop-TuiLoop "tool:$($M.Sel)" } } } } #endregion #region ---------- Programme principal ---------- $savedEncoding = [Console]::OutputEncoding $failure = $null try { try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch {} [Console]::CursorVisible = $false while ($true) { $choice = Invoke-TuiLoop $MenuRender $MenuKey if ($choice -ne 'tool:' + $M.Sel) { break } # 'quit' $tool = $Tools[$M.Sel] $M.Msg = '' try { $code = Get-IBText $tool.Path } catch { $M.Msg = "Unable to load $($tool.Label): $($_.Exception.Message)"; continue } # L'outil s'execute dans une portee enfant : ses variables et fonctions disparaissent a son retour $result = @(& ([scriptblock]::Create($code)) -IB $IB) if ($result -contains 'quit') { break } } } catch { $failure = $_ } finally { [Console]::ResetColor() [Console]::CursorVisible = $true try { [Console]::Clear() } catch {} try { [Console]::OutputEncoding = $savedEncoding } catch {} } if ($failure) { Write-Host "Error: $($failure.Exception.Message)" -ForegroundColor Red } #endregion }