forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebListener.psm1
More file actions
290 lines (251 loc) · 8.35 KB
/
WebListener.psm1
File metadata and controls
290 lines (251 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Class WebListener
{
[int]$HttpPort
[int]$HttpsPort
[int]$Tls11Port
[int]$TlsPort
[System.Management.Automation.Job]$Job
WebListener () { }
[String] GetStatus()
{
return $this.Job.JobStateInfo.State
}
}
[WebListener]$WebListener
function New-ClientCertificate
{
param([string]$CertificatePath, [string]$Password)
if ($Password)
{
$Passphrase = ConvertTo-SecureString -Force -AsPlainText $Password
}
$distinguishedName = @{
CN = 'adatum.com'
C = 'US'
S = 'Washington'
L = 'Redmond'
O = 'A. Datum Corporation'
OU = 'R&D'
E = 'randd@adatum.com'
}
$certificateParameters = @{
OutCertPath = $CertificatePath
StartDate = [datetime]::Now.Subtract([timespan]::FromDays(30))
Duration = [timespan]::FromDays(365)
Passphrase = $Passphrase
CertificateFormat = 'Pfx'
KeyLength = 4096
ForCertificateAuthority = $true
Force = $true
} + $distinguishedName
SelfSignedCertificate\New-SelfSignedCertificate @certificateParameters
}
function New-ServerCertificate
{
param([string]$CertificatePath, [string]$Password)
if ($Password)
{
$Passphrase = ConvertTo-SecureString -Force -AsPlainText $Password
}
$distinguishedName = @{
CN = 'localhost'
}
$certificateParameters = @{
OutCertPath = $CertificatePath
StartDate = [datetime]::Now.Subtract([timespan]::FromDays(30))
Duration = [timespan]::FromDays(1000)
Passphrase = $Passphrase
KeyUsage = 'DigitalSignature','KeyEncipherment'
EnhancedKeyUsage = 'ServerAuthentication','ClientAuthentication'
CertificateFormat = 'Pfx'
KeyLength = 2048
Force = $true
} + $distinguishedName
SelfSignedCertificate\New-SelfSignedCertificate @certificateParameters
}
function Get-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param()
process
{
return [WebListener]$Script:WebListener
}
}
function Start-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([WebListener])]
param
(
[ValidateRange(1,65535)]
[int]$HttpPort = 8083,
[ValidateRange(1,65535)]
[int]$HttpsPort = 9084,
[ValidateRange(1,65535)]
[int]$Tls11Port = 8085,
[ValidateRange(1,65535)]
[int]$TlsPort = 8086,
[ValidateRange(1,65535)]
[int]$Tls13Port = 8087
)
process
{
$runningListener = Get-WebListener
if ($null -ne $runningListener -and $runningListener.GetStatus() -eq 'Running')
{
return $runningListener
}
$initTimeoutSeconds = 15
$appExe = (Get-Command WebListener).Path
$serverPfx = 'ServerCert.pfx'
$serverPfxPassword = New-RandomHexString
$clientPfx = 'ClientCert.pfx'
$initCompleteMessage = 'Now listening on'
$sleepMilliseconds = 100
$serverPfxPath = Join-Path ([System.IO.Path]::GetTempPath()) $serverPfx
$Script:ClientPfxPath = Join-Path ([System.IO.Path]::GetTempPath()) $clientPfx
$Script:ClientPfxPassword = New-RandomHexString
New-ServerCertificate -CertificatePath $serverPfxPath -Password $serverPfxPassword
New-ClientCertificate -CertificatePath $Script:ClientPfxPath -Password $Script:ClientPfxPassword
$Job = Start-Job {
$path = Split-Path -Parent (Get-Command WebListener).Path -Verbose
Push-Location $path -Verbose
'appEXE: {0}' -f $using:appExe
'serverPfxPath: {0}' -f $using:serverPfxPath
'serverPfxPassword: {0}' -f $using:serverPfxPassword
'HttpPort: {0}' -f $using:HttpPort
'Https: {0}' -f $using:HttpsPort
'Tls13Port: {0}' -f $using:Tls13Port
'Tls11Port: {0}' -f $using:Tls11Port
'TlsPort: {0}' -f $using:TlsPort
$env:ASPNETCORE_ENVIRONMENT = 'Development'
& $using:appExe $using:serverPfxPath $using:serverPfxPassword $using:HttpPort $using:HttpsPort $using:Tls11Port $using:TlsPort $using:Tls13Port
}
$Script:WebListener = [WebListener]@{
HttpPort = $HttpPort
HttpsPort = $HttpsPort
Tls11Port = $Tls11Port
TlsPort = $TlsPort
Job = $Job
}
# Count iterations of $sleepMilliseconds instead of using system time to work around possible CI VM sleep/delays
$sleepCountRemaining = $initTimeoutSeconds * 1000 / $sleepMilliseconds
do
{
Start-Sleep -Milliseconds $sleepMilliseconds
$initStatus = $Job.ChildJobs[0].Output | Out-String
$isRunning = $initStatus -match $initCompleteMessage
$sleepCountRemaining--
}
while (-not $isRunning -and $sleepCountRemaining -gt 0)
if (-not $isRunning)
{
$jobErrors = $Job.ChildJobs[0].Error | Out-String
$jobOutput = $Job.ChildJobs[0].Output | Out-String
$jobVerbose = $Job.ChildJobs[0].Verbose | Out-String
$Job | Stop-Job
$Job | Remove-Job -Force
$message = 'WebListener did not start before the timeout was reached.{0}Errors:{0}{1}{0}Output:{0}{2}{0}Verbose:{0}{3}' -f ([System.Environment]::NewLine), $jobErrors, $jobOutput, $jobVerbose
throw $message
}
return $Script:WebListener
}
}
function Stop-WebListener
{
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([Void])]
param()
process
{
$Script:WebListener.job | Stop-Job -PassThru | Remove-Job
$Script:WebListener = $null
}
}
function Get-WebListenerClientCertificate {
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])]
param()
process {
[System.Security.Cryptography.X509Certificates.X509Certificate2]::new($Script:ClientPfxPath, $Script:ClientPfxPassword)
}
}
function Get-WebListenerUrl {
[CmdletBinding()]
[OutputType([Uri])]
param (
[switch]$Https,
[ValidateSet('Default', 'Tls13', 'Tls12', 'Tls11', 'Tls')]
[string]$SslProtocol = 'Default',
[ValidateSet(
'Auth',
'Cert',
'Compression',
'Delay',
'Delete',
'Dos',
'Encoding',
'Get',
'Home',
'Link',
'Multipart',
'Patch',
'Post',
'Put',
'Redirect',
'Response',
'ResponseHeaders',
'Resume',
'Retry',
'/'
)]
[String]$Test,
[String]$TestValue,
[System.Collections.IDictionary]$Query
)
process {
$runningListener = Get-WebListener
if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running')
{
return $null
}
$Uri = [System.UriBuilder]::new()
# Use 127.0.0.1 and not localhost due to https://github.com/dotnet/corefx/issues/24104
$Uri.Host = '127.0.0.1'
$Uri.Port = $runningListener.HttpPort
$Uri.Scheme = 'Http'
if ($Https.IsPresent)
{
switch ($SslProtocol)
{
'Tls11' { $Uri.Port = $runningListener.Tls11Port }
'Tls' { $Uri.Port = $runningListener.TlsPort }
# The base HTTPs port is configured for Tls12 only
default { $Uri.Port = $runningListener.HttpsPort }
}
$Uri.Scheme = 'Https'
}
if ($TestValue)
{
$Uri.Path = '{0}/{1}' -f $Test, $TestValue
}
else
{
$Uri.Path = $Test
}
$StringBuilder = [System.Text.StringBuilder]::new()
foreach ($key in $Query.Keys)
{
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($key))
$null = $StringBuilder.Append('=')
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($Query[$key].ToString()))
$null = $StringBuilder.Append('&')
}
$Uri.Query = $StringBuilder.ToString()
return [Uri]$Uri.ToString()
}
}