-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.ps1
558 lines (509 loc) · 24.7 KB
/
WebServer.ps1
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
Param(
[int]$PortNumber = 8080,
[string]$AdminUserName = 'admin'
)
<#
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
#>
Add-Type -TypeDefinition @'
namespace WebServer
{
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Linq;
public class ApplicationSession
{
public const int AUTH_TOKEN_LENGTH = 64;
public const int COMBINED_TOKEN_LENGTH = 128;
public static readonly StringComparer DefaultComparer = StringComparer.InvariantCultureIgnoreCase;
public static readonly Encoding DefaultEncoding = new UTF8Encoding(false, false);
private readonly RNGCryptoServiceProvider _csp = new RNGCryptoServiceProvider();
private readonly byte[] _authToken = new byte[AUTH_TOKEN_LENGTH];
private readonly Uri _baseUrl;
public Uri BaseUrl { get { return _baseUrl; } }
private readonly User _adminUser;
public User AdminUser { get { return _adminUser; } }
private readonly Collection<User> _backingUsers = new Collection<User>();
private readonly ReadOnlyCollection<User> _users;
public ReadOnlyCollection<User> Users { get { return _users; } }
public ApplicationSession(int portNumber, string userName, string displayName = null)
{
UriBuilder ub = new UriBuilder("http://localhost");
ub.Port = portNumber;
_baseUrl = ub.Uri;
_users = new ReadOnlyCollection<User>(_backingUsers);
_csp.GetBytes(_authToken);
User adminUser;
User.TryAdd(this, userName, displayName, out adminUser);
_adminUser = adminUser;
}
public class User
{
private readonly byte[] _authToken = new byte[AUTH_TOKEN_LENGTH];
private readonly string _userName;
public string UserName { get { return _userName; } }
private string _displayName;
public string DisplayName
{
get { return _displayName; }
set
{
string displayName = value;
_displayName = (displayName != null && (displayName = displayName.Trim()).Length > 0) ? displayName : _userName;
}
}
private User(string userName, string displayName)
{
_userName = userName;
_displayName = (displayName != null && (displayName = displayName.Trim()).Length > 0) ? displayName : userName;
}
public string GetTokenString(ApplicationSession appSession)
{
if (appSession == null)
throw new ArgumentNullException("appSession");
byte[] combined = new byte[COMBINED_TOKEN_LENGTH];
Array.Copy(_authToken, combined, AUTH_TOKEN_LENGTH);
Array.Copy(appSession._authToken, 0, combined, AUTH_TOKEN_LENGTH, AUTH_TOKEN_LENGTH);
return _userName + ":" + Convert.ToBase64String(ProtectedData.Protect(combined, DefaultEncoding.GetBytes(_userName), DataProtectionScope.CurrentUser));
}
public static bool TryFindFromTokenString(ApplicationSession appSession, string tokenString, out User user)
{
if (tokenString != null && (tokenString = tokenString.Trim()).Length > 0)
{
int i = tokenString.IndexOf(':');
if (i > 0 && i < tokenString.Length - 1)
{
try
{
string userName = tokenString.Substring(0, i);
byte[] combined = ProtectedData.Unprotect(Convert.FromBase64String(tokenString.Substring(i + 1)), DefaultEncoding.GetBytes(userName), DataProtectionScope.CurrentUser);
if (combined != null && combined.Length == COMBINED_TOKEN_LENGTH)
{
if ((user = appSession._users.FirstOrDefault(u => DefaultComparer.Equals(userName, u._userName))) != null &&
combined.Take(AUTH_TOKEN_LENGTH).SequenceEqual(user._authToken) && combined.Skip(AUTH_TOKEN_LENGTH).SequenceEqual(appSession._authToken))
return true;
}
}
catch { /* okay to ignore */ }
}
}
user = null;
return false;
}
public static bool TryAdd(ApplicationSession appSession, string userName, out User result) { return TryAdd(appSession, userName, null, out result); }
public static bool TryAdd(ApplicationSession appSession, string userName, string displayName, out User result)
{
if (appSession == null || userName == null || (userName = userName.Trim()).Length == 0)
{
result = null;
return false;
}
lock (appSession._backingUsers)
{
if (appSession._backingUsers.Any(u => DefaultComparer.Equals(userName, u._userName)))
{
result = null;
return false;
}
result = new User(userName, displayName);
appSession._csp.GetBytes(result._authToken);
appSession._backingUsers.Add(result);
}
return true;
}
}
}
public class UserSession
{
public const string CookieName_AuthToken = "AuthToken";
private readonly HttpListenerResponse _response;
public HttpListenerResponse Response { get { return _response; } }
private readonly ApplicationSession _appSession;
public ApplicationSession AppSession { get { return _appSession; } }
private readonly ApplicationSession.User _user;
public ApplicationSession.User User { get { return _user; } }
private readonly string _localPath;
public string LocalPath { get { return _localPath; } }
private readonly string _httpMethod;
public string HttpMethod { get { return _httpMethod; } }
private readonly NameValueCollection _query = new NameValueCollection(ApplicationSession.DefaultComparer);
public NameValueCollection Query { get { return _query; } }
private readonly string _fragment;
public string Fragment { get { return _fragment; } }
private readonly NameValueCollection _formData = new NameValueCollection(ApplicationSession.DefaultComparer);
public NameValueCollection FormData { get { return _formData; } }
private static void DecodeUriFormData(string uriEncodedData, NameValueCollection target)
{
if (uriEncodedData != null && (uriEncodedData = uriEncodedData.Trim()).Length > 0)
foreach (string pair in uriEncodedData.Split('&'))
{
int i = pair.IndexOf('=');
if (i < 0)
target.Add(pair, null);
else
target.Add(pair.Substring(0, i), pair.Substring(i + 1));
}
}
public UserSession(ApplicationSession appSession, HttpListenerContext context)
{
_response = context.Response;
_appSession = appSession;
HttpListenerRequest request = context.Request;
Cookie cookie = request.Cookies[CookieName_AuthToken];
ApplicationSession.User user;
if (cookie != null && ApplicationSession.User.TryFindFromTokenString(appSession, cookie.Value, out user))
{
_user = user;
_response.SetCookie(new Cookie(CookieName_AuthToken, cookie.Value));
}
Uri url = request.Url;
string s = url.LocalPath;
_localPath = (s.EndsWith("/") && s.Length > 1) ? s.Substring(0, s.Length - 1) : s;
_httpMethod = request.HttpMethod;
DecodeUriFormData((s = url.Query).StartsWith("?") ? s.Substring(1) : s, _query);
_fragment = (s = url.Fragment).StartsWith("#") ? s.Substring(1) : s;
if (ApplicationSession.DefaultComparer.Equals(_httpMethod, "GET"))
try
{
using (StreamReader reader = new StreamReader(request.InputStream, ApplicationSession.DefaultEncoding, true))
DecodeUriFormData(reader.ReadToEnd(), _formData);
}
catch { /* okay to ignore */ }
}
public static string ToStatusDescription(HttpStatusCode statusCode)
{
switch (statusCode)
{
case HttpStatusCode.BadGateway:
return "Bad Gateway";
case HttpStatusCode.BadRequest:
return "Bad Request";
case HttpStatusCode.ExpectationFailed:
return "Expectation Failed";
case HttpStatusCode.GatewayTimeout:
return "Gateway Timeout";
case HttpStatusCode.HttpVersionNotSupported:
return "Http Version Not Supported";
case HttpStatusCode.InternalServerError:
return "Internal ServerError";
case HttpStatusCode.LengthRequired:
return "Length Required";
case HttpStatusCode.MethodNotAllowed:
return "Method Not Allowed";
case HttpStatusCode.NoContent:
return "No Content";
case HttpStatusCode.NonAuthoritativeInformation:
return "Non-Authoritative Information";
case HttpStatusCode.NotAcceptable:
return "Not Acceptable";
case HttpStatusCode.NotFound:
return "Not Found";
case HttpStatusCode.NotImplemented:
return "Not Implemented";
case HttpStatusCode.NotModified:
return "Not Modified";
case HttpStatusCode.PartialContent:
return "Partial Content";
case HttpStatusCode.PaymentRequired:
return "Payment Required";
case HttpStatusCode.PreconditionFailed:
return "Precondition Failed";
case HttpStatusCode.ProxyAuthenticationRequired:
return "Proxy Authentication Required";
case HttpStatusCode.RedirectKeepVerb:
return "Redirect Keep Verb";
case HttpStatusCode.RedirectMethod:
return "Redirect Method";
case HttpStatusCode.RequestedRangeNotSatisfiable:
return "Requested Range Not Satisfiable";
case HttpStatusCode.RequestEntityTooLarge:
return "Request Entity Too Large";
case HttpStatusCode.RequestTimeout:
return "Request Timeout";
case HttpStatusCode.RequestUriTooLong:
return "Request Uri Too Long";
case HttpStatusCode.ResetContent:
return "Reset Content";
case HttpStatusCode.ServiceUnavailable:
return "Service Unavailable";
case HttpStatusCode.SwitchingProtocols:
return "Switching Protocols";
case HttpStatusCode.UnsupportedMediaType:
return "Unsupported Media Type";
case HttpStatusCode.UpgradeRequired:
return "Upgrade Required";
case HttpStatusCode.UseProxy:
return "Use Proxy";
default:
return statusCode.ToString("F");
}
}
public void SendPlainText(string content, HttpStatusCode statusCode = HttpStatusCode.OK, string statusDescription = null)
{
_response.StatusCode = (int)statusCode;
_response.StatusDescription = (statusDescription != null && (statusDescription = statusDescription.Trim()).Length > 0) ? statusDescription : ToStatusDescription(statusCode);
byte[] bytes = ApplicationSession.DefaultEncoding.GetBytes(content);
_response.ContentType = MediaTypeNames.Text.Plain;
_response.ContentLength64 = bytes.Length;
_response.ContentEncoding = ApplicationSession.DefaultEncoding;
Stream outputStream = _response.OutputStream;
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
}
public static XDocument ToXHtmlDocumentWithTitle(string title, params object[] bodyContent)
{
string titleText = "Scrum Poker";
if (title != null && (title = title.Trim()).Length > 0)
titleText += " - " + title;
return new XDocument(new XhtmlElementBuilder("html",
new XhtmlElementBuilder("head",
new XhtmlElementBuilder("meta").SetAttribute("charset", ApplicationSession.DefaultEncoding.WebName),
new XhtmlElementBuilder("meta").SetAttribute("http-equiv", "X-UA-Compatible").SetAttribute("content", "IE=edge"),
new XhtmlElementBuilder("title", titleText),
new XhtmlElementBuilder("base").SetAttribute("href", "/"),
new XhtmlElementBuilder("meta").SetAttribute("name", "viewport").SetAttribute("content", "width=device-width, initial-scale=1.0"),
new XhtmlElementBuilder("link").SetAttribute("rel", "icon").SetAttribute("type", "image/x-icon").SetAttribute("href", "favicon.ico")
),
new XhtmlElementBuilder("body", bodyContent)
).SetAttribute("lang", "en").Element);
}
public static XDocument ToXHtmlDocument(params object[] bodyContent) { return ToXHtmlDocumentWithTitle(null, bodyContent); }
public void SendHtml(XDocument content, HttpStatusCode statusCode = HttpStatusCode.OK, string statusDescription = null)
{
_response.StatusCode = (int)statusCode;
_response.StatusDescription = (statusDescription != null && (statusDescription = statusDescription.Trim()).Length > 0) ? statusDescription : ToStatusDescription(statusCode);
byte[] bytes = ApplicationSession.DefaultEncoding.GetBytes(content.ToString(SaveOptions.None));
_response.ContentType = MediaTypeNames.Text.Html;
_response.ContentLength64 = bytes.Length;
_response.ContentEncoding = ApplicationSession.DefaultEncoding;
Stream outputStream = _response.OutputStream;
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
}
public void SendNotFoundResponse(string url)
{
SendHtml(ToXHtmlDocumentWithTitle("Resource not found", new XhtmlElementBuilder("h1", "Resource not found"), url + " was not found."), HttpStatusCode.NotFound);
}
public void SendRedirect(string url)
{
string absUrl = new Uri(_appSession.BaseUrl, url).AbsoluteUri;
byte[] bytes = ApplicationSession.DefaultEncoding.GetBytes(ToXHtmlDocumentWithTitle("Redirect",
new XhtmlElementBuilder("h1", "Redirect"),
new XhtmlElementBuilder("a", url).SetAttribute("href", absUrl)
).ToString(SaveOptions.None));
_response.ContentType = MediaTypeNames.Text.Html;
_response.ContentLength64 = bytes.Length;
_response.ContentEncoding = ApplicationSession.DefaultEncoding;
Stream outputStream = _response.OutputStream;
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
_response.Redirect(absUrl);
}
}
public class XhtmlElementBuilder
{
private readonly XElement _element;
public XElement Element { get { return _element; } }
private XmlDateTimeSerializationMode _dateTimeOption = XmlDateTimeSerializationMode.RoundtripKind;
public XmlDateTimeSerializationMode DateTimeOption
{
get { return _dateTimeOption; }
set { _dateTimeOption = value; }
}
public XhtmlElementBuilder(string localName, params object[] content)
{
_element = new XElement(XNamespace.None.GetName(localName));
Add(content);
}
public XhtmlElementBuilder WithDateTimeOption(XmlDateTimeSerializationMode dateTimeOption)
{
_dateTimeOption = dateTimeOption;
return this;
}
private string ToStringValue(object obj)
{
if (obj == null)
return "";
if (obj is string)
return obj as string;
if (obj is char)
return XmlConvert.ToString((char)obj);
if (obj is bool)
return XmlConvert.ToString((bool)obj);
if (obj is byte)
return XmlConvert.ToString((byte)obj);
if (obj is sbyte)
return XmlConvert.ToString((sbyte)obj);
if (obj is short)
return XmlConvert.ToString((short)obj);
if (obj is ushort)
return XmlConvert.ToString((ushort)obj);
if (obj is int)
return XmlConvert.ToString((int)obj);
if (obj is uint)
return XmlConvert.ToString((uint)obj);
if (obj is long)
return XmlConvert.ToString((long)obj);
if (obj is ulong)
return XmlConvert.ToString((ulong)obj);
if (obj is float)
return XmlConvert.ToString((float)obj);
if (obj is double)
return XmlConvert.ToString((double)obj);
if (obj is decimal)
return XmlConvert.ToString((decimal)obj);
if (obj is TimeSpan)
return XmlConvert.ToString((TimeSpan)obj);
if (obj is Guid)
return XmlConvert.ToString((Guid)obj);
if (obj is Uri)
{
Uri uri = obj as Uri;
return uri.IsAbsoluteUri ? uri.AbsoluteUri : uri.OriginalString;
}
if (obj is byte[])
return string.Join("", ((byte[])obj).Select(b => b.ToString("x2")));
if (obj is char[])
return new string((char[])obj);
if (obj is DateTime)
return XmlConvert.ToString((DateTime)obj, _dateTimeOption);
Type t = obj.GetType();
if (t.IsEnum)
return Enum.GetName(t, obj);
return Convert.ToString(obj) ?? "";
}
public XhtmlElementBuilder AddComment(string text)
{
if (!string.IsNullOrWhiteSpace(text))
{
XNode lastNode = _element.LastNode;
if (lastNode is XComment)
((XComment)lastNode).Value += text;
else
_element.Add(new XComment(text));
}
return this;
}
public XhtmlElementBuilder SetAttribute(string localName, object value)
{
XName name = XNamespace.None.GetName(localName);
XAttribute attribute = _element.Attribute(name);
if (attribute == null)
{
if (value != null)
_element.Add(new XAttribute(name, ToStringValue(value)));
}
else if (value == null)
attribute.Remove();
else
attribute.Value = ToStringValue(value);
return this;
}
public XhtmlElementBuilder Add(params object[] content)
{
if (content != null)
foreach (object obj in content)
{
if (obj == null)
continue;
if (obj is XAttribute || obj is XNode)
_element.Add(obj);
else if (obj is XhtmlElementBuilder)
_element.Add(((XhtmlElementBuilder)obj)._element);
else
{
string value = ToStringValue(obj);
XNode lastNode = _element.LastNode;
if (lastNode is XText)
{
if (value.Length > 0)
((XText)lastNode).Value += value;
}
else
_element.Add(new XText(value));
}
}
return this;
}
}
}
'@ -ReferencedAssemblies 'System.Xml', 'System.Xml.Linq', 'System.Security' -ErrorAction Stop;
Function Get-LocalFilePath {
[CmdletBinding()]
[OutputType([string])]
Param(
[Parameter(Mandatory = $true)]
[string]$Path
)
$LocalPath = "MyPowerShellSite:$Path";
if (Test-Path -LiteralPath $LocalPath -PathType Leaf) { return $LocalPath }
if (Test-Path -LiteralPath $LocalPath -PathType Container) {
foreach ($FileName in @('index.html', 'index.htm', 'default.html', 'default.htm')) {
$LocalPath = "MyPowerShellSite:$Path" | Join-Path -ChildPath $FileName;
if (Test-Path -LiteralPath $LocalPath -PathType Leaf) { return $LocalPath }
}
}
}
Function Send-LocalFileContents {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[System.Net.HttpListenerResponse]$Response,
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory = $true)]
[AllowNull()]
[SessionUser]$User
)
$LocalPath = Get-LocalFilePath -Path $Path;
if ($null -eq $LocalPath) {
Send-NotFoundResponse -Response $Response -Uri $Path;
} else {
$Content = Get-Content -Encoding Byte -LiteralPath $LocalPath;
$Response.ContentType = [System.Web.MimeMapping]::GetMimeMapping($LocalPath);
$Response.ContentLength64 = $Content.Length;
Write-Information -MessageData "Sending $($Response.ContentLength64) bytes ($($Response.ContentType)) from $LocalPath";
$Response.OutputStream.Write($Content, 0, $Content.Length);
$Response.Close();
}
}
Set-Variable -Name 'ApplicationSession' -Option Constant -Scope 'Script' -Value ([WebServer.ApplicationSession]::new($PortNumber, $AdminUserName));
$HttpListener = [System.Net.HttpListener]::new();
$HttpListener.Prefixes.Add($Script:ApplicationSession.BaseUrl.AbsoluteUri);
if ($null -eq (Get-PSDrive -LiteralName 'ScrumPokerSite' -ErrorAction SilentlyContinue)) {
(New-PSDrive -Name 'ScrumPokerSite' -PSProvider FileSystem -Root ($PSScriptRoot | Join-Path -ChildPath 'wwwRoot')) | Out-Null;
}
$HttpListener.Start();
"Listening on $($Script:ApplicationSession.BaseUrl.AbsoluteUri)" | Write-Host;
$AdminTokenString = $Script:ApplicationSession.AdminUser.GetTokenString($Script:ApplicationSession);
"Administrative token is $([Uri]::EscapeDataString($AdminTokenString))" | Write-Host;
$UriBuilder = [UriBuilder]::new($Script:ApplicationSession.BaseUrl);
$UriBuilder.Path = '/login';
$UriBuilder.Query = "?userName=$([Uri]::EscapeDataString($AdminUserName))&adminToken=$([Uri]::EscapeDataString($AdminTokenString))";
$BrowserUrl = $UriBuilder.Uri.AbsoluteUri;
[System.IO.File]::WriteAllLines(($PSScriptRoot | Join-Path -ChildPath 'SessionInfo.txt'), ([string[]]@(
$UriBuilder.Uri.AbsoluteUri,
$AdminTokenString
)), $Script:UTF8Encoding);
start $BrowserUrl;
try {
$IsRunnning = $true;
do {
$UserSession = [WebServer.UserSession]::new($Script:ApplicationSession, $HttpListener.GetContext());
switch ($UserSession.LocalPath) {
'/quit' {
$IsRunnning = $false;
$UserSession.SendPlainText('Shutting down');
break;
}
}
} while ($IsRunnning);
} finally { $HttpListener.Stop(); }