void InvokeErrorAction(Controllers.HomeController.ErrorPages errPage, HttpContext httpContext, Exception exception) { AutoAddBlackList(errPage, httpContext.Request); System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/"); System.Web.Configuration.CustomErrorsSection section = (System.Web.Configuration.CustomErrorsSection)configuration.GetSection("system.web/customErrors"); #if DEBUG return; #endif //var routeData = new RouteData(); //routeData.Values["controller"] = "Home"; //routeData.Values["action"] = "Error"; //routeData.Values["errPage"] = errPage.ToString(); //routeData.Values["InvokeErrorAction"] = true; ////var httpcx = new HttpContext(new HttpRequest("error", "https://www.hlidacstatu.cz/error",""), httpContext.Response); //Response.Redirect("/Error/" + errPage); //if (false) //{ // using (var controller = new Controllers.HomeController()) // { // ((IController)controller).Execute( // new RequestContext(new HttpContextWrapper(httpContext), routeData)); // } //} }
void AutoAddBlackList(Controllers.HomeController.ErrorPages errPage, HttpRequest req) { bool add = false; var dt = DateTime.Now; string ip = req.UserHostAddress; lock (_autoAddBlackList) { if (!attackers.ContainsKey(ip)) { attackers.Add(ip, new attack() { ip = ip, last = DateTime.Now, num = 0 }); } } var att = attackers[ip]; var diff = (dt - att.last).TotalSeconds; if (diff >= 600) { att.num = 1; } else { att.num = att.num + 1; } switch (errPage) { case Controllers.HomeController.ErrorPages.Ok: break; case Controllers.HomeController.ErrorPages.NotFound: add = att.num > 20; break; case Controllers.HomeController.ErrorPages.Error: add = att.num > 15; break; case Controllers.HomeController.ErrorPages.ErrorHack: add = att.num > 10; break; default: break; } if (add) { HlidacStatu.Util.Consts.Logger.Warning($"added bannedIP {ip}"); try { using (Devmasters.PersistLib p = new Devmasters.PersistLib()) { p.ExecuteNonQuery(Devmasters.Config.GetWebConfigValue("CnnString"), System.Data.CommandType.Text, "INSERT INTO [dbo].[BannedIPs] ([IP],[Expiration],[Created]) " + " VALUES(@ip,@dat,GetDate())", new System.Data.IDataParameter[] { new System.Data.SqlClient.SqlParameter("ip", ip), new System.Data.SqlClient.SqlParameter("dat", dt.AddHours(6)) }); } BannedIPs.ForceRefreshCache(); } catch (Exception e) { HlidacStatu.Util.Consts.Logger.Warning($"added bannedIP {ip}", e); } } }