public void Connection(Socket socket, EndPoint ip, Revision revision, Crypto crypto) { long id = this.GetNextID(); //every socket have their own unique id SocketsConnection connection = new SocketsConnection(id, socket, ip); //for easy to use if (AntiDDoSManager.OnConnection(connection)) //not blocked { if (this.Connections.TryAdd(id, connection)) { Skylight.GetGame().GetGameClientManager().Connection(connection, revision, crypto); if (Skylight.GetConfig()["emu.messages.connections"] == "1") { Logging.WriteLine(">> Connection [" + id + "] from [" + connection.GetIP() + "]"); } } else { connection.Disconnect("Connection TryAdd failed"); } } else { connection.Disconnect("Temp blocked IP"); } }
public void Disconnection(long id) { SocketsConnection connection; if (this.Connections.TryRemove(id, out connection)) { Skylight.GetGame().GetGameClientManager().Disconnection(id); AntiDDoSManager.OnDisconnect(connection); } }
//return false if the connection shoudn't be accepted public static bool OnConnection(SocketsConnection connection) { if (AntiDDoSManager.DDoSProtectionEnabled) { string ip = connection.GetIP(); if (!AntiDDoSManager.IsIPTempBlock(ip)) //not blocked { if (!AntiDDoSManager.IsViolatingConnectionLimit(connection)) { return(true); } else { CacheItem failureCount = AntiDDoSManager.ConnectionLimitByIPViolationCounter.GetCacheItem(ip); if (failureCount == null) //havent violated in 5s { AntiDDoSManager.ConnectionLimitByIPViolationCounter.Set(ip, 1, DateTimeOffset.Now.AddSeconds(5)); //violating expires after 5s } else { AntiDDoSManager.ConnectionLimitByIPViolationCounter.Set(ip, ((int)failureCount.Value) + 1, DateTimeOffset.Now.AddSeconds(5)); if (((int)failureCount.Value) >= 5) //5 failures in 5s?!? I call DDoS { AntiDDoSManager.BlockForDDoS(ip); } } return(false); } } else { return(false); } } else { return(true); } }