Implementation method of C site IP access frequency restriction for a single site

  • 2021-11-13 02:30:02
  • OfStack

Site IP access frequency limit for a single site


using System;
using System.Collections.Generic;
using System.IO;
//using System.Linq;
using System.Web;

// <summary>
// IP Access frequency control 
// </summary> 
public static class IPCacheManager
{

  /// <summary>
  /// IP Cache set  
  /// </summary> 
  private static List<IPCacheInfo> dataList = new List<IPCacheInfo>();
  private static object lockObj = new object();

  /// <summary> 
  /// 1 Maximum number of requests in a period of time , Must be greater than or equal to 1 
  /// </summary> 
  private static int maxTimes = 3;

  /// <summary> 
  /// 1 Length of period of time (in seconds) ) , must be greater than or equal to 1 
  /// </summary> 
  private static int partSecond = 30;

  /// <summary> 
  ///  Request rejected whether to join the number of requests  
  /// </summary> 
  private static bool isFailAddIn = false;

  static IPCacheManager()
  {
  }

  /// <summary> 
  ///  Set time, default maxTimes=3, partSecond=30 
  /// </summary> 
  /// <param name="_maxTimes"> Maximum number of requests </param> 
  /// <param name="_partSecond"> Request unit time </param> 
  public static void SetTime(int _maxTimes, int _partSecond)
  {
    maxTimes = _maxTimes;
    partSecond = _partSecond;
  }

  /// <summary> 
  ///  Detection 1 For a period of time, IP Can the request continue if the number of requests for  
  ///  And use  
  /// </summary> 
  /// <param name="ip"></param> 
  /// <returns></returns> 
  public static bool CheckIsAble(string ip)
  {
    lock (lockObj)
    {
      var item = dataList.Find(p => p.IP == ip);
      if (item == null)
      {
        item = new IPCacheInfo();
        item.IP = ip;
        item.ReqTime.Add(DateTime.Now);
        dataList.Add(item);

        return true;
      }
      else
      {
        if (item.ReqTime.Count > maxTimes)
        {
          item.ReqTime.RemoveAt(0);
        }

        var nowTime = DateTime.Now;
        if (isFailAddIn)
        {
          #region  If the request is rejected, you need to join the current request 
          item.ReqTime.Add(nowTime);
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              return true;
            }
          }
          else
          {
            return true;
          }
          #endregion
        }
        else
        {
          #region  If the request is rejected, there is no need to join the current request 
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              item.ReqTime.Add(nowTime);
              return true;
            }
          }
          else
          {
            item.ReqTime.Add(nowTime);
            return true;
          }
          #endregion
        }
      }
    }
  }
}

public class IPCacheInfo
{
  public string IP { get; set; }

  private List<DateTime> reqTime = new List<DateTime>();
  public List<DateTime> ReqTime
  {
    get { return this.reqTime; }
    set { this.reqTime = value; }
  }
}

Related articles: