1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ; |
20: | |
21: | use GameQ\Exception\Protocol as ProtocolException; |
22: | use GameQ\Exception\Query as QueryException; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | class GameQ |
43: | { |
44: | |
45: | const PROTOCOLS_DIRECTORY = __DIR__ . '/Protocols'; |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | protected static $instance = null; |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | public static function factory() |
62: | { |
63: | |
64: | self::$instance = new self(); |
65: | |
66: | |
67: | return self::$instance; |
68: | } |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | protected $options = [ |
78: | 'debug' => false, |
79: | 'timeout' => 3, |
80: | 'filters' => [ |
81: | |
82: | 'normalize_d751713988987e9331980363e24189ce' => [ |
83: | 'filter' => 'normalize', |
84: | 'options' => [], |
85: | ], |
86: | ], |
87: | |
88: | 'stream_timeout' => 200000, |
89: | 'write_wait' => 500, |
90: | |
91: | |
92: | |
93: | 'capture_packets_file' => null, |
94: | ]; |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | protected $servers = []; |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | protected $queryLibrary = 'GameQ\\Query\\Native'; |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | protected $query = null; |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function __get($option) |
125: | { |
126: | return isset($this->options[$option]) ? $this->options[$option] : null; |
127: | } |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | public function __set($option, $value) |
138: | { |
139: | $this->options[$option] = $value; |
140: | |
141: | return true; |
142: | } |
143: | |
144: | public function getServers() |
145: | { |
146: | return $this->servers; |
147: | } |
148: | |
149: | public function getOptions() |
150: | { |
151: | return $this->options; |
152: | } |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | public function setOption($var, $value) |
163: | { |
164: | |
165: | $this->{$var} = $value; |
166: | |
167: | return $this; |
168: | } |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | public function addServer(array $server_info = []) |
178: | { |
179: | |
180: | $this->servers[uniqid()] = new Server($server_info); |
181: | |
182: | return $this; |
183: | } |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | public function addServers(array $servers = []) |
193: | { |
194: | |
195: | foreach ($servers as $server_info) { |
196: | $this->addServer($server_info); |
197: | } |
198: | |
199: | return $this; |
200: | } |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | public function addServersFromFiles($files = []) |
213: | { |
214: | |
215: | if (!is_array($files)) { |
216: | $files = [$files]; |
217: | } |
218: | |
219: | |
220: | foreach ($files as $file) { |
221: | |
222: | if (!file_exists($file) || !is_readable($file)) { |
223: | continue; |
224: | } |
225: | |
226: | |
227: | if (($servers = json_decode(file_get_contents($file), true)) === null |
228: | && json_last_error() !== JSON_ERROR_NONE |
229: | ) { |
230: | |
231: | continue; |
232: | } |
233: | |
234: | |
235: | $this->addServers($servers); |
236: | } |
237: | |
238: | return $this; |
239: | } |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | public function clearServers() |
247: | { |
248: | |
249: | $this->servers = []; |
250: | |
251: | return $this; |
252: | } |
253: | |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | public function addFilter($filterName, $options = []) |
263: | { |
264: | |
265: | $filterHash = sprintf('%s_%s', strtolower($filterName), md5(json_encode($options))); |
266: | |
267: | |
268: | $this->options['filters'][$filterHash] = [ |
269: | 'filter' => strtolower($filterName), |
270: | 'options' => $options, |
271: | ]; |
272: | |
273: | unset($filterHash); |
274: | |
275: | return $this; |
276: | } |
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | public function removeFilter($filterHash) |
286: | { |
287: | |
288: | $filterHash = strtolower($filterHash); |
289: | |
290: | |
291: | if (array_key_exists($filterHash, $this->options['filters'])) { |
292: | unset($this->options['filters'][$filterHash]); |
293: | } |
294: | |
295: | unset($filterHash); |
296: | |
297: | return $this; |
298: | } |
299: | |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | public function listFilters() |
306: | { |
307: | return $this->options['filters']; |
308: | } |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | |
316: | public function process() |
317: | { |
318: | |
319: | $class = new \ReflectionClass($this->queryLibrary); |
320: | |
321: | |
322: | $this->query = $class->newInstance(); |
323: | |
324: | unset($class); |
325: | |
326: | |
327: | $results = []; |
328: | |
329: | |
330: | |
331: | |
332: | $this->doChallenges(); |
333: | |
334: | |
335: | $this->doQueries(); |
336: | |
337: | |
338: | foreach ($this->servers as $server) { |
339: | |
340: | |
341: | |
342: | $result = $this->doParseResponse($server); |
343: | |
344: | |
345: | $result = array_merge($result, $this->doApplyFilters($result, $server)); |
346: | |
347: | |
348: | ksort($result); |
349: | |
350: | |
351: | $results[$server->id()] = $result; |
352: | } |
353: | |
354: | return $results; |
355: | } |
356: | |
357: | |
358: | |
359: | |
360: | protected function doChallenges() |
361: | { |
362: | |
363: | $sockets = []; |
364: | |
365: | |
366: | $server_challenge = false; |
367: | |
368: | |
369: | foreach ($this->servers as $server_id => $server) { |
370: | |
371: | |
372: | |
373: | if ($server->protocol()->hasChallenge()) { |
374: | |
375: | $server_challenge = true; |
376: | |
377: | |
378: | $socket = clone $this->query; |
379: | |
380: | |
381: | $socket->set( |
382: | $server->protocol()->transport(), |
383: | $server->ip, |
384: | $server->port_query, |
385: | $this->timeout |
386: | ); |
387: | |
388: | try { |
389: | |
390: | $socket->write($server->protocol()->getPacket(Protocol::PACKET_CHALLENGE)); |
391: | |
392: | |
393: | $sockets[(int)$socket->get()] = [ |
394: | 'server_id' => $server_id, |
395: | 'socket' => $socket, |
396: | ]; |
397: | } catch (QueryException $exception) { |
398: | |
399: | if ($this->debug) { |
400: | throw new \Exception($exception->getMessage(), $exception->getCode(), $exception); |
401: | } |
402: | } |
403: | |
404: | unset($socket); |
405: | |
406: | |
407: | usleep($this->write_wait); |
408: | } |
409: | } |
410: | |
411: | |
412: | if ($server_challenge) { |
413: | |
414: | $responses = call_user_func_array( |
415: | [$this->query, 'getResponses'], |
416: | [$sockets, $this->timeout, $this->stream_timeout] |
417: | ); |
418: | |
419: | |
420: | foreach ($responses as $socket_id => $response) { |
421: | |
422: | $server_id = $sockets[$socket_id]['server_id']; |
423: | |
424: | |
425: | $challenge = new Buffer(implode('', $response)); |
426: | |
427: | |
428: | |
429: | $server = $this->servers[$server_id]; |
430: | |
431: | |
432: | $server->protocol()->challengeParseAndApply($challenge); |
433: | |
434: | |
435: | $server->socketAdd($sockets[$socket_id]['socket']); |
436: | |
437: | |
438: | unset($server); |
439: | } |
440: | } |
441: | } |
442: | |
443: | |
444: | |
445: | |
446: | protected function doQueries() |
447: | { |
448: | |
449: | $sockets = []; |
450: | |
451: | |
452: | foreach ($this->servers as $server_id => $server) { |
453: | |
454: | |
455: | |
456: | $server->protocol()->beforeSend($server); |
457: | |
458: | |
459: | $packets = $server->protocol()->getPacket('!' . Protocol::PACKET_CHALLENGE); |
460: | |
461: | if (count($packets) == 0) { |
462: | |
463: | continue; |
464: | } |
465: | |
466: | |
467: | if (($socket = $server->socketGet()) === null) { |
468: | |
469: | $socket = clone $this->query; |
470: | |
471: | |
472: | $socket->set( |
473: | $server->protocol()->transport(), |
474: | $server->ip, |
475: | $server->port_query, |
476: | $this->timeout |
477: | ); |
478: | } |
479: | |
480: | try { |
481: | |
482: | foreach ($packets as $packet_data) { |
483: | |
484: | $socket->write($packet_data); |
485: | |
486: | |
487: | usleep($this->write_wait); |
488: | } |
489: | |
490: | unset($packets); |
491: | |
492: | |
493: | $sockets[(int)$socket->get()] = [ |
494: | 'server_id' => $server_id, |
495: | 'socket' => $socket, |
496: | ]; |
497: | } catch (QueryException $exception) { |
498: | |
499: | if ($this->debug) { |
500: | throw new \Exception($exception->getMessage(), $exception->getCode(), $exception); |
501: | } |
502: | |
503: | continue; |
504: | } |
505: | |
506: | |
507: | $server->socketCleanse(); |
508: | } |
509: | |
510: | |
511: | $responses = call_user_func_array( |
512: | [$this->query, 'getResponses'], |
513: | [$sockets, $this->timeout, $this->stream_timeout] |
514: | ); |
515: | |
516: | |
517: | foreach ($responses as $socket_id => $response) { |
518: | |
519: | $server_id = $sockets[$socket_id]['server_id']; |
520: | |
521: | |
522: | |
523: | $server = $this->servers[$server_id]; |
524: | |
525: | |
526: | $server->protocol()->packetResponse($response); |
527: | |
528: | unset($server); |
529: | } |
530: | |
531: | |
532: | foreach ($sockets as $socketInfo) { |
533: | |
534: | $socket = $socketInfo['socket']; |
535: | |
536: | |
537: | $socket->close(); |
538: | |
539: | unset($socket); |
540: | } |
541: | |
542: | unset($sockets); |
543: | } |
544: | |
545: | |
546: | |
547: | |
548: | |
549: | |
550: | |
551: | |
552: | |
553: | protected function doParseResponse(Server $server) |
554: | { |
555: | try { |
556: | |
557: | |
558: | if (!is_null($this->capture_packets_file)) { |
559: | file_put_contents( |
560: | $this->capture_packets_file, |
561: | implode(PHP_EOL . '||' . PHP_EOL, $server->protocol()->packetResponse()) |
562: | ); |
563: | } |
564: | |
565: | |
566: | |
567: | $results = $server->protocol()->processResponse(); |
568: | |
569: | |
570: | $results['gq_online'] = (count($results) > 0); |
571: | } catch (ProtocolException $e) { |
572: | |
573: | if ($this->debug) { |
574: | throw new \Exception($e->getMessage(), $e->getCode(), $e); |
575: | } |
576: | |
577: | |
578: | $results = [ |
579: | 'gq_online' => false, |
580: | ]; |
581: | } |
582: | |
583: | |
584: | $results['gq_address'] = (isset($results['gq_address'])) ? $results['gq_address'] : $server->ip(); |
585: | $results['gq_port_client'] = $server->portClient(); |
586: | $results['gq_port_query'] = (isset($results['gq_port_query'])) ? $results['gq_port_query'] : $server->portQuery(); |
587: | $results['gq_protocol'] = $server->protocol()->getProtocol(); |
588: | $results['gq_type'] = (string)$server->protocol(); |
589: | $results['gq_name'] = $server->protocol()->nameLong(); |
590: | $results['gq_transport'] = $server->protocol()->transport(); |
591: | |
592: | |
593: | if (!isset($results['gq_joinlink']) || empty($results['gq_joinlink'])) { |
594: | $results['gq_joinlink'] = $server->getJoinLink(); |
595: | } |
596: | |
597: | return $results; |
598: | } |
599: | |
600: | |
601: | |
602: | |
603: | |
604: | |
605: | |
606: | |
607: | |
608: | protected function doApplyFilters(array $results, Server $server) |
609: | { |
610: | |
611: | foreach ($this->options['filters'] as $filterOptions) { |
612: | |
613: | try { |
614: | |
615: | $class = new \ReflectionClass(sprintf('GameQ\\Filters\\%s', ucfirst($filterOptions['filter']))); |
616: | |
617: | |
618: | $filter = $class->newInstanceArgs([$filterOptions['options']]); |
619: | |
620: | |
621: | $results = $filter->apply($results, $server); |
622: | } catch (\ReflectionException $exception) { |
623: | |
624: | continue; |
625: | } |
626: | } |
627: | |
628: | return $results; |
629: | } |
630: | } |
631: | |