Advertisement
lelejau

DDoS

Dec 31st, 2011
3,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. #region Includes
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using ExtensionMethods;
  8. #endregion
  9.  
  10. #region RemoveAll for Dictionary
  11.  
  12. namespace ExtensionMethods
  13. {
  14.     public static class Extensions
  15.     {
  16.         public static void RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> dict,
  17.                                              Func<KeyValuePair<TKey, TValue>, bool> condition)
  18.         {
  19.             foreach (var cur in dict.Where(condition).ToList())
  20.                 dict.Remove(cur.Key);
  21.         }
  22.     }
  23. }
  24.  
  25. #endregion
  26.  
  27. namespace PTEmu.Network
  28. {
  29.     public class FloodProtector
  30.     {
  31.         /// <summary>a list containing the ips connected and the respective interval</summary>
  32.         private static SortedList<string, FloodCounter> floods = new SortedList<string, FloodCounter>();
  33.  
  34.         /// <summary>the wait interval(ms) to accept new connections from the same IP.</summary>
  35.         private readonly int wait = 500;
  36.         private readonly int packet_wait = 10;
  37.  
  38.  
  39.         public bool HandlePacket(string id)
  40.         {
  41.             var p = floods[id];
  42.             if (p.PacketWatch.ElapsedMilliseconds < packet_wait) // too much packets
  43.             {
  44.                 p.PacketWatch.Reset();
  45.                 p.PacketWatch.Start();
  46.                 return false;
  47.             }
  48.             else
  49.             {
  50.                 p.PacketWatch.Reset();
  51.                 p.PacketWatch.Start();
  52.                 return true;
  53.             }
  54.         }
  55.  
  56.  
  57.         /// <summary>Handle continuous connect request</summary>
  58.         /// <param name="id">IP</param>
  59.         /// <returns>false if denied, true if OK</returns>
  60.         public bool HandleConnect(string id)
  61.         {
  62.             System.Threading.Thread.Sleep(100);
  63.             // he never connected to server. create a new key for him.
  64.             if (!floods.ContainsKey(id))
  65.             {
  66.                 var counter = new FloodCounter();
  67.                 counter.StopWatch = new Stopwatch();
  68.                 counter.PacketWatch = new Stopwatch();
  69.                 counter.StopWatch.Start(); // starts his counter
  70.                 counter.PacketWatch.Start();
  71.                 counter.Packet = 0;
  72.                 floods.Add(id, counter);
  73.                 return true; // return true, after all he never connected here.
  74.             }
  75.             else
  76.             {
  77.                 var p = floods[id];
  78.                 var tick = p.StopWatch.ElapsedMilliseconds;
  79.                 // the interval wasnt satisfied and yet he's trying to connect again
  80.                 // so, we found this as an attack. return false and reset the counter.
  81.                 if (tick < wait)
  82.                 {
  83.                     p.StopWatch.Reset();
  84.                     p.StopWatch.Start();
  85.                     Clients.pInfo.RemoveAll<uint, AccountInfo>(n => n.Value.IP == id);
  86.                     return false;
  87.                 }
  88.                 else // he's not attacking, return true and reset the counter.
  89.                 {
  90.                     p.StopWatch.Reset();
  91.                     p.StopWatch.Start();
  92.                     return true;
  93.                 }
  94.             }
  95.         }
  96.  
  97.         #region struct
  98.  
  99.         private struct FloodCounter
  100.         {
  101.             private int packet_count;
  102.             private Stopwatch k__BackingField;
  103.             private Stopwatch pack;
  104.  
  105.  
  106.             public Stopwatch PacketWatch
  107.             {
  108.                 get { return pack; }
  109.                 set { pack = value; }
  110.             }
  111.             public int Packet
  112.             {
  113.                 get { return packet_count; }
  114.                 set { packet_count = value; }
  115.             }
  116.             public Stopwatch StopWatch
  117.             {
  118.                 get { return this.k__BackingField;  }
  119.                 set { this.k__BackingField = value; }
  120.             }
  121.         }
  122.  
  123.         #endregion
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement