# Gemeinsame Funktionen für launch-rdp.ps1 und launch-ssh.ps1. # Parst und prüft Links der Form # itrdp://connect?host=&port=[&user=] # itssh://connect?host=&port=[&user=] # Regeln wie im Backend (backend/app/validierung.py): # Ziel: Hostname (Buchstaben, Ziffern, Punkte, Bindestriche) oder IPv6-Adresse # Port: 1-65535 # Benutzer: Buchstaben, Ziffern, Punkte, Bindestriche, Unterstriche # Wichtig: Regex-Ende immer mit \z, weil $ in .NET einen abschliessenden Zeilenumbruch erlaubt. Set-StrictMode -Version 2.0 $script:ItcLabelRegex = '^(?!-)[A-Za-z0-9-]{1,63}(? param( [Parameter(Mandatory = $true)][AllowEmptyString()][string]$Url, [Parameter(Mandatory = $true)][ValidateSet('itrdp', 'itssh')][string]$Schema, [Parameter(Mandatory = $true)][int]$StandardPort ) if ($Url.Length -gt 1024) { throw 'Der Link ist zu lang.' } # Erlaubt: ://connect?... bzw. ://connect/?... # Im Abfrageteil nur unbedenkliche Zeichen; alles andere muss %-kodiert sein. $muster = '^(?i:' + $Schema + ')://connect/?\?(?[A-Za-z0-9._~%=&-]*)\z' $treffer = [regex]::Match($Url, $muster) if (-not $treffer.Success) { throw 'Ungültiger Link. Erwartet wird ' + $Schema + '://connect?host=...' } $werte = @{} foreach ($teil in $treffer.Groups['q'].Value.Split('&')) { if ($teil -eq '') { continue } $kv = $teil.Split('=', 2) if ($kv.Count -ne 2) { throw 'Ungültiger Parameter im Link.' } $schluessel = $kv[0].ToLowerInvariant() if (@('host', 'port', 'user') -notcontains $schluessel) { throw ('Unbekannter Parameter im Link: ' + $kv[0]) } if ($werte.ContainsKey($schluessel)) { throw ('Parameter doppelt im Link: ' + $schluessel) } try { $werte[$schluessel] = [System.Uri]::UnescapeDataString($kv[1]) } catch { throw 'Ungültige Kodierung im Link.' } } if (-not $werte.ContainsKey('host')) { throw 'Im Link fehlt das Ziel (host).' } $zielHost = $werte['host'] if (-not (Test-ItcHost $zielHost)) { throw ('Ungültiges Ziel: ' + $zielHost) } $port = $StandardPort if ($werte.ContainsKey('port') -and $werte['port'] -ne '') { if ($werte['port'] -cnotmatch $script:ItcPortRegex) { throw 'Ungültiger Port.' } $port = [int]$werte['port'] if ($port -lt 1 -or $port -gt 65535) { throw 'Ungültiger Port.' } } $benutzer = $null if ($werte.ContainsKey('user') -and $werte['user'] -ne '') { if (-not (Test-ItcBenutzer $werte['user'])) { throw ('Ungültiger Benutzername: ' + $werte['user']) } $benutzer = $werte['user'] } return [pscustomobject]@{ Host = $zielHost; Port = $port; User = $benutzer } } function Format-ItcAdresse { # host:port, IPv6 in eckigen Klammern param([Parameter(Mandatory = $true)]$Ziel) if ($Ziel.Host.Contains(':')) { return ('[' + $Ziel.Host + ']:' + $Ziel.Port) } return ($Ziel.Host + ':' + $Ziel.Port) } function Show-ItcFehler { param([string]$Text) try { Add-Type -AssemblyName System.Windows.Forms [void][System.Windows.Forms.MessageBox]::Show($Text, 'it-connect', 'OK', 'Error') } catch { Write-Error $Text } }