-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (77 loc) · 2.21 KB
/
Copy pathserver.js
File metadata and controls
97 lines (77 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const os = require('os');
const pty = require('node-pty');
const path = require('path');
const app = express();
const server = http.createServer(app);
console.log("ENV PORT:", process.env.PORT);
console.log("Starting server...");
const PORT = process.env.PORT || 3000;
app.use((req, res, next) => {
console.log("Incoming request:", req.method, req.url);
next();
});
app.get('/health', (req, res) => {
console.log('Health check hit');
res.status(200).send('OK');
});
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
console.log('Root path hit');
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/download/:filename', (req, res) => {
const filePath = path.join(__dirname, req.params.filename);
res.download(filePath);
});
const io = new Server(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
},
transports: ['websocket', 'polling'],
pingTimeout: 60000,
pingInterval: 25000
});
console.log('Socket.io server initialized');
io.engine.on('connection_error', (err) => {
console.log('Connection error:', err.code, err.message);
});
const shell = '/bin/bash';
console.log('Shell path:', shell);
io.on('connection', (socket) => {
console.log('Client connected:', socket.id, socket.conn.transport.name);
console.log('Socket handshake:', socket.handshake);
let ptyProcess;
try {
ptyProcess = pty.spawn(shell, [], {
name: 'xterm-256color',
cols: 80,
rows: 30,
cwd: process.env.HOME || process.cwd(),
env: process.env
});
console.log('PTY spawned for:', socket.id);
} catch (err) {
console.error('PTY spawn error:', err);
return;
}
ptyProcess.onData((data) => {
socket.emit('terminal:data', data);
});
socket.on('terminal:write', (data) => {
ptyProcess.write(data);
});
socket.on('resize', (size) => {
ptyProcess.resize(size.cols, size.rows);
});
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
ptyProcess.kill();
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});