Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function ping($host, $port = 25565, $timeout = 1)
- {
- //Set up our socket
- $fp = fsockopen("tcp://" . $host, $port, $errno, $errstr, $timeout);
- stream_set_timeout($fp, 1);
- $arr = stream_get_meta_data($fp);
- if($arr['timed_out'] == TRUE)
- return array();
- if(!$fp)
- return array();
- //Send 0xFE: Server list ping
- fwrite($fp, "\xFE");
- //Read as much data as we can (max packet size: 241 bytes)
- $d = fread($fp, 256);
- //Check we've got a 0xFF Disconnect
- if($d[0] != "\xFF")
- return array();
- //Remove the packet ident (0xFF) and the short containing the length of the string
- $d = substr($d, 3);
- //Decode UCS-2 string
- $d = mb_convert_encoding($d, 'auto', 'UCS-2');
- //Split into array
- $d = explode("\xA7", $d);
- //Return an associative array of values
- return array(
- 'motd' => $d[0],
- 'players' => intval($d[1]),
- 'max_players' => intval($d[2])
- );
- }
- function full_stat($host, $port = 25565, $timeout = 1)
- {
- //Set up our socket
- $fp = fsockopen("udp://" . $host, $port, $errno, $errstr, $timeout);
- stream_set_timeout($fp, 1);
- $arr = stream_get_meta_data($fp);
- if($arr['timed_out'] == TRUE)
- return array();
- if(!$fp)
- return array();
- // Get the challenge token; send 0xFE 0xFD 0x09 and a 4-byte session id
- $str1 = "\xFE\xFD\x09\x01\x02\x03\x04"; // Arbitrary session id at the end (4 bytes)
- fwrite($fp, $str1);
- $resp1 = fread($fp, 256);
- if(strlen($resp1) > 0)
- {
- if($resp1[0] != "\x09") // Check for a valid response
- return array();
- }
- else
- return array();
- // Parse the challenge token from string to integer
- $token = 0;
- for($i = 5; $i < (strlen($resp1) - 1); $i++)
- {
- $token *= 10;
- $token += $resp1[$i];
- }
- // Divide the int32 into 4 bytes
- $token_arr = array( 0 => ($token / (256*256*256)) % 256,
- 1 => ($token / (256*256)) % 256,
- 2 => ($token / 256) % 256,
- 3 => ($token % 256)
- );
- /*
- // Get the short version of server status. ID and challenge tokens are appended to the command 0x00.
- $str = "\xFE\xFD\x00\x01\x02\x03\x04"
- . chr($token_arr[0]) . chr($token_arr[1]) . chr($token_arr[2]) . chr($token_arr[3]);
- fwrite($fp, $str);
- $data1 = fread($fp, 4096);
- $short_stat = substr($data1, 5); // Strip the crap from the start
- $short_stat = explode("\x00", $short_stat); // Explode the payload from the NULL-terminated string ends
- $port = ord($short_stat[5][1]) * 256 + ord($short_stat[5][0]); // Big-endian short
- $short_stat = array( 'motd' => $short_stat[0],
- 'gametype' => $short_stat[1],
- 'map' => $short_stat[2],
- 'numplayers' => $short_stat[3],
- 'maxplayers' => $short_stat[4],
- 'portnum' => $port,
- 'hostname' => substr($short_stat[5], 2) // Host IP after the port number
- );
- */
- // Get the full version of server status. ID and challenge tokens appended to command 0x00, payload padded to 8 bytes.
- $str = "\xFE\xFD\x00\x01\x02\x03\x04"
- . chr($token_arr[0]) . chr($token_arr[1]) . chr($token_arr[2]) . chr($token_arr[3])
- . "\x00\x00\x00\x00";
- fwrite($fp, $str);
- $data2 = fread($fp, 4096);
- $full_stat = substr($data2, 11); // Strip the crap from the start
- $tmp = explode("\x00\x01player_\x00\x00", $full_stat); // First, split the payload in two parts
- $t = explode("\x00", $tmp[0]); // Divide the first part from every NULL-terminated string end into key1 val1 key2 val2...
- unset($t[count($t) - 1]); // Unset the last entry, because the are two 0x00 bytes at the end
- $t2 = explode("\x00", $tmp[1]); // Explode the player information from the second part
- $info = array();
- for($i = 0; $i < count($t); $i += 2)
- {
- if($t[$i] == "")
- break;
- $info[$t[$i]] = $t[$i + 1];
- }
- $players = array();
- foreach($t2 as $one)
- {
- if($one == "")
- break;
- $players[] = $one;
- }
- $full_stat = $info;
- $full_stat['players'] = $players;
- return $full_stat;
- }
- /*
- $arr = ping("localhost", 25565, 2);
- echo "<pre>\n";
- print_r($arr);
- echo "</pre>\n";
- */
- $arr = full_stat("localhost", 25565, 1);
- echo "<pre>\n";
- print_r($arr);
- echo "</pre>\n";
- /*
- echo "<pre>\n";
- echo "token (resp1): ";
- for($i = 0; $i < strlen($resp1); $i++)
- {
- printf("0x%02X ", ord($resp1[$i]));
- }
- echo "\nstr: ";
- for($i = 0; $i < strlen($str); $i++)
- {
- printf("0x%02X ", ord($str[$i]));
- }
- echo "\ndata1: ";
- for($i = 0; $i < strlen($data1); $i++)
- {
- printf("0x%02X ", ord($data1[$i]));
- }
- echo "\ndata2: ";
- for($i = 0; $i < strlen($data2); $i++)
- {
- printf("0x%02X ", ord($data2[$i]));
- }
- echo "\ntoken (int32): $token\ntoken_arr: ";
- print_r($token_arr);
- for($i = 0; $i < 4; $i++)
- printf("0x%02X ", $token_arr[$i]);
- echo "\n\nshort_stat:\n";
- print_r($short_stat);
- echo "\n\nfull_stat:\n";
- print_r($full_stat);
- echo "</pre>\n";
- echo "<br />errno:\n";
- print_r($errno);
- echo "<br />errstr:\n";
- print_r($errstr);
- echo "<br />data1:\n";
- for($i = 0; $i < strlen($data1); $i++)
- {
- printf("0x%02X ", ord($data1[$i]));
- }
- echo "</pre>\n";
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement