MainModule.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Web;
  7. namespace HUT
  8. {
  9. public class MainModule : IHttpModule
  10. {
  11. private static readonly string Pass = ConfigurationManager.AppSettings[nameof(Pass)];
  12. public void Init(HttpApplication context)
  13. {
  14. context.PostMapRequestHandler += Context_PostMapRequestHandler;
  15. }
  16. private void Context_PostMapRequestHandler(object sender, EventArgs e)
  17. {
  18. var ctx = HttpContext.Current;
  19. ctx.ApplicationInstance.CompleteRequest();
  20. var request = ctx.Request;
  21. if (Pass != request.QueryString["pass"])
  22. {
  23. ctx.Error(403, "Access Denied");
  24. return;
  25. }
  26. if (string.IsNullOrWhiteSpace(request.QueryString["host"]))
  27. {
  28. ctx.Error(400, "Missing host");
  29. return;
  30. }
  31. var host = request.QueryString["host"];
  32. if (int.TryParse(request.QueryString["port"], out var targetPort) == false)
  33. {
  34. ctx.Error(400, "Missing port");
  35. return;
  36. }
  37. if (int.TryParse(request.QueryString["bsk"], out var blockSizeKb) == false)
  38. {
  39. ctx.Error(400, "Missing bsk");
  40. return;
  41. }
  42. var rsp = ctx.Response;
  43. rsp.ContentType = "text/plain";
  44. rsp.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Prep random buffer {blockSizeKb:N0} KB {Environment.NewLine}");
  45. var buf = new byte[blockSizeKb * 1024];
  46. new Random().NextBytes(buf);
  47. try
  48. {
  49. rsp.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Connecting to {host}:{targetPort} {Environment.NewLine}");
  50. var client = new TcpClient(host, targetPort);
  51. var ns = client.GetStream();
  52. var writer = new BinaryWriter(ns);
  53. var reader = new BinaryReader(ns);
  54. var start = DateTime.Now;
  55. rsp.Write($"[{start:yyyy-MM-dd HH:mm:ss.ff}] Connected, Sending length... {Environment.NewLine}");
  56. writer.Write(blockSizeKb);
  57. writer.Flush();
  58. var code = reader.ReadInt32();
  59. if (code != 0) throw new Exception($"No expected response code {code}");
  60. writer.Write(buf, 0, buf.Length);
  61. writer.Flush();
  62. var sentCode = reader.ReadInt32();
  63. if (sentCode != 0) throw new Exception($"No expected sentCode {sentCode}");
  64. var end = DateTime.Now;
  65. var seconds = (end - start).TotalSeconds;
  66. rsp.Write($"[{end:yyyy-MM-dd HH:mm:ss.ff}] OK. DUR {seconds:N2} SEC , AVG SPD {blockSizeKb / seconds:N2} KBS {Environment.NewLine}");
  67. ns.Close();
  68. client.Close();
  69. rsp.Write($"[{ DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Closed");
  70. }
  71. catch (Exception exception)
  72. {
  73. rsp.Write($"[{ DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Error: {exception}");
  74. }
  75. }
  76. public void Dispose()
  77. {
  78. }
  79. }
  80. internal static class Ex
  81. {
  82. public static void Error(this HttpContext context, int code, string content)
  83. {
  84. var response = context.Response;
  85. response.StatusCode = code;
  86. response.ContentType = "text/plain";
  87. response.TrySkipIisCustomErrors = true;
  88. response.Write(content);
  89. }
  90. }
  91. }