Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- namespace GameServer.Networking
- {
- public class SocketProtection
- {
- public ConcurrentDictionary<string, ConnectionInfo> Connections;
- private readonly string Server;
- public SocketProtection(string server)
- {
- Connections = new ConcurrentDictionary<string, ConnectionInfo>(1, 100);
- Server = server;
- }
- public bool IsBanned(string IP) => Connections.TryGetValue(IP, out ConnectionInfo connection) && connection.BannedTime > DateTime.Now;
- public void Oversee(string IP)
- {
- if (Connections.TryGetValue(IP, out ConnectionInfo connection))
- {
- if (DateTime.Now.AddSeconds(-1) > connection.LastConnection) connection.Count++;
- connection.LastConnection = DateTime.Now;
- if (connection.Count >= 10)
- {
- Console.WriteLine($"{IP} Has been ban from {Server}.");
- connection.BannedTime = DateTime.Now.AddMinutes(1);
- }
- }
- else Connections.TryAdd(IP, new ConnectionInfo()
- {
- BannedTime = DateTime.Now.AddSeconds(-1),
- IP = IP,
- LastConnection = DateTime.Now
- });
- }
- public void CheckConnections()
- {
- foreach (var connection in Connections.Values)
- {
- if (connection.BannedTime > DateTime.Now)
- continue;
- if (DateTime.Now >= connection.LastConnection.AddMinutes(1))
- Connections.TryRemove(connection.IP, out _);
- }
- }
- public class ConnectionInfo
- {
- public string IP;
- public DateTime BannedTime;
- public DateTime LastConnection;
- public uint Count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement