-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
46 lines (39 loc) · 1.04 KB
/
server.js
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
const express = require('express');
const pty = require('node-pty');
const app = express();
const expressWs = require('express-ws')(app);
const challenges = require('./challenges/index.json');
app.get('/challenges', (req, res) => {
res.json(challenges);
})
// Serve static assets from ./static
app.use(express.static(`${__dirname}/dist`));
// Instantiate shell and set up data handlers
expressWs.app.ws('/:challenge', (ws, req) => {
const challenge = req.params.challenge;
// Spawn the shell
const shell = pty.spawn('docker', [
'run', '-it', '--rm',
'--network', 'none',
`git-ch:${challenge}`,
], {
name: 'xterm-color',
cwd: process.env.PWD,
env: process.env
});
// For all shell data send it to the websocket
shell.on('data', (data) => {
if (ws.readyState === 1) {
ws.send(data);
}
});
// For all websocket data send it to the shell
ws.on('message', (msg) => {
shell.write(msg);
});
ws.on('close', () => {
shell.write('exit\r');
});
});
// Start the application
app.listen(3000);