-
Notifications
You must be signed in to change notification settings - Fork 22
/
MinecraftServer.php
126 lines (103 loc) · 3.34 KB
/
MinecraftServer.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
<?php
/**
* Minecraft server status
* Query minecraft server info
* @author noxifoxi https://github.com/noxifoxi
* @license GNU Public Licence - Version 3
* @copyright © 2011-2022 noxifoxi
*/
class MinecraftServer {
// command byte which tell the server to send the status
const STATUS = 0x00;
// challenge (handshake)
const HANDSHAKE = 0x09;
// magic bytes
const B1 = 0xFE;
const B2 = 0xFD;
private $socket;
private $info = [];
public function __get($property) {
if (array_key_exists($property, $this->info))
return $this->info[$property];
}
public function __construct(string $host, int $port = 25565, int $timeout = 1) {
$this->socket = @stream_socket_client('udp://' . $host . ':' . $port, $errNo, $errStr, $timeout);
if (!$this->socket) {
throw new Exception("$errStr ($errNo)");
}
stream_set_timeout($this->socket, $timeout);
/*
Create handshake and request server status
*/
try {
$data = $this->send(self::STATUS, pack('N', $this->send(self::HANDSHAKE)).pack('c*', 0x00, 0x00, 0x00, 0x00));
} catch (Exception $e) {
// Try fallback if query is not enabled on the server
if(!class_exists('MinecraftServerBasic') && file_exists(__DIR__.'/MinecraftServerBasic.php'))
require_once(__DIR__.'/MinecraftServerBasic.php');
if(class_exists('MinecraftServerBasic')) {
$fallback = new MinecraftServerBasic($host, $port, $timeout);
$this->info = $fallback->getInfoArray();
}
fclose($this->socket);
return;
}
/*
Prepare the data for parsing
*/
// Split the data string on the player position
$data = explode("\00\00\01player_\00\00", $data);
// Save the players
$players = '';
if(isset($data[1]))
$players = substr($data[1], 0, -2);
// Split the server infos (status)
$data = explode("\x00", $data[0]);
/*
populate server info
*/
for($i = 0; $i < sizeof($data); $i += 2) {
$this->info[$data[$i]] = $data[$i+1];
}
// Parse plugins and try to determine the server software
if($this->info['plugins']) {
$data = explode(": ", $this->info['plugins']);
$this->info['software'] = $data[0];
if(isset($data[1]))
$this->info['plugins'] = explode('; ', $data[1]);
else
unset($this->info['plugins']);
} else {
// It seems to be a vanilla server
$this->info['software'] = 'Vanilla';
unset($this->info['plugins']);
}
// Parse players
if($players)
$this->info['players'] = explode("\00", $players);
// encode utf8 server names
$this->info['hostname'] = utf8_encode($this->info['hostname']);
// set online status
$this->info['online'] = 1;
/*
Close connection
*/
fclose($this->socket);
}
public function getInfoArray(): array {
return $this->info;
}
private function send(int $command, string $addition = NULL): string {
// pack the command into a binary string
$command = pack('c*', self::B1, self::B2, $command, 0x01, 0x02, 0x03, 0x04).$addition;
// send the binary string to the server
if(strlen($command) !== @fwrite($this->socket, $command, strlen($command)))
throw new Exception('Failed to write on socket', 2);
// listen what the server has to say now
$data = fread($this->socket, 2048);
if($data === false)
throw new Exception('Failed to read from socket', 3);
// remove the first 5 unnecessary bytes (0x00, 0x01, 0x02, 0x03, 0x04) Status type and own ID token
return substr($data, 5);
}
}