Meilenstein 3: Windows-Anbindung für itrdp:// und itssh://
- Gemeinsame, strenge Link-Prüfung in itconnect-common.ps1 (Regeln wie im Backend, Regex-Ende mit \z, nur Parameter host/port/user) - launch-rdp.ps1: mstsc /v bzw. temporäre .rdp-Datei bei Benutzername - launch-ssh.ps1: wt.exe ssh, Fallback auf ssh.exe - register-handlers.reg / unregister-handlers.reg (HKCU, Skripte unter Program Files) - test-launcher.ps1 mit gültigen und bösartigen Testlinks - .gitattributes: Windows-Dateien unverändert (CRLF, BOM) - SETUP.md: Installation, Test und Fehlerbilder Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2e89c4ad59
commit
37a60fc62a
8 files changed
+361
No files matched your search
@@ -0,0 +1,114 @@
|
||||
# Gemeinsame Funktionen für launch-rdp.ps1 und launch-ssh.ps1.
|
||||
# Parst und prüft Links der Form
|
||||
# itrdp://connect?host=<ziel>&port=<port>[&user=<benutzer>]
|
||||
# itssh://connect?host=<ziel>&port=<port>[&user=<benutzer>]
|
||||
# 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}(?<!-)\z'
|
||||
$script:ItcBenutzerRegex = '^[A-Za-z0-9._-]{1,64}\z'
|
||||
$script:ItcPortRegex = '^[0-9]{1,5}\z'
|
||||
|
||||
function Test-ItcHost {
|
||||
param([string]$Wert)
|
||||
if ([string]::IsNullOrEmpty($Wert) -or $Wert.Length -gt 253) { return $false }
|
||||
|
||||
if ($Wert.Contains(':')) {
|
||||
# IPv6: nur Hex-Ziffern, Doppelpunkte und Punkte (IPv4-Anteil), keine Klammern, keine Zonen-ID
|
||||
if ($Wert -cnotmatch '^[0-9A-Fa-f:.]+\z') { return $false }
|
||||
$ip = $null
|
||||
if (-not [System.Net.IPAddress]::TryParse($Wert, [ref]$ip)) { return $false }
|
||||
return ($ip.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6)
|
||||
}
|
||||
|
||||
# Hostname oder IPv4 (IPv4 besteht aus gültigen Labels aus Ziffern)
|
||||
$name = $Wert
|
||||
if ($name.EndsWith('.')) { $name = $name.Substring(0, $name.Length - 1) }
|
||||
if ($name -eq '') { return $false }
|
||||
foreach ($label in $name.Split('.')) {
|
||||
if ($label -cnotmatch $script:ItcLabelRegex) { return $false }
|
||||
}
|
||||
return $true
|
||||
}
|
||||
|
||||
function Test-ItcBenutzer {
|
||||
param([string]$Wert)
|
||||
return ($Wert -cmatch $script:ItcBenutzerRegex)
|
||||
}
|
||||
|
||||
function ConvertFrom-ItcUrl {
|
||||
<#
|
||||
Zerlegt einen it-connect-Link und prüft alle Werte.
|
||||
Rückgabe: Objekt mit Host, Port, User (User kann $null sein).
|
||||
Bei ungültigen Eingaben wird eine Ausnahme mit deutscher Meldung ausgelöst.
|
||||
#>
|
||||
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: <schema>://connect?... bzw. <schema>://connect/?...
|
||||
# Im Abfrageteil nur unbedenkliche Zeichen; alles andere muss %-kodiert sein.
|
||||
$muster = '^(?i:' + $Schema + ')://connect/?\?(?<q>[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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user