forked from AvilanceLtd/StarryboundServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForwardThread.cs
420 lines (392 loc) · 24.1 KB
/
ForwardThread.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* 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.Text;
using System.Threading.Tasks;
using com.avilance.Starrybound.Util;
using com.avilance.Starrybound.Extensions;
using Ionic.Zlib;
using com.avilance.Starrybound.Packets;
namespace com.avilance.Starrybound
{
class ForwardThread
{
BinaryReader mInput;
BinaryWriter mOutput;
ClientThread mParent;
Direction mDirection;
private string passwordSalt;
public ForwardThread(ClientThread aParent, BinaryReader aInput, BinaryWriter aOutput, Direction aDirection) {
this.mParent = aParent;
this.mInput = aInput;
this.mOutput = aOutput;
this.mDirection = aDirection;
}
public void run()
{
try
{
for (;;)
{
if (!this.mParent.connectionAlive)
{
this.mParent.forceDisconnect("Connection Lost");
return;
}
if (this.mParent.kickTargetTimestamp != 0)
{
if (this.mParent.kickTargetTimestamp < Utils.getTimestamp())
{
this.mParent.forceDisconnect("Kicked from server");
return;
}
continue;
}
#region Process Packet
//Packet ID and Vaildity Check.
uint temp = this.mInput.ReadVarUInt32();
if (temp < 1 || temp > 48)
{
this.mParent.errorDisconnect(mDirection, "Sent invalid packet ID [" + temp + "].");
return;
}
Packet packetID = (Packet)temp;
//Packet Size and Compression Check.
bool compressed = false;
int packetSize = this.mInput.ReadVarInt32();
if (packetSize < 0)
{
packetSize = -packetSize;
compressed = true;
}
//Create buffer for forwarding
byte[] dataBuffer = this.mInput.ReadFully(packetSize);
//Do decompression
MemoryStream ms = new MemoryStream();
if (compressed)
{
ZlibStream compressedStream = new ZlibStream(new MemoryStream(dataBuffer), CompressionMode.Decompress);
byte[] buffer = new byte[32768];
for (;;)
{
int read = compressedStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
ms.Seek(0, SeekOrigin.Begin);
}
else
{
ms = new MemoryStream(dataBuffer);
}
//Create packet parser
BinaryReader packetData = new BinaryReader(ms);
#endregion
//Return data for packet processor
object returnData = true;
if (packetID != Packet.Heartbeat && packetID != Packet.UniverseTimeUpdate)
{
if (mDirection == Direction.Client)
#region Handle Client Packets
{
#region Protocol State Security
ClientState curState = this.mParent.clientState;
if (curState != ClientState.Connected)
{
if (curState == ClientState.PendingConnect && packetID != Packet.ClientConnect)
{
this.mParent.forceDisconnect("Violated PendingConnect protocol state with " + packetID);
}
else if (curState == ClientState.PendingAuthentication && packetID != Packet.HandshakeResponse)
{
this.mParent.forceDisconnect("Violated PendingAuthentication protocol state with " + packetID);
}
else if (curState == ClientState.PendingConnectResponse)
{
this.mParent.forceDisconnect("Violated PendingConnectResponse protocol state with " + packetID);
}
}
#endregion
if (packetID == Packet.ChatSend)
{
returnData = new Packet11ChatSend(this.mParent, packetData, this.mDirection).onReceive();
}
else if (packetID == Packet.ClientConnect)
{
this.mParent.clientState = ClientState.PendingAuthentication;
returnData = new Packet7ClientConnect(this.mParent, packetData, this.mDirection).onReceive();
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
passwordSalt = Utils.GenerateSecureSalt();
packetWrite.WriteStarString("");
packetWrite.WriteStarString(passwordSalt);
packetWrite.WriteBE(StarryboundServer.config.passwordRounds);
this.mParent.sendClientPacket(Packet.HandshakeChallenge, packet.ToArray());
}
else if (packetID == Packet.HandshakeResponse)
{
string claimResponse = packetData.ReadStarString();
string passwordHash = packetData.ReadStarString();
string verifyHash = Utils.StarHashPassword(StarryboundServer.config.proxyPass, this.mParent.playerData.account + passwordSalt, StarryboundServer.config.passwordRounds);
if (passwordHash != verifyHash)
{
this.mParent.rejectPreConnected("Your password was incorrect.");
}
this.mParent.clientState = ClientState.PendingConnectResponse;
returnData = false;
}
else if (packetID == Packet.WarpCommand)
{
uint warp = packetData.ReadUInt32BE();
WorldCoordinate coord = packetData.ReadStarWorldCoordinate();
string player = packetData.ReadStarString();
WarpType cmd = (WarpType)warp;
if (cmd == WarpType.WarpToHomePlanet)
{
this.mParent.playerData.inPlayerShip = false;
}
else if (cmd == WarpType.WarpToOrbitedPlanet)
{
this.mParent.playerData.inPlayerShip = false;
}
else if (cmd == WarpType.WarpToOwnShip)
{
this.mParent.playerData.inPlayerShip = true;
}
else if (cmd == WarpType.WarpToPlayerShip)
{
this.mParent.playerData.inPlayerShip = true;
}
StarryboundServer.logDebug("WarpCommand", "[" + this.mParent.playerData.client + "][" + warp + "]" + (coord != null ? "[" + coord.ToString() + "]" : "") + "[" + player + "]");
}
else if (packetID == Packet.ModifyTileList || packetID == Packet.DamageTileGroup || packetID == Packet.DamageTile || packetID == Packet.ConnectWire || packetID == Packet.DisconnectAllWires)
{
if (!this.mParent.playerData.canBuild) continue;
if (this.mParent.playerData.loc != null)
{
string planetCheck = this.mParent.playerData.loc.ToString();
string spawnPlanet = StarryboundServer.serverConfig.defaultWorldCoordinate;
if (StarryboundServer.serverConfig.defaultWorldCoordinate.Split(':').Length == 5) spawnPlanet = spawnPlanet + ":0";
if ((planetCheck == spawnPlanet) && !this.mParent.playerData.group.hasPermission("admin.spawnbuild") && !this.mParent.playerData.inPlayerShip)
{
continue;
}
}
}
}
#endregion
else
#region Handle Server Packets
{
if (packetID == Packet.ChatReceive)
{
returnData = new Packet5ChatReceive(this.mParent, packetData, this.mDirection).onReceive();
}
else if (packetID == Packet.ProtocolVersion)
{
uint protocolVersion = packetData.ReadUInt32BE();
if (protocolVersion != StarryboundServer.ProtocolVersion)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(protocolVersion);
this.mParent.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
this.mParent.rejectPreConnected("Starrybound Server was unable to handle the parent server protocol version.");
returnData = false;
}
}
else if (packetID == Packet.HandshakeChallenge)
{
string claimMessage = packetData.ReadString();
string passwordSalt = packetData.ReadStarString();
int passwordRounds = packetData.ReadInt32BE();
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
string passwordHash = Utils.StarHashPassword(StarryboundServer.config.serverPass, StarryboundServer.config.serverAccount + passwordSalt, passwordRounds);
packetWrite.WriteStarString("");
packetWrite.WriteStarString(passwordHash);
this.mParent.sendServerPacket(Packet.HandshakeResponse, packet.ToArray());
returnData = false;
}
else if (packetID == Packet.ConnectResponse)
{
while (this.mParent.clientState != ClientState.PendingConnectResponse) { } //TODO: needs timeout
returnData = new Packet2ConnectResponse(this.mParent, packetData, this.mDirection).onReceive();
}
else if (packetID == Packet.WorldStart)
{
if (!this.mParent.playerData.sentMotd)
{
this.mParent.sendChatMessage(Config.GetMotd());
this.mParent.playerData.sentMotd = true;
}
byte[] planet = packetData.ReadStarByteArray();
byte[] worldStructure = packetData.ReadStarByteArray();
byte[] sky = packetData.ReadStarByteArray();
byte[] serverWeather = packetData.ReadStarByteArray();
float spawnX = packetData.ReadSingleBE();
float spawnY = packetData.ReadSingleBE();
uint mapParamsSize = packetData.ReadVarUInt32();
Dictionary<string, object> mapParams = new Dictionary<string, object>();
for (int i = 0; i < mapParamsSize; i++)
{
mapParams.Add(packetData.ReadStarString(), packetData.ReadStarVariant());
}
uint clientID = packetData.ReadUInt32BE();
bool bool1 = packetData.ReadBoolean();
WorldCoordinate coords = Utils.findGlobalCoords(sky);
if (coords != null)
{
this.mParent.playerData.loc = coords;
StarryboundServer.logDebug("WorldStart", "[" + this.mParent.playerData.client + "][" + bool1 + ":" + clientID + "] CurLoc:[" + this.mParent.playerData.loc.ToString() + "][" + this.mParent.playerData.inPlayerShip + "]");
}
}
else if (packetID == Packet.WorldStop)
{
string status = packetData.ReadStarString();
}
else if (packetID == Packet.GiveItem)
{
string name = packetData.ReadStarString();
uint count = packetData.ReadVarUInt32();
List<object> itemDesc = packetData.ReadStarVariant();
}
else if (packetID == Packet.EnvironmentUpdate)
{
byte[] sky = packetData.ReadStarByteArray();
byte[] serverWeather = packetData.ReadStarByteArray();
/*WorldCoordinate coords = Utils.findGlobalCoords(sky);
if (coords != null)
{
this.mParent.playerData.loc = coords;
StarryboundServer.logDebug("EnvUpdate", "[" + this.mParent.playerData.client + "] CurLoc:[" + this.mParent.playerData.loc.ToString() + "]");
}*/
}
else if (packetID == Packet.ClientContextUpdate)
{
try
{
byte[] clientContextData = packetData.ReadStarByteArray();
if (clientContextData.Length != 0)
{
BinaryReader clientContextReader = new BinaryReader(new MemoryStream(clientContextData));
byte[] data = clientContextReader.ReadStarByteArray();
if (data.Length > 8) //Should at least be more than 8 bytes for it to contain the data we want.
{
BinaryReader dataReader = new BinaryReader(new MemoryStream(data));
byte dataBufferLength = dataReader.ReadByte();
if (dataBufferLength == 2)
{
byte arrayLength = dataReader.ReadByte();
if (arrayLength == 2 || arrayLength == 4) //Only observed these being used so far for what we want.
{
byte dataType = dataReader.ReadByte(); //04 = String, 0E = CelestialLog
if (dataType == 4)
{
string string1 = dataReader.ReadStarString();
if (dataReader.BaseStream.Position != dataReader.BaseStream.Length)
{
if (string1 == "null")
{
byte[] worldHeader = dataReader.ReadStarByteArray(); //0008020A000C
byte[] worldData = dataReader.ReadStarByteArray();
byte typeByte = dataReader.ReadByte(); //0E = CelestialLog
if (typeByte == 14)
{
Dictionary<string, WorldCoordinate> log = dataReader.ReadStarCelestialLog();
log.TryGetValue("loc", out this.mParent.playerData.loc);
if (!log.TryGetValue("home", out this.mParent.playerData.home))
this.mParent.playerData.home = this.mParent.playerData.loc;
StarryboundServer.logDebug("ClientContext", "[" + this.mParent.playerData.client + "] CurLoc:[" + this.mParent.playerData.loc.ToString() + "][" + this.mParent.playerData.inPlayerShip + "]");
StarryboundServer.logDebug("ClientContext", "[" + this.mParent.playerData.client + "] CurHome:[" + this.mParent.playerData.home.ToString() + "]");
}
}
}
}
else if (dataType == 14)
{
Dictionary<string, WorldCoordinate> log = dataReader.ReadStarCelestialLog();
log.TryGetValue("loc", out this.mParent.playerData.loc);
if (!log.TryGetValue("home", out this.mParent.playerData.home))
this.mParent.playerData.home = this.mParent.playerData.loc;
StarryboundServer.logDebug("ClientContext", "[" + this.mParent.playerData.client + "] CurLoc:[" + this.mParent.playerData.loc.ToString() + "][" + this.mParent.playerData.inPlayerShip + "]");
StarryboundServer.logDebug("ClientContext", "[" + this.mParent.playerData.client + "] CurHome:[" + this.mParent.playerData.home.ToString() + "]");
}
}
}
}
}
}
catch (Exception e)
{
StarryboundServer.logException("[" + this.mParent.playerData.client + "] Failed to parse ClientContextUpdate from Server: " + e.Message);
}
}
}
#endregion
}
#if DEBUG
if(packetID != Packet.Heartbeat)
{
//if (ms.Position != ms.Length)
//StarryboundServer.logDebug("ForwardThread", "[" + this.mParent.playerData.client + "] [" + this.mDirection.ToString() + "][" + packetID + "] failed parse (" + ms.Position + " != " + ms.Length + ")");
//StarryboundServer.logDebug("ForwardThread", "[" + this.mParent.playerData.client + "] [" + this.mDirection.ToString() + "][" + packetID + "] Dumping " + ms.Length + " bytes: " + Utils.ByteArrayToString(ms.ToArray()));
}
#endif
//Check return data
if (returnData is Boolean)
{
if ((Boolean)returnData == false) continue;
}
else if (returnData is int)
{
if ((int)returnData == -1)
{
this.mParent.forceDisconnect("Command processor requested to drop client");
}
}
#region Forward Packet
//Write data to dest
this.mOutput.WriteVarUInt32((uint)packetID);
if (compressed)
{
this.mOutput.WriteVarInt32(-packetSize);
this.mOutput.Write(dataBuffer, 0, packetSize);
}
else
{
this.mOutput.WriteVarInt32(packetSize);
this.mOutput.Write(dataBuffer, 0, packetSize);
}
this.mOutput.Flush();
#endregion
//If disconnect was forwarded to client, lets disconnect.
if(packetID == Packet.ServerDisconnect && mDirection == Direction.Server)
{
this.mParent.forceDisconnect();
}
}
}
catch (EndOfStreamException)
{
this.mParent.forceDisconnect();
}
catch (Exception e)
{
this.mParent.errorDisconnect(mDirection, "ForwardThread Exception: " + e.ToString());
}
}
}
}