-
Notifications
You must be signed in to change notification settings - Fork 25
/
status.php
148 lines (126 loc) · 3.34 KB
/
status.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Minecraft Server Status Query
*
* Adapted by StevenLawson for TotalFreedom Website
*
* @link https://github.com/FunnyItsElmo/PHP-Minecraft-Server-Status-Query/
* @author Julian Spravil <[email protected]>
* @copyright Copyright (c) 2016 Julian Spravil
* @license https://github.com/FunnyItsElmo/PHP-Minecraft-Server-Status-Query/blob/master/LICENSE
*/
class Packet
{
protected $packetID;
protected $data;
public function __construct($packetID)
{
$this->packetID = $packetID;
$this->data = pack('C', $packetID);
}
public function addSignedChar($data)
{
$this->data .= pack('c', $data);
}
public function addUnsignedChar($data)
{
$this->data .= pack('C', $data);
}
public function addSignedShort($data)
{
$this->data .= pack('s', $data);
}
public function addUnsignedShort($data)
{
$this->data .= pack('S', $data);
}
public function addString($data)
{
$this->data .= pack('C', strlen($data));
$this->data .= $data;
}
public function send($socket)
{
$this->data = pack('C', strlen($this->data)) . $this->data;
socket_send($socket, $this->data, strlen($this->data), 0);
}
}
class HandshakePacket extends Packet
{
public function __construct($host, $port, $protocol, $nextState)
{
parent::__construct(0);
$this->addUnsignedChar($protocol);
$this->addString($host);
$this->addUnsignedShort($port);
$this->addUnsignedChar($nextState);
}
}
class PingPacket extends Packet
{
public function __construct()
{
parent::__construct(0);
}
}
class MinecraftServerStatus
{
public static function query($host, $port = 25565)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!@socket_connect($socket, $host, $port))
{
return false;
}
$handshakePacket = new HandshakePacket($host, $port, 107, 1);
$pingPacket = new PingPacket();
$handshakePacket->send($socket);
$start = microtime(true);
$pingPacket->send($socket);
$length = self::readVarInt($socket);
$ping = round((microtime(true) - $start) * 1000);
$data = socket_read($socket, $length, PHP_NORMAL_READ);
$data = strstr($data, '{');
$data = json_decode($data, true);
$data['ping'] = $ping;
return $data;
}
private static function readVarInt($socket)
{
$a = 0;
$b = 0;
while (true)
{
$c = socket_read($socket, 1);
if (!$c)
{
return 0;
}
$c = Ord($c);
$a |= ($c & 0x7F) << $b ++ * 7;
if ($b > 5)
{
return false;
}
if (($c & 0x80) != 128)
{
break;
}
}
return $a;
}
}
$response = MinecraftServerStatus::query('192.99.69.234', 25565);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
if (!$response)
{
$response = array('status' => false);
}
else
{
$response['status'] = true;
}
echo json_encode($response, JSON_PRETTY_PRINT);