1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ\Protocols; |
20: | |
21: | use GameQ\Buffer; |
22: | use GameQ\Exception\Protocol as Exception; |
23: | use GameQ\Protocol; |
24: | use GameQ\Result; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | class Source extends Protocol |
37: | { |
38: | |
39: | |
40: | |
41: | |
42: | const SOURCE_ENGINE = 0, |
43: | GOLDSOURCE_ENGINE = 1; |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
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%s", |
54: | self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s", |
55: | self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s", |
56: | ]; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | protected $responses = [ |
64: | "\x49" => "processDetails", |
65: | "\x6d" => "processDetailsGoldSource", |
66: | "\x44" => "processPlayers", |
67: | "\x45" => "processRules", |
68: | ]; |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | protected $protocol = 'source'; |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | protected $name = 'source'; |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | protected $name_long = "Source Server"; |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | protected $source_engine = self::SOURCE_ENGINE; |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | protected $join_link = "steam://connect/%s:%d/"; |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | protected $normalize = [ |
111: | |
112: | 'general' => [ |
113: | |
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: | |
124: | 'player' => [ |
125: | 'name' => 'name', |
126: | 'score' => 'score', |
127: | 'time' => 'time', |
128: | ], |
129: | ]; |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | public function challengeParseAndApply(Buffer $challenge_buffer) |
140: | { |
141: | |
142: | |
143: | $challenge_buffer->skip(5); |
144: | |
145: | |
146: | return $this->challengeApply($challenge_buffer->read(4)); |
147: | } |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | public function processResponse() |
156: | { |
157: | |
158: | $results = []; |
159: | |
160: | |
161: | $packets = []; |
162: | |
163: | |
164: | foreach ($this->packets_response as $response) { |
165: | $buffer = new Buffer($response); |
166: | |
167: | |
168: | $header = $buffer->readInt32Signed(); |
169: | |
170: | |
171: | if ($header == -1) { |
172: | |
173: | if ($buffer->lookAhead(1) == "\x6d") { |
174: | $this->source_engine = self::GOLDSOURCE_ENGINE; |
175: | } |
176: | |
177: | $packets[] = $buffer->getBuffer(); |
178: | continue; |
179: | } else { |
180: | |
181: | |
182: | |
183: | $packet_id = $buffer->readInt32Signed() + 10; |
184: | |
185: | |
186: | $packets[$packet_id][] = $buffer->getBuffer(); |
187: | } |
188: | } |
189: | |
190: | |
191: | unset($response, $packet_id, $buffer, $header); |
192: | |
193: | |
194: | foreach ($packets as $packet_id => $packet) { |
195: | |
196: | if (is_array($packet)) { |
197: | $buffer = new Buffer($this->processPackets($packet_id, $packet)); |
198: | } else { |
199: | $buffer = new Buffer($packet); |
200: | } |
201: | |
202: | |
203: | $response_type = $buffer->read(1); |
204: | |
205: | |
206: | if (!array_key_exists($response_type, $this->responses)) { |
207: | throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
208: | } |
209: | |
210: | |
211: | $results = array_merge( |
212: | $results, |
213: | call_user_func_array([$this, $this->responses[$response_type]], [$buffer]) |
214: | ); |
215: | |
216: | unset($buffer); |
217: | } |
218: | |
219: | |
220: | unset($packets, $packet, $packet_id, $response_type); |
221: | |
222: | return $results; |
223: | } |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | protected function processPackets($packet_id, array $packets = []) |
241: | { |
242: | |
243: | |
244: | $packs = []; |
245: | |
246: | |
247: | foreach ($packets as $i => $packet) { |
248: | |
249: | $buffer = new Buffer($packet); |
250: | |
251: | |
252: | if ($this->source_engine == self::GOLDSOURCE_ENGINE) { |
253: | |
254: | $packet_number = $buffer->readInt8(); |
255: | |
256: | |
257: | if ($i == 0) { |
258: | $buffer->read(4); |
259: | } |
260: | |
261: | |
262: | $packs[$packet_number] = $buffer->getBuffer(); |
263: | } else { |
264: | |
265: | $buffer->readInt8(); |
266: | |
267: | |
268: | $packet_number = $buffer->readInt8(); |
269: | |
270: | |
271: | |
272: | if ($packet_id & 0x80000000) { |
273: | |
274: | if (!function_exists('bzdecompress')) { |
275: | |
276: | throw new Exception( |
277: | 'Bzip2 is not installed. See http://www.php.net/manual/en/book.bzip2.php for more info.', |
278: | 0 |
279: | ); |
280: | |
281: | } |
282: | |
283: | |
284: | $packet_length = $buffer->readInt32Signed(); |
285: | |
286: | |
287: | $buffer->readInt32Signed(); |
288: | |
289: | |
290: | $result = bzdecompress($buffer->getBuffer()); |
291: | |
292: | |
293: | if (strlen($result) != $packet_length) { |
294: | |
295: | throw new Exception( |
296: | "Checksum for compressed packet failed! Length expected: {$packet_length}, length |
297: | returned: " . strlen($result) |
298: | ); |
299: | |
300: | } |
301: | |
302: | |
303: | if ($i == 0) { |
304: | $result = substr($result, 4); |
305: | } |
306: | } else { |
307: | |
308: | $buffer->readInt16Signed(); |
309: | |
310: | |
311: | if ($i == 0) { |
312: | $buffer->read(4); |
313: | } |
314: | |
315: | |
316: | $result = $buffer->getBuffer(); |
317: | } |
318: | |
319: | |
320: | $packs[$packet_number] = $result; |
321: | } |
322: | |
323: | unset($buffer); |
324: | } |
325: | |
326: | |
327: | unset($packets, $packet); |
328: | |
329: | |
330: | ksort($packs); |
331: | |
332: | |
333: | return implode("", $packs); |
334: | } |
335: | |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | protected function processDetails(Buffer $buffer) |
345: | { |
346: | |
347: | |
348: | $result = new Result(); |
349: | |
350: | $result->add('protocol', $buffer->readInt8()); |
351: | $result->add('hostname', $buffer->readString()); |
352: | $result->add('map', $buffer->readString()); |
353: | $result->add('game_dir', $buffer->readString()); |
354: | $result->add('game_descr', $buffer->readString()); |
355: | $result->add('steamappid', $buffer->readInt16()); |
356: | $result->add('num_players', $buffer->readInt8()); |
357: | $result->add('max_players', $buffer->readInt8()); |
358: | $result->add('num_bots', $buffer->readInt8()); |
359: | $result->add('dedicated', $buffer->read()); |
360: | $result->add('os', $buffer->read()); |
361: | $result->add('password', $buffer->readInt8()); |
362: | $result->add('secure', $buffer->readInt8()); |
363: | |
364: | |
365: | if ($result->get('steamappid') == 2400) { |
366: | $result->add('game_mode', $buffer->readInt8()); |
367: | $result->add('witness_count', $buffer->readInt8()); |
368: | $result->add('witness_time', $buffer->readInt8()); |
369: | } |
370: | |
371: | $result->add('version', $buffer->readString()); |
372: | |
373: | |
374: | $edfCheck = $buffer->lookAhead(1); |
375: | |
376: | |
377: | if (!empty($edfCheck)) { |
378: | $edf = $buffer->readInt8(); |
379: | |
380: | if ($edf & 0x80) { |
381: | $result->add('port', $buffer->readInt16Signed()); |
382: | } |
383: | |
384: | if ($edf & 0x10) { |
385: | $result->add('steam_id', $buffer->readInt64()); |
386: | } |
387: | |
388: | if ($edf & 0x40) { |
389: | $result->add('sourcetv_port', $buffer->readInt16Signed()); |
390: | $result->add('sourcetv_name', $buffer->readString()); |
391: | } |
392: | |
393: | if ($edf & 0x20) { |
394: | $result->add('keywords', $buffer->readString()); |
395: | } |
396: | |
397: | if ($edf & 0x01) { |
398: | $result->add('game_id', $buffer->readInt64()); |
399: | } |
400: | |
401: | unset($edf); |
402: | } |
403: | |
404: | unset($buffer); |
405: | |
406: | return $result->fetch(); |
407: | } |
408: | |
409: | |
410: | |
411: | |
412: | |
413: | |
414: | |
415: | |
416: | |
417: | protected function processDetailsGoldSource(Buffer $buffer) |
418: | { |
419: | |
420: | |
421: | $result = new Result(); |
422: | |
423: | $result->add('address', $buffer->readString()); |
424: | $result->add('hostname', $buffer->readString()); |
425: | $result->add('map', $buffer->readString()); |
426: | $result->add('game_dir', $buffer->readString()); |
427: | $result->add('game_descr', $buffer->readString()); |
428: | $result->add('num_players', $buffer->readInt8()); |
429: | $result->add('max_players', $buffer->readInt8()); |
430: | $result->add('version', $buffer->readInt8()); |
431: | $result->add('dedicated', $buffer->read()); |
432: | $result->add('os', $buffer->read()); |
433: | $result->add('password', $buffer->readInt8()); |
434: | |
435: | |
436: | $result->add('ismod', $buffer->readInt8()); |
437: | |
438: | |
439: | if ($result->get('ismod') == 1) { |
440: | $result->add('mod_urlinfo', $buffer->readString()); |
441: | $result->add('mod_urldl', $buffer->readString()); |
442: | $buffer->skip(); |
443: | $result->add('mod_version', $buffer->readInt32Signed()); |
444: | $result->add('mod_size', $buffer->readInt32Signed()); |
445: | $result->add('mod_type', $buffer->readInt8()); |
446: | $result->add('mod_cldll', $buffer->readInt8()); |
447: | } |
448: | |
449: | $result->add('secure', $buffer->readInt8()); |
450: | $result->add('num_bots', $buffer->readInt8()); |
451: | |
452: | unset($buffer); |
453: | |
454: | return $result->fetch(); |
455: | } |
456: | |
457: | |
458: | |
459: | |
460: | |
461: | |
462: | |
463: | |
464: | protected function processPlayers(Buffer $buffer) |
465: | { |
466: | |
467: | |
468: | $result = new Result(); |
469: | |
470: | |
471: | $num_players = $buffer->readInt8(); |
472: | |
473: | |
474: | $result->add('num_players', $num_players); |
475: | |
476: | |
477: | if ($num_players == 0) { |
478: | return $result->fetch(); |
479: | } |
480: | |
481: | |
482: | while ($buffer->getLength()) { |
483: | $result->addPlayer('id', $buffer->readInt8()); |
484: | $result->addPlayer('name', $buffer->readString()); |
485: | $result->addPlayer('score', $buffer->readInt32Signed()); |
486: | $result->addPlayer('time', $buffer->readFloat32()); |
487: | } |
488: | |
489: | unset($buffer); |
490: | |
491: | return $result->fetch(); |
492: | } |
493: | |
494: | |
495: | |
496: | |
497: | |
498: | |
499: | |
500: | |
501: | protected function processRules(Buffer $buffer) |
502: | { |
503: | |
504: | |
505: | $result = new Result(); |
506: | |
507: | |
508: | $num_rules = $buffer->readInt16Signed(); |
509: | |
510: | |
511: | $result->add('num_rules', $num_rules); |
512: | |
513: | |
514: | while ($buffer->getLength()) { |
515: | $result->add($buffer->readString(), $buffer->readString()); |
516: | } |
517: | |
518: | unset($buffer); |
519: | |
520: | return $result->fetch(); |
521: | } |
522: | } |
523: | |