Added basiic TCP Cmdlets and handling

This commit is contained in:
Christian Stein 2020-03-24 12:42:14 +01:00
parent 5770956533
commit edd8b4b6ed
7 changed files with 100 additions and 14 deletions

View file

@ -1,14 +0,0 @@
function Close-IcingaTcpConnection()
{
param(
$TcpClient
);
if ($null -eq $TcpClient) {
return;
}
$TcpClient.Close();
$TcpClient.Dispose();
$TcpClient = $null;
}

View file

@ -0,0 +1,14 @@
function Close-IcingaTCPConnection()
{
param(
[System.Net.Sockets.TcpClient]$Client = $null
);
if ($null -eq $Client) {
return;
}
$Client.Close();
$Client.Dispose();
$Client = $null;
}

View file

@ -0,0 +1,12 @@
function Close-IcingaTCPSocket()
{
param(
[System.Net.Sockets.TcpListener]$Socket = $null
);
if ($null -eq $Socket) {
return;
}
$Socket.Stop();
}

View file

@ -0,0 +1,12 @@
function New-IcingaSSLStream()
{
param(
[System.Net.Sockets.TcpClient]$Client = @{},
[Security.Cryptography.X509Certificates.X509Certificate2]$Certificate = $null
);
$SSLStream = New-Object System.Net.Security.SslStream($Client.GetStream(), $false)
$SSLStream.AuthenticateAsServer($Certificate, $false, [System.Security.Authentication.SslProtocols]::Tls12, $true) | Out-Null;
return $SSLStream;
}

View file

@ -0,0 +1,14 @@
function New-IcingaTCPClient()
{
param(
[System.Net.Sockets.TcpListener]$Socket = $null
);
if ($null -eq $Socket) {
return $null;
}
[System.Net.Sockets.TcpClient]$Client = $Socket.AcceptTcpClient();
return $Client;
}

View file

@ -0,0 +1,19 @@
function New-IcingaTCPSocket()
{
param(
[int]$Port = 0,
[switch]$Start = $FALSE
);
if ($Port -eq 0) {
throw 'Please specify a valid port to open a TCP socket for';
}
$TCPSocket = [System.Net.Sockets.TcpListener]$Port;
if ($Start) {
$TCPSocket.Start();
}
return $TCPSocket;
}

View file

@ -0,0 +1,29 @@
function Read-IcingaTCPStream()
{
param(
[System.Net.Sockets.TcpClient]$Client = @{},
[System.Net.Security.SslStream]$Stream = $null
);
# Get the maxium size of our buffer
[byte[]]$bytes = New-Object byte[] $Client.ReceiveBufferSize;
# Read the content of our SSL stream
$Stream.Read($bytes, 0, $Client.ReceiveBufferSize);
# Now ready the actual content size of the received message
[int]$count = 0;
for ($i=0; $i -le $Client.ReceiveBufferSize; $i++) {
if ($bytes[$i] -ne 0) {
$count = $count + 1;
} else {
break;
}
}
# Resize our array to the correct size
[byte[]]$resized = New-Object byte[] $count;
[array]::Copy($bytes, 0, $resized, 0, $count);
# Return our message content
return [System.Text.Encoding]::UTF8.GetString($resized);
}