forked from xPaw/PHP-Source-Query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceRcon.class.php
More file actions
168 lines (130 loc) · 3.95 KB
/
SourceRcon.class.php
File metadata and controls
168 lines (130 loc) · 3.95 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
<?php
/**
* Class written by xPaw
*
* Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/
class SourceQuerySourceRcon
{
/**
* Points to buffer class
*
* @var SourceQueryBuffer
*/
private $Buffer;
/**
* Points to socket class
*
* @var SourceQuerySocket
*/
private $Socket;
private $RconSocket;
private $RconRequestId;
public function __construct( $Buffer, $Socket )
{
$this->Buffer = $Buffer;
$this->Socket = $Socket;
}
public function Close( )
{
if( $this->RconSocket )
{
FClose( $this->RconSocket );
$this->RconSocket = null;
}
$this->RconRequestId = 0;
}
public function Open( )
{
if( !$this->RconSocket )
{
$this->RconSocket = @FSockOpen( $this->Socket->Ip, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
if( $ErrNo || !$this->RconSocket )
{
throw new SourceQueryException( 'Can\'t connect to RCON server: ' . $ErrStr );
}
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
Stream_Set_Blocking( $this->RconSocket, true );
}
}
public function Write( $Header, $String = '' )
{
// Pack the packet together
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00\x00";
// Prepend packet length
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
$Length = StrLen( $Command );
return $Length === FWrite( $this->RconSocket, $Command, $Length );
}
public function Read( $Length = 1400 )
{
$this->Buffer->Set( FRead( $this->RconSocket, $Length ) );
$PacketSize = $this->Buffer->GetLong( );
$Buffer = $this->Buffer->Get( );
$Remaining = $PacketSize - StrLen( $Buffer );
while( $Remaining > 0 )
{
$Buffer2 = FRead( $this->RconSocket, $Length );
$Buffer .= $Buffer2;
$Remaining -= StrLen( $Buffer2 );
}
$this->Buffer->Set( $Buffer );
}
public function Command( $Command )
{
$this->Write( SourceQuery :: SERVERDATA_EXECCOMMAND, $Command );
$this->Read( );
$this->Buffer->GetLong( ); // RequestID
$Type = $this->Buffer->GetLong( );
if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE )
{
throw new SourceQueryException( 'Bad rcon_password.' );
}
else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
{
return false;
}
$Buffer = $this->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( $Buffer ) >= 4000 )
{
do
{
$this->Write( SourceQuery :: SERVERDATA_RESPONSE_VALUE );
$this->Read( );
$this->Buffer->GetLong( ); // RequestID
if( $this->Buffer->GetLong( ) !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
{
break;
}
$Buffer .= $this->Buffer->Get( );
}
while( false ); // TODO: This is so broken that we don't even try to read multiple times, needs to be revised
}
// TODO: It should use GetString, but there are no null bytes at the end, why?
// $Buffer = $this->Buffer->GetString( );
return $Buffer;
}
public function Authorize( $Password )
{
$this->Write( SourceQuery :: SERVERDATA_AUTH, $Password );
$this->Read( );
$RequestID = $this->Buffer->GetLong( );
$Type = $this->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 )
{
$this->Read( );
$RequestID = $this->Buffer->GetLong( );
$Type = $this->Buffer->GetLong( );
}
if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE )
{
throw new SourceQueryException( 'RCON authorization failed.' );
}
return true;
}
}