lundi 16 février 2009, par Zyzko
Ce petit script PHP permet d’envoyer des commandes RCON à un serveur HL2 Source.
rcon2_class.php
<?php
/*
CS:S Rcon PHP class - code by 1FO|zyzko 01/12/2006
www.1formatik.com
--------------------------------------------------
*/
define("SERVERDATA_EXECCOMMAND",2);
define("SERVERDATA_AUTH",3);
class RCon {
var $Password;
var $Host;
var $Port = 27015;
var $_Sock = null;
var $_Id = 0;
function RCon ($Host,$Port,$Password) {
$this->Password = $Password;
$this->Host = $Host;
$this->Port = $Port;
$this->_Sock = @fsockopen($this->Host,$this->Port, $errno, $errstr, 30) or
die("Impossible d'ouvrir le socket: $errstr ($errno)\n");
$this->_Set_Timeout($this->_Sock,2,500);
}
function Auth () {
$PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);
$ret = $this->_PacketRead();
if ($ret[1]['id'] == -1) {
die("Erreur\n");
}
}
function _Set_Timeout(&$res,$s,$m=0) {
if (version_compare(phpversion(),'4.3.0','<')) {
return socket_set_timeout($res,$s,$m);
}
return stream_set_timeout($res,$s,$m);
}
function _Write($cmd, $s1='', $s2='') {
$id = ++$this->_Id;
$data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
$data = pack("V",strlen($data)).$data;
fwrite($this->_Sock,$data,strlen($data));
return $id;
}
function _PacketRead() {
$retarray = array();
while ($size = @fread($this->_Sock,4)) {
$size = unpack('V1Size',$size);
if ($size["Size"] > 4096) {
$packet = "\x00\x00\x00\x00\x00\x00\x00\x00".fread($this->_Sock,4096);
} else {
$packet = fread($this->_Sock,$size["Size"]);
}
array_push($retarray,unpack("V1ID/V1Reponse/a*S1/a*S2",$packet));
}
return $retarray;
}
function Read() {
$Packets = $this->_PacketRead();
foreach($Packets as $pack) {
if (isset($ret[$pack['ID']])) {
$ret[$pack['ID']]['S1'] .= $pack['S1'];
$ret[$pack['ID']]['S2'] .= $pack['S1'];
} else {
$ret[$pack['ID']] = array(
'Reponse' => $pack['Reponse'],
'S1' => $pack['S1'],
'S2' => $pack['S2'],
);
}
}
return $ret;
}
function sendCommand($Command) {
$Command = '"'.trim(str_replace(' ','" "', $Command)).'"';
$this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
}
function rconCommand($Command) {
$this->sendcommand($Command);
$ret = $this->Read();
return $ret[0]['S1'];
}
}
?>
demo.php
<?php
/*
CS:S Rcon PHP exemple - code by 1FO|zyzko 01/12/2006
www.1formatik.com
--------------------------------------------------
*/
if ( (!isset($_POST["IP"]))&&(!isset($_POST["Port"]))&&(!isset($_POST["Pass"]))&&(!isset($_POST["Rcon"])) )
{
echo "Veuillez remplir l'ensemble des champs texte.</br>";
echo '<form name="RconForm" method="post" action="./rcon.php">';
echo 'IP : <input type="text" name="IP" value=""></br>';
echo 'Port : <input type="text" name="Port" value=""></br>';
echo 'Pass Admin : <input type="password" name="Pass" value=""></br>';
echo 'Commande <a href ="http://www.hl2world.com/wiki/index.php/CSS_Server_Commands" target="_blank">Rcon</a> : ';
echo '<input type="text" name="Rcon" value=""></br>';
echo '<input type="submit" name="Submit" value="Envoyer">';
echo '</form>';
}
else
{
$IP = $_POST["IP"];
$Pass = $_POST["Pass"];
$Port = $_POST["Port"];
$Rcon = $_POST["Rcon"];
echo "<div style=\"visibility:hidden\">";
include_once("rcon2_class.php");
$r = new rcon("$IP",$Port,"$Pass");
$r->Auth();
var_dump($r->rconCommand("$Rcon"));
echo "</div>";
echo "Commande rcon \"$Rcon\" envoyée avec succès au serveur $IP:$Port\n";
echo '<form name="RconForm" method="post" action="./rcon.php">';
echo 'IP : <input type="text" name="IP" value=""></br>';
echo 'Port : <input type="text" name="Port" value=""></br>';
echo 'Pass Admin : <input type="password" name="Pass" value=""></br>';
echo 'Commande <a href="http://www.hl2world.com/wiki/index.php/CSS_Server_Commands" target="_blank">Rcon</a> : ';
echo '<input type="text" name="Rcon" value=""></br>';
echo '<input type="submit" name="Submit" value="Envoyer">';
echo '</form>';
}
?>