forked from Austinb/GameQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameQ.php
796 lines (677 loc) · 19 KB
/
GameQ.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Init some stuff
*/
// Figure out where we are so we can set the proper references
define('GAMEQ_BASE', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
// Define the autoload so we can require files easy
spl_autoload_register(array('GameQ', 'auto_load'));
/**
* Base GameQ Class
*
* This class should be the only one that is included when you use GameQ to query
* any games servers. All necessary sub-classes are loaded as needed.
*
* Requirements:
* - PHP 5.3+
* * Bzip2 - http://www.php.net/manual/en/book.bzip2.php
* * Zlib - http://www.php.net/manual/en/book.zlib.php
*
* @author Austin Bischoff <[email protected]>
*/
class GameQ
{
/*
* Constants
*/
const VERSION = '2.0.0';
/*
* Server array keys
*/
const SERVER_TYPE = 'type';
const SERVER_HOST = 'host';
const SERVER_ID = 'id';
const SERVER_OPTIONS = 'options';
/* Static Section */
protected static $instance = NULL;
/**
* Create a new instance of this class
*/
public static function factory()
{
// Create a new instance
self::$instance = new self();
// Return this new instance
return self::$instance;
}
/**
* Attempt to auto-load a class based on the name
*
* @param string $class
* @throws GameQException
*/
public static function auto_load($class)
{
try
{
// Transform the class name into a path
$file = str_replace('_', '/', strtolower($class));
if ($path = self::find_file($file))
{
// Load the class file
require $path;
// Class has been found
return TRUE;
}
// Class is not in the filesystem
return FALSE;
}
catch (Exception $e)
{
throw new GameQException($e->getMessage(), $e->getMessage(), $e);
die;
}
}
/**
* Try to find the file based on the class passed.
*
* @param string $file
*/
public static function find_file($file)
{
$found = FALSE; // By default we did not find anything
// Create a partial path of the filename
$path = GAMEQ_BASE.$file.'.php';
if(is_file($path))
{
$found = $path;
}
return $found;
}
/* Dynamic Section */
/*
* Defined properties
*/
/**
* Defined options by default
*
* @var array
*/
protected $options = array(
'debug' => FALSE,
'raw' => FALSE,
'timeout' => 3, // Seconds
);
/**
* Array of servers being queried
*
* @var array
*/
protected $servers = array();
/**
* Holds the list of active sockets. This array is automaically cleaned as needed
*
* @var array
*/
protected $sockets = array();
/**
* Holds the list of filters that need to be applied to the results
*
* @var array
*/
protected $filters = array();
/**
* Make new class and check for requirements
*
* @throws GameQException
* @return boolean
*/
public function __construct()
{
// @todo: Add PHP version check?
// Check for Bzip2
if(!function_exists('bzdecompress'))
{
throw new GameQException('Bzip2 is not installed. See http://www.php.net/manual/en/book.bzip2.php for more info.', 0);
return FALSE;
}
// Check for Zlib
if(!function_exists('gzuncompress'))
{
throw new GameQException('Zlib is not installed. See http://www.php.net/manual/en/book.zlib.php for more info.', 0);
return FALSE;
}
}
/**
* Set an option.
*
* @param string $var Option name
* @param mixed $value Option value
* @return GameQ
*/
public function setOption($var, $value)
{
$this->options[$var] = $value;
return $this; // Make chainable
}
/**
* Return the value for a specific option.
*
* @param string $var
* @return Mixed <NULL, multitype:>
*/
public function getOption($var)
{
return isset($this->options[$var]) ? $this->options[$var] : null;
}
/**
* Set an output filter.
*
* @param string $name
* @param array $params
* @return GameQ
*/
public function setFilter($name, $params = array())
{
$filter_class = 'GameQ_Filters_'.$name;
// Pass any parameters
$this->filters[$name] = new $filter_class($params);
return $this; // Make chainable
}
/**
* Remove an output filter.
*
* @param string $name
* @return GameQ
*/
public function removeFilter($name)
{
unset($this->filters[$name]);
return $this; // Make chainable
}
/**
* Add a server to be queried
*
* Example:
* $this->addServer(array(
* // Required keys
* 'type' => 'cs',
* 'host' => '127.0.0.1:27015', or 'somehost.com:27015' Port also not required
*
* // Optional keys
* 'id' => 'someServerId', // By default will use pased host info
* 'options' => array('timeout' => 5), // By default will use global options
* ));
*
* @param array $server_info
* @throws GameQException
* @return boolean|GameQ
*/
public function addServer(Array $server_info=NULL)
{
// Check for server type
if(!key_exists(self::SERVER_TYPE, $server_info) || empty($server_info[self::SERVER_TYPE]))
{
throw new GameQException("Missing server info key '".self::SERVER_TYPE."'");
return false;
}
// Check for server host
if(!key_exists(self::SERVER_HOST, $server_info) || empty($server_info[self::SERVER_HOST]))
{
throw new GameQException("Missing server info key '".self::SERVER_HOST."'");
return false;
}
// Check for server id
if(!key_exists(self::SERVER_ID, $server_info) || empty($server_info[self::SERVER_ID]))
{
// Make an id so each server has an id when returned
$server_info[self::SERVER_ID] = $server_info[self::SERVER_HOST];
}
// Check for options
if(!key_exists(self::SERVER_OPTIONS, $server_info)
|| !is_array($server_info[self::SERVER_OPTIONS])
|| empty($server_info[self::SERVER_OPTIONS]))
{
// Make an id so each server has an id when returned
$server_info[self::SERVER_OPTIONS] = array();
}
// Define these
$server_id = $server_info[self::SERVER_ID];
$server_addr = '127.0.0.1';
$server_port = FALSE;
// Pull out the information
if(strstr($server_info[self::SERVER_HOST], ':')) // We have a port defined
{
list($server_addr, $server_port) = explode(':', $server_info[self::SERVER_HOST]);
}
else // No port
{
$server_addr = $server_info[self::SERVER_HOST];
}
// Set the ip to the address by default
$server_ip = $server_addr;
// Now lets validate the server address
if(!filter_var($server_addr, FILTER_VALIDATE_IP, array(
'flags' => FILTER_FLAG_NO_PRIV_RANGE,
))) // Is not valid ip so assume hostname
{
// Try to resolve to ipv4 address, slow
$server_ip = gethostbyname($server_addr);
// When gethostbyname fails it returns the original string
// so if ip and address are equal this failed.
if($server_ip === $server_addr)
{
throw new GameQException("Unable to lookup ip for hostname '{$server_addr}'");
return false;
}
}
// Create the class so we can reference it properly
$protocol_class = 'GameQ_Protocols_'.ucfirst($server_info[self::SERVER_TYPE]);
// Create the new instance and add it to the servers list
$this->servers[$server_id] = new $protocol_class(
$server_ip,
$server_port,
array_merge($this->options, $server_info[self::SERVER_OPTIONS])
);
return $this; // Make calls chaninable
}
/**
* Add multiple servers at once
*
* @param array $servers
* @return GameQ
*/
public function addServers(Array $servers=NULL)
{
// Loop thru all the servers and add them
foreach($servers AS $server_info)
{
$this->addServer($server_info);
}
return $this; // Make calls chaninable
}
/**
* Clear all the added servers. Creates clean instance.
*
* @return GameQ
*/
public function clearServers()
{
// Reset all the servers
$this->servers = array();
$this->sockets = array();
return $this; // Make Chainable
}
/**
* Make all the data requests (i.e. challenges, queries, etc...)
*
* @return multitype:multitype:
*/
public function requestData()
{
// Data returned array
$data = array();
// Init the query array
$queries = array(
'multi' => array(
'challenges' => array(),
'info' => array(),
),
'linear' => array(),
);
// Loop thru all of the servers added and ceatgorize them
foreach($this->servers AS $server_id => $instance)
{
// Check to see what kind of server this is and if we have a specific challenge type
if($instance->packet_mode() == $instance::PACKET_MODE_LINEAR)
{
$queries['linear'][$server_id] = $instance;
}
else // We can send this out in a multi request
{
// Check to make sure we should issue a challenge
if($instance->hasChallenge())
{
$queries['multi']['challenges'][$server_id] = $instance;
}
// Add this instance to do info call
$queries['multi']['info'][$server_id] = $instance;
}
}
// First lets do the faster, multi queries
if(count($queries['multi']['info']) > 0)
{
$this->requestMulti($queries['multi']);
}
// Now lets do the slower linear queries.
if(count($queries['linear']) > 0)
{
$this->requestLinear($queries['linear']);
}
// Now let's loop the servers and process the response data
foreach($this->servers AS $server_id => $instance)
{
// Lets process this and filter
$data[$server_id] = $this->filterResponse($instance->processResponse(), $instance);
}
// Send back the data array, could be empty
return $data;
}
/* Working Methods */
/**
* Apply all set filters to the data returned by gameservers.
*
* @param array $data
* @param GameQ_Protocols_Core $protocol_instance
* @return array
*/
protected function filterResponse($data, GameQ_Protocols_Core $protocol_instance)
{
foreach($this->filters AS $filter_name => $filter_instance)
{
$data = $filter_instance->filter($data, $protocol_instance);
}
return $data;
}
/**
* Process the servers that support multi requests. That being multiple packets can be sent out at once.
*
* @param array $servers
* @return boolean
*/
protected function requestMulti($servers=array())
{
// See if we have any challenges to send off
if(count($servers['challenges']) > 0)
{
// Now lets send off all the challenges
$this->challenge($servers['challenges']);
// Now let's process the challenges
// Loop thru all the instances
foreach($servers['challenges'] AS $server_id => $instance)
{
$instance->challengeVerifyAndParse();
}
}
// Send out all the query packets to get data for
$this->queryServerInfo($servers['info']);
return TRUE;
}
/**
* Process "linear" servers. Servers that do not support multiple packet calls at once. So Slow!
*
* @param array $servers
* @return boolean
*/
protected function requestLinear($servers=array())
{
// Loop thru all the linear servers
foreach($servers AS $server_id => $instance)
{
// First we need to get a socket
$socket = $this->socket_open($instance, TRUE);
// Socket id
$socket_id = (int) $socket;
// See if we have challenges to send off
if($instance->hasChallenge())
{
// Now send off the challenge packet
fwrite($socket, $instance->getPacket('challenge'));
// Read in the challenge response
$instance->challengeResponse(array(fread($socket, 4096)));
// Now we need to parse and apply the challenge response to all the packets that require it
$instance->challengeVerifyAndParse();
}
// Grab the packets we need to send, minus the challenge packet
$packets = $instance->getPacket('!challenge');
// Now loop the packets, begin the slowness
foreach($packets AS $packet_type => $packet)
{
// Add the socket information so we can retreive it easily
$this->sockets = array(
$socket_id => array(
'server_id' => $server_id,
'packet_type' => $packet_type,
'socket' => $socket,
)
);
// Write the packet
fwrite($socket, $packet);
// If this is TCP we have to handle differently
if($instance->transport() == $instance::TRANSPORT_TCP)
{
// Save the response from this packet now
$instance->packetResponse($packet_type, stream_get_contents($socket));
}
else // UDP
{
// Get the responses from the query
$responses = $this->sockets_listen();
// Lets look at our responses
foreach($responses AS $socket_id => $response)
{
// Save the response from this packet
$instance->packetResponse($packet_type, $response);
}
}
}
}
// Now close all the socket(s) and clean up any data
$this->sockets_close();
return TRUE;
}
/**
* Send off needed challenges and get the response
*
* @param array $instances
* @return boolean
*/
protected function challenge(Array $instances=NULL)
{
// Loop thru all the instances we need to send out challenges for
foreach($instances AS $server_id => $instance)
{
// Make a new socket
$socket = $this->socket_open($instance);
// Now write the challenge packet to the socket.
fwrite($socket, $instance->getPacket($instance::PACKET_CHALLENGE));
// Add the socket information so we can retreive it easily
$this->sockets[(int) $socket] = array(
'server_id' => $server_id,
'packet_type' => $instance::PACKET_CHALLENGE,
'socket' => $socket,
);
// Let's sleep shortly so we are not hammering out calls raipd fire style
usleep(200000);
}
// Now we need to listen for challenge response(s)
$responses = $this->sockets_listen();
// Lets look at our responses
foreach($responses AS $socket_id => $response)
{
// Back out the server_id we need to update the challenge response for
$server_id = $this->sockets[$socket_id]['server_id'];
// Now set the proper response for the challenge because we might need it later
$this->servers[$server_id]->challengeResponse($response);
}
// Now close all the socket(s) and clean up any data
$this->sockets_close();
return TRUE;
}
/**
* Query the server for actual server information (i.e. info, players, etc...)
*
* @param array $instances
* @return boolean
*/
protected function queryServerInfo(Array $instances=NULL)
{
foreach($instances AS $server_id => $instance)
{
// Invoke the beforeSend method
$instance->beforeSend();
// Get all the non-challenge packets we need to send
$packets = $instance->getPacket('!challenge');
if(count($packets) == 0)
{
// Skip nothing else to do for some reason.
continue;
}
// Now lets send off the packets
foreach($packets AS $packet_type => $packet)
{
// Make a new socket
$socket = $this->socket_open($instance);
// Now write the packet to the socket.
fwrite($socket, $packet);
// Add the socket information so we can retreive it easily
$this->sockets[(int) $socket] = array(
'server_id' => $server_id,
'packet_type' => $packet_type,
'socket' => $socket,
);
// Let's sleep shortly so we are not hammering out calls raipd fire style
usleep(50000);
}
}
// Now we need to listen for packet response(s)
$responses = $this->sockets_listen();
// Lets look at our responses
foreach($responses AS $socket_id => $response)
{
// Back out the server_id
$server_id = $this->sockets[$socket_id]['server_id'];
// Back out the packet type
$packet_type = $this->sockets[$socket_id]['packet_type'];
// Save the response from this packet
$this->servers[$server_id]->packetResponse($packet_type, $response);
}
// Now close all the socket(s) and clean up any data
$this->sockets_close();
return TRUE;
}
/* Sockets/streams stuff */
/**
* Open a new socket based on the instance information
*
* @param GameQ_Protocols_Core $instance
* @param bool $blocking
* @throws GameQException
* @return boolean|resource
*/
protected function socket_open(GameQ_Protocols_Core $instance, $blocking=FALSE)
{
// Define some local vars really fast
$errno = null;
$errstr = null;
// Grab the options for this instance
$options = $instance->options();
// Create the remote address
$remote_addr = sprintf("%s://%s:%d", $instance->transport(), $instance->ip(), $instance->port());
// Create context
$context = stream_context_create(array(
'socket' => array(
'bindto' => '0:0', // Bind to any available IP and OS decided port
),
));
// Create the socket
if(($socket = stream_socket_client($remote_addr, $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $context)) !== FALSE)
{
// Create the read timeout on the streams
stream_set_timeout($socket, $options['timeout']);
// Set blocking mode
stream_set_blocking($socket, $blocking);
}
else // Throw an error
{
throw new GameQException(__METHOD__ . ' Error: ' .$errstr, $errno);
return false;
}
// return the socket
return $socket;
}
/**
* Listen to all the created sockets and return the responses
*
* @return array
*/
protected function sockets_listen()
{
$responses = array();
$sockets = array();
// Loop and pull out all the actual sockets
foreach($this->sockets AS $socket_id => $socket_data)
{
// Append the actual socket we are listening to
$sockets[$socket_id] = $socket_data['socket'];
}
$results = array();
$read = $sockets;
$write = NULL;
$except = NULL;
$starttime = microtime(true);
while (($t = $this->options['timeout'] * 1000000 - (microtime(true) - $starttime) * 10000) > 0)
{
// Now lets listen
$streams = stream_select($read, $write, $except, 0, $t);
// We had error or no streams left
if($streams === FALSE || $streams <= 0)
{
break;
}
foreach($read AS $socket)
{
// See if we have a response
if(($response = stream_socket_recvfrom($socket, 8192, STREAM_OOB)) === FALSE)
//if(($response = stream_get_line($socket, 16384)) === FALSE)
{
continue; // No response yet so lets continue.
}
// Add the response we got back
$responses[(int) $socket][] = $response;
}
// Because stream_select modifies read we need to reset it each
// time to the original array of sockets
$read = $sockets;
// Sleep for a short bit so we dont 99% the cpu
usleep(50000);
}
return $responses;
}
/**
* Close all the open sockets
*/
protected function sockets_close()
{
foreach($this->sockets AS $socket_id => $data)
{
fclose($data['socket']);
unset($this->sockets[$socket_id]);
}
}
}
/**
* GameQ Exception Class
*
* Thrown when there is any kind of internal configuration error or
* some unhandled or unexpected error or response.
*
* @author Austin Bischoff <[email protected]>
*/
class GameQException extends Exception {}