-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRcon.php
212 lines (180 loc) · 5.13 KB
/
Rcon.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
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
212
<?php
/**
* See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
* more information about Source RCON Packets
*
* PHP Version 7
*
* @copyright 2013-2017 Chris Churchwell
* @author thedudeguy
* @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
*/
namespace Thedudeguy;
class Rcon
{
private $host;
private $port;
private $password;
private $timeout;
private $socket;
private $authorized = false;
private $lastResponse = '';
const PACKET_AUTHORIZE = 5;
const PACKET_COMMAND = 6;
const SERVERDATA_AUTH = 3;
const SERVERDATA_AUTH_RESPONSE = 2;
const SERVERDATA_EXECCOMMAND = 2;
const SERVERDATA_RESPONSE_VALUE = 0;
/**
* Create a new instance of the Rcon class.
*
* @param string $host
* @param integer $port
* @param string $password
* @param integer $timeout
*/
public function __construct($host, $port, $password, $timeout)
{
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->timeout = $timeout;
}
/**
* Get the latest response from the server.
*
* @return string
*/
public function getResponse()
{
return $this->lastResponse;
}
/**
* Connect to a server.
*
* @return boolean
*/
public function connect()
{
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if (!$this->socket) {
$this->lastResponse = $errstr;
return false;
}
//set timeout
stream_set_timeout($this->socket, 3, 0);
// check authorization
return $this->authorize();
}
/**
* Disconnect from server.
*
* @return void
*/
public function disconnect()
{
if ($this->socket) {
fclose($this->socket);
}
}
/**
* True if socket is connected and authorized.
*
* @return boolean
*/
public function isConnected()
{
return $this->authorized;
}
/**
* Send a command to the connected server.
*
* @param string $command
*
* @return boolean|mixed
*/
public function sendCommand($command)
{
if (!$this->isConnected()) {
return false;
}
// send command packet
$this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
// get response
$response_packet = $this->readPacket();
if ($response_packet['id'] == self::PACKET_COMMAND) {
if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
$this->lastResponse = $response_packet['body'];
return $response_packet['body'];
}
}
return false;
}
/**
* Log into the server with the given credentials.
*
* @return boolean
*/
private function authorize()
{
$this->writePacket(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);
$response_packet = $this->readPacket();
if ($response_packet['type'] == self::SERVERDATA_AUTH_RESPONSE) {
if ($response_packet['id'] == self::PACKET_AUTHORIZE) {
$this->authorized = true;
return true;
}
}
$this->disconnect();
return false;
}
/**
* Writes a packet to the socket stream.
*
* @param $packetId
* @param $packetType
* @param string $packetBody
*
* @return void
*/
private function writePacket($packetId, $packetType, $packetBody)
{
/*
Size 32-bit little-endian Signed Integer Varies, see below.
ID 32-bit little-endian Signed Integer Varies, see below.
Type 32-bit little-endian Signed Integer Varies, see below.
Body Null-terminated ASCII String Varies, see below.
Empty String Null-terminated ASCII String 0x00
*/
//create packet
$packet = pack('VV', $packetId, $packetType);
$packet = $packet.$packetBody."\x00";
$packet = $packet."\x00";
// get packet size.
$packet_size = strlen($packet);
// attach size to packet.
$packet = pack('V', $packet_size).$packet;
// write packet.
fwrite($this->socket, $packet, strlen($packet));
}
/**
* Read a packet from the socket stream.
*
* @return array
*/
private function readPacket()
{
//get packet size.
$size_data = fread($this->socket, 4);
$size_pack = unpack('V1size', $size_data);
$size = $size_pack['size'];
// if size is > 4096, the response will be in multiple packets.
// this needs to be address. get more info about multi-packet responses
// from the RCON protocol specification at
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
// currently, this script does not support multi-packet responses.
$packet_data = fread($this->socket, $size);
$packet_pack = unpack('V1id/V1type/a*body', $packet_data);
return $packet_pack;
}
}