This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David
committed
Jan 22, 2016
1 parent
9beb5a0
commit 0356bc7
Showing
8 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 David Cole <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,27 @@ | |
|
||
CS:GO Rcon client made in PHP. | ||
|
||
### Usage | ||
|
||
```php | ||
<?php | ||
|
||
use Reflex\Rcon\Rcon; | ||
|
||
$rcon = new Rcon(':ip_address', :port, ':rcon_password'); | ||
$rcon->connect(); | ||
|
||
// Set the socket timeout if you want to, defaults to 2 seconds. | ||
$rcon->setTimeout(:seconds); | ||
|
||
// Execute a rcon command | ||
$rcon->exec('say PHP!'); | ||
``` | ||
|
||
### License | ||
|
||
The code in this repository is subject to the MIT license which can be found in the `LICENSE` file in this repository. | ||
|
||
### Credits | ||
|
||
- [David Cole](mailto:[email protected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "team-reflex/rcon-php", | ||
"description": "CS:GO Rcon client made in PHP.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "David Cole", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Reflex\\Rcon\\": "src/Rcon" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Reflex\Rcon\Exceptions; | ||
|
||
class NotAuthenticatedException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Reflex\Rcon\Exceptions; | ||
|
||
class RconAuthException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Reflex\Rcon\Exceptions; | ||
|
||
class RconConnectException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
|
||
namespace Reflex\Rcon; | ||
|
||
use Reflex\Rcon\Exceptions\NotAuthenticatedException; | ||
use Reflex\Rcon\Exceptions\RconAuthException; | ||
use Reflex\Rcon\Exceptions\RconConnectException; | ||
|
||
class Rcon | ||
{ | ||
const SERVERDATA_AUTH = 3; | ||
const SERVERDATA_EXECCOMMAND = 2; | ||
|
||
/** | ||
* The Rcon server IP. | ||
* | ||
* @var string | ||
*/ | ||
protected $ip; | ||
|
||
/** | ||
* The Rcon server port. | ||
* | ||
* @var integer | ||
*/ | ||
protected $port; | ||
|
||
/** | ||
* The Rcon password. | ||
* | ||
* @var string | ||
*/ | ||
protected $password; | ||
|
||
/** | ||
* The Rcon socket. | ||
* | ||
* @var resource | ||
*/ | ||
protected $socket; | ||
|
||
/** | ||
* The current packet ID. | ||
* | ||
* @var integer | ||
*/ | ||
protected $packetID; | ||
|
||
/** | ||
* Is the client connected to the server? | ||
* | ||
* @var boolean | ||
*/ | ||
public $connected = false; | ||
|
||
/** | ||
* Constructs the Rcon client. | ||
* | ||
* @param string $ip | ||
* @param int $port | ||
* @param string $password | ||
* @return void | ||
*/ | ||
public function __construct($ip, $port, $password) | ||
{ | ||
$this->ip = $ip; | ||
$this->port = $port; | ||
$this->password = $password; | ||
} | ||
|
||
/** | ||
* Connects and authenticates with the CS:GO server. | ||
* | ||
* @return boolean | ||
*/ | ||
public function connect() | ||
{ | ||
$socket = stream_socket_client("tcp://{$this->ip}:{$this->port}", $errno, $errstr); | ||
|
||
stream_set_timeout($socket, 2, 500); | ||
|
||
if (!$socket) { | ||
throw new RconConnectException("Error while connecting to the Rcon server: {$errstr}"); | ||
} | ||
|
||
$this->socket = $socket; | ||
|
||
$this->write(self::SERVERDATA_AUTH, $this->password); | ||
|
||
$read = $this->read(); | ||
if ($read[1]['ID'] == -1) { | ||
throw new RconAuthException('Authentication to the Rcon server failed.'); | ||
} | ||
|
||
$this->connected = true; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Sets the timeout on the socket in seconds. | ||
* | ||
* @param integer $timeout | ||
* @return boolean | ||
*/ | ||
public function setTimeout($timeout = 2) | ||
{ | ||
stream_set_timeout($this->stream, $timeout); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Executes a command on the server. | ||
* | ||
* @param string $command | ||
* @return string | ||
*/ | ||
public function exec($command) | ||
{ | ||
if (!$this->connected) { | ||
throw new NotAuthenticatedException('Client has not connected to the Rcon server.'); | ||
} | ||
|
||
$this->write(self::SERVERDATA_EXECCOMMAND, $command); | ||
|
||
return $this->read()[0]['S1']; | ||
} | ||
|
||
/** | ||
* Writes to the socket. | ||
* | ||
* @param integer $type | ||
* @param string $s1 | ||
* @param string $s2 | ||
* @return integer | ||
*/ | ||
public function write($type, $s1 = '', $s2 = '') | ||
{ | ||
$id = $this->packetID++; | ||
|
||
$data = pack('VV', $id, $type); | ||
$data .= $s1.chr(0).$s2.chr(0); | ||
$data = pack('V', strlen($data)).$data; | ||
|
||
fwrite($this->socket, $data, strlen($data)); | ||
|
||
return $id; | ||
} | ||
|
||
/** | ||
* Reads from the socket. | ||
* | ||
* @return array | ||
*/ | ||
public function read() | ||
{ | ||
$rarray = []; | ||
$count = 0; | ||
|
||
while ($data = fread($this->socket, 4)) { | ||
$data = unpack('V1Size', $data); | ||
|
||
if ($data['Size'] > 4096) { | ||
$packet = ''; | ||
for ($i = 0; $i < 8; $i++) { | ||
$packet .= "\x00"; | ||
} | ||
$packet .= fread($this->socket, 4096); | ||
} else { | ||
$packet = fread($this->socket, $data['Size']); | ||
} | ||
|
||
$rarray[] = unpack('V1ID/V1Response/a*S1/a*S2', $packet); | ||
} | ||
|
||
return $rarray; | ||
} | ||
|
||
/** | ||
* Closes the socket. | ||
* | ||
* @return boolean | ||
*/ | ||
public function close() | ||
{ | ||
if (!$this->connected) { | ||
return false; | ||
} | ||
|
||
$this->connected = false; | ||
fclose($this->socket); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Alias for close(). | ||
* | ||
* @return boolean | ||
*/ | ||
public function disconnect() | ||
{ | ||
return $this->close(); | ||
} | ||
} |