diff --git a/lib/core/tools/Close-IcingaTcpConnection.psm1 b/lib/core/tools/Close-IcingaTcpConnection.psm1 deleted file mode 100644 index 65b532a..0000000 --- a/lib/core/tools/Close-IcingaTcpConnection.psm1 +++ /dev/null @@ -1,14 +0,0 @@ -function Close-IcingaTcpConnection() -{ - param( - $TcpClient - ); - - if ($null -eq $TcpClient) { - return; - } - - $TcpClient.Close(); - $TcpClient.Dispose(); - $TcpClient = $null; -} diff --git a/lib/web/Close-IcingaTCPConnection.psm1 b/lib/web/Close-IcingaTCPConnection.psm1 new file mode 100644 index 0000000..03103b1 --- /dev/null +++ b/lib/web/Close-IcingaTCPConnection.psm1 @@ -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; +} diff --git a/lib/web/Close-IcingaTCPSocket.psm1 b/lib/web/Close-IcingaTCPSocket.psm1 new file mode 100644 index 0000000..05ef283 --- /dev/null +++ b/lib/web/Close-IcingaTCPSocket.psm1 @@ -0,0 +1,12 @@ +function Close-IcingaTCPSocket() +{ + param( + [System.Net.Sockets.TcpListener]$Socket = $null + ); + + if ($null -eq $Socket) { + return; + } + + $Socket.Stop(); +} diff --git a/lib/web/New-IcingaSSLStream.psm1 b/lib/web/New-IcingaSSLStream.psm1 new file mode 100644 index 0000000..6ffc693 --- /dev/null +++ b/lib/web/New-IcingaSSLStream.psm1 @@ -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; +} diff --git a/lib/web/New-IcingaTCPClient.psm1 b/lib/web/New-IcingaTCPClient.psm1 new file mode 100644 index 0000000..a110aa7 --- /dev/null +++ b/lib/web/New-IcingaTCPClient.psm1 @@ -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; +} diff --git a/lib/web/New-IcingaTCPSocket.psm1 b/lib/web/New-IcingaTCPSocket.psm1 new file mode 100644 index 0000000..ea8596c --- /dev/null +++ b/lib/web/New-IcingaTCPSocket.psm1 @@ -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; +} diff --git a/lib/web/Read-IcingaTCPStream.psm1 b/lib/web/Read-IcingaTCPStream.psm1 new file mode 100644 index 0000000..3ff3d6e --- /dev/null +++ b/lib/web/Read-IcingaTCPStream.psm1 @@ -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); +}