This commit is contained in:
Bozhidar 2024-04-28 15:00:29 +03:00
parent 21a545829e
commit 56e9682c66
3 changed files with 53 additions and 20 deletions

View file

@ -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 [

View file

@ -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];

View file

@ -10,7 +10,7 @@
@vite('resources/js/web-terminal.js')
<div class="bg-black/5 dark:bg-white/5 rounded p-4">
<div id="js-web-terminal"></div>
<div id="js-web-terminal"></div>
</div>
</div>