2015-06-21 20:43:18 +02:00
|
|
|
<?php
|
2017-03-26 19:44:55 +02:00
|
|
|
|
2015-06-22 20:22:17 +02:00
|
|
|
namespace ManiaControl\Communication;
|
2015-06-21 20:43:18 +02:00
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\Callbacks;
|
2015-06-21 20:43:18 +02:00
|
|
|
use ManiaControl\Callbacks\Listening;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2015-06-21 21:33:45 +02:00
|
|
|
use ManiaControl\Logger;
|
2015-06-21 20:43:18 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
2015-06-21 21:33:45 +02:00
|
|
|
use ManiaControl\Settings\Setting;
|
|
|
|
use ManiaControl\Settings\SettingManager;
|
2015-06-21 20:43:18 +02:00
|
|
|
use React\EventLoop\Factory;
|
2015-06-21 20:45:48 +02:00
|
|
|
use React\EventLoop\LoopInterface;
|
|
|
|
use React\Socket\Connection;
|
|
|
|
use React\Socket\ConnectionException;
|
|
|
|
use React\Socket\Server;
|
2015-06-21 20:43:18 +02:00
|
|
|
|
2015-06-21 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Class for managing Socket Callbacks
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2019-01-05 21:02:24 +01:00
|
|
|
* @copyright 2014-2019 ManiaControl Team
|
2015-06-21 20:45:48 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class CommunicationManager implements CallbackListener, UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2015-06-22 22:52:26 +02:00
|
|
|
/** Constants */
|
|
|
|
const SETTING_SOCKET_ENABLED = "Activate Socket";
|
|
|
|
const SETTING_SOCKET_PASSWORD = "Password for the Socket Connection";
|
|
|
|
const SETTING_SOCKET_PORT = "Socket Port for Server ";
|
|
|
|
|
|
|
|
const ENCRYPTION_IV = "kZ2Kt0CzKUjN2MJX";
|
|
|
|
const ENCRYPTION_METHOD = "aes-192-cbc";
|
2015-06-21 20:43:18 +02:00
|
|
|
|
|
|
|
/** @var ManiaControl $maniaControl */
|
|
|
|
private $maniaControl = null;
|
2015-06-21 20:45:48 +02:00
|
|
|
|
|
|
|
/** @var LoopInterface $loop */
|
|
|
|
private $loop = null;
|
|
|
|
|
2015-06-22 20:22:17 +02:00
|
|
|
/** @var Listening[] $communicationListenings */
|
|
|
|
private $communicationListenings = array();
|
2015-06-21 20:43:18 +02:00
|
|
|
|
2015-06-21 20:45:48 +02:00
|
|
|
/** @var Server $socket */
|
|
|
|
private $socket = null;
|
2015-06-22 22:52:26 +02:00
|
|
|
/** @var Communication[] $communcations */
|
|
|
|
private $communications = array();
|
2015-06-21 20:45:48 +02:00
|
|
|
|
2015-06-21 20:43:18 +02:00
|
|
|
/**
|
2015-06-22 20:22:17 +02:00
|
|
|
* Create a new Communication Handler Instance
|
2015-06-21 20:43:18 +02:00
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings');
|
2015-06-22 20:22:17 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'initCommunicationManager');
|
2015-06-26 16:53:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONRESTART, $this, 'onShutDown');
|
2015-06-22 22:52:26 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONSHUTDOWN, $this, 'onShutDown');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Communication to another ManiaControl
|
|
|
|
*
|
|
|
|
* @param $ip
|
|
|
|
* @param $port
|
|
|
|
* @return \ManiaControl\Communication\Communication
|
|
|
|
*/
|
|
|
|
public function createCommunication($ip, $port, $encryptionKey) {
|
|
|
|
$communication = new Communication($ip, $port, $encryptionKey);
|
|
|
|
$communication->createConnection();
|
|
|
|
|
|
|
|
$this->communications[] = $communication;
|
|
|
|
return $communication;
|
2015-06-21 20:43:18 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
|
2015-06-22 22:52:26 +02:00
|
|
|
/**
|
|
|
|
* Closes a opened Communication
|
|
|
|
* Does not necessarily need be called, all connections get destroyed on ManiaControl Shutdown
|
|
|
|
*
|
|
|
|
* @param Communication $communication
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function closeCommunication($communication) {
|
|
|
|
$key = array_search($communication, $this->communications);
|
|
|
|
if (isset($this->communications[$key])) {
|
|
|
|
$this->communications[$key]->closeConnection();
|
|
|
|
unset($this->communications[$key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Close all Sockets on maniaControl Shutdown */
|
|
|
|
public function onShutDown() {
|
2015-06-26 16:53:41 +02:00
|
|
|
if ($this->socket && $this->socket->master) {
|
|
|
|
//Stop the Socket Listening
|
|
|
|
$this->socket->shutdown();
|
|
|
|
$this->socket = null;
|
|
|
|
}
|
|
|
|
|
2015-06-22 22:52:26 +02:00
|
|
|
foreach ($this->communications as $communication) {
|
|
|
|
$this->closeCommunication($communication);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 20:45:48 +02:00
|
|
|
/**
|
2015-06-22 20:22:17 +02:00
|
|
|
* Register a new Communication Listener
|
2015-06-21 20:45:48 +02:00
|
|
|
*
|
2017-05-21 20:30:26 +02:00
|
|
|
* @param string $communicationName
|
2015-06-22 20:22:17 +02:00
|
|
|
* @param CommunicationListener $listener
|
|
|
|
* @param string $method
|
2015-06-21 20:45:48 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-05-21 20:30:26 +02:00
|
|
|
public function registerCommunicationListener($communicationName, CommunicationListener $listener, $method) {
|
2015-06-21 20:45:48 +02:00
|
|
|
if (!Listening::checkValidCallback($listener, $method)) {
|
|
|
|
$listenerClass = get_class($listener);
|
2017-05-21 20:30:26 +02:00
|
|
|
trigger_error("Given Listener '{$listenerClass}' can't handle Callback '{$communicationName}': No callable Method '{$method}'!");
|
2015-06-21 20:45:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-21 20:30:26 +02:00
|
|
|
if (!array_key_exists($communicationName, $this->communicationListenings)) {
|
|
|
|
$this->communicationListenings[$communicationName] = new Listening($listener, $method);
|
2015-06-21 20:45:48 +02:00
|
|
|
} else {
|
|
|
|
//TODO say which is already listening and other stuff
|
2015-06-22 20:22:17 +02:00
|
|
|
trigger_error("Only one Listener can listen on a specific Communication Message");
|
2015-06-21 20:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a specific Callback
|
|
|
|
*
|
|
|
|
* @param mixed $callback
|
|
|
|
*/
|
2015-06-22 20:22:17 +02:00
|
|
|
public function triggerCommuncationCallback($callbackName) {
|
|
|
|
if (!array_key_exists($callbackName, $this->communicationListenings)) {
|
2015-06-21 20:45:48 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = func_get_args();
|
|
|
|
$params = array_slice($params, 1, null, true);
|
|
|
|
|
2015-06-22 20:22:17 +02:00
|
|
|
$listening = $this->communicationListenings[$callbackName];
|
2015-06-21 20:45:48 +02:00
|
|
|
/** @var Listening $listening */
|
|
|
|
return $listening->triggerCallbackWithParams($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-22 20:22:17 +02:00
|
|
|
* Unregister a Communication Listener
|
2015-06-21 20:45:48 +02:00
|
|
|
*
|
2015-06-22 20:22:17 +02:00
|
|
|
* @param CommunicationListener $listener
|
2015-06-21 20:45:48 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-06-22 20:22:17 +02:00
|
|
|
public function unregisterCommunicationListener(CommunicationListener $listener) {
|
|
|
|
return $this->removeCommunicationListener($this->communicationListenings, $listener);
|
2015-06-21 20:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-22 20:22:17 +02:00
|
|
|
* Remove the Communication Listener from the given Listeners Array
|
2015-06-21 20:45:48 +02:00
|
|
|
*
|
2015-06-22 20:22:17 +02:00
|
|
|
* @param Listening[] $listeningsArray
|
|
|
|
* @param CommunicationListener $listener
|
2015-06-21 20:45:48 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-06-22 20:22:17 +02:00
|
|
|
private function removeCommunicationListener(array &$listeningsArray, CommunicationListener $listener) {
|
2015-06-21 20:45:48 +02:00
|
|
|
$removed = false;
|
2017-05-21 20:30:26 +02:00
|
|
|
|
|
|
|
foreach ($listeningsArray as $key => &$listening) {
|
|
|
|
if($listening->listener === $listener){
|
|
|
|
unset($listeningsArray[$key]);
|
2015-06-21 20:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
/**
|
2015-06-22 20:22:17 +02:00
|
|
|
* Inits the Communication Manager after ManiaControl Startup
|
2015-06-21 21:33:45 +02:00
|
|
|
*/
|
2015-06-22 20:22:17 +02:00
|
|
|
public function initCommunicationManager() {
|
2015-06-21 21:33:45 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_ENABLED, false);
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PASSWORD, "");
|
|
|
|
|
|
|
|
$servers = $this->maniaControl->getServer()->getAllServers();
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PORT . $server->login, 31500 + $server->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-22 22:52:26 +02:00
|
|
|
$this->createListeningSocket();
|
2015-06-21 21:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update Setting
|
|
|
|
*
|
|
|
|
* @param Setting $setting
|
|
|
|
*/
|
|
|
|
public function updateSettings(Setting $setting) {
|
|
|
|
if (!$setting->belongsToClass($this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
|
|
|
|
|
|
|
|
if ($socketEnabled && !$this->socket) {
|
2015-06-22 22:52:26 +02:00
|
|
|
$this->createListeningSocket();
|
2015-06-21 21:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$socketEnabled) {
|
2015-06-23 18:17:03 +02:00
|
|
|
$this->socket = null;
|
2015-06-21 21:33:45 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-21 20:45:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates The Socket
|
|
|
|
*/
|
2015-06-22 22:52:26 +02:00
|
|
|
private function createListeningSocket() {
|
2015-06-21 21:33:45 +02:00
|
|
|
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
|
|
|
|
if ($socketEnabled) {
|
|
|
|
|
2015-06-22 20:22:17 +02:00
|
|
|
Logger::log("[CommunicationManager] Trying to create Socket");
|
2015-06-21 21:33:45 +02:00
|
|
|
|
|
|
|
// Check for MySQLi
|
2015-06-22 20:22:17 +02:00
|
|
|
$message = '[CommunicationManager] Checking for installed openssl ... ';
|
2015-06-21 21:33:45 +02:00
|
|
|
if (!extension_loaded('openssl')) {
|
|
|
|
Logger::log($message . 'NOT FOUND!');
|
|
|
|
Logger::log(" -- You don't have openssl installed! Check: http://www.php.net/manual/en/openssl.installation.php");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Logger::log($message . 'FOUND!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$serverLogin = $this->maniaControl->getServer()->login;
|
|
|
|
$socketPort = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PORT . $serverLogin);
|
2015-06-22 22:52:26 +02:00
|
|
|
$password = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PASSWORD);
|
2015-06-21 21:33:45 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->loop = Factory::create();
|
|
|
|
$this->socket = new Server($this->loop);
|
|
|
|
|
|
|
|
$this->socket->on('error', function ($e) {
|
2015-06-22 20:22:17 +02:00
|
|
|
Logger::log("[CommunicationManager] Socket Error" . $e);
|
2015-06-21 21:33:45 +02:00
|
|
|
});
|
|
|
|
|
2015-06-22 20:22:17 +02:00
|
|
|
$this->socket->on('connection', function (Connection $connection) use ($password) {
|
2015-06-21 21:33:45 +02:00
|
|
|
$buffer = '';
|
2015-06-22 20:22:17 +02:00
|
|
|
$connection->on('data', function ($data) use (&$buffer, &$connection, $password) {
|
2015-06-21 21:33:45 +02:00
|
|
|
$buffer .= $data;
|
2017-03-26 19:44:55 +02:00
|
|
|
$arr = explode("\n", $buffer, 2);
|
2015-06-21 21:33:45 +02:00
|
|
|
while (count($arr) == 2 && strlen($arr[1]) >= (int) $arr[0]) {
|
|
|
|
// received full message
|
|
|
|
$len = (int) $arr[0];
|
|
|
|
$msg = substr($arr[1], 0, $len); // clip msg
|
|
|
|
$buffer = substr($buffer, strlen((string) $len) + 1 /* newline */ + $len); // clip buffer
|
|
|
|
|
|
|
|
// Decode Message
|
2015-06-22 22:52:26 +02:00
|
|
|
$data = openssl_decrypt($msg, self::ENCRYPTION_METHOD, $password, OPENSSL_RAW_DATA, self::ENCRYPTION_IV);
|
2015-06-21 21:33:45 +02:00
|
|
|
$data = json_decode($data);
|
|
|
|
|
|
|
|
if ($data == null) {
|
|
|
|
$data = array("error" => true, "data" => "Data is not provided as an valid AES-196-encrypted encrypted JSON");
|
|
|
|
} else if (!property_exists($data, "method") || !property_exists($data, "data")) {
|
|
|
|
$data = array("error" => true, "data" => "Invalid Message");
|
2015-06-21 20:45:48 +02:00
|
|
|
} else {
|
2015-06-23 16:57:11 +02:00
|
|
|
$answer = $this->triggerCommuncationCallback($data->method, $data->data);
|
2015-06-21 21:33:45 +02:00
|
|
|
//Prepare Response
|
|
|
|
if (!$answer) {
|
2015-06-26 11:48:21 +02:00
|
|
|
$data = new CommunicationAnswer("No listener or response on the given Message", true);
|
2015-06-21 21:33:45 +02:00
|
|
|
} else {
|
2015-06-26 11:48:21 +02:00
|
|
|
$data = $answer;
|
2015-06-21 21:33:45 +02:00
|
|
|
}
|
2015-06-21 20:45:48 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
//Encode, Encrypt and Send Response
|
|
|
|
$data = json_encode($data);
|
2015-06-23 21:20:44 +02:00
|
|
|
|
2015-06-22 22:52:26 +02:00
|
|
|
$data = openssl_encrypt($data, self::ENCRYPTION_METHOD, $password, OPENSSL_RAW_DATA, self::ENCRYPTION_IV);
|
2015-06-21 21:33:45 +02:00
|
|
|
$connection->write(strlen($data) . "\n" . $data);
|
2015-06-21 20:43:18 +02:00
|
|
|
|
2015-06-21 21:33:45 +02:00
|
|
|
// next msg
|
|
|
|
$arr = explode("\n", $buffer, 2);
|
|
|
|
}
|
|
|
|
});
|
2015-06-21 20:45:48 +02:00
|
|
|
});
|
2015-06-21 21:33:45 +02:00
|
|
|
//TODO check if port is closed
|
2015-06-23 18:17:03 +02:00
|
|
|
$this->socket->listen($socketPort, $this->maniaControl->getServer()->ip);
|
2015-06-21 21:33:45 +02:00
|
|
|
|
2015-06-26 17:20:27 +02:00
|
|
|
Logger::log("[CommunicationManager] Socket " . $this->maniaControl->getServer()->ip . ":" . $this->socket->getPort() . " Successfully created!");
|
2015-06-21 21:33:45 +02:00
|
|
|
} catch (ConnectionException $e) {
|
2015-06-22 20:22:17 +02:00
|
|
|
Logger::log("[CommunicationManager] Exception: " . $e->getMessage());
|
2015-06-21 21:33:45 +02:00
|
|
|
}
|
2015-06-21 20:45:48 +02:00
|
|
|
}
|
2015-06-21 20:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-21 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Processes Data on every ManiaControl Tick, don't call this Method
|
|
|
|
*/
|
|
|
|
public function tick() {
|
2015-06-21 21:33:45 +02:00
|
|
|
if ($this->loop) {
|
|
|
|
$this->loop->tick();
|
|
|
|
}
|
2015-06-22 22:52:26 +02:00
|
|
|
|
|
|
|
foreach ($this->communications as $communication) {
|
|
|
|
$communication->tick();
|
|
|
|
}
|
2015-06-21 20:43:18 +02:00
|
|
|
}
|
|
|
|
}
|