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
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* GameSpy3 Protocol class |
27
|
|
|
* |
28
|
|
|
* Given the ability for non utf-8 characters to be used as hostnames, player names, etc... this |
29
|
|
|
* version returns all strings utf-8 encoded (utf8_encode). To access the proper version of a |
30
|
|
|
* string response you must use utf8_decode() on the specific response. |
31
|
|
|
* |
32
|
|
|
* @author Austin Bischoff <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Gamespy3 extends Protocol |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Array of packets we want to look up. |
39
|
|
|
* Each key should correspond to a defined method in this or a parent class |
40
|
|
|
* |
41
|
|
|
* @type array |
42
|
|
|
*/ |
43
|
|
|
protected $packets = [ |
44
|
|
|
self::PACKET_CHALLENGE => "\xFE\xFD\x09\x10\x20\x30\x40", |
45
|
|
|
self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40%s\xFF\xFF\xFF\x01", |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The query protocol used to make the call |
50
|
|
|
* |
51
|
|
|
* @type string |
52
|
|
|
*/ |
53
|
|
|
protected $protocol = 'gamespy3'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* String name of this protocol class |
57
|
|
|
* |
58
|
|
|
* @type string |
59
|
|
|
*/ |
60
|
|
|
protected $name = 'gamespy3'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Longer string name of this protocol class |
64
|
|
|
* |
65
|
|
|
* @type string |
66
|
|
|
*/ |
67
|
|
|
protected $name_long = "GameSpy3 Server"; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The client join link |
71
|
|
|
* |
72
|
|
|
* @type string |
73
|
|
|
*/ |
74
|
|
|
protected $join_link = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This defines the split between the server info and player/team info. |
78
|
|
|
* This value can vary by game. This value is the default split. |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $packetSplit = "/\\x00\\x00\\x01/m"; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Parse the challenge response and apply it to all the packet types |
86
|
|
|
* |
87
|
|
|
* @param \GameQ\Buffer $challenge_buffer |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
* @throws \GameQ\Exception\Protocol |
91
|
|
|
*/ |
92
|
1 |
|
public function challengeParseAndApply(Buffer $challenge_buffer) |
93
|
|
|
{ |
94
|
|
|
// Pull out the challenge |
95
|
1 |
|
$challenge = substr(preg_replace("/[^0-9\-]/si", "", $challenge_buffer->getBuffer()), 1); |
96
|
|
|
|
97
|
|
|
// By default, no challenge result (see #197) |
98
|
1 |
|
$challenge_result = ''; |
99
|
|
|
|
100
|
|
|
// Check for valid challenge (see #197) |
101
|
1 |
|
if ($challenge) { |
102
|
|
|
// Encode chellenge result |
103
|
1 |
|
$challenge_result = sprintf( |
104
|
1 |
|
"%c%c%c%c", |
105
|
1 |
|
($challenge >> 24), |
106
|
1 |
|
($challenge >> 16), |
107
|
1 |
|
($challenge >> 8), |
108
|
1 |
|
($challenge >> 0) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Apply the challenge and return |
113
|
1 |
|
return $this->challengeApply($challenge_result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Process the response |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
20 |
|
public function processResponse() |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
// Holds the processed packets |
125
|
20 |
|
$processed = []; |
126
|
|
|
|
127
|
|
|
// Iterate over the packets |
128
|
20 |
|
foreach ($this->packets_response as $response) { |
129
|
|
|
// Make a buffer |
130
|
20 |
|
$buffer = new Buffer($response, Buffer::NUMBER_TYPE_BIGENDIAN); |
131
|
|
|
|
132
|
|
|
// Packet type = 0 |
133
|
20 |
|
$buffer->readInt8(); |
134
|
|
|
|
135
|
|
|
// Session Id |
136
|
20 |
|
$buffer->readInt32(); |
137
|
|
|
|
138
|
|
|
// We need to burn the splitnum\0 because it is not used |
139
|
20 |
|
$buffer->skip(9); |
140
|
|
|
|
141
|
|
|
// Get the id |
142
|
20 |
|
$id = $buffer->readInt8(); |
143
|
|
|
|
144
|
|
|
// Burn next byte not sure what it is used for |
145
|
20 |
|
$buffer->skip(1); |
146
|
|
|
|
147
|
|
|
// Add this packet to the processed |
148
|
20 |
|
$processed[$id] = $buffer->getBuffer(); |
149
|
|
|
|
150
|
20 |
|
unset($buffer, $id); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Sort packets, reset index |
154
|
20 |
|
ksort($processed); |
155
|
|
|
|
156
|
|
|
// Offload cleaning up the packets if they happen to be split |
157
|
20 |
|
$packets = $this->cleanPackets(array_values($processed)); |
158
|
|
|
|
159
|
|
|
// Split the packets by type general and the rest (i.e. players & teams) |
160
|
20 |
|
$split = preg_split($this->packetSplit, implode('', $packets)); |
161
|
|
|
|
162
|
|
|
// Create a new result |
163
|
20 |
|
$result = new Result(); |
164
|
|
|
|
165
|
|
|
// Assign variable due to pass by reference in PHP 7+ |
166
|
20 |
|
$buffer = new Buffer($split[0], Buffer::NUMBER_TYPE_BIGENDIAN); |
167
|
|
|
|
168
|
|
|
// First key should be server details and rules |
169
|
20 |
|
$this->processDetails($buffer, $result); |
170
|
|
|
|
171
|
|
|
// The rest should be the player and team information, if it exists |
172
|
20 |
|
if (array_key_exists(1, $split)) { |
173
|
20 |
|
$buffer = new Buffer($split[1], Buffer::NUMBER_TYPE_BIGENDIAN); |
174
|
20 |
|
$this->processPlayersAndTeams($buffer, $result); |
175
|
|
|
} |
176
|
|
|
|
177
|
20 |
|
unset($buffer); |
178
|
|
|
|
179
|
20 |
|
return $result->fetch(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/* |
183
|
|
|
* Internal methods |
184
|
|
|
*/ |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Handles cleaning up packets since the responses can be a bit "dirty" |
188
|
|
|
* |
189
|
|
|
* @param array $packets |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
20 |
|
protected function cleanPackets(array $packets = []) |
194
|
|
|
{ |
195
|
|
|
|
196
|
|
|
// Get the number of packets |
197
|
20 |
|
$packetCount = count($packets); |
198
|
|
|
|
199
|
|
|
// Compare last var of current packet with first var of next packet |
200
|
|
|
// On a partial match, remove last var from current packet, |
201
|
|
|
// variable header from next packet |
202
|
20 |
|
for ($i = 0, $x = $packetCount; $i < $x - 1; $i++) { |
203
|
|
|
// First packet |
204
|
5 |
|
$fst = substr($packets[$i], 0, -1); |
205
|
|
|
// Second packet |
206
|
5 |
|
$snd = $packets[$i + 1]; |
207
|
|
|
// Get last variable from first packet |
208
|
5 |
|
$fstvar = substr($fst, strrpos($fst, "\x00") + 1); |
209
|
|
|
// Get first variable from last packet |
210
|
5 |
|
$snd = substr($snd, strpos($snd, "\x00") + 2); |
211
|
5 |
|
$sndvar = substr($snd, 0, strpos($snd, "\x00")); |
212
|
|
|
// Check if fstvar is a substring of sndvar |
213
|
|
|
// If so, remove it from the first string |
214
|
5 |
|
if (!empty($fstvar) && strpos($sndvar, $fstvar) !== false) { |
215
|
3 |
|
$packets[$i] = preg_replace("#(\\x00[^\\x00]+\\x00)$#", "\x00", $packets[$i]); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Now let's loop the return and remove any dupe prefixes |
220
|
20 |
|
for ($x = 1; $x < $packetCount; $x++) { |
221
|
5 |
|
$buffer = new Buffer($packets[$x], Buffer::NUMBER_TYPE_BIGENDIAN); |
222
|
|
|
|
223
|
5 |
|
$prefix = $buffer->readString(); |
224
|
|
|
|
225
|
|
|
// Check to see if the return before has the same prefix present |
226
|
5 |
|
if ($prefix != null && strstr($packets[($x - 1)], $prefix)) { |
227
|
|
|
// Update the return by removing the prefix plus 2 chars |
228
|
3 |
|
$packets[$x] = substr(str_replace($prefix, '', $packets[$x]), 2); |
229
|
|
|
} |
230
|
|
|
|
231
|
5 |
|
unset($buffer); |
232
|
|
|
} |
233
|
|
|
|
234
|
20 |
|
unset($x, $i, $snd, $sndvar, $fst, $fstvar); |
235
|
|
|
|
236
|
|
|
// Return cleaned packets |
237
|
20 |
|
return $packets; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Handles processing the details data into a usable format |
242
|
|
|
* |
243
|
|
|
* @param \GameQ\Buffer $buffer |
244
|
|
|
* @param \GameQ\Result $result |
245
|
|
|
*/ |
246
|
20 |
|
protected function processDetails(Buffer &$buffer, Result &$result) |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
// We go until we hit an empty key |
250
|
20 |
|
while ($buffer->getLength()) { |
251
|
20 |
|
$key = $buffer->readString(); |
252
|
20 |
|
if (strlen($key) == 0) { |
253
|
|
|
break; |
254
|
|
|
} |
255
|
20 |
|
$result->add($key, utf8_encode($buffer->readString())); |
256
|
|
|
} |
257
|
20 |
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Handles processing the player and team data into a usable format |
261
|
|
|
* |
262
|
|
|
* @param \GameQ\Buffer $buffer |
263
|
|
|
* @param \GameQ\Result $result |
264
|
|
|
*/ |
265
|
17 |
|
protected function processPlayersAndTeams(Buffer &$buffer, Result &$result) |
266
|
|
|
{ |
267
|
|
|
|
268
|
|
|
/* |
269
|
|
|
* Explode the data into groups. First is player, next is team (item_t) |
270
|
|
|
* Each group should be as follows: |
271
|
|
|
* |
272
|
|
|
* [0] => item_ |
273
|
|
|
* [1] => information for item_ |
274
|
|
|
* ... |
275
|
|
|
*/ |
276
|
17 |
|
$data = explode("\x00\x00", $buffer->getBuffer()); |
277
|
|
|
|
278
|
|
|
// By default item_group is blank, this will be set for each loop thru the data |
279
|
17 |
|
$item_group = ''; |
280
|
|
|
|
281
|
|
|
// By default the item_type is blank, this will be set on each loop |
282
|
17 |
|
$item_type = ''; |
283
|
|
|
|
284
|
|
|
// Save count as variable |
285
|
17 |
|
$count = count($data); |
286
|
|
|
|
287
|
|
|
// Loop through all of the $data for information and pull it out into the result |
288
|
17 |
|
for ($x = 0; $x < $count - 1; $x++) { |
289
|
|
|
// Pull out the item |
290
|
15 |
|
$item = $data[$x]; |
291
|
|
|
// If this is an empty item, move on |
292
|
15 |
|
if ($item == '' || $item == "\x00") { |
293
|
2 |
|
continue; |
294
|
|
|
} |
295
|
|
|
/* |
296
|
|
|
* Left as reference: |
297
|
|
|
* |
298
|
|
|
* Each block of player_ and team_t have preceding junk chars |
299
|
|
|
* |
300
|
|
|
* player_ is actually \x01player_ |
301
|
|
|
* team_t is actually \x00\x02team_t |
302
|
|
|
* |
303
|
|
|
* Probably a by-product of the change to exploding the data from the original. |
304
|
|
|
* |
305
|
|
|
* For now we just strip out these characters |
306
|
|
|
*/ |
307
|
|
|
// Check to see if $item has a _ at the end, this is player info |
308
|
15 |
|
if (substr($item, -1) == '_') { |
309
|
|
|
// Set the item group |
310
|
15 |
|
$item_group = 'players'; |
311
|
|
|
// Set the item type, rip off any trailing stuff and bad chars |
312
|
15 |
|
$item_type = rtrim(str_replace("\x01", '', $item), '_'); |
313
|
14 |
|
} elseif (substr($item, -2) == '_t') { |
314
|
|
|
// Check to see if $item has a _t at the end, this is team info |
315
|
|
|
// Set the item group |
316
|
8 |
|
$item_group = 'teams'; |
317
|
|
|
// Set the item type, rip off any trailing stuff and bad chars |
318
|
8 |
|
$item_type = rtrim(str_replace(["\x00", "\x02"], '', $item), '_t'); |
319
|
|
|
} else { |
320
|
|
|
// We can assume it is data belonging to a previously defined item |
321
|
|
|
|
322
|
|
|
// Make a temp buffer so we have easier access to the data |
323
|
14 |
|
$buf_temp = new Buffer($item, Buffer::NUMBER_TYPE_BIGENDIAN); |
324
|
|
|
// Get the values |
325
|
14 |
|
while ($buf_temp->getLength()) { |
326
|
|
|
// No value so break the loop, end of string |
327
|
14 |
|
if (($val = $buf_temp->readString()) === '') { |
328
|
|
|
break; |
329
|
|
|
} |
330
|
|
|
// Add the value to the proper item in the correct group |
331
|
14 |
|
$result->addSub($item_group, $item_type, utf8_encode(trim($val))); |
332
|
|
|
} |
333
|
|
|
// Unset our buffer |
334
|
14 |
|
unset($buf_temp); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
// Free up some memory |
338
|
17 |
|
unset($count, $data, $item, $item_group, $item_type, $val); |
339
|
17 |
|
} |
340
|
|
|
} |
341
|
|
|
|