'use strict'; const express = require('express'); const { WebSocketServer } = require('ws'); const pty = require('node-pty'); const http = require('http'); const path = require('path'); const app = express(); const PORT = process.env.PORT || 3333; app.use(express.static(path.join(__dirname))); const server = http.createServer(app); const wss = new WebSocketServer({ server, path: '/terminal' }); wss.on('connection', (ws) => { const shell = process.env.SHELL || '/bin/bash'; const ptyProc = pty.spawn(shell, [], { name: 'xterm-256color', cols: 80, rows: 24, cwd: process.env.HOME || '/home/ubuntu-pc', env: process.env }); ptyProc.onData((data) => { if (ws.readyState === 1) { ws.send(JSON.stringify({ type: 'output', data })); } }); ws.on('message', (raw) => { try { const msg = JSON.parse(raw.toString()); if (msg.type === 'input') ptyProc.write(msg.data); if (msg.type === 'resize') ptyProc.resize(Number(msg.cols), Number(msg.rows)); } catch {} }); ws.on('close', () => { try { ptyProc.kill(); } catch {} }); ptyProc.onExit(() => { try { ws.close(); } catch {} }); }); server.listen(PORT, '0.0.0.0', () => { console.log(`posimai-dev running on http://0.0.0.0:${PORT}`); });