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
85 lines (68 loc) · 2.31 KB
/
ClientManager.cs
File metadata and controls
85 lines (68 loc) · 2.31 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using log4net;
using L2dotNET.Network;
namespace L2dotNET
{
class ClientManager
{
private static readonly ILog log = LogManager.GetLogger(typeof(ClientManager));
private static volatile ClientManager instance;
private static readonly object syncRoot = new object();
public static ClientManager Instance
{
get
{
if (instance != null)
return instance;
lock (syncRoot)
{
if (instance == null)
instance = new ClientManager();
}
return instance;
}
}
protected SortedList<string, DateTime> Flood = new SortedList<string, DateTime>();
protected NetworkBlock Banned;
public SortedList<string, GameClient> Clients = new SortedList<string, GameClient>();
public void AddClient(TcpClient client)
{
if (Banned == null)
Banned = NetworkBlock.Instance;
string ip = client.Client.RemoteEndPoint.ToString().Split(':')[0];
lock (Flood)
{
if (Flood.ContainsKey(ip))
{
if (Flood[ip].CompareTo(DateTime.Now) == 1)
{
log.Warn($"Active flooder: {ip}");
client.Close();
return;
}
lock (Flood)
Flood.Remove(ip);
}
}
Flood.Add(ip, DateTime.Now.AddMilliseconds(3000));
if (!Banned.Allowed(ip))
{
client.Close();
log.Error($"NetworkBlock: connection attemp failed. {ip} banned.");
return;
}
GameClient gc = new GameClient(client);
lock (Clients)
Clients.Add(gc.Address.ToString(), gc);
log.Info($"NetController: {Clients.Count} active connections");
}
public void Terminate(string sock)
{
lock (Clients)
Clients.Remove(sock);
log.Info($"NetController: {Clients.Count} active connections");
}
}
}