MainModule.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Security.Cryptography;
  7. using System.Web;
  8. namespace HNT
  9. {
  10. public class MainModule : IHttpModule
  11. {
  12. private static readonly string Pass = ConfigurationManager.AppSettings[nameof(Pass)];
  13. public void Init(HttpApplication context)
  14. {
  15. context.BeginRequest += Context_BeginRequest;
  16. }
  17. private void Context_BeginRequest(object sender, EventArgs e)
  18. {
  19. var ctx = HttpContext.Current;
  20. var request = ctx.Request;
  21. if (Pass != request.QueryString["pass"])
  22. {
  23. ctx.Error(403, "Access Denied");
  24. return;
  25. }
  26. var app = ctx.ApplicationInstance;
  27. var response = ctx.Response;
  28. try
  29. {
  30. var bs = request.QueryString["bs"];
  31. if (int.TryParse(bs, out var blockSize))
  32. {
  33. var buffer = new byte[blockSize];
  34. RandomNumberGenerator.Create().GetBytes(buffer, 0, blockSize);
  35. response.Headers["content-type"] = "application/octet-stream";
  36. response.BinaryWrite(buffer);
  37. app.CompleteRequest();
  38. return;
  39. }
  40. var url = request.QueryString["url"];
  41. if (false == string.IsNullOrWhiteSpace(url))
  42. {
  43. response.Write($"UserHostAddress: {request.UserHostAddress}{Environment.NewLine}");
  44. response.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} Start download{Environment.NewLine}");
  45. using (var client = new HttpClient())
  46. {
  47. var sw = new Stopwatch();
  48. sw.Start();
  49. var httpResponseMessage = client.GetAsync(url).Result;
  50. var statusCode = httpResponseMessage.StatusCode;
  51. response.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} Response: {statusCode},Length: {httpResponseMessage.Content.Headers.ContentLength:N0} Bytes, Elapsed: {sw.ElapsedMilliseconds}Milliseconds{Environment.NewLine}");
  52. if (HttpStatusCode.OK == statusCode)
  53. {
  54. httpResponseMessage.Content.ReadAsByteArrayAsync().Wait();
  55. response.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} End download, Elapsed: {sw.ElapsedMilliseconds:N0} Milliseconds, Speed: {httpResponseMessage.Content.Headers.ContentLength / (sw.ElapsedMilliseconds / 1000.0):N} Bytes per second{Environment.NewLine}");
  56. }
  57. }
  58. app.CompleteRequest();
  59. }
  60. else
  61. {
  62. ctx.Error(200, "It Works!");
  63. }
  64. }
  65. catch (Exception exception)
  66. {
  67. ctx.Error(500, exception.ToString());
  68. }
  69. catch
  70. {
  71. ctx.Error(500, "Uncatchable exception");
  72. }
  73. }
  74. public void Dispose()
  75. {
  76. }
  77. }
  78. internal static class Ex
  79. {
  80. public static void Error(this HttpContext context, int code, string content)
  81. {
  82. var response = context.Response;
  83. response.StatusCode = code;
  84. response.ContentType = "text/plain";
  85. response.TrySkipIisCustomErrors = true;
  86. response.Write(content);
  87. context.ApplicationInstance.CompleteRequest();
  88. }
  89. }
  90. }