forked from iJoshuaHD/dutok
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
71 lines (66 loc) · 1.95 KB
/
query.php
File metadata and controls
71 lines (66 loc) · 1.95 KB
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
<?php
define('SERVEROK', 0);
define('SERVERDOWN', 1);
define('SERVERINVALID', 2);
define('UNKNOWN_ERROR', 3);
define('TIMEOUT', 1);
function UT3QueryServer($host,$port,&$return)
{
$sock = @fsockopen( "udp://" . $host, $port );
if ( !$sock ) return SERVERINVALID;
socket_set_timeout( $sock, 0, 500000 );
if ( !@fwrite( $sock, "\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01" ) )
return SERVERDOWN;
$challenge = fread( $sock, 1400 );
if ( !$challenge )
return UNKNOWN_ERROR;
$challenge = substr( preg_replace( "/[^0-9\-]/si", "", $challenge ), 1 );
$query = sprintf(
"\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01",
( $challenge >> 24 ),
( $challenge >> 16 ),
( $challenge >> 8 ),
( $challenge >> 0 )
);
if ( !@fwrite( $sock, $query ) )
return UNKNOWN_ERROR;
$response = array();
for ($x = 0; $x < 2; $x++)
{
$response[] = @fread($sock,2048);
}
$response = implode($response);
$response = substr($response,16);
$response = explode("\0",$response);
array_pop($response);
array_pop($response);
array_pop($response);
array_pop($response);
$return = array();
$type = 0;
foreach ($response as $key)
{
if ($type == 0) $val = $key;
if ($type == 1) $return[$val] = $key;
$type == 0 ? $type = 1 : $type = 0;
}
return SERVEROK;
}
$host = SERVER_HOST;
$port = SERVER_PORT;
if (empty($_GET['port'])) {
$port = 19132;
}
$values = '';
$returnvalue = UT3QueryServer($host,$port,$values);
if($returnvalue != SERVEROK){
echo '<p class="text-danger text-center">Server is down.</p>';
}else{
$currentplayers = (int) $values['numplayers'];
$maxplayers = (int) $values['maxplayers'];
$version = $values['version'];
$engine = $values['server_engine'];
$hostname = $values['hostname'];
$percent = $currentplayers / $maxplayers*100;
$ostatus = 1;
}