mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
Added basiic TCP Cmdlets and handling
This commit is contained in:
parent
5770956533
commit
edd8b4b6ed
7 changed files with 100 additions and 14 deletions
|
|
@ -1,14 +0,0 @@
|
|||
function Close-IcingaTcpConnection()
|
||||
{
|
||||
param(
|
||||
$TcpClient
|
||||
);
|
||||
|
||||
if ($null -eq $TcpClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
$TcpClient.Close();
|
||||
$TcpClient.Dispose();
|
||||
$TcpClient = $null;
|
||||
}
|
||||
14
lib/web/Close-IcingaTCPConnection.psm1
Normal file
14
lib/web/Close-IcingaTCPConnection.psm1
Normal 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;
|
||||
}
|
||||
12
lib/web/Close-IcingaTCPSocket.psm1
Normal file
12
lib/web/Close-IcingaTCPSocket.psm1
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function Close-IcingaTCPSocket()
|
||||
{
|
||||
param(
|
||||
[System.Net.Sockets.TcpListener]$Socket = $null
|
||||
);
|
||||
|
||||
if ($null -eq $Socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
$Socket.Stop();
|
||||
}
|
||||
12
lib/web/New-IcingaSSLStream.psm1
Normal file
12
lib/web/New-IcingaSSLStream.psm1
Normal 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;
|
||||
}
|
||||
14
lib/web/New-IcingaTCPClient.psm1
Normal file
14
lib/web/New-IcingaTCPClient.psm1
Normal 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;
|
||||
}
|
||||
19
lib/web/New-IcingaTCPSocket.psm1
Normal file
19
lib/web/New-IcingaTCPSocket.psm1
Normal 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;
|
||||
}
|
||||
29
lib/web/Read-IcingaTCPStream.psm1
Normal file
29
lib/web/Read-IcingaTCPStream.psm1
Normal 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);
|
||||
}
|
||||
Loading…
Reference in a new issue