forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.cs
More file actions
68 lines (54 loc) · 2.1 KB
/
Copy pathClientManager.cs
File metadata and controls
68 lines (54 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using L2dotNET.Network;
using L2dotNET.Services.Contracts;
using NLog;
namespace L2dotNET
{
public class ClientManager
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private readonly ConcurrentDictionary<string, DateTime> _flood;
private readonly ConcurrentDictionary<string, GameClient> _loggedClients;
private readonly GamePacketHandler _gamePacketHandler;
public ClientManager(GamePacketHandler gamePacketHandler)
{
_gamePacketHandler = gamePacketHandler;
_flood = new ConcurrentDictionary<string, DateTime>();
_loggedClients = new ConcurrentDictionary<string, GameClient>();
}
public void AddClient(TcpClient client)
{
string ip = client.Client.RemoteEndPoint.ToString().Split(':')[0];
if (_flood.ContainsKey(ip))
{
if (_flood[ip].CompareTo(DateTime.UtcNow) == 1)
{
Log.Warn($"Active flooder: {ip}");
client.Close();
return;
}
DateTime oldDate;
_flood.TryRemove(ip, out oldDate);
}
_flood.AddOrUpdate(ip, DateTime.UtcNow.AddMilliseconds(3000), (a, b) => DateTime.UtcNow.AddMilliseconds(3000));
if (!NetworkBlock.Instance.Allowed(ip))
{
client.Close();
Log.Error($"NetworkBlock: connection attemp failed. IP: {ip} banned.");
return;
}
GameClient gameClient = new GameClient(this, client, _gamePacketHandler);
_loggedClients.TryAdd(gameClient.Address.ToString(), gameClient);
Log.Info($"{_loggedClients.Count} active connections");
}
public void Disconnect(string sock)
{
GameClient o;
_loggedClients.TryRemove(sock, out o);
Log.Info($"{_loggedClients.Count} active connections");
}
}
}