123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Configuration;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Web;
- namespace HUT
- {
- public class MainModule : IHttpModule
- {
- private static readonly string Pass = ConfigurationManager.AppSettings[nameof(Pass)];
- public void Init(HttpApplication context)
- {
- context.PostMapRequestHandler += Context_PostMapRequestHandler;
- }
- private void Context_PostMapRequestHandler(object sender, EventArgs e)
- {
- var ctx = HttpContext.Current;
- ctx.ApplicationInstance.CompleteRequest();
- var request = ctx.Request;
- if (Pass != request.QueryString["pass"])
- {
- ctx.Error(403, "Access Denied");
- return;
- }
- if (string.IsNullOrWhiteSpace(request.QueryString["host"]))
- {
- ctx.Error(400, "Missing host");
- return;
- }
- var host = request.QueryString["host"];
- if (int.TryParse(request.QueryString["port"], out var targetPort) == false)
- {
- ctx.Error(400, "Missing port");
- return;
- }
- if (int.TryParse(request.QueryString["bsk"], out var blockSizeKb) == false)
- {
- ctx.Error(400, "Missing bsk");
- return;
- }
- var rsp = ctx.Response;
- rsp.ContentType = "text/plain";
- rsp.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Prep random buffer {blockSizeKb:N0} KB {Environment.NewLine}");
- var buf = new byte[blockSizeKb * 1024];
- new Random().NextBytes(buf);
- try
- {
- rsp.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Connecting to {host}:{targetPort} {Environment.NewLine}");
- var client = new TcpClient(host, targetPort);
- var ns = client.GetStream();
- var writer = new BinaryWriter(ns);
- var reader = new BinaryReader(ns);
- var start = DateTime.Now;
- rsp.Write($"[{start:yyyy-MM-dd HH:mm:ss.ff}] Connected, Sending length... {Environment.NewLine}");
- writer.Write(blockSizeKb);
- writer.Flush();
- var code = reader.ReadInt32();
- if (code != 0) throw new Exception($"No expected response code {code}");
- writer.Write(buf, 0, buf.Length);
- writer.Flush();
- var sentCode = reader.ReadInt32();
- if (sentCode != 0) throw new Exception($"No expected sentCode {sentCode}");
- var end = DateTime.Now;
- var seconds = (end - start).TotalSeconds;
- rsp.Write($"[{end:yyyy-MM-dd HH:mm:ss.ff}] OK. DUR {seconds:N2} SEC , AVG SPD {blockSizeKb / seconds:N2} KBS {Environment.NewLine}");
- ns.Close();
- client.Close();
- rsp.Write($"[{ DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Closed");
- }
- catch (Exception exception)
- {
- rsp.Write($"[{ DateTime.Now:yyyy-MM-dd HH:mm:ss.ff}] Error: {exception}");
- }
- }
- public void Dispose()
- {
- }
- }
- internal static class Ex
- {
- public static void Error(this HttpContext context, int code, string content)
- {
- var response = context.Response;
- response.StatusCode = code;
- response.ContentType = "text/plain";
- response.TrySkipIisCustomErrors = true;
- response.Write(content);
- }
- }
- }
|