| | | 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 HostName |
| | | 13 | | The hostname for the listener. This parameter is Mandatory if using the 'HostName' parameter set. |
| | | 14 | | .PARAMETER Uri |
| | | 15 | | The full URI for the listener. This parameter is Mandatory if using the 'Uri' parameter set. |
| | | 16 | | .PARAMETER AddressFamily |
| | | 17 | | An array of address families to filter resolved addresses (e.g., IPv4-only). This parameter is optional. |
| | | 18 | | .PARAMETER CertPath |
| | | 19 | | The path to the SSL certificate file. This parameter is Mandatory if using HTTPS. |
| | | 20 | | .PARAMETER CertPassword |
| | | 21 | | The password for the SSL certificate, if applicable. This parameter is optional. |
| | | 22 | | .PARAMETER SelfSignedCert |
| | | 23 | | If specified, a self-signed certificate will be generated and used for HTTPS. This parameter is optional. |
| | | 24 | | .PARAMETER X509Certificate |
| | | 25 | | An X509Certificate2 object representing the SSL certificate. This parameter is Mandatory if using HTTPS |
| | | 26 | | .PARAMETER Protocols |
| | | 27 | | The HTTP protocols to use (e.g., Http1, Http2). Defaults to Http1 for HTTP listeners and Http1OrHttp2 for HTTPS |
| | | 28 | | .PARAMETER UseConnectionLogging |
| | | 29 | | If specified, enables connection logging for the listener. This is useful for debugging and monitoring purposes. |
| | | 30 | | .PARAMETER PassThru |
| | | 31 | | If specified, the cmdlet will return the modified server instance after adding the listener. |
| | | 32 | | .EXAMPLE |
| | | 33 | | New-KrServer -Name 'MyKestrunServer' |
| | | 34 | | Creates a new Kestrun server instance with the specified name. |
| | | 35 | | .NOTES |
| | | 36 | | This function is designed to be used after the server has been configured with routes and listeners. |
| | | 37 | | #> |
| | | 38 | | function Add-KrEndpoint { |
| | | 39 | | [KestrunRuntimeApi('Definition')] |
| | | 40 | | [CmdletBinding(defaultParameterSetName = 'NoCert')] |
| | | 41 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 42 | | param( |
| | | 43 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 44 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 45 | | [Parameter()] |
| | | 46 | | [int]$Port = 0, |
| | | 47 | | [Parameter()] |
| | | 48 | | [System.Net.IPAddress]$IPAddress, |
| | | 49 | | [Parameter()] |
| | | 50 | | [string]$HostName, |
| | | 51 | | [Parameter()] |
| | | 52 | | [System.Uri]$Uri, |
| | | 53 | | [Parameter()] |
| | | 54 | | [System.Net.Sockets.AddressFamily[]]$AddressFamily, |
| | | 55 | | |
| | | 56 | | [Parameter(mandatory = $true, ParameterSetName = 'CertFile')] |
| | | 57 | | [string]$CertPath, |
| | | 58 | | |
| | | 59 | | [Parameter(mandatory = $false, ParameterSetName = 'CertFile')] |
| | | 60 | | [SecureString]$CertPassword = $null, |
| | | 61 | | |
| | | 62 | | [Parameter(ParameterSetName = 'SelfSignedCert')] |
| | | 63 | | [switch]$SelfSignedCert, |
| | | 64 | | |
| | | 65 | | [Parameter(Mandatory = $true, ParameterSetName = 'x509Certificate')] |
| | | 66 | | [System.Security.Cryptography.X509Certificates.X509Certificate2]$X509Certificate = $null, |
| | | 67 | | |
| | | 68 | | [Parameter(ParameterSetName = 'x509Certificate')] |
| | | 69 | | [Parameter(ParameterSetName = 'CertFile')] |
| | | 70 | | [Parameter(ParameterSetName = 'SelfSignedCert')] |
| | | 71 | | [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]$Protocols, |
| | | 72 | | |
| | | 73 | | [Parameter()] |
| | | 74 | | [switch]$UseConnectionLogging, |
| | | 75 | | |
| | | 76 | | [Parameter()] |
| | | 77 | | [switch]$PassThru |
| | | 78 | | ) |
| | | 79 | | begin { |
| | | 80 | | # Ensure the server instance is resolved |
| | 0 | 81 | | $Server = Resolve-KestrunServer -Server $Server |
| | 0 | 82 | | if ($null -ne $IPAddress) { |
| | 0 | 83 | | if (-not [string]::IsNullOrEmpty($HostName)) { |
| | 0 | 84 | | throw "Cannot specify both IPAddress and HostName. Please choose one." |
| | | 85 | | } |
| | 0 | 86 | | if ($null -ne $Uri) { |
| | 0 | 87 | | throw "Cannot specify both IPAddress and Uri. Please choose one." |
| | | 88 | | } |
| | 0 | 89 | | if ($AddressFamily -and -not ($AddressFamily -contains $IPAddress.AddressFamily)) { |
| | 0 | 90 | | throw "The specified IPAddress does not match the provided AddressFamily filter." |
| | | 91 | | } |
| | | 92 | | } else { |
| | | 93 | | |
| | 0 | 94 | | if ($null -ne $Uri -and (-not ([string]::IsNullOrEmpty($HostName)))) { |
| | 0 | 95 | | throw "Cannot specify both HostName and Uri. Please choose one." |
| | | 96 | | } |
| | 0 | 97 | | if ($null -eq $Uri -and [string]::IsNullOrEmpty($HostName)) { |
| | 0 | 98 | | $IPAddress = [System.Net.IPAddress]::Loopback |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | process { |
| | | 103 | | |
| | | 104 | | # Validate parameters based on the parameter set |
| | 0 | 105 | | if ($null -eq $Protocols) { |
| | 0 | 106 | | if ($PSCmdlet.ParameterSetName -eq 'NoCert') { |
| | 0 | 107 | | $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1 |
| | | 108 | | } else { |
| | 0 | 109 | | $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1AndHttp2 |
| | | 110 | | } |
| | | 111 | | } |
| | 0 | 112 | | if ($PSCmdlet.ParameterSetName -eq 'CertFile') { |
| | 0 | 113 | | if (-not (Test-Path $CertPath)) { |
| | 0 | 114 | | throw "Certificate file not found: $CertPath" |
| | | 115 | | } |
| | 0 | 116 | | $X509Certificate = Import-KrCertificate -FilePath $CertPath -Password $CertPassword |
| | 0 | 117 | | } elseif ($SelfSignedCert.IsPresent) { |
| | 0 | 118 | | $X509Certificate = New-KrSelfSignedCertificate -DnsNames localhost, 127.0.0.1 -ValidDays 30 |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | |
| | 0 | 122 | | if (-not ([string]::IsNullOrEmpty($HostName))) { |
| | 0 | 123 | | $Server.ConfigureListener($HostName, $Port, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $ |
| | 0 | 124 | | } elseif ($null -ne $Uri) { |
| | 0 | 125 | | $Server.ConfigureListener($Uri, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $AddressFamil |
| | 0 | 126 | | } elseif ($null -ne $IPAddress) { |
| | 0 | 127 | | $Server.ConfigureListener($Port, $IPAddress, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent) |
| | | 128 | | } else { |
| | 0 | 129 | | throw "Invalid parameter set: $($PSCmdlet.ParameterSetName). Please specify either HostName, Uri, or IPAddre |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | if ($PassThru.IsPresent) { |
| | | 133 | | # Return the modified server instance |
| | 0 | 134 | | return $Server |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |