-
Notifications
You must be signed in to change notification settings - Fork 1
/
SessionManager.cs
81 lines (70 loc) · 2.49 KB
/
SessionManager.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Margatroid.Alice
{
class SessionManager
{
private List<Session> Sessions = new List<Session>();
private Dictionary<Guid, Session> GuidSessionIndex
= new Dictionary<Guid, Session>();
private Dictionary<IPAddress, Session> VirtualAddressSessionIndex
= new Dictionary<IPAddress, Session>();
private List<IPAddress> VirtualIPAddressPool = new List<IPAddress>();
public SessionManager()
{
VirtualIPAddressPool = Enumerable.Range(1, 100).Select(
end => IPAddress.Parse(string.Format("10.42.42.{0}", end))).ToList();
}
public Session Register(IPEndPoint remote, IPAddress localAddress)
{
var virtualAddress = pickIPAddress();
var session = new Session
{
TunnelEndPoint = remote,
VirtualAddress = virtualAddress,
LocalAddress = localAddress
};
Sessions.Add(session);
GuidSessionIndex.Add(session.Id, session);
VirtualAddressSessionIndex.Add(virtualAddress, session);
return session;
}
public IPAddress pickIPAddress()
{
var avaliableIPAddress = VirtualIPAddressPool[0];
VirtualIPAddressPool.RemoveAt(0);
return avaliableIPAddress;
}
public bool TryGet(IPAddress virtualAddress, out Session session)
=> VirtualAddressSessionIndex.TryGetValue(virtualAddress, out session);
public bool TryGet(Guid id, out Session session)
=> GuidSessionIndex.TryGetValue(id, out session);
public void Delete(Session session)
{
GuidSessionIndex.Remove(session.Id);
VirtualAddressSessionIndex.Remove(session.VirtualAddress);
VirtualIPAddressPool.Add(session.VirtualAddress);
Sessions.Remove(session);
}
public void Delete(IPAddress virtualAddress)
{
Session sessionToDelete;
if (TryGet(virtualAddress, out sessionToDelete))
{
Delete(sessionToDelete);
}
}
public void Delete(Guid id)
{
Session sessionToDelete;
if (TryGet(id, out sessionToDelete))
{
Delete(sessionToDelete);
}
}
}
}