forked from xPaw/PHP-Source-Query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceRcon.php
More file actions
199 lines (154 loc) · 4.93 KB
/
Copy pathSourceRcon.php
File metadata and controls
199 lines (154 loc) · 4.93 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
<?php
/**
* @author Pavel Djundik
*
* @link https://xpaw.me
* @link https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery;
use xPaw\SourceQuery\Exception\AuthenticationException;
use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;
/**
* Class SourceRcon
*
* @package xPaw\SourceQuery
*
* @uses xPaw\SourceQuery\Exception\AuthenticationException
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
* @uses xPaw\SourceQuery\Exception\SocketException
*/
class SourceRcon
{
/**
* Points to socket class
*/
private BaseSocket $Socket;
/** @var ?resource */
private $RconSocket;
private int $RconRequestId = 0;
public function __construct( BaseSocket $Socket )
{
$this->Socket = $Socket;
}
public function Close( ) : void
{
if( $this->RconSocket )
{
fclose( $this->RconSocket );
$this->RconSocket = null;
}
$this->RconRequestId = 0;
}
public function Open( ) : void
{
if( !$this->RconSocket )
{
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
if( $ErrNo || !$RconSocket )
{
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
}
$this->RconSocket = $RconSocket;
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
stream_set_blocking( $this->RconSocket, true );
}
}
public function Write( int $Header, string $String = '' ) : bool
{
// Pack the packet together
$Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
// Prepend packet length
$Command = pack( 'V', strlen( $Command ) ) . $Command;
$Length = strlen( $Command );
return $Length === fwrite( $this->RconSocket, $Command, $Length );
}
public function Read( ) : Buffer
{
$Buffer = new Buffer( );
$Buffer->Set( fread( $this->RconSocket, 4 ) );
if( $Buffer->Remaining( ) < 4 )
{
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
}
$PacketSize = $Buffer->GetLong( );
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
$Data = $Buffer->Get( );
$Remaining = $PacketSize - strlen( $Data );
while( $Remaining > 0 )
{
$Data2 = fread( $this->RconSocket, $Remaining );
$PacketSize = strlen( $Data2 );
if( $PacketSize === 0 )
{
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
}
$Data .= $Data2;
$Remaining -= $PacketSize;
}
$Buffer->Set( $Data );
return $Buffer;
}
public function Command( string $Command ) : string
{
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
$Buffer = $this->Read( );
$Buffer->GetLong( ); // RequestID
$Type = $Buffer->GetLong( );
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
{
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
}
else if( $Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
$Data = $Buffer->Get( );
// We do this stupid hack to handle split packets
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if( strlen( $Data ) >= 4000 )
{
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
do
{
$Buffer = $this->Read( );
$Buffer->GetLong( ); // RequestID
if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
break;
}
$Data2 = $Buffer->Get( );
if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
{
break;
}
$Data .= $Data2;
}
while( true );
}
return rtrim( $Data, "\0" );
}
public function Authorize( string $Password ) : void
{
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
$Buffer = $this->Read( );
$RequestID = $Buffer->GetLong( );
$Type = $Buffer->GetLong( );
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
$Buffer = $this->Read( );
$RequestID = $Buffer->GetLong( );
$Type = $Buffer->GetLong( );
}
if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE )
{
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
}
}
}