param ( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $false)] [int]$Port = 8080 ) # Prüfen, ob der Pfad existiert if (-Not (Test-Path $Path)) { Write-Error "❌ Der angegebene Pfad '$Path' existiert nicht." exit 1 } # Zielverzeichnis und ggf. Dateiname extrahieren if (Test-Path $Path -PathType Leaf) { $Directory = Split-Path $Path $FileName = Split-Path $Path -Leaf } else { $Directory = $Path $FileName = $null } # HTTP-Listener starten Add-Type -AssemblyName System.Net.HttpListener $listener = New-Object System.Net.HttpListener $listener.Prefixes.Add("http://+:$Port/") $listener.Start() Write-Host "🌐 HTTP-Server läuft auf Port $Port" Write-Host "📂 Freigegeben: $Path" Write-Host "📡 Warte auf Anfragen... (Strg+C zum Beenden)" while ($listener.IsListening) { try { $context = $listener.GetContext() $request = $context.Request $response = $context.Response $clientIP = $request.RemoteEndPoint.Address.ToString() $urlPath = $request.Url.AbsolutePath.TrimStart('/') $localPath = Join-Path $Directory $urlPath if ($FileName -and ($urlPath -ne $FileName)) { $response.StatusCode = 403 $response.Close() continue } if (Test-Path $localPath -PathType Leaf) { $bytes = [System.IO.File]::ReadAllBytes($localPath) $response.ContentType = "application/octet-stream" $response.ContentLength64 = $bytes.Length $response.OutputStream.Write($bytes, 0, $bytes.Length) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Write-Host "[$timestamp] $clientIP → /$urlPath ($($bytes.Length) Bytes)" } else { $response.StatusCode = 404 } $response.Close() } catch { Write-Warning "⚠️ Fehler: $_" } }