Skip to content

Instantly share code, notes, and snippets.

@shalomb
Last active January 4, 2024 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shalomb/0df881006fc05f960dfbac84d891d87f to your computer and use it in GitHub Desktop.
Save shalomb/0df881006fc05f960dfbac84d891d87f to your computer and use it in GitHub Desktop.
Powershell Echo Server
#! 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