From 56e9682c668b7a9c545066fae2bf1afaa1d0c11c Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Sun, 28 Apr 2024 15:00:29 +0300 Subject: [PATCH] update --- .../Terminal/Filament/Pages/Terminal.php | 34 ++++++++++++++++- .../Terminal/nodejs/terminal/server.js | 37 ++++++++++--------- .../views/filament/pages/terminal.blade.php | 2 +- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/web/Modules/Terminal/Filament/Pages/Terminal.php b/web/Modules/Terminal/Filament/Pages/Terminal.php index 8a9ff3e..4687a4c 100644 --- a/web/Modules/Terminal/Filament/Pages/Terminal.php +++ b/web/Modules/Terminal/Filament/Pages/Terminal.php @@ -19,7 +19,37 @@ class Terminal extends Page protected function getViewData(): array { - $sessionId = session()->getId(); + // Get server ip + $serverIp = shell_exec("hostname -I | awk '{print $1}'"); + $serverIp = trim($serverIp); + + $sessionId = md5(session()->getId()); + + $appTerminalConfigFile = storage_path('app/terminal/config.json'); + if (!is_dir($appTerminalConfigFile)) { + shell_exec('mkdir -p ' . dirname($appTerminalConfigFile)); + } + + file_put_contents($appTerminalConfigFile, json_encode([ + 'serverIp' => $serverIp, + ], JSON_PRETTY_PRINT)); + + $appTerminalSessionsPath = storage_path('app/terminal/sessions'); + if (!is_dir($appTerminalSessionsPath)) { + shell_exec('mkdir -p ' . $appTerminalSessionsPath); + } + if (is_dir($appTerminalSessionsPath)) { + shell_exec('rm -rf ' . $appTerminalSessionsPath.'/*'); + } + + $sessionStorageFile = $appTerminalSessionsPath . '/' . $sessionId; + if (!is_file($sessionStorageFile)) { + file_put_contents($sessionStorageFile, json_encode([ + 'sessionId' => $sessionId, + 'commands' => [], + 'user' => 'root', + ], JSON_PRETTY_PRINT)); + } $runNewTerminal = true; $checkPort = shell_exec('netstat -tuln | grep 8449'); @@ -29,7 +59,7 @@ class Terminal extends Page } } if ($runNewTerminal) { - $exec = shell_exec('node /usr/local/phyre/web/Modules/Terminal/nodejs/terminal/server.js >> /usr/local/phyre/web/storage/logs/terminal/server-terminal.log &'); + // $exec = shell_exec('node /usr/local/phyre/web/Modules/Terminal/nodejs/terminal/server.js >> /usr/local/phyre/web/storage/logs/terminal/server-terminal.log &'); } return [ diff --git a/web/Modules/Terminal/nodejs/terminal/server.js b/web/Modules/Terminal/nodejs/terminal/server.js index 337db6e..4a558d0 100644 --- a/web/Modules/Terminal/nodejs/terminal/server.js +++ b/web/Modules/Terminal/nodejs/terminal/server.js @@ -5,15 +5,13 @@ import { readFileSync } from 'node:fs'; import { spawn } from 'node-pty'; import { WebSocketServer } from 'ws'; -const sessionName = 'PHYRESID'; +const sessionName = 'phyre_panel_session'; const hostname = execSync('hostname', { silent: true }).toString().trim(); -// const systemIPs = JSON.parse( -// execSync(`${process.env.PHYRE}/bin/v-list-sys-ips json`, { silent: true }).toString(), -// ); + const systemIPs = []; -// const { config } = JSON.parse( -// execSync(`${process.env.PHYRE}/bin/v-list-sys-config json`, { silent: true }).toString(), -// ); +const terminalConfig = JSON.parse(readFileSync("/usr/local/phyre/web/storage/app/terminal/config.json").toString()); +systemIPs.push(terminalConfig.serverIp); + const config = { WEB_TERMINAL_PORT: 8449, BACKEND_PORT: 8443, @@ -23,32 +21,37 @@ const wss = new WebSocketServer({ port: parseInt(config.WEB_TERMINAL_PORT, 10), verifyClient: async (info, cb) => { - // if (!info.req.headers.cookie.includes(sessionName)) { - // cb(false, 401, 'Unauthorized'); - // console.error('Unauthorized connection attempt'); - // return; - // } + if (!info.req.headers.cookie.includes(sessionName)) { + cb(false, 401, 'Unauthorized'); + console.error('Unauthorized connection attempt'); + return; + } const origin = info.origin || info.req.headers.origin; let matches = origin === `https://${hostname}:${config.BACKEND_PORT}`; +// console.log(`Origin: ${origin}`); + if (!matches) { - for (const ip of Object.keys(systemIPs)) { + for (const ip of systemIPs) { if (origin === `https://${ip}:${config.BACKEND_PORT}`) { matches = true; break; } + if (origin === `http://${ip}:${config.BACKEND_PORT}`) { + matches = true; + break; + } } } - matches = true; if (matches) { cb(true); console.log(`Accepted connection from ${info.req.headers['x-real-ip']} to ${origin}`); return; } - // console.error(`Forbidden connection attempt from ${info.req.headers['x-real-ip']} to ${origin}`); - // cb(false, 403, 'Forbidden'); + console.error(`Forbidden connection attempt from ${info.req.headers['x-real-ip']} to ${origin}`); + cb(false, 403, 'Forbidden'); }, }); @@ -64,7 +67,7 @@ wss.on('connection', (ws, req) => { const remoteIP = req.headers['x-real-ip'] || req.socket.remoteAddress; - console.log(req.headers); + console.log(req.url); // Check if session is valid // const sessionID = req.headers.cookie.split(`${sessionName}=`)[1].split(';')[0]; diff --git a/web/Modules/Terminal/resources/views/filament/pages/terminal.blade.php b/web/Modules/Terminal/resources/views/filament/pages/terminal.blade.php index 000bd7e..7902206 100644 --- a/web/Modules/Terminal/resources/views/filament/pages/terminal.blade.php +++ b/web/Modules/Terminal/resources/views/filament/pages/terminal.blade.php @@ -10,7 +10,7 @@ @vite('resources/js/web-terminal.js')
-
+