/// <summary> /// Stops the server /// </summary> /// <param name="immediately">If this parameters is true, the server will be down as soon as possible</param> /// <returns>Any errors that could have happened</returns> public WebkitErrors Stop(bool immediately = false) { if (!httpServer.IsListening) { return(WebkitErrors.NotRunning); } Tools.ConsolePrint("Stopping the server!\n"); _lock = true; fr.Stop(); Thread.Sleep(1000); _lock = false; if (immediately) { httpServer.Abort(); } else { httpServer.Stop(); } CurrentStatus = CurrentServerStatus.NotListening; AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException; Tools.ConsolePrint("Server stopped!\n"); return(WebkitErrors.Ok); }
/// <summary> /// Starts the server /// </summary> /// <returns>Any errors that could have happened</returns> public WebkitErrors Start() { if (httpServerConfig == null) { return(WebkitErrors.ConfigEmpty); } Tools.EnablePrint = httpServerConfig.EnableConsolePrint; rl = new RateLimit(httpServerConfig.RateLimitPacketAmount, httpServerConfig.RateLimitWaitTime, httpServerConfig.RateLimitSecLimit); fr = new Firewall(httpServerConfig.FirewallPacketBan, httpServerConfig.FirewallPacketInterval, OnFirewallBanEvent); sFM.Start(httpServerConfig); InitStatusCodes(); rateLimited = Encoding.UTF8.GetBytes("You are being rate limited! Please wait " + httpServerConfig.RateLimitWaitTime + " seconds. (pro tip: every time you try to refresh the page, the timer will be set to max)"); string[] blackPrefixes = new[] { "http://*.com", "http://*:", "https://*.com", "https://*:", "http://+.com", "http://+:", "https://+.com", "https://+:" }; // no racism lol, also the only way to prevent any false detection try { foreach (var _prefix in httpServerConfig.Prefixes) { var prefix = _prefix; if (blackPrefixes.Any((x) => prefix.StartsWith(x))) { Tools.ConsolePrint($"Due to RFC 7230, we can't let you use \"{prefix}\" as it's classified as unsafe.\n"); continue; } if (prefix.StartsWith("https://") && httpServerConfig.SSLCertificate == null) { Tools.ConsolePrint($"As you don't have SSLCertificate setup, we can't let you use https. Skipping \"{prefix}\"\n"); continue; } var port = new Uri(prefix, UriKind.Absolute).Port; if (!isPortAvalaible(port)) { Tools.ConsolePrint($"Oops! Port {port} is unavailable for prefix \"{prefix}\"!\n"); continue; } if (!prefix.EndsWith("/")) { prefix += "/"; Tools.ConsolePrint($"Prefixes must end with '/'. Fixing it \"{prefix}\" for you! <3\n");; } httpServer.Prefixes.Add(prefix); Tools.ConsolePrint($"Adding \"{prefix}\" to prefix list\n"); } if (httpServer.Prefixes.Count <= 0) { bool hmm = Tools.EnablePrint; Tools.EnablePrint = true; Tools.ConsolePrint("Oops! Looks like no you don't have any valid prefixes! Server will not start.\n"); Tools.EnablePrint = hmm; return(WebkitErrors.NotRunning); } Tools.ConsolePrint("Starting the server!\n"); if (httpServerConfig.EnforceProtectionPolicy) { httpServer.ExtendedProtectionPolicy = new System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy(System.Security.Authentication.ExtendedProtection.PolicyEnforcement.Always); } httpServer.AuthenticationSchemes = AuthenticationSchemes.Anonymous; httpServer.Start(); Thread listenThread = new Thread(ServerThread) { Name = "Webkit server" }; listenThread.Start(); CurrentStatus = CurrentServerStatus.Listening; } catch (Exception) { CurrentStatus = CurrentServerStatus.InternalExceptionHappened; return(WebkitErrors.UnknownError); } Tools.ConsolePrint("Server started!\n"); return(WebkitErrors.Ok); }