拒绝频繁IP访问--转载
//首先我们要实现 IHttpModule接口 using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Configuration; namespace MyHttp
{
public class UrlReWrite : IHttpModule
{
/// <summary>
/// 单个IP最大连接限制数量
/// </summary>
private int rowCount = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpRowCount"]);
/// <summary>
/// 指定区域时间范围 单位分
/// </summary>
private int httpTime = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpTime"]); public void Init(HttpApplication application)
{
application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
application.EndRequest += (new
EventHandler(this.Application_EndRequest)); }
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication Application = (HttpApplication)source;
HttpContext ctx = Application.Context;
//IP地址
string isIp = ctx.Request.UserHostAddress;
if (ctx.Application["time"] == null)
{
ctx.Application["time"] = DateTime.Now;
}
else
{
DateTime isTime = (DateTime)ctx.Application["time"];
int timeTract = Convert.ToInt32(DateTime.Now.Subtract(isTime).Minutes.ToString());
if (timeTract > (httpTime - ))
{
ctx.Application["time"] = null;
ctx.Application["myip"] = null;
}
}
if (ctx.Application["myip"] != null && ctx.Application["myip"] is CartIp)
{
CartIp cartIp = (CartIp)ctx.Application["myip"];
cartIp.Insert(isIp);
ctx.Application["myip"] = cartIp;
if (cartIp.GetCount(isIp) > rowCount)
{
ctx.Response.Clear();
ctx.Response.Close();
}
}
else
{
CartIp cartIp = new CartIp();
cartIp.Insert(isIp);
HttpContext.Current.Application["myip"] = cartIp;
}
} private void Application_EndRequest(Object source, EventArgs e)
{ } public void Dispose()
{ } }
} //ListIp 类 using System;
using System.Collections.Generic;
using System.Text; namespace MyHttp
{
[Serializable]
public class ListIp
{
private string ip;
private int count;
/// <summary>
/// IP地址
/// </summary>
public string IP
{
get { return ip; }
set { ip = value; }
}
/// <summary>
/// 累加数量
/// </summary>
public int Count
{
get { return count; }
set { count = value; }
}
}
[Serializable]
public class CartIp
{
public CartIp()
{
if (_listIp == null)
{
_listIp = new List<ListIp>();
}
}
private List<ListIp> _listIp;
public List<ListIp> _ListIp
{
get { return _listIp; }
set { _listIp = value; }
}
/// <summary>
/// 添加IP
/// </summary>
public void Insert(string ip)
{
int indexof = ItemLastInfo(ip);
if (indexof == -)
{
//不存在
ListIp item = new ListIp();
item.IP = ip;
_listIp.Add(item);
}
else
{
_listIp[indexof].Count += ;
}
}
//判断IP是否存在
public int ItemLastInfo(string ip)
{
int index = ;
foreach (ListIp item in _ListIp)
{
if (item.IP == ip)
{
return index;//存在
}
index += ;
}
return -;//不存在
}
/// <summary>
/// 获得IP的数量
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public int GetCount(string ip)
{
foreach (ListIp item in _ListIp)
{
if (item.IP == ip)
{
return item.Count;//存在
}
}
return -;//不存在
}
}
} //在web.config 配置访问规则 <appSettings> <add key="HttpRowCount" value=""/>
<add key="HttpTime" value=""/> </appSettings> <system.web> <httpModules>
<add name="UrlReWrite" type="MyHttp.UrlReWrite"/>
</httpModules>
</system.web>
来源:http://www.cnblogs.com/Fooo/archive/2013/01/27/2878820.html
拒绝频繁IP访问--转载的更多相关文章
- Nginx 拒绝指定IP访问
来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/ 闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...
- nginx拒绝国外IP访问
nginx拒绝国外IP访问方法很多,比如iptables,geoip模块,域名解析等等.这些方法不会相互冲突,可以结合起来一起使用. 今天来教大家利用两个小方法解决 域名解析禁止掉海外IP访问网站. ...
- nginx 拒绝本地ip访问
server { listen default_server; server_name _; server_name localhost; ; }
- linux配置IP访问权限
允许访问vi /etc/hosts.allow添加(可以添加多行,其中“:allow”可以省率)sshd:192.168.81.*:allow #表示192.1 ...
- 【转载】阿里云ECS Linux服务器禁止某些IP访问
在阿里云ECS Linux服务器运维过程中,如果发现某些IP访问异常,如怀疑有攻击行为或者怀疑是别人写的爬虫程序长时间占用你的服务器资源,则可以通过相关的设置来禁止这些IP段的访问,拒绝这些IP的请求 ...
- 【转载】 腾讯云通过设置安全组禁止某些IP访问你的服务器
有时候我们在运维网站的过程中会发现一些漏洞扫描者的IP信息,或者个人爬虫网站的IP信息,此时我们想禁止掉这些IP访问到你的服务器,可以通过腾讯云的安全组功能来设置禁止这些IP访问你的服务器,也可以通过 ...
- 【转载】 禁止国外IP访问你的网站
在网站的运维过程中,我们通过网站记录的IP列表记录有时候会发现很多国外的IP的访问,如美国的IP等,而很多的服务器攻击行为的发起点很有可能在国外,此时为了服务器安全的考虑,我们可以考虑禁止国外IP访问 ...
- 【转载】使用宝塔Linux面板屏蔽某些IP访问你的服务器
在服务器的运维过程中,有时候发现一些异常IP或者扫描漏洞攻击者IP访问你的网站,此时如果想屏蔽该IP访问你的服务器,可以通过云服务器厂商提供的安全组进行设置.如果服务器安装有宝塔面板,也可以通过宝塔面 ...
- linux安全篇:禁止频繁访问的ip访问nginx
实验环境 版本:redhat6.5ip:172.16.1.100,172.16.10软件:nginx 172.16.1.10部署nginx [root@localhost tools]# lsngin ...
随机推荐
- 记录一下putty的pscp的用法【转】
转自 记录一下putty的pscp的用法 - 刘荣星的博客 https://www.liurongxing.com/how-use-the-putty-and-pscp.html 以前一直用Secu ...
- hibernate框架学习之数据查询(HQL)helloworld
package cn.itcast.h3.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Se ...
- [JLOI2011]飞行路线 不同的算法,不同的悲伤
题目 :BZOJ2763 洛谷P4568 [JLOI2011]飞行路线 一道最短路的题目,想想写个题解也不错(好久没写题解了_(:з」∠)_) 然后这道题中心思路是dijikstra处理最短路,所以没 ...
- 安装python2、python3
先安装python2: python安装 D:\Python27 目录下的 "python.exe" 重命名为 "python2.exe",则在cmd中输入 p ...
- 持续集成之④:GitLab触发jenkins构建项目
持续集成之④:GitLab触发jenkins构建项目 一:目的为在公司的测试环境当中一旦开发向gitlab仓库提交成功代码,gitlab通知jenkins进行构建项目.代码质量测试然后部署至测试环境, ...
- datepicker 属性设置 以及方法和事件
DatePicker支持鼠标点选日期,同时还可以通过键盘控制选择: page up/down - 上一月.下一月 ctrl+page up/down - 上一年.下一年 ctrl+home - 当前月 ...
- mysql:赋予用户权限、查看及修改端口号
一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...
- 找到 Confluence 6 的日志和配置文件
找到 Confluence 的日志文件 这部分内容对 Confluence 的默认日志表现进行描述并且假设你没有对 Confluence 的默认日志配置进行修改.为了统一在不同平台中的日志输出,Con ...
- Confluence 6 生产环境备份策略
如果你是下面的情况,Confluence 的自动每日 XML 备份可能适合你: 正在评估使用 Confluence 你对数据库的管理并不是非常熟悉同时你的 Confluence 安装实例的数据量并不大 ...
- Java的动手动脑(六)
日期:2018.11.8 星期四 博客期:022 --------------------------------------------------------------------------- ...