Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const http = require('http');
|
| 2 |
+
const WebSocket = require('ws');
|
| 3 |
+
const pty = require('node-pty');
|
| 4 |
+
const url = require('url');
|
| 5 |
+
|
| 6 |
+
const port = 7860;
|
| 7 |
+
const requiredPassword = process.env.TERMINAL_PASSWORD;
|
| 8 |
+
|
| 9 |
+
const server = http.createServer();
|
| 10 |
+
const wss = new WebSocket.Server({ server });
|
| 11 |
+
|
| 12 |
+
wss.on('connection', (ws, req) => {
|
| 13 |
+
const query = url.parse(req.url, true).query;
|
| 14 |
+
const password = query.password;
|
| 15 |
+
|
| 16 |
+
if (password !== requiredPassword) {
|
| 17 |
+
ws.send('Erro: Senha incorreta');
|
| 18 |
+
ws.close();
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const shell = '/bin/fish';
|
| 23 |
+
const cwd = process.env.HOME || '/tmp';
|
| 24 |
+
|
| 25 |
+
const ptyProcess = pty.spawn(shell, [], {
|
| 26 |
+
name: 'xterm-256color',
|
| 27 |
+
cols: 80,
|
| 28 |
+
rows: 24,
|
| 29 |
+
cwd: cwd,
|
| 30 |
+
env: {
|
| 31 |
+
...process.env,
|
| 32 |
+
TERM: 'xterm-256color',
|
| 33 |
+
COLORTERM: 'truecolor',
|
| 34 |
+
HOME: process.env.HOME || '/home/node'
|
| 35 |
+
}
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
console.log(`Terminal iniciado (PID: ${ptyProcess.pid})`);
|
| 39 |
+
|
| 40 |
+
ptyProcess.on('data', (data) => {
|
| 41 |
+
if (ws.readyState === WebSocket.OPEN) {
|
| 42 |
+
ws.send(data);
|
| 43 |
+
}
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
ws.on('message', (msg) => {
|
| 47 |
+
ptyProcess.write(msg.toString());
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
ws.on('resize', (size) => {
|
| 51 |
+
try {
|
| 52 |
+
ptyProcess.resize(size.cols || 80, size.rows || 24);
|
| 53 |
+
} catch (e) {
|
| 54 |
+
console.error('Erro ao redimensionar:', e);
|
| 55 |
+
}
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
ws.on('close', () => {
|
| 59 |
+
console.log('Conexão encerrada');
|
| 60 |
+
ptyProcess.kill();
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
ws.on('error', (err) => {
|
| 64 |
+
console.error('Erro na conexão WebSocket:', err);
|
| 65 |
+
ptyProcess.kill();
|
| 66 |
+
});
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
server.listen(port, () => {
|
| 70 |
+
console.log(`Servidor rodando na porta ${port}`);
|
| 71 |
+
});
|