forked from xPaw/PHP-Minecraft-Query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinecraftRcon.class.php
122 lines (95 loc) · 2.41 KB
/
MinecraftRcon.class.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
<?php
class MinecraftRconException extends Exception
{
// Exception thrown by MinecraftRcon class
}
class MinecraftRcon
{
/*
* Class written by xPaw
*
* Website: http://xpaw.ru
* GitHub: https://github.com/xPaw/PHP-Minecraft-Query
*
* Protocol: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
*/
// Sending
const SERVERDATA_EXECCOMMAND = 2;
const SERVERDATA_AUTH = 3;
// Receiving
const SERVERDATA_RESPONSE_VALUE = 0;
const SERVERDATA_AUTH_RESPONSE = 2;
private $Socket;
private $RequestId;
public function __destruct( )
{
$this->Disconnect( );
}
public function Connect( $Ip, $Port = 25575, $Password, $Timeout = 3 )
{
$this->RequestId = 0;
if( $this->Socket = FSockOpen( $Ip, (int)$Port ) )
{
Socket_Set_TimeOut( $this->Socket, $Timeout );
if( !$this->Auth( $Password ) )
{
$this->Disconnect( );
throw new MinecraftRconException( "Authorization failed." );
}
}
else
{
throw new MinecraftQueryException( "Can't open socket." );
}
}
public function Disconnect( )
{
if( $this->Socket )
{
FClose( $this->Socket );
$this->Socket = null;
}
}
public function Command( $String )
{
if( !$this->WriteData( self :: SERVERDATA_EXECCOMMAND, $String ) )
{
return false;
}
$Data = $this->ReadData( );
if( $Data[ 'RequestId' ] < 1 || $Data[ 'Response' ] != self :: SERVERDATA_RESPONSE_VALUE )
{
return false;
}
return $Data[ 'String' ];
}
private function Auth( $Password )
{
if( !$this->WriteData( self :: SERVERDATA_AUTH, $Password ) )
{
return false;
}
$Data = $this->ReadData( );
return $Data[ 'RequestId' ] > -1 && $Data[ 'Response' ] == self :: SERVERDATA_AUTH_RESPONSE;
}
private function ReadData( )
{
$Packet = Array( );
$Size = FRead( $this->Socket, 4 );
$Size = UnPack( 'V1Size', $Size );
$Size = $Size[ 'Size' ];
// TODO: Add multiple packets (Source)
$Packet = FRead( $this->Socket, $Size );
$Packet = UnPack( 'V1RequestId/V1Response/a*String/a*String2', $Packet );
return $Packet;
}
private function WriteData( $Command, $String = "" )
{
// Pack the packet together
$Data = Pack( 'VV', $this->RequestId++, $Command ) . $String . "\x00\x00\x00";
// Prepend packet length
$Data = Pack( 'V', StrLen( $Data ) ) . $Data;
$Length = StrLen( $Data );
return $Length === FWrite( $this->Socket, $Data, $Length );
}
}