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