-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.js
70 lines (60 loc) · 1.82 KB
/
bridge.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { spawn } = require("child_process");
const httpServer = require("http").createServer();
const net = require("net")
const io = require("socket.io")(httpServer, {
cors: {
origin: "*"
}
});
export function startListen(port_left, port_right) {
httpServer.listen(left_port, () =>
console.log(`server listening at http://localhost:${left_port}`)
);
io.use((_socket, next) => {
next();
});
io.on("connection", (socket) => {
let to_parse = 0;
let proc_data = ""
console.log("new connection");
let client = new net.Socket();
client.setEncoding("utf8");
client.connect({
port:port_right
});
setTimeout(() => socket.disconnect(true), 900000); // For now a 15 minute timeout?
socket.on("command", (command) => {
console.log(command);
client.write("Content-Length:" + command.length + "\r\nContent-Type: jrpcei\r\n\r\n" + command);
});
socket.on("disconnect", () => {
console.log("Client disconnected");
});
client.on("data", (data) => {
if (to_parse !== 0) {
if (data.length < to_parse) {
proc_data += data
to_parse -= data.length
} else {
proc_data += data.substring(0, to_parse);
data = data.substring(to_parse);
to_parse = 0;
socket.emit("data", proc_data);
proc_data = "";
}
} else {
data_split = data.split('\r\n', 4);
console.log(data_split.length == 4);
console.log(data_split[3]);
let ln = parseInt(data_split[0].split(":")[1])
if (ln > data_split[3].length) {
proc_data = data_split[3];
to_parse = ln - data_split[3].length;
} else {
console.log("Emitting: " + data_split[3]);
socket.emit("data", data_split[3]);
}
}
})
});
}