1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GameQ. |
4
|
|
|
* |
5
|
|
|
* GameQ is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* GameQ is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace GameQ\Protocols; |
20
|
|
|
|
21
|
|
|
use GameQ\Protocol; |
22
|
|
|
use GameQ\Buffer; |
23
|
|
|
use GameQ\Result; |
24
|
|
|
use GameQ\Exception\Protocol as Exception; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Valve Source Engine Protocol Class (A2S) |
28
|
|
|
* |
29
|
|
|
* This class is used as the basis for all other source based servers |
30
|
|
|
* that rely on the source protocol for game querying. |
31
|
|
|
* |
32
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
33
|
|
|
* |
34
|
|
|
* @author Austin Bischoff <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class Source extends Protocol |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/* |
40
|
|
|
* Source engine type constants |
41
|
|
|
*/ |
42
|
|
|
const SOURCE_ENGINE = 0, |
43
|
|
|
GOLDSOURCE_ENGINE = 1; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of packets we want to look up. |
47
|
|
|
* Each key should correspond to a defined method in this or a parent class |
48
|
|
|
* |
49
|
|
|
* @type array |
50
|
|
|
*/ |
51
|
|
|
protected $packets = [ |
52
|
|
|
self::PACKET_CHALLENGE => "\xFF\xFF\xFF\xFF\x56\x00\x00\x00\x00", |
53
|
|
|
self::PACKET_DETAILS => "\xFF\xFF\xFF\xFFTSource Engine Query\x00", |
54
|
|
|
self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s", |
55
|
|
|
self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s", |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Use the response flag to figure out what method to run |
60
|
|
|
* |
61
|
|
|
* @type array |
62
|
|
|
*/ |
63
|
|
|
protected $responses = [ |
64
|
|
|
"\x49" => "processDetails", // I |
65
|
|
|
"\x6d" => "processDetailsGoldSource", // m, goldsource |
66
|
|
|
"\x44" => "processPlayers", // D |
67
|
|
|
"\x45" => "processRules", // E |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The query protocol used to make the call |
72
|
|
|
* |
73
|
|
|
* @type string |
74
|
|
|
*/ |
75
|
|
|
protected $protocol = 'source'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* String name of this protocol class |
79
|
|
|
* |
80
|
|
|
* @type string |
81
|
|
|
*/ |
82
|
|
|
protected $name = 'source'; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Longer string name of this protocol class |
86
|
|
|
* |
87
|
|
|
* @type string |
88
|
|
|
*/ |
89
|
|
|
protected $name_long = "Source Server"; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Define the Source engine type. By default it is assumed to be Source |
93
|
|
|
* |
94
|
|
|
* @type int |
95
|
|
|
*/ |
96
|
|
|
protected $source_engine = self::SOURCE_ENGINE; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The client join link |
100
|
|
|
* |
101
|
|
|
* @type string |
102
|
|
|
*/ |
103
|
|
|
protected $join_link = "steam://connect/%s:%d/"; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Normalize settings for this protocol |
107
|
|
|
* |
108
|
|
|
* @type array |
109
|
|
|
*/ |
110
|
|
|
protected $normalize = [ |
111
|
|
|
// General |
112
|
|
|
'general' => [ |
113
|
|
|
// target => source |
114
|
|
|
'dedicated' => 'dedicated', |
115
|
|
|
'gametype' => 'game_descr', |
116
|
|
|
'hostname' => 'hostname', |
117
|
|
|
'mapname' => 'map', |
118
|
|
|
'maxplayers' => 'max_players', |
119
|
|
|
'mod' => 'game_dir', |
120
|
|
|
'numplayers' => 'num_players', |
121
|
|
|
'password' => 'password', |
122
|
|
|
], |
123
|
|
|
// Individual |
124
|
|
|
'player' => [ |
125
|
|
|
'name' => 'name', |
126
|
|
|
'score' => 'score', |
127
|
|
|
'time' => 'time', |
128
|
|
|
], |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Parse the challenge response and apply it to all the packet types |
133
|
|
|
* |
134
|
|
|
* @param \GameQ\Buffer $challenge_buffer |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
* @throws \GameQ\Exception\Protocol |
138
|
|
|
*/ |
139
|
1 |
|
public function challengeParseAndApply(Buffer $challenge_buffer) |
140
|
|
|
{ |
141
|
|
|
|
142
|
|
|
// Skip the header |
143
|
1 |
|
$challenge_buffer->skip(5); |
144
|
|
|
|
145
|
|
|
// Apply the challenge and return |
146
|
1 |
|
return $this->challengeApply($challenge_buffer->read(4)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Process the response |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
* @throws \GameQ\Exception\Protocol |
154
|
|
|
*/ |
155
|
61 |
|
public function processResponse() |
156
|
|
|
{ |
157
|
|
|
|
158
|
61 |
|
$results = [ ]; |
159
|
|
|
|
160
|
61 |
|
$packets = [ ]; |
161
|
|
|
|
162
|
|
|
// We need to pre-sort these for split packets so we can do extra work where needed |
163
|
61 |
|
foreach ($this->packets_response as $response) { |
164
|
61 |
|
$buffer = new Buffer($response); |
165
|
|
|
|
166
|
|
|
// Get the header of packet (long) |
167
|
61 |
|
$header = $buffer->readInt32Signed(); |
168
|
|
|
|
169
|
|
|
// Single packet |
170
|
61 |
|
if ($header == -1) { |
171
|
|
|
// We need to peek and see what kind of engine this is for later processing |
172
|
61 |
|
if ($buffer->lookAhead(1) == "\x6d") { |
173
|
4 |
|
$this->source_engine = self::GOLDSOURCE_ENGINE; |
174
|
4 |
|
} |
175
|
|
|
|
176
|
61 |
|
$packets[] = $buffer->getBuffer(); |
177
|
61 |
|
continue; |
178
|
|
|
} else { |
179
|
|
|
// Split packet |
180
|
|
|
|
181
|
|
|
// Packet Id (long) |
182
|
25 |
|
$packet_id = $buffer->readInt32Signed(); |
183
|
|
|
|
184
|
|
|
// Add the buffer to the packet as another array |
185
|
25 |
|
$packets[$packet_id][] = $buffer->getBuffer(); |
186
|
|
|
} |
187
|
61 |
|
} |
188
|
|
|
|
189
|
|
|
// Now that we have the packets sorted we need to iterate and process them |
190
|
61 |
|
foreach ($packets as $packet_id => $packet) { |
191
|
|
|
// We first need to off load split packets to combine them |
192
|
61 |
|
if (is_array($packet)) { |
193
|
23 |
|
$buffer = new Buffer($this->processPackets($packet_id, $packet)); |
194
|
23 |
|
} else { |
195
|
61 |
|
$buffer = new Buffer($packet); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Figure out what packet response this is for |
199
|
61 |
|
$response_type = $buffer->read(1); |
200
|
|
|
|
201
|
|
|
// Figure out which packet response this is |
202
|
61 |
|
if (!array_key_exists($response_type, $this->responses)) { |
203
|
2 |
|
throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Now we need to call the proper method |
207
|
59 |
|
$results = array_merge( |
208
|
59 |
|
$results, |
209
|
59 |
|
call_user_func_array([ $this, $this->responses[$response_type] ], [ $buffer ]) |
210
|
59 |
|
); |
211
|
|
|
|
212
|
59 |
|
unset($buffer); |
213
|
59 |
|
} |
214
|
|
|
|
215
|
59 |
|
unset($packets, $packet); |
216
|
|
|
|
217
|
59 |
|
return $results; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/* |
221
|
|
|
* Internal methods |
222
|
|
|
*/ |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Process the split packets and decompress if necessary |
226
|
|
|
* |
227
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
228
|
|
|
* |
229
|
|
|
* @param $packet_id |
230
|
|
|
* @param array $packets |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
* @throws \GameQ\Exception\Protocol |
234
|
|
|
*/ |
235
|
23 |
|
protected function processPackets($packet_id, array $packets = [ ]) |
236
|
|
|
{ |
237
|
|
|
|
238
|
|
|
// Init array so we can order |
239
|
23 |
|
$packs = [ ]; |
240
|
|
|
|
241
|
|
|
// We have multiple packets so we need to get them and order them |
242
|
23 |
|
foreach ($packets as $i => $packet) { |
243
|
|
|
// Make a buffer so we can read this info |
244
|
23 |
|
$buffer = new Buffer($packet); |
245
|
|
|
|
246
|
|
|
// Gold source |
247
|
23 |
|
if ($this->source_engine == self::GOLDSOURCE_ENGINE) { |
248
|
|
|
// Grab the packet number (byte) |
249
|
9 |
|
$packet_number = $buffer->readInt8(); |
250
|
|
|
|
251
|
|
|
// We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop |
252
|
9 |
|
if ($i == 0) { |
253
|
9 |
|
$buffer->read(4); |
254
|
9 |
|
} |
255
|
|
|
|
256
|
|
|
// Now add the rest of the packet to the new array with the packet_number as the id so we can order it |
257
|
9 |
|
$packs[$packet_number] = $buffer->getBuffer(); |
258
|
9 |
|
} else { |
259
|
|
|
// Number of packets in this set (byte) |
260
|
14 |
|
$buffer->readInt8(); |
261
|
|
|
|
262
|
|
|
// The current packet number (byte) |
263
|
14 |
|
$packet_number = $buffer->readInt8(); |
264
|
|
|
|
265
|
|
|
// Check to see if this is compressed |
266
|
|
|
// @todo: Check to make sure these decompress correctly, new changes may affect this loop. |
267
|
14 |
|
if ($packet_id & 0x80000000) { |
268
|
|
|
// Check to see if we have Bzip2 installed |
269
|
|
|
if (!function_exists('bzdecompress')) { |
270
|
|
|
throw new Exception( |
271
|
|
|
'Bzip2 is not installed. See http://www.php.net/manual/en/book.bzip2.php for more info.', |
272
|
|
|
0 |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Get the length of the packet (long) |
277
|
|
|
$packet_length = $buffer->readInt32Signed(); |
278
|
|
|
|
279
|
|
|
// Checksum for the decompressed packet (long) |
280
|
|
|
$buffer->readInt32Signed(); |
281
|
|
|
|
282
|
|
|
// Try to decompress |
283
|
|
|
$result = bzdecompress($buffer->getBuffer()); |
284
|
|
|
|
285
|
|
|
// Now verify the length |
286
|
|
|
if (strlen($result) != $packet_length) { |
287
|
|
|
throw new Exception( |
288
|
|
|
"Checksum for compressed packet failed! Length expected: {$packet_length}, length |
289
|
|
|
returned: " . strlen($result) |
290
|
|
|
); |
291
|
|
|
} |
292
|
|
|
} else { |
293
|
|
|
// Get the packet length (short) |
294
|
14 |
|
$buffer->readInt16Signed(); |
295
|
|
|
|
296
|
|
|
// We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop |
297
|
14 |
|
if ($i == 0) { |
298
|
14 |
|
$buffer->read(4); |
299
|
14 |
|
} |
300
|
|
|
|
301
|
|
|
// Grab the rest of the buffer as a result |
302
|
14 |
|
$result = $buffer->getBuffer(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// Add this packet to the list |
306
|
14 |
|
$packs[$packet_number] = $result; |
307
|
|
|
} |
308
|
|
|
|
309
|
23 |
|
unset($buffer); |
310
|
23 |
|
} |
311
|
|
|
|
312
|
|
|
// Free some memory |
313
|
23 |
|
unset($packets, $packet); |
314
|
|
|
|
315
|
|
|
// Sort the packets by packet number |
316
|
23 |
|
ksort($packs); |
317
|
|
|
|
318
|
|
|
// Now combine the packs into one and return |
319
|
23 |
|
return implode("", $packs); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Handles processing the details data into a usable format |
324
|
|
|
* |
325
|
|
|
* @param \GameQ\Buffer $buffer |
326
|
|
|
* |
327
|
|
|
* @return mixed |
328
|
|
|
* @throws \GameQ\Exception\Protocol |
329
|
|
|
*/ |
330
|
57 |
|
protected function processDetails(Buffer $buffer) |
331
|
|
|
{ |
332
|
|
|
|
333
|
|
|
// Set the result to a new result instance |
334
|
57 |
|
$result = new Result(); |
335
|
|
|
|
336
|
57 |
|
$result->add('protocol', $buffer->readInt8()); |
337
|
57 |
|
$result->add('hostname', $buffer->readString()); |
338
|
57 |
|
$result->add('map', $buffer->readString()); |
339
|
57 |
|
$result->add('game_dir', $buffer->readString()); |
340
|
57 |
|
$result->add('game_descr', $buffer->readString()); |
341
|
57 |
|
$result->add('steamappid', $buffer->readInt16()); |
342
|
57 |
|
$result->add('num_players', $buffer->readInt8()); |
343
|
57 |
|
$result->add('max_players', $buffer->readInt8()); |
344
|
57 |
|
$result->add('num_bots', $buffer->readInt8()); |
345
|
57 |
|
$result->add('dedicated', $buffer->read()); |
346
|
57 |
|
$result->add('os', $buffer->read()); |
347
|
57 |
|
$result->add('password', $buffer->readInt8()); |
348
|
57 |
|
$result->add('secure', $buffer->readInt8()); |
349
|
|
|
|
350
|
|
|
// Special result for The Ship only (appid=2400) |
351
|
57 |
|
if ($result->get('steamappid') == 2400) { |
352
|
3 |
|
$result->add('game_mode', $buffer->readInt8()); |
353
|
3 |
|
$result->add('witness_count', $buffer->readInt8()); |
354
|
3 |
|
$result->add('witness_time', $buffer->readInt8()); |
355
|
3 |
|
} |
356
|
|
|
|
357
|
57 |
|
$result->add('version', $buffer->readString()); |
358
|
|
|
|
359
|
|
|
// Extra data flag |
360
|
57 |
|
if ($buffer->lookAhead(1) !== false) { |
361
|
54 |
|
$edf = $buffer->readInt8(); |
362
|
|
|
|
363
|
54 |
|
if ($edf & 0x80) { |
364
|
54 |
|
$result->add('port', $buffer->readInt16Signed()); |
365
|
54 |
|
} |
366
|
|
|
|
367
|
54 |
|
if ($edf & 0x10) { |
368
|
52 |
|
$result->add('steam_id', $buffer->readInt64()); |
369
|
52 |
|
} |
370
|
|
|
|
371
|
54 |
|
if ($edf & 0x40) { |
372
|
1 |
|
$result->add('sourcetv_port', $buffer->readInt16Signed()); |
373
|
1 |
|
$result->add('sourcetv_name', $buffer->readString()); |
374
|
1 |
|
} |
375
|
|
|
|
376
|
54 |
|
if ($edf & 0x20) { |
377
|
42 |
|
$result->add('keywords', $buffer->readString()); |
378
|
42 |
|
} |
379
|
|
|
|
380
|
54 |
|
if ($edf & 0x01) { |
381
|
52 |
|
$result->add('game_id', $buffer->readInt64()); |
382
|
52 |
|
} |
383
|
|
|
|
384
|
54 |
|
unset($edf); |
385
|
54 |
|
} |
386
|
|
|
|
387
|
57 |
|
unset($buffer); |
388
|
|
|
|
389
|
57 |
|
return $result->fetch(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Handles processing the server details from goldsource response |
394
|
|
|
* |
395
|
|
|
* @param \GameQ\Buffer $buffer |
396
|
|
|
* |
397
|
|
|
* @return array |
398
|
|
|
* @throws \GameQ\Exception\Protocol |
399
|
|
|
*/ |
400
|
4 |
|
protected function processDetailsGoldSource(Buffer $buffer) |
401
|
|
|
{ |
402
|
|
|
|
403
|
|
|
// Set the result to a new result instance |
404
|
4 |
|
$result = new Result(); |
405
|
|
|
|
406
|
4 |
|
$result->add('address', $buffer->readString()); |
407
|
4 |
|
$result->add('hostname', $buffer->readString()); |
408
|
4 |
|
$result->add('map', $buffer->readString()); |
409
|
4 |
|
$result->add('game_dir', $buffer->readString()); |
410
|
4 |
|
$result->add('game_descr', $buffer->readString()); |
411
|
4 |
|
$result->add('num_players', $buffer->readInt8()); |
412
|
4 |
|
$result->add('max_players', $buffer->readInt8()); |
413
|
4 |
|
$result->add('version', $buffer->readInt8()); |
414
|
4 |
|
$result->add('dedicated', $buffer->read()); |
415
|
4 |
|
$result->add('os', $buffer->read()); |
416
|
4 |
|
$result->add('password', $buffer->readInt8()); |
417
|
|
|
|
418
|
|
|
// Mod section |
419
|
4 |
|
$result->add('ismod', $buffer->readInt8()); |
420
|
|
|
|
421
|
|
|
// We only run these if ismod is 1 (true) |
422
|
4 |
|
if ($result->get('ismod') == 1) { |
423
|
4 |
|
$result->add('mod_urlinfo', $buffer->readString()); |
424
|
4 |
|
$result->add('mod_urldl', $buffer->readString()); |
425
|
4 |
|
$buffer->skip(); |
426
|
4 |
|
$result->add('mod_version', $buffer->readInt32Signed()); |
427
|
4 |
|
$result->add('mod_size', $buffer->readInt32Signed()); |
428
|
4 |
|
$result->add('mod_type', $buffer->readInt8()); |
429
|
4 |
|
$result->add('mod_cldll', $buffer->readInt8()); |
430
|
4 |
|
} |
431
|
|
|
|
432
|
4 |
|
$result->add('secure', $buffer->readInt8()); |
433
|
4 |
|
$result->add('num_bots', $buffer->readInt8()); |
434
|
|
|
|
435
|
4 |
|
unset($buffer); |
436
|
|
|
|
437
|
4 |
|
return $result->fetch(); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Handles processing the player data into a usable format |
442
|
|
|
* |
443
|
|
|
* @param \GameQ\Buffer $buffer |
444
|
|
|
* |
445
|
|
|
* @return mixed |
446
|
|
|
*/ |
447
|
56 |
|
protected function processPlayers(Buffer $buffer) |
448
|
|
|
{ |
449
|
|
|
|
450
|
|
|
// Set the result to a new result instance |
451
|
56 |
|
$result = new Result(); |
452
|
|
|
|
453
|
|
|
// Pull out the number of players |
454
|
56 |
|
$num_players = $buffer->readInt8(); |
455
|
|
|
|
456
|
|
|
// Player count |
457
|
56 |
|
$result->add('num_players', $num_players); |
458
|
|
|
|
459
|
|
|
// No players so no need to look any further |
460
|
56 |
|
if ($num_players == 0) { |
461
|
7 |
|
return $result->fetch(); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// Players list |
465
|
49 |
|
while ($buffer->getLength()) { |
466
|
49 |
|
$result->addPlayer('id', $buffer->readInt8()); |
467
|
49 |
|
$result->addPlayer('name', $buffer->readString()); |
468
|
49 |
|
$result->addPlayer('score', $buffer->readInt32Signed()); |
469
|
49 |
|
$result->addPlayer('time', $buffer->readFloat32()); |
470
|
49 |
|
} |
471
|
|
|
|
472
|
49 |
|
unset($buffer); |
473
|
|
|
|
474
|
49 |
|
return $result->fetch(); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Handles processing the rules data into a usable format |
479
|
|
|
* |
480
|
|
|
* @param \GameQ\Buffer $buffer |
481
|
|
|
* |
482
|
|
|
* @return mixed |
483
|
|
|
*/ |
484
|
49 |
|
protected function processRules(Buffer $buffer) |
485
|
|
|
{ |
486
|
|
|
|
487
|
|
|
// Set the result to a new result instance |
488
|
49 |
|
$result = new Result(); |
489
|
|
|
|
490
|
|
|
// Count the number of rules |
491
|
49 |
|
$num_rules = $buffer->readInt16Signed(); |
492
|
|
|
|
493
|
|
|
// Add the count of the number of rules this server has |
494
|
49 |
|
$result->add('num_rules', $num_rules); |
495
|
|
|
|
496
|
|
|
// Rules |
497
|
49 |
|
while ($buffer->getLength()) { |
498
|
49 |
|
$result->add($buffer->readString(), $buffer->readString()); |
499
|
49 |
|
} |
500
|
|
|
|
501
|
49 |
|
unset($buffer); |
502
|
|
|
|
503
|
49 |
|
return $result->fetch(); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|