Skip to content

Instantly share code, notes, and snippets.

@rahulmr
Forked from byt3bl33d3r/ws.ps1
Created October 13, 2023 10:29
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 rahulmr/fb8ceac26b1d9cd4bff1868be0f515cc to your computer and use it in GitHub Desktop.
Save rahulmr/fb8ceac26b1d9cd4bff1868be0f515cc to your computer and use it in GitHub Desktop.
Async Websocket PowerShell client (producer/consumer pattern)
<#
References:
- https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=netframework-4.5
- https://github.com/poshbotio/PoshBot/blob/master/PoshBot/Implementations/Slack/SlackConnection.ps1
- https://www.leeholmes.com/blog/2018/09/05/producer-consumer-parallelism-in-powershell/
#>
$client_id = [System.GUID]::NewGuid()
$recv_queue = New-Object 'System.Collections.Concurrent.ConcurrentQueue[String]'
$send_queue = New-Object 'System.Collections.Concurrent.ConcurrentQueue[String]'
$ws = New-Object Net.WebSockets.ClientWebSocket
$cts = New-Object Threading.CancellationTokenSource
$ct = New-Object Threading.CancellationToken($false)
Write-Output "Connecting..."
$connectTask = $ws.ConnectAsync("ws://192.168.1.1:8000/$client_id", $cts.Token)
do { Sleep(1) }
until ($connectTask.IsCompleted)
Write-Output "Connected!"
$recv_job = {
param($ws, $client_id, $recv_queue)
$buffer = [Net.WebSockets.WebSocket]::CreateClientBuffer(1024,1024)
$ct = [Threading.CancellationToken]::new($false)
$taskResult = $null
while ($ws.State -eq [Net.WebSockets.WebSocketState]::Open) {
$jsonResult = ""
do {
$taskResult = $ws.ReceiveAsync($buffer, $ct)
while (-not $taskResult.IsCompleted -and $ws.State -eq [Net.WebSockets.WebSocketState]::Open) {
[Threading.Thread]::Sleep(10)
}
$jsonResult += [Text.Encoding]::UTF8.GetString($buffer, 0, $taskResult.Result.Count)
} until (
$ws.State -ne [Net.WebSockets.WebSocketState]::Open -or $taskResult.Result.EndOfMessage
)
if (-not [string]::IsNullOrEmpty($jsonResult)) {
#"Received message(s): $jsonResult" | Out-File -FilePath "logs.txt" -Append
$recv_queue.Enqueue($jsonResult)
}
}
}
$send_job = {
param($ws, $client_id, $send_queue)
$ct = New-Object Threading.CancellationToken($false)
$workitem = $null
while ($ws.State -eq [Net.WebSockets.WebSocketState]::Open){
if ($send_queue.TryDequeue([ref] $workitem)) {
#"Sending message: $workitem" | Out-File -FilePath "logs.txt" -Append
[ArraySegment[byte]]$msg = [Text.Encoding]::UTF8.GetBytes($workitem)
$ws.SendAsync(
$msg,
[System.Net.WebSockets.WebSocketMessageType]::Binary,
$true,
$ct
).GetAwaiter().GetResult() | Out-Null
}
}
}
Write-Output "Starting recv runspace"
$recv_runspace = [PowerShell]::Create()
$recv_runspace.AddScript($recv_job).
AddParameter("ws", $ws).
AddParameter("client_id", $client_id).
AddParameter("recv_queue", $recv_queue).BeginInvoke() | Out-Null
Write-Output "Starting send runspace"
$send_runspace = [PowerShell]::Create()
$send_runspace.AddScript($send_job).
AddParameter("ws", $ws).
AddParameter("client_id", $client_id).
AddParameter("send_queue", $send_queue).BeginInvoke() | Out-Null
try {
do {
$msg = $null
while ($recv_queue.TryDequeue([ref] $msg)) {
Write-Output "Processed message: $msg"
$hash = @{
ClientID = $client_id
Payload = "Wat"
}
$test_payload = New-Object PSObject -Property $hash
$json = ConvertTo-Json $test_payload
$send_queue.Enqueue($json)
}
} until ($ws.State -ne [Net.WebSockets.WebSocketState]::Open)
}
finally {
Write-Output "Closing WS connection"
$closetask = $ws.CloseAsync(
[System.Net.WebSockets.WebSocketCloseStatus]::Empty,
"",
$ct
)
do { Sleep(1) }
until ($closetask.IsCompleted)
$ws.Dispose()
Write-Output "Stopping runspaces"
$recv_runspace.Stop()
$recv_runspace.Dispose()
$send_runspace.Stop()
$send_runspace.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment