Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Last active July 11, 2018 11:17
Show Gist options
  • Save Frago9876543210/9eed18689aa5a85bd1782eb0c54daa60 to your computer and use it in GitHub Desktop.
Save Frago9876543210/9eed18689aa5a85bd1782eb0c54daa60 to your computer and use it in GitHub Desktop.
Get server info
<?php
/**
* Script for get info from target server
* @link https://github.com/Frago9876543210
* @author Frago9876543210
*/
declare(strict_types=1);
class ServerInfo{
/** @var string $host */
private $host;
/** @var int $port */
private $port;
/** @var resource */
private $socket;
const MAGIC = "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78";
const MAX_INT = 2147483647;
public function __construct(string $host, int $port = 19132){
$this->host = $host;
$this->port = $port;
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$randomPort = mt_rand(49152, 65535);
if(!socket_bind($this->socket, "0.0.0.0", $randomPort)){
echo "Port $randomPort is already used!" . PHP_EOL;
exit(1);
}
}
public function sendUnconnectedPing() : array{
socket_sendto($this->socket, "\x01" . pack("NN", mt_rand(0, self::MAX_INT), mt_rand(0, self::MAX_INT)) . self::MAGIC, 25, 0, $this->host, $this->port);
socket_recvfrom($this->socket, $bytes, 65535, 0, $address, $port);
socket_close($this->socket);
return (ord($bytes[0]) === 0x1c) ? explode(";", substr($bytes, 35)) : [];
}
}
$serverInfo = new ServerInfo("play.lbsg.net");
var_dump($serverInfo->sendUnconnectedPing());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment