Advertisement
djazz

SGIP - query.php - Get servername, gamemode etc..

Mar 19th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. // For SGIP:
  3. // http://djazz.mine.nu/apps/sgip/
  4.  
  5. header("content-type: text/html");
  6. header("Access-Control-Allow-Origin: *");
  7. set_time_limit(3);
  8.  
  9. $ip = $_GET['ip'];
  10. $port = isset($_GET['port'])? intval($_GET['port']) : 10052;
  11.  
  12. $result = array();
  13. $result['error'] = 0;
  14.  
  15. function echoPacket($title, $pack) {
  16.     $len = strlen($pack);
  17.     echo "<!-- Echo of packet $title - START -->\n<table border=\"1\">\n";
  18.     echo "<tr>\n<td> </td>";        //Offsets
  19.     for ($i = 0; $i < $len; $i++) echo "<td>".$i."</td>";
  20.     echo "\n</tr>\n";
  21.     echo "<tr>\n<td>$title (txt):</td>\n";  //echo in text form
  22.     for ($i = 0; $i < $len; $i++) echo "<td>".$pack[$i]."</td>";
  23.     echo "\n</tr>\n";
  24.     echo "<tr>\n<td>$title (hex):</td>\n";  //Echo in hex form
  25.     for ($i = 0; $i < $len; $i++) echo "<td>".strtoupper(str_pad(dechex(ord($pack[$i])), 2, "0", STR_PAD_LEFT))."</td>";
  26.     echo "\n</tr>\n";  
  27.     echo "<tr>\n<td>$title (dec):</td>\n";  //Echo in decimal form
  28.     for ($i = 0; $i < $len; $i++) echo "<td>".str_pad(ord($pack[$i]),2,"0",STR_PAD_LEFT)."</td>";
  29.     echo "\n</tr>\n";
  30.     echo "</table>\n<!-- Echo of packet $title - END -->\n";
  31. }
  32.  
  33. function udpchecksum ($buf) {
  34.     $x = 1;
  35.     $y = 1;
  36.     $size = strlen($buf);
  37.     for($i = 2; $i < $size; $i++) {
  38.         $x += ord($buf[$i]);
  39.         $y += $x;
  40.     }
  41.     $buf[0] = chr($x % 251);
  42.     $buf[1] = chr($y % 251);
  43.     return $buf;
  44. }
  45. function sendUDP($s) {
  46.     global $udp;
  47.     $s = udpchecksum($s);
  48.     fwrite($udp, $s);
  49. }
  50.  
  51. $udp = @fsockopen("udp://".$ip, $port, $errno, $errstr, 5);
  52. stream_set_timeout($udp, 5);
  53. if(!$udp) {
  54.     $result['error'] = 1;
  55.     echo json_encode($result);
  56.     exit;
  57. }
  58. $toSend = "yy\x05"."\x00";
  59. sendUDP($toSend);
  60. $query = @fread($udp, 128);
  61. if(!$query) {
  62.     $result['error'] = 2;
  63.     echo json_encode($result);
  64.     exit;
  65. }
  66. fclose($udp);
  67.  
  68. //echoPacket('query', $query);
  69.  
  70. $result['version'] = substr($query, 8, 4);
  71. $result['capacity'] = array(ord($query[12]), ord($query[15]));
  72. $result['isPlus'] = strlen($query) >= 19+ord($query[16]); // An extra byta
  73. $result['gamemode'] = ord($query[14]);
  74. $result['servername'] = htmlentities(utf8_encode(substr($query, 17, ord($query[16]))));
  75.  
  76. echo json_encode($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement