-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceServerQuery.cs
More file actions
640 lines (540 loc) · 23.1 KB
/
SourceServerQuery.cs
File metadata and controls
640 lines (540 loc) · 23.1 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Checksums;
namespace SteamMasterServer.Lib
{
public class SourceServerQuery
{
// this will hold our ip address as an object (required by Socket & UdpClient)
private IPEndPoint remote;
// used for single-packet responses (mainly A2S_INFO, A2S_PLAYER and Challenge)
private Socket socket;
// multi-packet responses (currently only A2S_RULES)
private UdpClient client;
// send & receive timeouts
private int send_timeout = 2500;
private int receive_timeout = 2500;
// raw response returned from the server
private byte[] raw_data;
private int offset = 0;
// constants
private readonly byte[] A2S_HEADER = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF };
private readonly byte A2S_INFO = 0x54;
private readonly byte A2S_PLAYER = 0x55;
private readonly byte A2S_RULES = 0x56;
private readonly byte[] A2S_INFO_STUB = new byte[] { 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00 };
private readonly byte[] CHALLENGE = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF};
public SourceServerQuery(String ip, int port)
{
this.remote = new IPEndPoint(IPAddress.Parse(ip), port);
}
/// <summary>
/// Retrieve general information from the Server via A2S_Info.
/// See https://developer.valvesoftware.com/wiki/Server_queries#A2S_INFO for more Information
/// </summary>
/// <returns>A ServerInfoResponse Object containing the publically available data</returns>
public ServerInfoResponse GetServerInformation()
{
// open socket if not already open
this.GetSocket();
// reset our pointer
this.offset = 6;
ServerInfoResponse sr = new ServerInfoResponse();
// construct request byte-array
byte[] request = new byte[A2S_HEADER.Length+A2S_INFO_STUB.Length+1];
Array.Copy(this.A2S_HEADER, 0, request, 0, A2S_HEADER.Length);
request[A2S_HEADER.Length] = this.A2S_INFO;
Array.Copy(this.A2S_INFO_STUB, 0, request, A2S_HEADER.Length + 1, A2S_INFO_STUB.Length);
this.socket.Send(request);
this.raw_data = new byte[1024];
try
{
this.socket.Receive(this.raw_data);
// read data
sr.name = this.ReadString();
sr.map = this.ReadString();
sr.directory = this.ReadString();
sr.game = this.ReadString();
sr.appid = this.ReadInt16();
sr.players = this.ReadByte();
sr.maxplayers = this.ReadByte();
sr.bots = this.ReadByte();
sr.dedicated = (this.ReadChar() == 'd') ? true : false;
sr.os = (this.ReadChar() == 'l') ? "Linux" : "Windows";
sr.password = (this.ReadByte() == 1) ? true : false;
sr.secure = (this.ReadByte() == 1) ? true : false;
sr.version = this.ReadString();
}
catch (SocketException e)
{
sr.name = "N/A (request timed out)";
sr.map = "N/A";
sr.directory = "N/A";
sr.game = "N/A";
sr.appid = -1;
sr.players = 0;
sr.maxplayers = 0;
sr.bots = -1;
sr.dedicated = false;
sr.os = "N/A";
sr.password = false;
sr.secure = false;
sr.version = "N/A";
}
return sr;
}
/// <summary>
/// Get a list of currently in-game clients on the specified gameserver.
/// <b>Please note:</b> the playtime is stored as a float in <i>seconds</i>, you might want to convert it.
///
/// See https://developer.valvesoftware.com/wiki/Server_queries#A2S_PLAYER for more Information
/// </summary>
/// <returns>A PLayersResponse Object containing the name, score and playtime of each player</returns>
public PlayersResponse GetPlayerList()
{
// open socket if not already open
this.GetSocket();
// we don't need the header, so set pointer to where the payload begins
this.offset = 5;
try
{
PlayersResponse pr = new PlayersResponse();
// since A2S_PLAYER requests require a valid challenge, get it first
byte[] challenge = this.GetChallenge(A2S_PLAYER, true);
byte[] request = new byte[challenge.Length+this.A2S_HEADER.Length+1];
Array.Copy(this.A2S_HEADER, 0, request, 0, this.A2S_HEADER.Length);
request[this.A2S_HEADER.Length] = A2S_PLAYER;
Array.Copy(challenge, 0, request, this.A2S_HEADER.Length + 1, challenge.Length);
this.socket.Send(request);
this.raw_data = new byte[1024];
this.socket.Receive(this.raw_data);
byte player_count = this.ReadByte();
// fill up the list of players
for (int i = 0; i < player_count; i++)
{
this.ReadByte();
PlayersResponse.Player p = new PlayersResponse.Player();
p.name = this.ReadString();
p.score = this.ReadInt32();
p.playtime = this.ReadFloat();
pr.players.Add(p);
}
pr.player_count = player_count;
return pr;
}
catch (SocketException e)
{
return null;
}
}
/// <summary>
/// Get a list of all publically available CVars ("rules") from the server.
/// <b>Note:</b> Due to a bug in the Source Engine, it might happen that some CVars/values are cut off.
///
/// Example: mp_idlemaxtime = [nothing]
/// Only Valve can fix that.
/// </summary>
/// <returns>A RulesResponse Object containing a Name-Value pair of each CVar</returns>
public RulesResponse GetRules()
{
// open udpclient if not already open
this.GetClient();
try
{
RulesResponse rr = new RulesResponse();
// similar to A2S_PLAYER requests, A2S_RULES require a valid challenge
byte[] challenge = this.GetChallenge(A2S_RULES, false);
byte[] request = new byte[challenge.Length + this.A2S_HEADER.Length + 1];
Array.Copy(this.A2S_HEADER, 0, request, 0, this.A2S_HEADER.Length);
request[this.A2S_HEADER.Length] = A2S_RULES;
Array.Copy(challenge, 0, request, this.A2S_HEADER.Length + 1, challenge.Length);
this.client.Send(request, request.Length);
//
// Since A2S_RULES responses might be split up into several packages/compressed, we have to do a special handling of them
//
int bytesRead;
// this will keep our assembled message
byte[] buffer = new byte[4096];
// send first request
this.raw_data = this.client.Receive(ref this.remote);
bytesRead = this.raw_data.Length;
// reset pointer
this.offset = 0;
int is_split = this.ReadInt32();
int requestid = this.ReadInt32();
this.offset = 4;
// response is split up into several packets
if (this.PacketIsSplit(is_split))
{
bool isCompressed = false;
byte[] splitData;
int packetCount, packetNumber, requestId;
int packetsReceived = 1;
int packetChecksum = 0;
int packetSplit = 0;
short splitSize;
int uncompressedSize = 0;
List<byte[]> splitPackets = new List<byte[]>();
do
{
// unique request id
requestId = this.ReverseBytes(this.ReadInt32());
isCompressed = this.PacketIsCompressed(requestId);
packetCount = this.ReadByte();
packetNumber = this.ReadByte() + 1;
// so we know how big our byte arrays have to be
splitSize = this.ReadInt16();
splitSize -= 4; // fix
if (packetsReceived == 1)
{
for (int i = 0; i < packetCount; i++)
{
splitPackets.Add(new byte[] { });
}
}
// if the packets are compressed, get some data to decompress them
if (isCompressed)
{
uncompressedSize = ReverseBytes(this.ReadInt32());
packetChecksum = ReverseBytes(this.ReadInt32());
}
// ommit header in first packet
if (packetNumber == 1) this.ReadInt32();
splitData = new byte[splitSize];
splitPackets[packetNumber - 1] = this.ReadBytes();
// fixes a case where the returned package might still contain a character after the last \0 terminator (truncated name => value)
// please note: this therefore also removes the value of said variable, but atleast the program won't crash
if (splitPackets[packetNumber-1].Length -1 > 0 && splitPackets[packetNumber - 1][splitPackets[packetNumber - 1].Length - 1] != 0x00)
{
splitPackets[packetNumber - 1][splitPackets[packetNumber - 1].Length - 1] = 0x00;
}
// reset pointer again, so we can copy over the contents
this.offset = 0;
if (packetsReceived < packetCount)
{
this.raw_data = this.client.Receive(ref this.remote);
bytesRead = this.raw_data.Length;
// continue with the next packets
packetSplit = this.ReadInt32();
packetsReceived++;
}
else
{
// all packets received
bytesRead = 0;
}
}
while (packetsReceived <= packetCount && bytesRead > 0 && packetSplit == -2);
// decompress
if (isCompressed)
{
buffer = ReassemblePacket(splitPackets, true, uncompressedSize, packetChecksum);
}
else
{
buffer = ReassemblePacket(splitPackets, false, 0, 0);
}
}
else
{
buffer = this.raw_data;
}
// move our final result over to handle it
this.raw_data = buffer;
// omitting header
this.offset = 1;
rr.rule_count = this.ReadInt16();
for (int i = 0; i < rr.rule_count; i++)
{
RulesResponse.Rule rule = new RulesResponse.Rule();
rule.name = this.ReadString();
rule.value = this.ReadString();
rr.rules.Add(rule);
}
return rr;
}
catch (SocketException e)
{
return null;
}
}
/// <summary>
/// Close all currently open socket/UdpClient connections
/// </summary>
public void CleanUp()
{
if (this.socket != null) this.socket.Close();
if (this.client != null) this.client.Close();
}
/// <summary>
/// Set the IP and Port used in this Object.
/// </summary>
/// <param name="ip">The Server IP</param>
/// <param name="port">The Server Port</param>
public void SetAddress(String ip, int port)
{
this.remote = new IPEndPoint(IPAddress.Parse(ip), port);
if (this.socket != null)
{
this.socket.Close();
this.socket = null;
}
if (this.client != null)
{
this.client.Close();
this.client = null;
}
}
/// <summary>
/// Sets the Send Timeout on both the Socket and the Client
/// </summary>
/// <param name="timeout"></param>
public void SetSendTimeout(int timeout)
{
this.send_timeout = timeout;
}
/// <summary>
/// Sets the Receive Timeout on both the Socket and the Client
/// </summary>
/// <param name="timeout"></param>
public void SetReceiveTimeout(int timeout)
{
this.receive_timeout = timeout;
}
/// <summary>
/// Open up a new Socket-based connection to a server, if not already open.
/// </summary>
private void GetSocket()
{
if (this.socket == null)
{
this.socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
this.socket.SendTimeout = this.send_timeout;
this.socket.ReceiveTimeout = this.receive_timeout;
this.socket.Connect(this.remote);
}
}
/// <summary>
/// Create a new UdpClient connection to a server (mostly used for multi-packet answers)
/// </summary>
private void GetClient()
{
if (this.client == null)
{
this.client = new UdpClient();
this.client.Connect(this.remote);
this.client.DontFragment = true;
this.client.Client.SendTimeout = this.send_timeout;
this.client.Client.ReceiveTimeout = this.receive_timeout;
}
}
/// <summary>
/// Reassmble a multi-packet response.
/// </summary>
/// <param name="splitPackets">The packets to assemble</param>
/// <param name="isCompressed">true: packets are compressed; false: not</param>
/// <param name="uncompressedSize">The size of the message after decompression (for comparison)</param>
/// <param name="packetChecksum">Validation of the result</param>
/// <returns>A byte-array containing all packets assembled together/decompressed.</returns>
private byte[] ReassemblePacket(List<byte[]> splitPackets, bool isCompressed, int uncompressedSize, int packetChecksum)
{
byte[] packetData, tmpData;
packetData = new byte[0];
foreach (byte[] splitPacket in splitPackets)
{
if (splitPacket == null)
{
throw new Exception();
}
tmpData = packetData;
packetData = new byte[tmpData.Length + splitPacket.Length];
MemoryStream memStream = new MemoryStream(packetData);
memStream.Write(tmpData, 0, tmpData.Length);
memStream.Write(splitPacket, 0, splitPacket.Length);
}
if (isCompressed)
{
BZip2InputStream bzip2 = new BZip2InputStream(new MemoryStream(packetData));
bzip2.Read(packetData, 0, uncompressedSize);
Crc32 crc32 = new Crc32();
crc32.Update(packetData);
if (crc32.Value != packetChecksum)
{
throw new Exception("CRC32 checksum mismatch of uncompressed packet data.");
}
}
return packetData;
}
/// <summary>
/// Invert the Byte-order Mark of an value, used for compatibility between Little <-> Large BOM
/// </summary>
/// <param name="value">The value to invert</param>
/// <returns>BOM-inversed value (if needed), otherwise the original value</returns>
private int ReverseBytes(int value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return BitConverter.ToInt32(bytes, 0);
}
/// <summary>
/// <see cref="ReverseBytes(int value)">See</see> for more details.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private short ReverseBytes(short value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return BitConverter.ToInt16(bytes, 0);
}
/// <summary>
/// Determine whetever or not a message is compressed.
/// Simply detects if the most significant bit is 1.
/// </summary>
/// <param name="value">The value to check</param>
/// <returns>true, if message is compressed, false otherwise</returns>
private bool PacketIsCompressed(int value)
{
return (value & 0x8000) != 0;
}
/// <summary>
/// Determine whetever or not a message is split up.
/// </summary>
/// <param name="paket">The value to check</param>
/// <returns>true, if message is split up, false otherwise</returns>
private bool PacketIsSplit(int paket)
{
return (paket == -2);
}
/// <summary>
/// Request the 4-byte challenge id from the server, required for A2S_RULES and A2S_PLAYER.
/// </summary>
/// <param name="type">The type of message to request the challenge for (see constants)</param>
/// <param name="socket">Request method to use (performance reasons)</param>
/// <returns>A Byte Array (4-bytes) containing the challenge</returns>
private Byte[] GetChallenge(byte type, bool socket = true)
{
byte[] request = new byte[this.A2S_HEADER.Length+this.CHALLENGE.Length+1];
Array.Copy(this.A2S_HEADER, 0, request, 0, this.A2S_HEADER.Length);
request[A2S_HEADER.Length] = type;
Array.Copy(this.CHALLENGE, 0, request, this.A2S_HEADER.Length + 1, this.CHALLENGE.Length);
byte[] raw_response = new byte[24];
byte[] challenge = new byte[4];
// using sockets
if (socket)
{
this.socket.Send(request);
this.socket.Receive(raw_response);
}
else
{
this.client.Send(request, request.Length);
raw_response = this.client.Receive(ref this.remote);
}
Array.Copy(raw_response, 5, challenge, 0, 4); // change this valve modifies the protocol!
return challenge;
}
/// <summary>
/// Read a single byte value from our raw data.
/// </summary>
/// <returns>A single Byte at the next Offset Address</returns>
private Byte ReadByte()
{
byte[] b = new byte[1];
Array.Copy(this.raw_data, this.offset, b, 0, 1);
this.offset++;
return b[0];
}
/// <summary>
/// Read all remaining Bytes from our raw data.
/// Used for multi-packet responses.
/// </summary>
/// <returns>All remaining data</returns>
private Byte[] ReadBytes()
{
int size = (this.raw_data.Length - this.offset - 4);
if (size < 1) return new Byte[] { };
byte[] b = new byte[size];
Array.Copy(this.raw_data, this.offset, b, 0, this.raw_data.Length - this.offset - 4);
this.offset += (this.raw_data.Length - this.offset - 4);
return b;
}
/// <summary>
/// Read a 32-Bit Integer value from the next offset address.
/// </summary>
/// <returns>The Int32 Value found at the offset address</returns>
private Int32 ReadInt32()
{
byte[] b = new byte[4];
Array.Copy(this.raw_data, this.offset, b, 0, 4);
this.offset += 4;
return BitConverter.ToInt32(b, 0);
}
/// <summary>
/// Read a 16-Bit Integer (also called "short") value from the next offset address.
/// </summary>
/// <returns>The Int16 Value found at the offset address</returns>
private Int16 ReadInt16()
{
byte[] b = new byte[2];
Array.Copy(this.raw_data, this.offset, b, 0, 2);
this.offset += 2;
return BitConverter.ToInt16(b, 0);
}
/// <summary>
/// Read a Float value from the next offset address.
/// </summary>
/// <returns>The Float Value found at the offset address</returns>
private float ReadFloat()
{
byte[] b = new byte[4];
Array.Copy(this.raw_data, this.offset, b, 0, 4);
this.offset += 4;
return BitConverter.ToSingle(b, 0);
}
/// <summary>
/// Read a single char value from the next offset address.
/// </summary>
/// <returns>The Char found at the offset address</returns>
private Char ReadChar()
{
byte[] b = new byte[1];
Array.Copy(this.raw_data, this.offset, b, 0, 1);
this.offset++;
return (char)b[0];
}
/// <summary>
/// Read a String until its end starting from the next offset address.
/// Reading stops once the method detects a 0x00 Character at the next position (\0 terminator)
/// </summary>
/// <returns>The String read</returns>
private String ReadString()
{
byte[] cache = new byte[1]{0x01};
String output = "";
while(cache[0] != 0x00)
{
if (this.offset == this.raw_data.Length) break; // fixes Valve's inability to code a proper query protocol
Array.Copy(this.raw_data, this.offset, cache, 0, 1);
this.offset++;
output += Encoding.UTF8.GetString(cache);
}
return output;
}
}
}