mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Adds generic support for sending TCP client and REST network messages
This commit is contained in:
parent
6de97f439f
commit
e56861d858
4 changed files with 117 additions and 0 deletions
4
lib/core/tools/New-IcingaNewLine.psm1
Normal file
4
lib/core/tools/New-IcingaNewLine.psm1
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
function New-IcingaNewLine()
|
||||||
|
{
|
||||||
|
return "`r`n";
|
||||||
|
}
|
||||||
36
lib/web/Icinga_HTTPResponse_Enums.psm1
Normal file
36
lib/web/Icinga_HTTPResponse_Enums.psm1
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<#
|
||||||
|
# This script will provide 'Enums' we can use within our module to
|
||||||
|
# easier access constants and to maintain a better overview of the
|
||||||
|
# entire components
|
||||||
|
#>
|
||||||
|
[hashtable]$HTTPResponseCode = @{
|
||||||
|
200 = 'Ok';
|
||||||
|
400 = 'Bad Request';
|
||||||
|
401 = 'Unauthorized';
|
||||||
|
403 = 'Forbidden';
|
||||||
|
404 = 'Not Found'
|
||||||
|
500 = 'Internal Server Error';
|
||||||
|
};
|
||||||
|
|
||||||
|
[hashtable]$HTTPResponseType = @{
|
||||||
|
'Ok' = 200;
|
||||||
|
'Bad Request' = 400;
|
||||||
|
'Unauthorized' = 401;
|
||||||
|
'Forbidden' = 403;
|
||||||
|
'Not Found' = 404;
|
||||||
|
'Internal Server Error' = 500;
|
||||||
|
};
|
||||||
|
|
||||||
|
<#
|
||||||
|
# Once we defined a new enum hashtable above, simply add it to this list
|
||||||
|
# to make it available within the entire module.
|
||||||
|
#
|
||||||
|
# Example usage:
|
||||||
|
# $IcingaHTTPEnums.HTTPResponseType.Ok
|
||||||
|
#>
|
||||||
|
[hashtable]$IcingaHTTPEnums = @{
|
||||||
|
HTTPResponseCode = $HTTPResponseCode;
|
||||||
|
HTTPResponseType = $HTTPResponseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
Export-ModuleMember -Variable @( 'IcingaHTTPEnums' );
|
||||||
63
lib/web/New-IcingaTCPClientRESTMessage.psm1
Normal file
63
lib/web/New-IcingaTCPClientRESTMessage.psm1
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
function New-IcingaTCPClientRESTMessage()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[Hashtable]$Headers = $null,
|
||||||
|
$ContentBody = $null,
|
||||||
|
[int]$HTTPResponse = 200,
|
||||||
|
[switch]$BasicAuth = $FALSE
|
||||||
|
);
|
||||||
|
|
||||||
|
[string]$ContentLength = '';
|
||||||
|
[string]$HTMLContent = '';
|
||||||
|
[string]$AuthHeader = '';
|
||||||
|
|
||||||
|
if ($null -ne $ContentBody) {
|
||||||
|
$json = ConvertTo-Json $ContentBody -Depth 100 -Compress;
|
||||||
|
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json);
|
||||||
|
$HTMLContent = [System.Text.Encoding]::UTF8.GetString($bytes);
|
||||||
|
if ($bytes.Length -gt 0) {
|
||||||
|
$ContentLength = [string]::Format(
|
||||||
|
'Content-Length: {0}{1}',
|
||||||
|
$bytes.Length,
|
||||||
|
(New-IcingaNewLine)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($BasicAuth) {
|
||||||
|
$AuthHeader = [string]::Format(
|
||||||
|
'WWW-Authenticate: Basic realm="Icinga for Windows"{0}',
|
||||||
|
(New-IcingaNewLine)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$ResponseMeessage = -Join(
|
||||||
|
[string]::Format(
|
||||||
|
'HTTP/1.1 {0} {1}{2}',
|
||||||
|
$HTTPResponse,
|
||||||
|
$IcingaHTTPEnums.HTTPResponseCode[$HTTPResponse],
|
||||||
|
(New-IcingaNewLine)
|
||||||
|
),
|
||||||
|
[string]::Format(
|
||||||
|
'Server: {0}{1}',
|
||||||
|
(Get-IcingaHostname -LowerCase $TRUE -AutoUseFQDN $TRUE),
|
||||||
|
(New-IcingaNewLine)
|
||||||
|
),
|
||||||
|
[string]::Format(
|
||||||
|
'Content-Type: application/json{0}',
|
||||||
|
(New-IcingaNewLine)
|
||||||
|
),
|
||||||
|
$AuthHeader,
|
||||||
|
$ContentLength,
|
||||||
|
(New-IcingaNewLine),
|
||||||
|
$HTMLContent
|
||||||
|
);
|
||||||
|
|
||||||
|
# Encode our message before sending it
|
||||||
|
$UTF8Message = [System.Text.Encoding]::UTF8.GetBytes($ResponseMeessage);
|
||||||
|
|
||||||
|
return @{
|
||||||
|
'message' = $UTF8Message;
|
||||||
|
'length' = $UTF8Message.Length;
|
||||||
|
};
|
||||||
|
}
|
||||||
14
lib/web/Send-IcingaTCPClientMessage.psm1
Normal file
14
lib/web/Send-IcingaTCPClientMessage.psm1
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
function Send-IcingaTCPClientMessage()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[Hashtable]$Message = @{},
|
||||||
|
[System.Net.Security.SslStream]$Stream = $null
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($null -eq $Message -Or $Message.Count -eq 0 -Or $Message.length -eq 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$Stream.Write($Message.message, 0, $Message.length);
|
||||||
|
$Stream.Flush();
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue