2024-12-14 22:55:58 -05:00
|
|
|
import express from 'express';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
import expressWs from 'express-ws';
|
|
|
|
|
import { spawn } from 'node-pty';
|
2025-06-24 22:17:56 -04:00
|
|
|
// import json from './secrets/config.json' with { type: 'json' };
|
2024-12-14 22:55:58 -05:00
|
|
|
|
2025-06-24 22:17:56 -04:00
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
const PORT = process.env.PORT || 5164;
|
|
|
|
|
const app = express();
|
2024-12-14 22:55:58 -05:00
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
|
2025-06-24 22:17:56 -04:00
|
|
|
// map of active sessions
|
|
|
|
|
const sessions = new Map();
|
2024-12-14 22:55:58 -05:00
|
|
|
|
|
|
|
|
// shell interface
|
2024-12-15 12:30:30 -05:00
|
|
|
app.get('/', (req, res) => {
|
2024-12-14 22:55:58 -05:00
|
|
|
res.sendFile('shell.html', { root: path.join(__dirname, 'HTML') });
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-24 22:17:56 -04:00
|
|
|
// websocket endpoint that spawns/attaches to a fish shell
|
2024-12-14 22:55:58 -05:00
|
|
|
app.ws('/shell-ws', (ws, req) => {
|
2025-06-24 22:17:56 -04:00
|
|
|
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);
|
2024-12-14 22:55:58 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-24 22:17:56 -04:00
|
|
|
// 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();
|
|
|
|
|
});
|
2024-12-15 10:35:09 -05:00
|
|
|
|
2024-12-15 12:30:30 -05:00
|
|
|
app.get('*', (req, res) => res.end());
|
2024-12-15 10:35:09 -05:00
|
|
|
|
2025-06-24 22:17:56 -04:00
|
|
|
app.listen(PORT, '0.0.0.0', () =>
|
|
|
|
|
console.log(`server listening on http://localhost:${PORT}`)
|
|
|
|
|
);
|