| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Creates a new Kestrun server instance with specified options and listeners. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function initializes a new Kestrun server instance, allowing configuration of various options and listeners |
| | 6 | | .PARAMETER Server |
| | 7 | | The Kestrun server instance to configure. This parameter is mandatory and must be a valid server object. |
| | 8 | | .PARAMETER Port |
| | 9 | | The port on which the server will listen for incoming requests. The default is 0, which means a random available |
| | 10 | | .PARAMETER IPAddress |
| | 11 | | The IP address on which the server will listen. Defaults to [System.Net.IPAddress]::Any, which means it will lis |
| | 12 | | .PARAMETER CertPath |
| | 13 | | The path to the SSL certificate file. This parameter is mandatory if using HTTPS. |
| | 14 | | .PARAMETER CertPassword |
| | 15 | | The password for the SSL certificate, if applicable. This parameter is optional. |
| | 16 | | .PARAMETER SelfSignedCert |
| | 17 | | If specified, a self-signed certificate will be generated and used for HTTPS. This parameter is optional. |
| | 18 | | .PARAMETER X509Certificate |
| | 19 | | An X509Certificate2 object representing the SSL certificate. This parameter is mandatory if using HTTPS |
| | 20 | | .PARAMETER Protocols |
| | 21 | | The HTTP protocols to use (e.g., Http1, Http2). Defaults to Http1 for HTTP listeners and Http1OrHttp2 for HTTPS |
| | 22 | | .PARAMETER UseConnectionLogging |
| | 23 | | If specified, enables connection logging for the listener. This is useful for debugging and monitoring purposes. |
| | 24 | | .PARAMETER PassThru |
| | 25 | | If specified, the cmdlet will return the modified server instance after adding the listener. |
| | 26 | | .EXAMPLE |
| | 27 | | New-KrServer -Name 'MyKestrunServer' |
| | 28 | | Creates a new Kestrun server instance with the specified name. |
| | 29 | | .NOTES |
| | 30 | | This function is designed to be used after the server has been configured with routes and listeners. |
| | 31 | | #> |
| | 32 | | function Add-KrListener { |
| | 33 | | [KestrunRuntimeApi('Definition')] |
| | 34 | | [CmdletBinding(defaultParameterSetName = 'NoCert')] |
| | 35 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 36 | | param( |
| | 37 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 38 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 39 | |
|
| | 40 | | [Parameter()] |
| | 41 | | [int]$Port = 0, |
| | 42 | |
|
| | 43 | | [System.Net.IPAddress]$IPAddress = [System.Net.IPAddress]::Loopback, |
| | 44 | | [Parameter(mandatory = $true, ParameterSetName = 'CertFile')] |
| | 45 | | [string]$CertPath, |
| | 46 | |
|
| | 47 | | [Parameter(mandatory = $false, ParameterSetName = 'CertFile')] |
| | 48 | | [SecureString]$CertPassword = $null, |
| | 49 | |
|
| | 50 | | [Parameter(ParameterSetName = 'SelfSignedCert')] |
| | 51 | | [switch]$SelfSignedCert, |
| | 52 | |
|
| | 53 | | [Parameter(mandatory = $true, ParameterSetName = 'x509Certificate')] |
| | 54 | | [System.Security.Cryptography.X509Certificates.X509Certificate2]$X509Certificate = $null, |
| | 55 | |
|
| | 56 | | [Parameter(ParameterSetName = 'x509Certificate')] |
| | 57 | | [Parameter(ParameterSetName = 'CertFile')] |
| | 58 | | [Parameter(ParameterSetName = 'SelfSignedCert')] |
| | 59 | | [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]$Protocols, |
| | 60 | |
|
| | 61 | | [Parameter()] |
| | 62 | | [switch]$UseConnectionLogging, |
| | 63 | |
|
| | 64 | | [Parameter()] |
| | 65 | | [switch]$PassThru |
| | 66 | | ) |
| | 67 | | begin { |
| | 68 | | # Ensure the server instance is resolved |
| 1 | 69 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 70 | | if ($null -eq $Server) { |
| 0 | 71 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 72 | | } |
| | 73 | | } |
| | 74 | | process { |
| | 75 | |
|
| | 76 | | # Validate parameters based on the parameter set |
| 1 | 77 | | if ($null -eq $Protocols) { |
| 1 | 78 | | if ($PSCmdlet.ParameterSetName -eq 'NoCert') { |
| 1 | 79 | | $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1 |
| | 80 | | } else { |
| 0 | 81 | | $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1AndHttp2 |
| | 82 | | } |
| | 83 | | } |
| 1 | 84 | | if ($PSCmdlet.ParameterSetName -eq 'CertFile') { |
| 0 | 85 | | if (-not (Test-Path $CertPath)) { |
| 0 | 86 | | throw "Certificate file not found: $CertPath" |
| | 87 | | } |
| 0 | 88 | | $X509Certificate = Import-KrCertificate -FilePath $CertPath -Password $CertPassword |
| 1 | 89 | | } elseif ($SelfSignedCert.IsPresent) { |
| 0 | 90 | | $X509Certificate = New-KrSelfSignedCertificate -DnsNames localhost, 127.0.0.1 -ValidDays 30 |
| | 91 | | } |
| | 92 | |
|
| 2 | 93 | | $Server.ConfigureListener($Port, $IPAddress, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent) | Ou |
| 1 | 94 | | if ($PassThru.IsPresent) { |
| | 95 | | # Return the modified server instance |
| 0 | 96 | | return $Server |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|