Last active
January 4, 2024 13:10
-
-
Save shalomb/0df881006fc05f960dfbac84d891d87f to your computer and use it in GitHub Desktop.
Powershell Echo Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! powershell | |
$HttpListener = New-Object System.Net.HttpListener | |
$HttpListener.Prefixes.Add("http://+:80/") | |
$HttpListener.Prefixes.Add("http://+:18093/") | |
$HttpListener.Prefixes.Add("http://+:18095/") | |
# $HttpListener.Prefixes.Add("https://+:443/") | |
$HttpListener.Start() | |
While ($HttpListener.IsListening) { | |
$HttpContext = $HttpListener.GetContext() | |
$HttpRequest = $HttpContext.Request | |
$RequestUrl = $HttpRequest.Url.OriginalString | |
Write-Output "Ping" | |
$HttpRequest | fl *Url, User*, Protocol*, *EndPoint | |
if($HttpRequest.HasEntityBody) { | |
$Reader = New-Object System.IO.StreamReader($HttpRequest.InputStream) | |
Write-Output $Reader.ReadToEnd() | |
} | |
$date = Get-Date | |
$HttpResponse = $HttpContext.Response | |
$HttpResponse.Headers.Add("Content-Type","application/json") | |
$HttpResponse.StatusCode = 200 | |
$json = '{{ "pong": "{0}", "date": "{1}" }}' -f $Env:COMPUTERNAME, $date | |
$ResponseBuffer = [System.Text.Encoding]::UTF8.GetBytes($json) | |
$HttpResponse.ContentLength64 = $ResponseBuffer.Length | |
$HttpResponse.OutputStream.Write($ResponseBuffer,0,$ResponseBuffer.Length) | |
$HttpResponse.Close() | |
$date | |
} | |
$HttpListener.Stop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment