1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for |
4
|
|
|
* more information about Source RCON Packets |
5
|
|
|
* |
6
|
|
|
* PHP Version 7 |
7
|
|
|
* |
8
|
|
|
* @copyright 2013-2017 Chris Churchwell |
9
|
|
|
* @author thedudeguy |
10
|
|
|
* @link https://github.com/thedudeguy/PHP-Minecraft-Rcon |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Rcon { |
|
|
|
|
14
|
|
|
private $host; |
15
|
|
|
private $port; |
16
|
|
|
private $password; |
17
|
|
|
private $timeout; |
18
|
|
|
|
19
|
|
|
private $socket; |
20
|
|
|
|
21
|
|
|
private $authorized; |
22
|
|
|
private $last_response; |
23
|
|
|
|
24
|
|
|
const PACKET_AUTHORIZE = 5; |
25
|
|
|
const PACKET_COMMAND = 6; |
26
|
|
|
|
27
|
|
|
const SERVERDATA_AUTH = 3; |
28
|
|
|
const SERVERDATA_AUTH_RESPONSE = 2; |
29
|
|
|
const SERVERDATA_EXECCOMMAND = 2; |
30
|
|
|
const SERVERDATA_RESPONSE_VALUE = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new instance of the Rcon class. |
34
|
|
|
* |
35
|
|
|
* @param string $host |
36
|
|
|
* @param integer $port |
37
|
|
|
* @param string $password |
38
|
|
|
* @param integer $timeout |
39
|
|
|
*/ |
40
|
|
|
public function __construct($host, $port, $password, $timeout) |
41
|
|
|
{ |
42
|
|
|
$this->host = $host; |
43
|
|
|
$this->port = $port; |
44
|
|
|
$this->password = $password; |
45
|
|
|
$this->timeout = $timeout; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the latest response from the server. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getResponse() |
54
|
|
|
{ |
55
|
|
|
return $this->last_response; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Connect to a server. |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function connect() |
64
|
|
|
{ |
65
|
|
|
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
66
|
|
|
|
67
|
|
|
if (!$this->socket) { |
68
|
|
|
$this->last_response = $errstr; |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//set timeout |
73
|
|
|
stream_set_timeout($this->socket, 3, 0); |
74
|
|
|
|
75
|
|
|
// check authorization |
76
|
|
|
if ($this->authorize()) |
77
|
|
|
return true; |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Disconnect from server. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function disconnect() |
88
|
|
|
{ |
89
|
|
|
if ($this->socket) |
90
|
|
|
fclose($this->socket); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* True if socket is connected and authorized. |
95
|
|
|
* |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
public function isConnected() |
99
|
|
|
{ |
100
|
|
|
return $this->authorized; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Send a command to the connected server. |
105
|
|
|
* |
106
|
|
|
* @param string $command |
107
|
|
|
* |
108
|
|
|
* @return boolean|mixed |
109
|
|
|
*/ |
110
|
|
|
public function sendCommand($command) |
111
|
|
|
{ |
112
|
|
|
if (!$this->isConnected()) |
113
|
|
|
return false; |
114
|
|
|
|
115
|
|
|
// send command packet |
116
|
|
|
$this->writePacket(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command); |
117
|
|
|
|
118
|
|
|
// get response |
119
|
|
|
$response_packet = $this->readPacket(); |
120
|
|
|
if ($response_packet['id'] == Rcon::PACKET_COMMAND) { |
121
|
|
|
if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) { |
122
|
|
|
$this->last_response = $response_packet['body']; |
123
|
|
|
|
124
|
|
|
return $response_packet['body']; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Log into the server with the given credentials. |
133
|
|
|
* |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
|
|
private function authorize() |
137
|
|
|
{ |
138
|
|
|
$this->writePacket(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password); |
139
|
|
|
$response_packet = $this->readPacket(); |
140
|
|
|
|
141
|
|
|
if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) { |
142
|
|
|
if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) { |
143
|
|
|
$this->authorized = true; |
144
|
|
|
|
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->disconnect(); |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Writes a packet to the socket stream. |
155
|
|
|
* |
156
|
|
|
* @param $packet_id |
157
|
|
|
* @param $packet_type |
158
|
|
|
* @param $packet_body |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
private function writePacket($packet_id, $packet_type, $packet_body) |
163
|
|
|
{ |
164
|
|
|
/* |
165
|
|
|
Size 32-bit little-endian Signed Integer Varies, see below. |
166
|
|
|
ID 32-bit little-endian Signed Integer Varies, see below. |
167
|
|
|
Type 32-bit little-endian Signed Integer Varies, see below. |
168
|
|
|
Body Null-terminated ASCII String Varies, see below. |
169
|
|
|
Empty String Null-terminated ASCII String 0x00 |
170
|
|
|
*/ |
171
|
|
|
|
172
|
|
|
//create packet |
173
|
|
|
$packet = pack("VV", $packet_id, $packet_type); |
174
|
|
|
$packet = $packet . $packet_body . "\x00"; |
175
|
|
|
$packet = $packet . "\x00"; |
176
|
|
|
|
177
|
|
|
// get packet size. |
178
|
|
|
$packet_size = strlen($packet); |
179
|
|
|
|
180
|
|
|
// attach size to packet. |
181
|
|
|
$packet = pack("V", $packet_size) . $packet; |
182
|
|
|
|
183
|
|
|
// write packet. |
184
|
|
|
fwrite($this->socket, $packet, strlen($packet)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Read a packet from the socket stream. |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
private function readPacket() |
193
|
|
|
{ |
194
|
|
|
//get packet size. |
195
|
|
|
$size_data = fread($this->socket, 4); |
196
|
|
|
$size_pack = unpack("V1size", $size_data); |
197
|
|
|
$size = $size_pack['size']; |
198
|
|
|
|
199
|
|
|
// if size is > 4096, the response will be in multiple packets. |
200
|
|
|
// this needs to be address. get more info about multi-packet responses |
201
|
|
|
// from the RCON protocol specification at |
202
|
|
|
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol |
203
|
|
|
// currently, this script does not support multi-packet responses. |
204
|
|
|
|
205
|
|
|
$packet_data = fread($this->socket, $size); |
206
|
|
|
$packet_pack = unpack("V1id/V1type/a*body", $packet_data); |
207
|
|
|
|
208
|
|
|
return $packet_pack; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// Below are the deprecated functions for reverse compatibility |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @deprecated |
215
|
|
|
* @see Rcon::getResponse() |
216
|
|
|
*/ |
217
|
|
|
public function get_response() |
218
|
|
|
{ |
219
|
|
|
return $this->getResponse(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @deprecated |
224
|
|
|
* @see Rcon::isConnected() |
225
|
|
|
*/ |
226
|
|
|
public function is_connected() |
227
|
|
|
{ |
228
|
|
|
return $this->isConnected(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @deprecated |
233
|
|
|
* @see Rcon::sendCommand() |
234
|
|
|
*/ |
235
|
|
|
public function send_command($command) |
236
|
|
|
{ |
237
|
|
|
return $this->sendCommand($command) ; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.