-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConnectionManager.cs
121 lines (99 loc) · 4.14 KB
/
ConnectionManager.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net.Security;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Text;
using WhirlpoolCore.Communication;
using WhirlpoolCore.Game;
using WhirlpoolCore.Communication.Messages.Server;
namespace WhirlpoolCore
{
static class ConnectionManager
{
private static List<Connection> ConnectionList;
private static X509Certificate2 NodebayTlsCert;
public static async Task InitializeAsync(Socket MainSocket, X509Certificate2 LoadedCert)
{
ConnectionList = new List<Connection>();
NodebayTlsCert = LoadedCert;
while (true)
{
Socket PlayerSocket = await MainSocket.AcceptAsync();
PlayerSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
NetworkStream PlayerStream = new NetworkStream(PlayerSocket);
SslStream EncryptedTlsStream;
try
{
EncryptedTlsStream = new SslStream(PlayerStream, false);
EncryptedTlsStream.AuthenticateAsServer(NodebayTlsCert);
}
catch (AuthenticationException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{PlayerSocket.RemoteEndPoint.ToString()}: Stream Authentication Failed");
Console.ForegroundColor = ConsoleColor.White;
PlayerSocket.Shutdown(SocketShutdown.Both);
PlayerSocket.Close();
continue;
}
catch (IOException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{PlayerSocket.RemoteEndPoint.ToString()}: Stream Invalid");
Console.ForegroundColor = ConsoleColor.White;
PlayerSocket.Shutdown(SocketShutdown.Both);
PlayerSocket.Close();
continue;
}
if (EncryptedTlsStream.IsEncrypted)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{PlayerSocket.RemoteEndPoint.ToString()}: Stream Encrypted");
Console.ForegroundColor = ConsoleColor.White;
}
if (EncryptedTlsStream.IsAuthenticated)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{PlayerSocket.RemoteEndPoint.ToString()}: Stream Authenticated");
Console.ForegroundColor = ConsoleColor.White;
Connection PlayerConnection = new Connection(PlayerSocket, EncryptedTlsStream);
ConnectionList.Add(PlayerConnection);
PlayerConnection.ProcessMessagesAsync();
}
}
}
public static void DestroyConnection(Connection c)
{
if (c == null)
{
Console.WriteLine(">> Warning: Destruction of non-existant connection attempted.");
return;
}
c.DestroySocketConnection();
ConnectionList.Remove(c);
}
public static Connection FindPlayerConnection(String PlayerId)
{
return ConnectionList.Find(c => c.PlayerId == PlayerId);
}
public static void SendToPlayers(List<String> PlayerList, ServerPacket Message)
{
foreach (String PlayerId in PlayerList)
{
if (PlayerId != Message.SenderId)
{
Connection PlayerConnection = ConnectionManager.ConnectionList.Find(c => PlayerId == c.PlayerId);
if (PlayerConnection != null)
{
PlayerConnection.SendMessage(Message);
}
}
}
}
}
}