-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connect method to set destination server. (#56)
Add connect method to set destination server. --------- Co-authored-by: Amirhossein <Amirhossein@DESKTOP-RGEIFHK> Co-authored-by: Seyed Mehran Siadati <[email protected]> Co-authored-by: Sayed Mehran Siadati <[email protected]>
- Loading branch information
1 parent
61143bc
commit 06ebb8d
Showing
6 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<# | ||
.SYNOPSIS | ||
This cmdlet establishes a connection to the BSN IP Tables with the specified server address. | ||
.DESCRIPTION | ||
The Connect-BsnIPTablesCli cmdlet connects to the BSN IP Tables using the provided server address. | ||
It is a mandatory parameter, and the connection is established in the begin block. | ||
.PARAMETER ServerAddress | ||
Specifies the target server address for the connection. This is a mandatory parameter. | ||
.EXAMPLE | ||
Connect-BsnIPTablesCli -ServerAddress "http://iptable.bsn.local" | ||
Establishes a connection to the BSN IP Tables with the server address "http://iptable.bsn.local". | ||
.NOTES | ||
File Name : Connect-BsnIPTablesCli.ps1 | ||
Prerequisite : PowerShell V5 | ||
Copyright 2019 - The BSN Team | ||
#> | ||
|
||
function Connect-BsnIPTablesCli { | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory)] | ||
[BSN.IpTables.V1.Category('Uri')] | ||
[System.String] | ||
# Target Server Address | ||
${ServerAddress} | ||
) | ||
|
||
begin { | ||
# Check if $ServerAddress is null | ||
if ($null -eq $ServerAddress) { | ||
Write-Error "ServerAddress is mandatory. Please provide a valid value." | ||
return | ||
} | ||
# Check if the ServerAddress environment variable exists | ||
$previousValue = $env:ServerAddress | ||
$envExists = [System.Environment]::GetEnvironmentVariable('ServerAddress', [System.EnvironmentVariableTarget]::Process) -ne $null | ||
|
||
if ($envExists) { | ||
Write-Output "Last ServerAddress was: $previousValue and now is changed to: $ServerAddress" | ||
} | ||
else { | ||
Write-Output "Connected to: $ServerAddress" | ||
} | ||
|
||
# Save the ServerAddress in a session variable | ||
$env:ServerAddress = $ServerAddress | ||
} | ||
|
||
end { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* The partial class Module is used to extend or modify the behavior of the AutoRest-generated classes and methods. | ||
* Here we are trying to get the environmental variable server address and set to request of all URLs in SendAsync method. | ||
*/ | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace BSN.IpTables.V1 | ||
{ | ||
public partial class Module | ||
{ | ||
/** | ||
* Pipeline Modification: The Module class contains methods (AfterCreatePipeline and BeforeCreatePipeline) that are | ||
* involved in the creation of an HTTP pipeline (HttpPipeline). | ||
* This pipeline is used for handling HTTP requests and responses. | ||
*/ | ||
partial void AfterCreatePipeline( | ||
global::System.Management.Automation.InvocationInfo invocationInfo, | ||
ref BSN.IpTables.V1.Runtime.HttpPipeline pipeline | ||
) | ||
{ | ||
if (pipeline == null) | ||
throw new NullReferenceException("Pipeline is null!"); | ||
pipeline.Append(SendAsync); | ||
} | ||
|
||
partial void BeforeCreatePipeline( | ||
global::System.Management.Automation.InvocationInfo invocationInfo, | ||
ref BSN.IpTables.V1.Runtime.HttpPipeline pipeline | ||
) { } | ||
|
||
/** | ||
* The SendAsync method is involved in processing HTTP requests. It uses the | ||
* GetIptableServerAddressAsync method to obtain a server address, modifies the request URI accordingly, | ||
* and then delegates to the next step in the pipeline. | ||
*/ | ||
public async System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync( | ||
System.Net.Http.HttpRequestMessage request, | ||
BSN.IpTables.V1.Runtime.IEventListener callback, | ||
BSN.IpTables.V1.Runtime.ISendAsync next | ||
) | ||
{ | ||
string serverAddress = Environment.GetEnvironmentVariable("ServerAddress").ToString(); | ||
if (serverAddress == null) | ||
{ | ||
throw new ArgumentNullException( | ||
nameof(serverAddress), | ||
"ServerAddress variable is not set." | ||
); | ||
} | ||
string requestUriString = request.RequestUri.ToString(); | ||
Uri newUri = new Uri(requestUriString); | ||
string host = newUri.Host; | ||
string finalUrl = requestUriString.Replace(host, serverAddress); | ||
request.RequestUri = new Uri(finalUrl); | ||
if (next == null) | ||
throw new NullReferenceException("Next is null!"); | ||
|
||
return await next.SendAsync(request, callback); | ||
} | ||
|
||
partial void CustomInit() { } | ||
} | ||
} |