web流量控制简化处理类

web流量控制简化处理类

该类可用于HttpListener类的web端或asp.net web服务端的访问控制,类代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Utility;

namespace My.BLL.Throttle
{
    /// <summary>
    /// 访问次数检验类
    /// 自动清理过期的用户数据不及时,处理不合理
    /// </summary>
    public class ThrottlingHandler
    {
        private  static IThrottleStore _store;
        private static TimeSpan _period;
        private static int _maxRequests;
        
        static readonly Log Log = new Log("Bll");


        static ThrottlingHandler()
        {
            _store = new InMemoryThrottleStore();
            _period = TimeSpan.FromSeconds(60);
        }

        /// <summary>
        /// 设置1分钟内用户的最大访问次数
        /// </summary>
        /// <param name="maxRequests"></param>
        public static void SetMaxReuest(int maxRequests)
        {
            _maxRequests = maxRequests;
        }

        /// <summary>
        /// 校验用户1分钟内是否超过最大访问次数限制
        /// </summary>
        /// <param name="identifier">用户标识符</param>
        /// <returns></returns>
        public static bool IsOverVisitLimit(string identifier)
        {
            //条目大于5000时清除所有数据,这里的处理不当,先将就这么处理,应定时检测过期的条目,清除过期的
            if (_store.GetCount() > 5000)
            {
                _store.Clear();
            }
            ThrottleEntry entry = null;
            if (_store.TryGetValue(identifier, out entry))
            {
                if (entry.PeriodStart + _period < DateTime.UtcNow)
                {
                    _store.Rollover(identifier);
                }
            }

            _store.IncrementRequests(identifier);
            if (!_store.TryGetValue(identifier, out entry))
            {
                return false;
            }

            if (entry.Requests > _maxRequests)
            {
                Log.Info(identifier + " 访问次数超限 " + _maxRequests.ToString());
                return true;
            }
            return false;
        }
    }
}


该类内部调用的类参见  http://blog.csdn.net/huwei2003/article/details/47297917

调用如下:

BLL.Throttle.ThrottlingHandler.SetMaxReuest(Global.ThrottleMaxRequestTimes);
if (Global.IsEnableThrottle == "1")
{
	var isOverVisit = BLL.Throttle.ThrottlingHandler.IsOverVisitLimit(ip);
	if (isOverVisit)
	{
		// 构造回应内容
		string responseString
			= "访问太频繁了";
		// 设置回应头部内容,长度,编码
		response.ContentLength64
			= Encoding.UTF8.GetByteCount(responseString);
		response.ContentType = "text/html; charset=UTF-8";
		// 输出回应内容
		Stream output2 = response.OutputStream;
		StreamWriter writer = new StreamWriter(output2);
		writer.Write(responseString);
		// 必须关闭输出流
		writer.Close();
		return;
	}
}

--- end ---



通过更改Chrome的隐私设置来配置WebRTC的网络流量。 在Chrome的隐私设置中配置WebRTC流量路由选项。 ★它做什么: 这配置WebRTC不使用某些IP地址或协议: - 公共互联网不可见的私人IP地址(例如地址192.168.1.2) - 与不用于网络流量的网络接口相关联的任何公共IP地址(例如,当通过VPN浏览时,ISP提供的地址) - 要求WebRTC流量通过Chrome中配置的代理服务器。由于大多数代理服务器不处理UDP,所以这会有效地关闭UDP,直到Chrome中提供UDP代理支持,并且这些代理被广泛部署。   当扩展程序安装在M48之前的Chrome版本上时,WebRTC将仅使用与用于Web通信的接口关联的公共IP地址,通常是已经提供给浏览器HTTP请求中的站点的相同地址。对于Chrome版本M48以及之后的版本,此扩展提供了一个允许WebRTC使用默认公共地址的配置,对于NAT后面的机器,则使用与公共地址关联的默认私有地址。在Chrome M48的全新安装扩展之后,所述行为将是默认的。对于升级方案,以前选择的配置不应该改变。 该扩展也可能会禁用非代理UDP,但默认情况下不会启用,并且必须使用扩展的“选项”页面进行配置。 ★注意事项: 此扩展可能会影响使用WebRTC进行音频/视频或实时数据通信的应用程序的性能。因为它限制了潜在的网络路径和协议,所以WebRTC可能选择导致明显更长的延迟或更低质量(例如,通过VPN)的路径,或者仅通过代理服务器使用TCP,这对于实时通信不是理想的。我们正试图确定这是多么普遍。 安装此项目即表示您同意Google服务条款和隐私权政策,网址为https://www.google.com/intl/zh-CN/policies/。 支持语言:English
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值