拒绝频繁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 ...
随机推荐
- Rasterization 学习笔记
======================Barycentric interpolation====================================== <1>2d/3d ...
- 【转】python类中super()和__init__()的区别
[转]python类中super()和__init__()的区别 单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(se ...
- Caching漫谈--关于Cache的几个理论【转】
转自:https://www.cnblogs.com/asis/p/cache-pattern.html 如今缓存是随处可见了,如果你的程序还没有使用到缓存,那可能是你的程序并发量很低,或对实时性要求 ...
- TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
delphi 学习群: 166637277 (Delphi学习交流与分享). 群主QQ: 1936431438 TreeGrid 控件集 收集: 1.https://www.lmd.de/produ ...
- webp 图形文件操作工具包 win32 (编译 libwebp-20171228-664c21dd 版本)
源码下载地址 https://chromium.googlesource.com/webm/libwebp/ 版本 libwebp-20171228-664 ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
- maven:手动安装JAR到本地仓库
mvn install:install-file -DgroupId=com.test -DartifactId=test -Dversion=1.0.0 -Dpackaging=jar -Dfile ...
- zipkin 整合elastic
前提: <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin</ar ...
- python-常用模块xml、shelve、configparser、hashlib
一.shelve模块 shelve模块也是用来序列化的. 使用方法: 1.open 2.读写 3.close import shelve # 序列化 sl = shelve.open('shlvete ...
- tp5学习
tp5的表单验证 tp5验证码的使用: tp5分页后页面跳转:少参数的处理方法: tp5绑定根目录为: public目录下的index.php 隐藏index.php .htaccess文件修改 控制 ...