-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathquery.php
More file actions
211 lines (168 loc) · 4.8 KB
/
query.php
File metadata and controls
211 lines (168 loc) · 4.8 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Connects to a minecraft server using the [Gamespy?] Query protocol.
* more info on the protocol can be found at
* http://wiki.vg/Query
*
* @copyright 2013 Chris Churchwell
*
*/
class Query {
private $host;
private $port;
private $timeout;
private $token = null;
private $socket;
private $errstr = "";
const SESSION_ID = 2;
const TYPE_HANDSHAKE = 0x09;
const TYPE_STAT = 0x00;
public function __construct($host, $port=25565, $timeout=3, $auto_connect = false) {
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
if (is_array($host))
{
$this->host = $host['host'];
$this->port = empty($host['port'])?$port:$host['port'];
$this->timeout = empty($host['timeout'])?$timeout:$host['timeout'];
$auto_connect = empty($host['auto_connect'])?$auto_connect:$host['auto_connect'];
}
if ($auto_connect === true) {
$this->connect();
}
}
/**
* Returns the description of the last error produced.
*
* @return String - Last error string.
*/
public function get_error() {
return $this->errstr;
}
/**
* Checks whether or not the current connection is established.
*
* @return boolean - True if connected; false otherwise.
*/
public function is_connected() {
if (empty($this->token)) return false;
return true;
}
/**
* Disconnects!
* duh
*/
public function disconnect() {
if ($this->socket) {
fclose($this->socket);
}
}
/**
* Connects to the host via UDP with the provided credentials.
* @return boolean - true if successful, false otherwise.
*/
public function connect()
{
$this->socket = fsockopen( 'udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout );
if (!$this->socket)
{
$this->errstr = $errstr;
return false;
}
stream_set_timeout( $this->socket, $this->timeout );
stream_set_blocking( $this->socket, true );
return $this->get_challenge();
}
/**
* Authenticates with the host server and saves the authentication token to a class var.
*
* @return boolean - True if succesfull; false otherwise.
*/
private function get_challenge()
{
if (!$this->socket)
{
return false;
}
//build packet to get challenge.
$packet = pack("c3N", 0xFE, 0xFD, Query::TYPE_HANDSHAKE, Query::SESSION_ID);
//write packet
if ( fwrite($this->socket, $packet, strlen($packet)) === FALSE) {
$this->errstr = "Unable to write to socket";
return false;
}
//read packet.
$response = fread($this->socket, 2056);
if (empty($response)) {
$this->errstr = "Unable to authenticate connection";
return false;
}
$response_data = unpack("c1type/N1id/a*token", $response);
if (!isset($response_data['token']) || empty($response_data['token'])) {
$this->errstr = "Unable to authenticate connection.";
return false;
}
$this->token = $response_data['token'];
return true;
}
/**
* Gets all the info from the server.
*
* @return boolean|array - Returns the data in an array, or false if there was an error.
*/
public function get_info()
{
if (!$this->is_connected()) {
$this->errstr = "Not connected to host";
return false;
}
//build packet to get info
$packet = pack("c3N2", 0xFE, 0xFD, Query::TYPE_STAT, Query::SESSION_ID, $this->token);
//add the full stat thingy.
$packet = $packet . pack("c4", 0x00, 0x00, 0x00, 0x00);
//write packet
if (!fwrite($this->socket, $packet, strlen($packet))) {
$this->errstr = "Unable to write to socket.";
return false;
}
//read packet header
$response = fread($this->socket, 16);
//$response = stream_get_contents($this->socket);
// first byte is type. next 4 are id. dont know what the last 11 are for.
$response_data = unpack("c1type/N1id", $response);
//read the rest of the stream.
$response = fread($this->socket, 2056);
//split the response into 2 parts.
$payload = explode ( "\x00\x01player_\x00\x00" , $response);
$info_raw = explode("\x00", rtrim($payload[0], "\x00"));
//extract key->value chunks from info
$info = array();
foreach (array_chunk($info_raw, 2) as $pair) {
list($key, $value) = $pair;
//strip possible color format codes from hostname
if ($key == "hostname") {
$value = $this->strip_color_codes($value);
}
$info[$key] = $value;
}
//get player data.
$players_raw = rtrim($payload[1], "\x00");
$players = array();
if (!empty($players_raw)) {
$players = explode("\x00", $players_raw);
}
//attach player data to info for simplicity
$info['players'] = $players;
return $info;
}
/**
* Clears Minecraft color codes from a string.
*
* @param String $string - the string to remove the codes from
* @return String - a clean string.
*/
public function strip_color_codes($string) {
return preg_replace('/[\x00-\x1F\x80-\xFF]./', '', $string);
}
}