diff --git a/plugin/YPTSocket/functions.php b/plugin/YPTSocket/functions.php index 8115d64cd..ae13e6a98 100644 --- a/plugin/YPTSocket/functions.php +++ b/plugin/YPTSocket/functions.php @@ -1,125 +1,133 @@ from_users_id = User::getId(); $msgObj->isAdmin = User::isAdmin(); $msgObj->user_name = User::getNameIdentification(); $msgObj->browser = get_browser_name(); $msgObj->yptDeviceId = getDeviceID(false); $msgObj->token = getToken($timeOut); $msgObj->time = time(); $msgObj->ip = getRealIpAddr(); $msgObj->send_to_uri_pattern = $send_to_uri_pattern; //var_dump($msgObj);exit; if (!empty($_REQUEST['webSocketSelfURI'])) { $msgObj->selfURI = $_REQUEST['webSocketSelfURI']; } else { $msgObj->selfURI = getSelfURI(); } if (empty($msgObj->videos_id)) { if (!empty($_REQUEST['webSocketVideos_id'])) { $msgObj->videos_id = $_REQUEST['webSocketVideos_id']; } else { $msgObj->videos_id = getVideos_id(); } } if (empty($msgObj->live_key)) { if (!empty($_REQUEST['webSocketLiveKey'])) { $msgObj->live_key = _json_decode($_REQUEST['webSocketLiveKey']); } else { $msgObj->live_key = isLive(); } } if (AVideoPlugin::isEnabledByName('User_location')) { $msgObj->location = User_Location::getThisUserLocation(); } else { $msgObj->location = false; } return encryptString(json_encode($msgObj)); } function getDecryptedInfo($string) { $decriptedString = decryptString($string); $json = _json_decode($decriptedString); if (!empty($json) && !empty($json->token)) { if (verifyTokenSocket($json->token)) { return $json; } else { _error_log("socket:getDecryptedInfo: token is invalid "); } } else { _error_log("socket:getDecryptedInfo: json->token is empty ({$decriptedString})"); } return false; } function verifyTokenSocket($token) { global $global; $obj = _json_decode(decryptString($token)); if (empty($obj)) { _error_log("verifyToken invalid token"); return false; } if ($obj->salt !== $global['salt']) { _error_log("verifyToken salt fail"); return false; } $time = time(); if (!($time >= $obj->time && $time <= $obj->timeout)) { _error_log("verifyToken token timout time = $time; obj->time = $obj->time; obj->timeout = $obj->timeout"); //return false; } return true; } class SocketMessageType { const NEW_CONNECTION = "NEW_CONNECTION"; const NEW_DISCONNECTION = "NEW_DISCONNECTION"; const DEFAULT_MESSAGE = "DEFAULT_MESSAGE"; const ON_VIDEO_MSG = "ON_VIDEO_MSG"; const ON_LIVE_MSG = "ON_LIVE_MSG"; const TESTING = "TESTING"; } function getTotalViewsLive_key($live_key) { if (empty($live_key)) { return false; } $live_key = object_to_array($live_key); _mysql_connect(); $liveUsersEnabled = \AVideoPlugin::isEnabledByName("LiveUsers"); if ($liveUsersEnabled) { $liveUsers = new \LiveOnlineUsers(0); $total = $liveUsers->getTotalUsersFromTransmitionKey($live_key['key'], $live_key['live_servers_id']); } else { $total = null; } _mysql_close(); return $total; } function killProcessOnPort() { $obj = \AVideoPlugin::getDataObject("YPTSocket"); $port = intval($obj->port); if (!empty($port)) { echo 'Searching for port: ' . $port . PHP_EOL; $command = 'netstat -ano | findstr ' . $port; exec($command, $output, $retval); $pid = getPIDUsingPort($port); if (!empty($pid)) { echo 'Killing, PID ' . $pid . PHP_EOL; killProcess($pid); } else { echo 'No Need to kill, port NOT found' . PHP_EOL; } } } + +function restartServer(){ + global $global; + killProcessOnPort(); + sleep(1); + $cmd = "nohup php {$global['systemRootPath']}plugin/YPTSocket/server.php &"; + return exec($cmd); +} \ No newline at end of file diff --git a/plugin/YPTSocket/test.php b/plugin/YPTSocket/test.php index 2fadeabcb..36dda31c3 100644 --- a/plugin/YPTSocket/test.php +++ b/plugin/YPTSocket/test.php @@ -1,205 +1,210 @@ host; $port = $socketobj->port; $url = "://localhost:{$port}"; $SocketURL = 'ws' . $url; _test_send($SocketURL, 'ws'); if (empty($socketobj->forceNonSecure)) { $SocketURL = 'wss' . $url; _test_send($SocketURL, 'wss'); } $url = "://127.0.0.1:{$port}"; $SocketURL = 'ws' . $url; _test_send($SocketURL, 'ws'); if (empty($socketobj->forceNonSecure)) { $SocketURL = 'wss' . $url; _test_send($SocketURL, 'wss'); } $url = "://{$address}:{$port}"; $SocketURL = 'ws' . $url; _test_send($SocketURL, 'ws'); if (empty($socketobj->forceNonSecure)) { $SocketURL = 'wss' . $url; _test_send($SocketURL, 'wss'); } function _test_send($SocketURL, $msg) { global $SocketSendObj, $count; $_count = $count; $_msg = "{$SocketURL} on " . date('Y/m/d H:i:s'); _log("Testing connection with [{$_count}]: " . $_msg); $SocketSendObj = new stdClass(); $SocketSendObj->webSocketToken = _test_getEncryptedInfo($_msg); $SocketSendObj->msg = $_msg; $SocketURL .= "?webSocketToken={$SocketSendObj->webSocketToken}"; \Ratchet\Client\connect($SocketURL)->then(function($conn) use ($_count) { global $SocketSendObj; $conn->on('message', function($msg) use ($conn, $_count) { global $responses; $json = _json_decode($msg->getPayload()); //var_dump($json); $parts = explode(':', $json->msg->test_msg); $c = new AVideoSocketConfiguration($parts[0], $parts[2], $parts[1], true); $responses[] = $c; $c->log(); printIfComplete(); }); $conn->send(json_encode($SocketSendObj)); $conn->close(); }, function ($e) { global $responses; preg_match('/(tcp|tls):\/\/([^:]+):([0-9]+)/i', $e->getMessage(), $matches); if (empty($matches)) { _log("ERROR on get connect response [" . $e->getMessage() . "]"); } $c = new AVideoSocketConfiguration($matches[1], $matches[3], $matches[2], false, $e->getMessage()); $responses[] = $c; $c->log(); printIfComplete(); }); $count++; } function _test_getEncryptedInfo($msg) { $timeOut = 43200; // valid for 12 hours $msgObj = new stdClass(); $msgObj->from_users_id = 0; $msgObj->isAdmin = 1; $msgObj->test_msg = $msg; $msgObj->user_name = SocketMessageType::TESTING; $msgObj->browser = SocketMessageType::TESTING; $msgObj->yptDeviceId = SocketMessageType::TESTING . "-" . uniqid(); $msgObj->token = getToken($timeOut); $msgObj->time = time(); $msgObj->ip = '127.0.0.1'; $msgObj->send_to_uri_pattern = ''; $msgObj->autoEvalCodeOnHTML = array(); $msgObj->selfURI = SocketMessageType::TESTING . '-terminal'; $msgObj->videos_id = 0; $msgObj->live_key = ''; $msgObj->location = false; return encryptString(json_encode($msgObj)); } function _log($msg) { echo '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL; ob_flush(); } class AVideoSocketConfiguration { public $wss; public $port; public $host; public $success; public $message; function __construct($wss, $port, $host, $success, $message = '') { if ($wss == 'tls') { $wss = 'wss'; } else if ($wss == 'tcp') { $wss = 'ws'; } $this->wss = $wss; $this->port = intval(preg_replace('/([0-9]+).*/', '$1', $port)); $this->host = preg_replace('/[^0-9a-z_.-]/i', '', $host); $this->success = $success; $this->message = $message; } function log() { $msg = $this->getSecureText(); if ($this->success) { if ($this->isLocalhost()) { $msg .= "\e[1;33;40m"; $msg .= "Good news, your localhost connects, that means your port [{$this->port}] is open "; } else { $msg .= "\e[1;32;40m"; $msg .= 'CONNECTION SUCCESS '; } } else { $msg .= "\e[1;31;40m"; $msg .= 'CONNECTION FAIL '; } $msg .= $this->toURL(); $msg .= "\e[0m "; $msg .= $this->message; _log($msg); } function getSecureText() { $msg = ""; if ($this->isSecure()) { $msg .= "\e[1;47;42m"; $msg .= " SECURE CONNECTION \e[0m "; } else { $msg .= "\e[1;37;43m"; $msg .= " NOT SECURE CONNECTION \e[0m "; } return $msg; } function toURL() { return "{$this->wss}://$this->host:$this->port"; } function isLocalhost() { if (preg_match('/localhost/i', $this->host) || preg_match('/127.0.0.1/i', $this->host)) { return true; } return false; } function isSecure() { return $this->wss == 'wss'; } } function printIfComplete() { global $count, $responses; if (count($responses) == $count) { foreach ($responses as $key => $value) { if (empty($value) || empty($value->success) || $value->isLocalhost()) { unset($responses[$key]); } } $msg = ' We found ' . count($responses) . ' possible configurations:' . PHP_EOL; foreach ($responses as $value) { $msg .= PHP_EOL . ' ' . $value->getSecureText() . PHP_EOL; $msg .= '-------------------------------------------------------' . PHP_EOL; $msg .= '*** Force not to use wss (non secure): ' . ($value->wss == 'ws' ? 'Checked' : 'Unchecked') . ' ' . PHP_EOL; $msg .= '*** Server Port: ' . ($value->port) . PHP_EOL; $msg .= '*** Server host: ' . ($value->host) . PHP_EOL; $msg .= '-------------------------------------------------------' . PHP_EOL . PHP_EOL; } + if(empty($responses)){ + $msg .= 'Restarting socket server' . PHP_EOL; + $msg .= restartServer(); + $msg .= 'Restarting complete' . PHP_EOL; + } _log($msg); } }