-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathClient.cs
337 lines (295 loc) · 13.6 KB
/
Client.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Starrybound Server
* Copyright 2013, Avilance Ltd
* Created by Zidonuke ([email protected]) and Crashdoom ([email protected])
*
* This file is a part of Starrybound Server.
* Starrybound Server is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Starrybound Server is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Starrybound Server. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using com.avilance.Starrybound.Util;
using com.avilance.Starrybound.Extensions;
using com.avilance.Starrybound.Packets;
using com.avilance.Starrybound.Permissions;
namespace com.avilance.Starrybound
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
class Client
{
public PlayerData playerData = new PlayerData();
public ClientState state = ClientState.PendingConnect;
private TcpClient cSocket;
private BinaryReader cIn;
private BinaryWriter cOut;
private TcpClient sSocket;
private BinaryReader sIn;
private BinaryWriter sOut;
private Thread ServerForwarder;
private Thread ClientForwarder;
public int connectedTime = 0;
public int kickTargetTimestamp = 0;
public bool connectionAlive { get { if (this.cSocket.Connected && this.sSocket.Connected && this.state != ClientState.Disposing) return true; else return false; } }
public List<Packet11ChatSend> packetQueue = new List<Packet11ChatSend>();
public Client(TcpClient aClient)
{
this.cSocket = aClient;
}
public void run()
{
try
{
this.cIn = new BinaryReader(this.cSocket.GetStream());
this.cOut = new BinaryWriter(this.cSocket.GetStream());
IPEndPoint ipep = (IPEndPoint)this.cSocket.Client.RemoteEndPoint;
IPAddress ipa = ipep.Address;
this.playerData.ip = ipep.Address.ToString();
StarryboundServer.logInfo("[" + playerData.client + "] Accepting new connection.");
if((int)StarryboundServer.serverState < 3)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Connection Failed: The server is not ready yet.");
return;
}
else if ((int)StarryboundServer.serverState > 3)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Connection Failed: The server is shutting down.");
return;
}
else if (StarryboundServer.restartTime != 0)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Connection Failed: The server is restarting.");
return;
}
sSocket = new TcpClient();
sSocket.ReceiveTimeout = StarryboundServer.config.internalSocketTimeout * 1000;
sSocket.SendTimeout = StarryboundServer.config.internalSocketTimeout * 1000;
IAsyncResult result = sSocket.BeginConnect((StarryboundServer.config.proxyIP.Equals("0.0.0.0") ? IPAddress.Loopback : IPAddress.Parse(StarryboundServer.config.proxyIP)), StarryboundServer.config.serverPort, null, null);
bool success = result.AsyncWaitHandle.WaitOne(3000, true);
if (!success || !sSocket.Connected)
{
StarryboundServer.failedConnections++;
if (StarryboundServer.failedConnections >= StarryboundServer.config.maxFailedConnections)
{
StarryboundServer.logFatal(StarryboundServer.failedConnections + " clients failed to connect in a row. Restarting...");
StarryboundServer.serverState = ServerState.Crashed;
}
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Connection Failed: Unable to connect to the parent server.");
return;
}
this.sIn = new BinaryReader(this.sSocket.GetStream());
this.sOut = new BinaryWriter(this.sSocket.GetStream());
this.connectedTime = Utils.getTimestamp();
// Forwarding for data from SERVER (sIn) to CLIENT (cOut)
this.ServerForwarder = new Thread(new ThreadStart(new ForwardThread(this, this.sIn, this.cOut, Direction.Server).run));
ServerForwarder.Start();
// Forwarding for data from CLIENT (cIn) to SERVER (sOut)
this.ClientForwarder = new Thread(new ThreadStart(new ForwardThread(this, this.cIn, this.sOut, Direction.Client).run));
ClientForwarder.Start();
StarryboundServer.failedConnections = 0;
}
catch (Exception e)
{
StarryboundServer.logException("ClientThread Exception: " + e.ToString());
StarryboundServer.failedConnections++;
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Connection Failed: A internal server error occurred (1)");
}
}
public void sendClientPacket(Packet packetID, byte[] packetData)
{
if (this.kickTargetTimestamp != 0) return;
try
{
if (this.cOut.BaseStream.CanWrite)
{
this.cOut.WriteVarUInt32((uint)packetID);
this.cOut.WriteVarInt32((int)packetData.Length);
this.cOut.Write(packetData);
this.cOut.Flush();
}
else
{
this.forceDisconnect(Direction.Client, "Cannot write to stream.");
}
}
catch (Exception e)
{
this.forceDisconnect(Direction.Client, "Failed to send packet: " + e.Message);
}
}
public void sendServerPacket(Packet packetID, byte[] packetData)
{
try
{
if (this.sOut.BaseStream.CanWrite)
{
this.sOut.WriteVarUInt32((uint)packetID);
this.sOut.WriteVarInt32((int)packetData.Length);
this.sOut.Write(packetData);
this.sOut.Flush();
}
else
{
this.forceDisconnect(Direction.Server, "Cannot write to stream.");
}
}
catch (Exception e)
{
this.forceDisconnect(Direction.Server, "Failed to send packet: " + e.Message);
}
}
public void sendCommandMessage(string message)
{
sendChatMessage(ChatReceiveContext.CommandResult, "", 0, "", message);
}
public void sendChatMessage(string message)
{
sendChatMessage(ChatReceiveContext.Broadcast, "", 0, "", message);
}
public void sendChatMessage(string name, string message)
{
sendChatMessage(ChatReceiveContext.Broadcast, "", 0, name, message);
}
public void sendChatMessage(ChatReceiveContext context, string name, string message)
{
sendChatMessage(context, "", 0, name, message);
}
public void sendChatMessage(ChatReceiveContext context, string world, uint clientID, string name, string message)
{
if (state != ClientState.Connected) return;
Packet11ChatSend packet = new Packet11ChatSend(this, Util.Direction.Client);
packet.prepare(context, world, clientID, name, message);
this.packetQueue.Add(packet);
}
public void flushChatQueue()
{
var buffer = this.packetQueue;
foreach (Packet11ChatSend chatPacket in buffer)
{
chatPacket.onSend();
}
this.packetQueue = new List<Packet11ChatSend>();
}
public void banClient(string reason)
{
delayDisconnect("You have been banned from the server for " + reason + ".", this.playerData.name + " has been banned from the server for " + reason + "!");
}
public void kickClient(string reason)
{
delayDisconnect("You have been kicked from the server for " + reason + ".", this.playerData.name + " has been kicked from the server for " + reason + "!");
}
public void kickClient()
{
delayDisconnect("You have been kicked from the server.", this.playerData.name + " has been kicked from the server!");
}
private void doDisconnect(string logMessage)
{
if (this.state == ClientState.Disposing) return;
this.state = ClientState.Disposing;
StarryboundServer.logInfo("[" + playerData.client + "] " + logMessage);
try
{
if (this.playerData.name != null)
{
Client target = StarryboundServer.getClient(this.playerData.name);
if (target != null)
{
Users.SaveUser(this.playerData);
StarryboundServer.removeClient(this);
if (this.kickTargetTimestamp == 0) StarryboundServer.sendGlobalMessage(this.playerData.name + " has left the server.");
}
}
}
catch (Exception e)
{
StarryboundServer.logException("Failed to remove client from clients: " + e.ToString());
}
try
{
this.sendServerPacket(Packet.ClientDisconnect, new byte[1]);
}
catch (Exception) { }
try
{
this.cSocket.Close();
this.sSocket.Close();
}
catch (Exception) { }
try
{
this.ClientForwarder.Abort();
this.ServerForwarder.Abort();
}
catch (Exception) { }
}
public void delayDisconnect(string reason)
{
if (kickTargetTimestamp != 0) return;
sendChatMessage("^#f75d5d;" + reason);
flushChatQueue();
kickTargetTimestamp = Utils.getTimestamp() + 6;
sendServerPacket(Packet.ClientDisconnect, new byte[1]);
StarryboundServer.logInfo("[" + playerData.client + "] is being kicked for " + reason);
}
public void delayDisconnect(string reason, string message)
{
if (kickTargetTimestamp != 0) return;
sendChatMessage("^#f75d5d;" + reason);
flushChatQueue();
kickTargetTimestamp = Utils.getTimestamp() + 6;
sendServerPacket(Packet.ClientDisconnect, new byte[1]);
StarryboundServer.sendGlobalMessage("^#f75d5d;" + message);
StarryboundServer.logInfo("[" + playerData.client + "] is being kicked for " + message);
}
public void rejectPreConnected(string reason)
{
Packet2ConnectResponse packet = new Packet2ConnectResponse(this, Util.Direction.Client, reason);
packet.onSend();
doDisconnect(reason);
}
public void forceDisconnect(Direction direction, string reason)
{
if (direction == Direction.Server)
{
if (state != ClientState.Connected)
rejectPreConnected("Connection Failed: " + reason);
else
delayDisconnect("Dropped by parent server for " + reason);
}
else
doDisconnect("Dropped by parent client for " + reason);
}
public void closeConnection()
{
doDisconnect("has left the server.");
}
}
}