Example #1
0
 /// <summary>
 /// This will host an API server, that can be accessed with the given API keys.
 /// </summary>
 /// <param name="port">The port to host the server on.</param>
 /// <param name="listenInterface">The interface to bind the socket to.</param>
 /// <param name="keys">The accepted API keys.</param>
 /// <param name="logger">The global logger.</param>
 public WebApiServer(ushort port, string listenInterface, string[] keys, ILogger <Class> logger, IGameManager manager, Class masterClass, QuiteEffectiveDetector detector, bool secure, X509Certificate2 certificate)
 {
     this.StartTime = DateTime.UtcNow;
     this.Counters  = new PerformanceMonitors();
     this.Running   = true;
     this.Commands  = new Dictionary <string, string>();
     Options        = new ParallelOptions
     {
         MaxDegreeOfParallelism = Environment.ProcessorCount
     };
     this.Logger        = logger;
     this.GameManager   = manager;
     this.QEDector      = detector;
     this.QEData        = new QuodEratDemonstrandum.QuiteEnigmaticData(keys);
     GlobalMessage      = new Structures.BaseMessage();
     ApiKeys            = new List <string>();
     GlobalMessage.Type = Structures.MessageFlag.ConsoleLogMessage;
     ApiKeys.AddRange(keys);
     if (!secure)
     {
         Server = new WebSocketServer($"ws://{listenInterface}:{port}");
         Logger.LogInformation("! WebApi server: bound insecure listener.");
     }
     else
     {
         Server = new WebSocketServer($"wss://{listenInterface}:{port}")
         {
             Certificate = certificate
         };
         Server.EnabledSslProtocols = SslProtocols.Tls12;
         Logger.LogInformation("! WebApi server: bound secure listener.");
     }
     //we start the listener.
     Server.Start(socket =>
     {
         //a client connects.
         socket.OnOpen += () => OnOpen(socket);
     });
     HeartbeatThread = new Thread(DoHeartbeat);
     HeartbeatThread.Start();
     this.Master = masterClass;
 }
Example #2
0
 /// <summary>
 /// Creates a new instance of our HTTP server. This is used to inject the HTML client into browsers.
 /// </summary>
 /// <param name="listenInterface">The interface to bind the socket to.</param>
 /// <param name="port">The port to listen on.</param>
 /// <param name="document">The webpage.</param>
 /// <param name="document404Error">An HTML document used to indicate 404 errors.</param>
 /// <param name="documentMimeError">An HTML document used to indicate that a type of data is unsupported. This should never happen under normal circumstances, as the dashboard only uses supported file types.</param>
 ///<param name="secure">True if you wish to enable HTTPS.</param>
 /// <param name="dosDetector">The DoS detector.</param>
 /// <param name="mainClass">The plugin's main.</param>
 /// <param name="apiServer">The WebAPI server.</param>
 /// <param name="certificate">The SSL certificate to use.</param>
 public HttpServer(string listenInterface, ushort port, string document, string document404Error, string documentMimeError, Impostor.Commands.Core.Class mainClass, WebApiServer apiServer, QuiteEffectiveDetector dosDetector, bool secure, X509Certificate2 certificate)
 {
     if (secure && certificate == null)
     {
         throw new Exception("What are you doing, anti?");
     }
     //utf8 is standard.
     this.Document                 = Encoding.UTF8.GetBytes(document);
     this.Document404Error         = Encoding.UTF8.GetBytes(document404Error);
     this.DocumentTypeNotSupported = Encoding.UTF8.GetBytes(documentMimeError);
     this.Running             = true;
     this.MainClass           = mainClass;
     this.Listener            = new TcpListener(IPAddress.Parse(listenInterface), port);
     this.ApiServer           = apiServer;
     this.LogComposer         = new CsvComposer();
     this.InvalidPageHandlers = new List <string>();
     this.QEDetector          = dosDetector;
     this.AttackerAddresses   = new ConcurrentBag <string>();
     this.EnableSecurity      = secure;
     this.Certificate         = certificate;
     Listener.Start();
     Listener.BeginAcceptTcpClient(EndAccept, Listener);
 }