Reputation: 1
i´m new hear, and have a problem with two Codes. I have created a HTTP Index for my PC for Drive C. And basically to visit it, it opens to "http://localhost:8080/". Now I used an other Code, which generates an Tunnel with "Serveo" and the URL to this Tunnel i get back in the Powershell Terminal. And actually the Serveo URL should tunnel to the HTTP Index, to visit it from everywhere via the URL which is given back. But when i visit this URL, which should direct to the HTTP Index, all i got is: "Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid."
So i really don´t know, what is wrong with the codes.
Here are they:
function Start-HttpServer {
param (
[int]$Port = 8080
)
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:$Port/")
$listener.Start()
Write-Host "HTTP-Server gestartet. Der Index wird unter http://localhost:$Port/ bereitgestellt."
while ($true) {
$context = $listener.GetContext()
$request = $context.Request
$response = $context.Response
$requestedPath = $request.Url.AbsolutePath.TrimStart('/')
if ([string]::IsNullOrEmpty($requestedPath)) {
$localPath = "C:\"
} else {
$localPath = "C:\" + [System.Uri]::UnescapeDataString($requestedPath.Replace('/', '\'))
}
if (Test-Path $localPath) {
if (Test-Path $localPath -PathType Container) {
try {
$items = Get-ChildItem -Path $localPath -Force
$responseContent = "<html><body><h1>Index of $localPath</h1><ul>"
foreach ($item in $items) {
$itemPath = Join-Path -Path $localPath -ChildPath $item.Name
$relativePath = "/" + ($itemPath -replace [regex]::Escape([System.IO.Path]::GetPathRoot($localPath)), "").Replace("\", "/")
if ($item.PSIsContainer) {
$responseContent += "<li><a href='$relativePath/'>$($item.Name)/</a></li>"
} else {
$responseContent += "<li><a href='$relativePath'>$($item.Name)</a></li>"
}
}
$responseContent += "</ul></body></html>"
$response.ContentType = "text/html"
} catch {
$responseContent = "<html><body><h1>Fehler beim Abrufen der Dateien</h1></body></html>"
$response.ContentType = "text/html"
}
} else {
try {
$fileContent = [System.IO.File]::ReadAllBytes($localPath)
$response.ContentType = "application/octet-stream"
$fileName = [System.IO.Path]::GetFileName($localPath)
$response.AddHeader("Content-Disposition", "attachment; filename=`"$fileName`"")
$response.ContentLength64 = $fileContent.Length
$response.OutputStream.Write($fileContent, 0, $fileContent.Length)
} catch {
$responseContent = "<html><body><h1>Fehler beim Abrufen der Datei</h1></body></html>"
$response.ContentType = "text/html"
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseContent)
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
}
$response.OutputStream.Close()
continue
}
} else {
$responseContent = "<html><body><h1>404 Not Found</h1></body></html>"
$response.ContentType = "text/html"
}
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseContent)
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
}
}
# Starten des HTTP-Servers im aktuellen PowerShell-Fenster
Start-HttpServer -Port 8080
And the Serveo Tunnel:
function Create-ServeoTunnel {
param (
[int]$LocalPort = 8080
)
# Überprüfen, ob SSH installiert ist
if (-not (Get-Command ssh -ErrorAction SilentlyContinue)) {
Write-Host "SSH ist nicht installiert. Bitte installieren Sie SSH mit Administratorrechten."
exit 1
}
# Erstellen und Starten des Serveo-Tunnels
try {
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
$processInfo.FileName = "ssh"
$processInfo.Arguments = "-R 80:localhost:$LocalPort serveo.net"
$processInfo.RedirectStandardOutput = $true
$processInfo.UseShellExecute = $false
$processInfo.CreateNoWindow = $true
$serveoProcess = New-Object System.Diagnostics.Process
$serveoProcess.StartInfo = $processInfo
$serveoProcess.Start() | Out-Null
Write-Host "Warte auf die Serveo Tunnel-URL..."
$url = $null
while ($serveoProcess.HasExited -eq $false) {
$serveoOutput = $serveoProcess.StandardOutput.ReadLine()
if ($serveoOutput -match "https://.*serveo.net") {
$url = $matches[0]
Write-Host "Serveo Tunnel URL: $url"
break
}
}
if ($url -eq $null) {
Write-Host "Fehler: Konnte die Serveo URL nicht abrufen."
} else {
Write-Host "Der Tunnel wird geöffnet gehalten. Drücken Sie Strg+C, um zu beenden."
# Den Tunnel geöffnet halten
while ($serveoProcess.HasExited -eq $false) {
Start-Sleep -Seconds 1
}
}
} catch {
Write-Host "Fehler beim Einrichten des Serveo Tunnels: $_"
} finally {
if ($serveoProcess -and -not $serveoProcess.HasExited) {
$serveoProcess.Kill()
}
}
}
# Aufrufen der Funktion zur Erstellung des Serveo-Tunnels
Create-ServeoTunnel -LocalPort 8080
I tried, that i can visit this HTTP Index from all over the world, using this Serveo URL. But as above, it didn´t worked. I tried also other ports, but its the same Error.
Upvotes: 0
Views: 198