diff --git a/HTML/shell.html b/HTML/shell.html index dce5c50..138cb1a 100644 --- a/HTML/shell.html +++ b/HTML/shell.html @@ -1,170 +1,137 @@ - + + + + + Shell + + + + + + +
+
+
+ - - + term.onData((data) => ws.send(data)); - -
-
-
+ ws.addEventListener("message", (event) => { + const data = JSON.parse(event.data); + if (data.event) { + switch (data.event) { + case "exit": + term.write( + "\r\nConnection closed. Refreshing...\r\n" + ); + setTimeout( + () => window.location.reload(), + 2000 + ); + break; + default: + term.write( + `\r\nUnknown event: ${data.event}\r\n` + ); + break; + } + } else { + term.write(data.data); + } + }); - - - - - - \ No newline at end of file + window.addEventListener("beforeunload", () => { + navigator.sendBeacon(`/kill-session?id=${sessionId}`); + }); + + + diff --git a/main.js b/main.js index 4cf5dbd..698f67f 100644 --- a/main.js +++ b/main.js @@ -3,53 +3,82 @@ import path from 'path'; import { fileURLToPath } from 'url'; import expressWs from 'express-ws'; import { spawn } from 'node-pty'; -import json from './secrets/config.json' with { type: 'json' }; +// import json from './secrets/config.json' with { type: 'json' }; -const __dirname = path.dirname(fileURLToPath(import.meta.url)), - PORT = process.env.PORT || 5164, - app = express(); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const PORT = process.env.PORT || 5164; +const app = express(); expressWs(app); // enable websockets for the app - // parse form data app.use(express.urlencoded({ extended: true })); app.use(express.json({ extended: true })); app.use('/node_modules', express.static('node_modules')); +// map of active sessions +const sessions = new Map(); // shell interface app.get('/', (req, res) => { res.sendFile('shell.html', { root: path.join(__dirname, 'HTML') }); }); - -// when a websocket is opened at /shell-ws, spawn a fish shell in a pty +// websocket endpoint that spawns/attaches to a fish shell app.ws('/shell-ws', (ws, req) => { - // spawn a fish shell using node-pty with a pseudo-terminal - const shell = spawn('su', ['-', 'ion606', '-s', '/usr/bin/fish'], { - cols: 80, - rows: 24, - cwd: process.env.HOME, - env: process.env + const { id } = req.query; + if (!id) { + ws.close(); + return; + } + + let session = sessions.get(id); + if (!session) { + const shell = spawn('su', ['-', 'ion606', '-s', '/usr/bin/fish'], { + cols: 80, + rows: 24, + cwd: process.env.HOME, + env: process.env + }); + + session = { shell, clients: new Set() }; + sessions.set(id, session); + + shell.onData((data) => { + for (const client of session.clients) { + client.send(JSON.stringify({ data })); + } + }); + + shell.onExit((e) => { + for (const client of session.clients) { + client.send(JSON.stringify({ event: 'exit', code: e })); + } + sessions.delete(id); + }); + } + + session.clients.add(ws); + ws.on('message', (msg) => session.shell.write(msg)); + + ws.on('close', () => { + session.clients.delete(ws); }); - - // send any data from the shell to the client - shell.onData((data) => { - ws.send(JSON.stringify({ data })); - }); - - shell.onExit((e) => ws.send(JSON.stringify({ event: 'exit', code: e }))); - - // when the client sends data, write it to the shell's stdin - ws.on('message', (msg) => shell.write(msg)); - - // when the websocket closes, kill the shell - ws.on('close', () => shell.kill()); }); +// endpoint used by the client to explicitly close a session +app.post('/kill-session', (req, res) => { + const { id } = req.query; + const session = sessions.get(id); + if (session) { + session.shell.kill(); + sessions.delete(id); + } + res.end(); +}); app.get('*', (req, res) => res.end()); - -app.listen(PORT, '0.0.0.0', () => console.log('server listening on http://localhost:' + PORT)); \ No newline at end of file +app.listen(PORT, '0.0.0.0', () => + console.log(`server listening on http://localhost:${PORT}`) +); \ No newline at end of file