Created
March 23, 2018 14:32
-
-
Save TheNewHEROBRINEX/1854260a8f8429f7ebee5817a20f6aee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const WRAPPER = "\xfe\xfd"; | |
const SESSIONID = "\x01\x02\x03\x04"; | |
const HANDSHAKE = "\x09"; | |
const STATISTICS = "\x00"; | |
function queryPMMPServer(string $ip, int $port = 19132): array { | |
$socket = fsockopen("udp://" . gethostbyname($ip), $port, $errno, $errstr); | |
$handshake = WRAPPER . HANDSHAKE . SESSIONID; //wrapper + handshake + sessionid | |
fwrite($socket, $handshake, strlen($handshake)); | |
$handshake_reply = fread($socket, 4096); //handshake + sessionid + token + \x00 | |
$token = substr($handshake_reply, 5, -1); | |
$statistics = WRAPPER . STATISTICS . SESSIONID . pack("N", $token) . str_repeat("\x00", 4); //wrapper + statistics + sessionid + token from handshake + 4 bytes for long query (if omitted the server will return short query) | |
fwrite($socket, $statistics, strlen($statistics)); | |
$statistics_reply = fread($socket, 4096); //info (key + \x00 + value + \x00) + \x00\x01player_\x00\x00 + player list (player + \x00) + \x00 | |
$query = explode("\x00\x00\x01player_\x00\x00", substr($statistics_reply, 16, -1)); | |
$data = $query[0]; | |
$n = 0; | |
$info = []; | |
foreach (explode("\x00", $data) as $key_value) { | |
if ($n % 2 == 0) { | |
$key = $key_value; | |
}else { | |
$info[$key] = $key_value; | |
} | |
$n++; | |
} | |
$players = $query[1]; | |
if ($players == "") { | |
$players = []; | |
}else { | |
$players = substr($players, 0, -1); | |
$players = explode("\x00", $players); | |
} | |
fclose($socket); | |
return ["info" => $info, "players" => $players]; | |
} | |
/* -------- TEST -------- */ | |
$result = queryPMMPServer("play.moonforce.eu", 19132); | |
$info = $result["info"]; | |
$players = $result["players"]; | |
foreach ($info as $key => $value) { | |
echo $key . ": " . $value . PHP_EOL; | |
} | |
echo "players: " . implode(", ", $players) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment