Skip to content

Commit

Permalink
Add RCON
Browse files Browse the repository at this point in the history
  • Loading branch information
Eywek committed Oct 13, 2017
1 parent d2c9aae commit eb26f15
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 64 deletions.
3 changes: 2 additions & 1 deletion app/Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function after($event = array(), $install = false, $updateContent = array
'server_cache' => 0,
'server_secretkey' => '',
'server_timeout' => 1,
'version' => '1.2.5',
'version' => '1.3.0',
'skype' => 'http://mineweb.org',
'youtube' => 'http://mineweb.org',
'twitter' => 'http://mineweb.org',
Expand Down Expand Up @@ -378,6 +378,7 @@ public function after($event = array(), $install = false, $updateContent = array
'ip' => array('type' => 'string', 'null' => false, 'length' => 120, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'port' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 5, 'unsigned' => false),
'type' => array('type' => 'integer', 'null' => false, 'default' => 0, 'length' => 1, 'unsigned' => false),
'port' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 255, 'unsigned' => false),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
),
Expand Down
87 changes: 68 additions & 19 deletions app/Controller/Component/ServerComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function parseResult($result)
return $methods;
}

public function call($methods = false, $server_id = false, $debug = false)
public function call($methods = [], $server_id = false, $debug = false)
{
$multi = true;
if (!$server_id)
Expand All @@ -67,18 +67,6 @@ public function call($methods = false, $server_id = false, $debug = false)
}
$config = $this->getConfig($server_id);

if ($config['type'] == 1) { // only ping
// ping
$ping = $this->ping(array('ip' => $config['ip'], 'port' => $config['port']));
// each method
$result = array();
if (!is_array($methods))
$methods = array($methods);
foreach ($methods as $methodName => $methodValue)
$result[$methodName] = (isset($ping[$methodName])) ? $ping[$methodName] : false;
return $result;
}

if (!is_array($methods)) {// transform into array
$methods = array(array($methods => array()));
$multi = false;
Expand All @@ -90,6 +78,41 @@ public function call($methods = false, $server_id = false, $debug = false)
$multi = false;
}

if ($config['type'] == 1 || $config['type'] == 2) {
$methodsName = array_map(function ($method) {
return array_keys($method)[0];
}, $methods);
$result = [];

if (in_array('RUN_COMMAND', $methodsName) && $config['type'] == 2) {
foreach ($methods as $key => $method) {
if (array_keys($method)[0] === 'RUN_COMMAND')
$result[$key]['RUN_COMMAND'] = $this->rcon(['ip' => $config['ip'], 'port' => $config['data']['rcon_port'], 'password' => $config['data']['rcon_password']], $method['RUN_COMMAND']) !== false;
}
$methodsName = array_delete_value($methodsName, 'RUN_COMMAND');
}

if (count($methodsName) > 0) {
$ping = $this->ping(array('ip' => $config['ip'], 'port' => $config['port']));
foreach ($methods as $key => $method) {
$name = array_keys($method)[0];
if (isset($ping[$name]))
$result[$key][$name] = $ping[$name];
}
}

if (!$multi) {
$parsedResult = [];
foreach ($result as $item) {
foreach ($item as $key => $value) {
$parsedResult[$key] = $value;
}
}
$result = $parsedResult;
}
return $result;
}

// plugin
$url = $this->getUrl($server_id);
$data = $this->encryptWithKey(json_encode($this->parse($methods)));
Expand Down Expand Up @@ -217,7 +240,12 @@ public function getConfig($server_id = false)
if (empty($search))
return $this->config[$server_id] = false;

return $this->config[$server_id] = array('ip' => $search['Server']['ip'], 'port' => $search['Server']['port'], 'type' => $search['Server']['type']);
return $this->config[$server_id] = array(
'ip' => $search['Server']['ip'],
'port' => $search['Server']['port'],
'type' => $search['Server']['type'],
'data' => json_decode($search['Server']['data'], true)
);
}

public function ping($config = false)
Expand All @@ -236,7 +264,24 @@ public function ping($config = false)
}
$Query->Close();

return (isset($Info['players'])) ? array('getMOTD' => $Info['description'], 'getVersion' => $Info['version']['name'], 'GET_PLAYER_COUNT' => $Info['players']['online'], 'GET_MAX_PLAYERS' => $Info['players']['max']) : false;
return (isset($Info['players'])) ? array(
'GET_MOTD' => $Info['description'],
'GET_VERSION' => $Info['version']['name'],
'GET_PLAYER_COUNT' => $Info['players']['online'],
'GET_MAX_PLAYERS' => $Info['players']['max']) : false;
}

public function rcon($config = false, $cmd = '')
{
if (!$config || !isset($config['ip']) || !isset($config['port']) || !isset($config['password']))
return false;

App::import('Vendor', 'Rcon', array('file' => 'rcon/Rcon.php'));

$rcon = new Thedudeguy\Rcon($config['ip'], $config['port'], $config['password'], $this->getTimeout());
if ($rcon->connect())
return $rcon->sendCommand($cmd);
return false;
}

public function getUrl($server_id)
Expand Down Expand Up @@ -265,8 +310,8 @@ public function online($server_id = false, $debug = false)
if (!$config) // server not found
return $this->online[$server_id] = false;

if ($config['type'] == 1) // ping only
return ($this->ping(array('ip' => $config['ip'], 'port' => $config['port']))) ? true : false;
if ($config['type'] == 1 || $config['type'] == 2) // ping only
return $this->online[$server_id] = ($this->ping(array('ip' => $config['ip'], 'port' => $config['port']))) ? true : false;

list($return, $code, $error) = $this->request($this->getUrl($server_id), $this->encryptWithKey("[]"));
if ($return && $code === 200)
Expand Down Expand Up @@ -375,7 +420,7 @@ public function banner_infos($serverId = false)
'GET_MAX_PLAYERS' => 0
);
foreach ($serverId as $id) {
$req = $this->call(array('GET_PLAYER_COUNT' => array(), 'GET_MAX_PLAYERS' => array()), $id);
$req = $this->call(['GET_PLAYER_COUNT' => [], 'GET_MAX_PLAYERS' => []], $id);
if (!$req) continue;
$data['GET_PLAYER_COUNT'] += intval($req['GET_PLAYER_COUNT']);
$data['GET_MAX_PLAYERS'] += intval($req['GET_MAX_PLAYERS']);
Expand All @@ -393,7 +438,11 @@ public function banner_infos($serverId = false)
public function userIsConnected($username, $server_id = false)
{
$result = $this->call(['IS_CONNECTED' => $username], $server_id);
return ($result && isset($result['IS_CONNECTED']) && $result['IS_CONNECTED']);
if ($result && isset($result['IS_CONNECTED']) && $result['IS_CONNECTED'])
return true;
else if (!isset($result['IS_CONNECTED']))
return true;
return false;
}

public function send_command($cmd, $server_id = false)
Expand Down
16 changes: 14 additions & 2 deletions app/Controller/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function admin_link()
}
}

foreach ($servers as $key => $value)
$servers[$key]['Server']['data'] = json_decode($servers[$key]['Server']['data'], true);

$bannerMsg = $this->Lang->get('SERVER__STATUS_MESSAGE');

$this->set(compact('servers', 'bannerMsg'));
Expand Down Expand Up @@ -195,6 +198,9 @@ public function admin_link_ajax()
else if ($this->request->data['type'] == 1) {
if (!$this->Server->ping(array('ip' => $this->request->data['host'], 'port' => $this->request->data['port'])))
return $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SERVER__LINK_FAILED'))));
} else if ($this->request->data['type'] == 2) {
if (!$this->Server->rcon(array('ip' => $this->request->data['host'], 'port' => $this->request->data['server_data']['rcon_port'], 'password' => $this->request->data['server_data']['rcon_password']), 'say ' . $this->Lang->get('SERVER__LINK_SUCCESS')))
return $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SERVER__LINK_FAILED'))));
} else {
return $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
}
Expand All @@ -205,10 +211,16 @@ public function admin_link_ajax()

$this->loadModel('Server');
$this->Server->read(null, $id);
$this->Server->set(array('name' => $this->request->data['name'], 'ip' => $this->request->data['host'], 'port' => $this->request->data['port'], 'type' => $this->request->data['type']));
$this->Server->set(array(
'name' => $this->request->data['name'],
'ip' => $this->request->data['host'],
'port' => $this->request->data['port'],
'type' => $this->request->data['type'],
'data' => json_encode($this->request->data['server_data'])
));
$this->Server->save();

if ($this->request->data['type'] != 1 && $secretKey)
if ($this->request->data['type'] == 0 && isset($secretKey) && $secretKey)
$this->Configuration->setKey('server_secretkey', $secretKey);

return $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SERVER__LINK_SUCCESS'))));
Expand Down
4 changes: 0 additions & 4 deletions app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,6 @@ function admin_edit_ajax()
$data['money'] = $this->request->data['money'];
}

if ($this->EyPlugin->isInstalled('eywek.vote.3')) {
$data['vote'] = $this->request->data['vote'];
}

$event = new CakeEvent('beforeEditUser', $this, array('user_id' => $findUser['User']['id'], 'data' => $data, 'password_updated' => $password_updated));
$this->getEventManager()->dispatch($event);
if ($event->isStopped()) {
Expand Down
30 changes: 18 additions & 12 deletions app/Model/Server.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php
class Server extends AppModel {

public function findSelectableServers() {
$search_servers = $this->find('all', array('conditions' => array('type' => 0)));
if (empty($search_servers))
return array();

$servers = array();
foreach ($search_servers as $server)
$servers[$server['Server']['id']] = $server['Server']['name'];
return $servers;
}

class Server extends AppModel
{

public function findSelectableServers($rcon = true)
{
$types = [0];
if ($rcon)
$types = [0, 2];
$search_servers = $this->find('all', array('conditions' => array('type' => $types)));
if (empty($search_servers))
return array();

$servers = array();
foreach ($search_servers as $server)
$servers[$server['Server']['id']] = $server['Server']['name'];
return $servers;
}

}
2 changes: 1 addition & 1 deletion app/Plugin/Shop
Submodule Shop updated from 879673 to 482851
2 changes: 1 addition & 1 deletion app/Plugin/Vote
Submodule Vote updated from bc5b6e to d868dc
61 changes: 46 additions & 15 deletions app/View/Server/admin_link.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<select class="form-control" name="type">
<option value="0"<?= ($value['Server']['type'] == '0') ? ' selected' : '' ?>><?= $Lang->get('SERVER__TYPE_DEFAULT') ?></option>
<option value="1"<?= ($value['Server']['type'] == '1') ? ' selected' : '' ?>><?= $Lang->get('SERVER__TYPE_QUERY') ?></option>
<option value="2"<?= ($value['Server']['type'] == '2') ? ' selected' : '' ?>><?= $Lang->get('SERVER__TYPE_RCON') ?></option>
</select>
</div>

Expand All @@ -87,9 +88,21 @@

<div class="form-group">
<label><?= $Lang->get('SERVER__PORT') ?></label>
<input type="text" class="form-control" name="port" value="<?= $value['Server']['port'] ?>" placeholder="Ex: 8080">
<input type="text" class="form-control" name="port" value="<?= $value['Server']['port'] ?>" placeholder="Ex: 25565">
</div>

<?php if ($value['Server']['type'] == '2'): ?>
<div class="form-group">
<label><?= $Lang->get('SERVER__RCON_PORT') ?></label>
<input type="text" class="form-control" name="server_data[rcon_port]" value="<?= $value['Server']['data']['rcon_port'] ?>" placeholder="Ex: 25575">
</div>

<div class="form-group">
<label><?= $Lang->get('SERVER__RCON_PASSWORD') ?></label>
<input type="password" class="form-control" name="server_data[rcon_password]" value="<?= $value['Server']['data']['rcon_password'] ?>" placeholder="**********">
</div>
<?php endif; ?>

<button type="submit" class="btn btn-success"><?= $Lang->get('GLOBAL__SUBMIT') ?></button>
<a href="<?= $this->Html->url(array('controller' => 'server', 'action' => 'delete', 'admin' => true, $value['Server']['id'])) ?>" type="submit" class="btn btn-danger"><?= $Lang->get('GLOBAL__DELETE') ?></a>

Expand Down Expand Up @@ -134,21 +147,38 @@ function initSelectInfos() {
})
}
$('select[name="type"]').each(function () {
selectInfos($(this))
selectInfos($(this), true)
})
function selectInfos(select) {
var type = select.val()

var infosDiv = select.parent().find('.infos-type')
if (infosDiv)
infosDiv.remove()

if (type == 0)
var infos = '<div class="alert alert-info"><?= addslashes($Lang->get('SERVER__TYPE_DEFAULT_INFOS')) ?></div>'
else
var infos = '<div class="alert alert-info"><?= addslashes($Lang->get('SERVER__TYPE_QUERY_INFOS')) ?></div>'
function selectInfos(select, init) {
var type = select.val()

var infosDiv = select.parent().find('.infos-type')
if (infosDiv)
infosDiv.remove()

if (type == 0) {
var infos = '<div class="alert alert-info"><?= addslashes($Lang->get('SERVER__TYPE_DEFAULT_INFOS')) ?></div>'
select.parent().parent().find('input[name="server_data[rcon_port]"]').parent().remove()
select.parent().parent().find('input[name="server_data[rcon_password]"]').parent().remove()
} else if (type == 1) {
var infos = '<div class="alert alert-info"><?= addslashes($Lang->get('SERVER__TYPE_QUERY_INFOS')) ?></div>'
select.parent().parent().find('input[name="server_data[rcon_port]"]').parent().remove()
select.parent().parent().find('input[name="server_data[rcon_password]"]').parent().remove()
} else if (type == 2) {
var infos = '<div class="alert alert-info"><?= addslashes($Lang->get('SERVER__TYPE_RCON_INFOS')) ?></div>'
var new_server = '<div class="form-group">';
new_server += '<label><?= $Lang->get('SERVER__RCON_PORT') ?></label>';
new_server += '<input type="text" class="form-control" name="server_data[rcon_port]" placeholder="Ex: 25575">';
new_server += '</div>';
new_server += '<div class="form-group">';
new_server += '<label><?= $Lang->get('SERVER__RCON_PASSWORD') ?></label>';
new_server += '<input type="password" class="form-control" name="server_data[rcon_password]" placeholder="**********">';
new_server += '</div>';
if (!init)
$(new_server).insertBefore($(select.parent().parent().find('button')[0]))
}

$('<div class="infos-type"><br>' + infos + '</div>').insertAfter(select)
$('<div class="infos-type"><br>' + infos + '</div>').insertAfter(select)
}

var i = 0;
Expand All @@ -168,6 +198,7 @@ $("#add_server").click(function() {
new_server += '<select class="form-control" name="type">';
new_server += '<option value="0"><?= $Lang->get('SERVER__TYPE_DEFAULT') ?></option>';
new_server += '<option value="1"><?= $Lang->get('SERVER__TYPE_QUERY') ?></option>';
new_server += '<option value="2"><?= $Lang->get('SERVER__TYPE_RCON') ?></option>';
new_server +='</select>';
new_server += '</div>';
new_server += '<div class="form-group">';
Expand All @@ -180,7 +211,7 @@ $("#add_server").click(function() {
new_server += '</div>';
new_server += '<div class="form-group">';
new_server += '<label><?= $Lang->get('SERVER__PORT') ?></label>';
new_server += '<input type="text" class="form-control" name="port" placeholder="Ex: 8080">';
new_server += '<input type="text" class="form-control" name="port" placeholder="Ex: 25565">';
new_server += '</div>';
new_server += '<button type="submit" class="btn btn-success"><?= $Lang->get('GLOBAL__SUBMIT') ?></button>';
new_server += '</form>';
Expand Down
8 changes: 0 additions & 8 deletions app/View/User/admin_edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@
</div>
<?php } ?>

<?php if($EyPlugin->isInstalled('eywek.vote.3')) { ?>
<div class="form-group">
<label><?= $Lang->get('VOTE__TITLE_ACTION') ?></label>
<input name="vote" class="form-control" value="<?= $search_user['vote'] ?>" type="text">
</div>
<?php } ?>


<div class="form-group">
<label>IP</label>
<input class="form-control" value="<?= $search_user['ip'] ?>" type="text" disabled="">
Expand Down
6 changes: 5 additions & 1 deletion lang/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,14 @@
"SERVER__TYPE": "Type de la connexion",
"SERVER__TYPE_DEFAULT": "Par défaut",
"SERVER__TYPE_QUERY": "Ping (pas besoin de plugin)",
"SERVER__TYPE_RCON": "RCON (pas besoin de plugin)",
"SERVER__TYPE_DEFAULT_INFOS": "Vous avez besoin d'installer le plugin MineWebBridge sur votre serveur pour utiliser ce type de liaison. Toutes les informations relatives à cette manipulation sont disponible sur <a href=\"http://docs.mineweb.org/#lier-serveur-site\">la documentation</a>",
"SERVER__TYPE_QUERY_INFOS": "Vous n'avez pas besoin d'installer le plugin MineWebBridge sur votre serveur pour utiliser ce type de liaison. Ce type de liaison vous permet seulement d'afficher le nombre de connectés sur votre infrastructure. Toutes les informations relatives à cette manipulation sont disponible sur <a href=\"http://docs.mineweb.org/#lier-serveur-site\">la documentation</a>",
"SERVER__TYPE_RCON_INFOS": "some explanation",
"SERVER__HOST":"IP du serveur",
"SERVER__PORT":"Port",
"SERVER__RCON_PASSWORD": "Mot de passe RCON",
"SERVER__RCON_PORT": "Port RCON",
"SERVER__TIMEOUT":"Temps maximum d'éxécution",
"SERVER__LINK_SUCCESS":"La connexion au serveur a bien été établie !",
"SERVER__LINK_ERROR_MINEWEB_DOWN":"L'API de MineWeb est indisponible.",
Expand Down Expand Up @@ -514,7 +518,7 @@
"SERVER__MUST_BE_ON":"Le serveur doit être allumé pour effectuer cette action.",
"SERVER__STATUS_OFF":"Le serveur est éteint",
"SERVER__DELETE_SERVER_SUCCESS":"Le serveur a bien été supprimé !",
"SERVER__STATUS_MESSAGE":"{MOTD} - {VERSION}. Il y a {ONLINE} connectés sur {ONLINE_LIMIT}.",
"SERVER__STATUS_MESSAGE":"Il y a {ONLINE} connectés sur {ONLINE_LIMIT}.",
"SERVER__PARSE_NEW_COMMAND":"Pour une nouvelle commande",
"SERVER__SUCCESS_SWITCH":"Vous avez bien changé l'état de la fonctionnalité serveur.",
"SERVER__DISABLE_SERVER":"Désactiver la fonctionnalité",
Expand Down

0 comments on commit eb26f15

Please sign in to comment.