forked from bluebrown/web-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
61 lines (45 loc) · 1.26 KB
/
server.mjs
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
import pty from "node-pty";
import { createWebSocketStream, WebSocketServer } from "ws";
import path from "path";
import cors from 'cors';
// ###############
// Creating Server
// ###############
import http from "http";
import express from "express";
var app = express();
const server = http.createServer(app);
server.listen(3000, () => {
console.log("listening on port 3000");
});
// ######
// SOCKET
// ######
const wss = new WebSocketServer({ server, path: "/socket" });
wss.on("connection", (ws) => {
console.log("new connection");
const duplex = createWebSocketStream(ws, { encoding: "utf8" });
const proc = pty.spawn("sh", [], { name: "xterm-color" });
const onData = proc.onData((data) => duplex.write(data));
const exit = proc.onExit(() => {
console.log("process exited");
onData.dispose();
exit.dispose();
});
duplex.on("data", (data) => proc.write(data.toString()));
ws.on("close", function () {
console.log("stream closed");
proc.kill();
duplex.destroy();
});
});
// ###########
// Serve index
// ###########
const __dirname = path.resolve();
app.use(express.static(__dirname + '/'));
app.use(cors())
app.get("/", (req, res) => {
const __dirname = path.resolve();
res.sendFile(path.join(__dirname, "/index.html"));
});