diff --git a/application/Espo/Core/defaults/systemConfig.php b/application/Espo/Core/defaults/systemConfig.php index aa205052db..6a5f6d7f2e 100644 --- a/application/Espo/Core/defaults/systemConfig.php +++ b/application/Espo/Core/defaults/systemConfig.php @@ -185,6 +185,9 @@ return [ 'adminNotificationsNewExtensionVersion', 'leadCaptureAllowOrigin', 'cronDisabled', + 'webSocketSslCertificateFile', + 'webSocketSslCertificateKeyFile', + 'webSocketSslAllowSelfSigned', ], 'superAdminItems' => [ 'jobMaxPortion', @@ -202,6 +205,10 @@ return [ 'maintenanceMode', 'siteUrl', 'useWebSocket', + 'webSocketUrl', + 'webSocketSslCertificateFile', + 'webSocketSslCertificateKeyFile', + 'webSocketSslAllowSelfSigned', ], 'userItems' => [ 'outboundEmailFromAddress', diff --git a/client/src/web-socket-manager.js b/client/src/web-socket-manager.js index bede5fa1e8..c8c6bdcc56 100644 --- a/client/src/web-socket-manager.js +++ b/client/src/web-socket-manager.js @@ -31,7 +31,6 @@ define('web-socket-manager', [], function () { var WebSocketManager = function (config) { this.config = config; var url = this.config.get('webSocketUrl'); - this.port = 8080; if (url) { if (url.indexOf('wss://') === 0) { @@ -59,6 +58,14 @@ define('web-socket-manager', [], function () { } } + if (!this.port) { + if (this.protocolPart === 'wss://') { + this.port = 8443; + } else { + this.port = 8080; + } + } + if (~this.url.indexOf(':')) { this.url = this.url.substr(0, this.url.indexOf(':')); } diff --git a/websocket.php b/websocket.php index d5487d989b..797d20f194 100644 --- a/websocket.php +++ b/websocket.php @@ -32,11 +32,12 @@ if (substr(php_sapi_name(), 0, 3) != 'cli') die('WebSocket can be run only via C include "bootstrap.php"; $app = new \Espo\Core\Application(); +$config = $app->getContainer()->get('config'); $categoriesData = $app->getContainer()->get('metadata')->get(['app', 'webSocket', 'categories'], []); -$phpExecutablePath = $app->getContainer()->get('config')->get('phpExecutablePath'); -$isDebugMode = (bool) $app->getContainer()->get('config')->get('webSocketDebugMode'); +$phpExecutablePath = $config->get('phpExecutablePath'); +$isDebugMode = (bool) $config->get('webSocketDebugMode'); $loop = \React\EventLoop\Factory::create(); $pusher = new \Espo\Core\WebSocket\Pusher($categoriesData, $phpExecutablePath, $isDebugMode); @@ -46,7 +47,42 @@ $pull = $context->getSocket(\ZMQ::SOCKET_PULL); $pull->bind('tcp://127.0.0.1:5555'); $pull->on('message', [$pusher, 'onMessageReceive']); -$webSocket = new \React\Socket\Server('0.0.0.0:8080', $loop); + +$useSsl = false; +$port = null; + +$webSocketUrl = $config->get('webSocketUrl'); +if ($webSocketUrl) { + if (stripos($webSocketUrl, 'wss://') === 0) { + $useSsl = true; + } + $port = parse_url($webSocketUrl, \PHP_URL_PORT); +} else { + $siteUrl = $config->get('siteUrl'); + if ($siteUrl && stripos($siteUrl, 'https://') === 0) { + $useSsl = true; + } +} + +if (!$port) { + $port = $useSsl ? '8443' : '8080'; +} + +$webSocket = new \React\Socket\Server('0.0.0.0:'.$port, $loop); + +if ($useSsl) { + $webSocket = new \React\Socket\SecureServer( + $webSocket, + $loop, + [ + 'local_cert' => $config->get('webSocketSslCertificateFile', 'cert.pem'), + 'local_pk' => $config->get('webSocketSslCertificateKeyFile', 'key.pem'), + 'allow_self_signed' => $config->get('webSocketSslAllowSelfSigned', true), + 'verify_peer' => false, + ] + ); +} + $webServer = new \Ratchet\Server\IoServer( new \Ratchet\Http\HttpServer( new \Ratchet\WebSocket\WsServer(