-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
153 lines (140 loc) · 3.57 KB
/
main.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import express, { Request, Response } from "express";
import http from "http";
import dotenv from "dotenv";
import bodyParser from "body-parser";
import { Server } from "socket.io";
import * as wrtc from "wrtc";
import * as NodePty from "node-pty";
const configuration = {
iceServers: [
{
urls: "stun:stun.l.google.com:19302",
},
],
};
dotenv.config();
const app = express();
const server = http.createServer(app);
const port = process.env.PORT;
const io = new Server(server);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("src"));
app.get("/", (req: Request, res: Response) => {
res.sendFile(__dirname + "/src/index.html");
});
io.on("connection", socket => {
var tty: Pty | null = null;
socket.on("start", message => {
tty = connectPty(data => {
socket.emit("data", data);
}, message.cols, message.rows)
});
socket.on("data", str => {
if (!tty) return;
tty.data(str);
});
socket.on("resize", message => {
if (!tty) return;
tty.resize(message.cols, message.rows);
});
socket.on("disconnect", () => {
if (!tty) return;
tty.close();
});
});
app.post('/connectrtc', async (req, res) => {
const { offer, candidates } = req.body;
const localCandidates: any[] = [];
let dataChannel;
const peer = new wrtc.RTCPeerConnection(configuration);
peer.ondatachannel = (event) => {
var tty: Pty | null = null;
dataChannel = event.channel;
dataChannel.onopen = () => {
tty = connectPty(dataChannel.send.bind(dataChannel), req.body.cols, req.body.rows);
};
dataChannel.onclose = (event) => {
if (!tty) return;
tty.close();
};
dataChannel.onmessage = (event) => {
if (!tty) return;
let msg = JSON.parse(event.data);
switch (msg.type) {
case "w": {
tty.data(msg.w);
break;
}
case "resize": {
tty.resize(msg.cols, msg.rows);
break;
}
}
};
};
peer.onconnectionstatechange = () => {
console.log('Connection state:', peer.connectionState);
};
peer.onsignalingstatechange = () => {
console.log('Signaling state:', peer.signalingState);
};
peer.oniceconnectionstatechange = () => {
console.log('ICE connection state:', peer.iceConnectionState);
};
peer.onicegatheringstatechange = () => {
console.log('ICE gathering state:', peer.iceGatheringState);
};
peer.onicecandidate = (event: any) => {
if (event.candidate) {
localCandidates.push(event.candidate);
return;
}
let payload = {
answer: peer.localDescription,
candidates: localCandidates,
};
res.json(payload);
};
await peer.setRemoteDescription(offer);
let answer = await peer.createAnswer();
await peer.setLocalDescription(answer);
for (let candidate of candidates) {
await peer.addIceCandidate(candidate);
}
});
function connectPty(sendData, cols, rows): Pty {
var open = true;
const tty = NodePty.spawn(process.env.TTY_SHELL!, [], {
name: 'xterm-color',
cols,
rows,
cwd: process.env.HOME,
env: process.env as any
});
tty.onData((data) => {
if (open) {
sendData(data);
}
})
return {
data: data => {
tty.write(data);
},
resize: (cols, rows) => {
tty.resize(cols, rows);
},
close: () => {
open = false;
tty.kill();
}
}
}
interface Pty {
data: (data: string) => void,
resize: (cols: number, rows: number) => void,
close: () => void,
}
server.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});