Newer
Older
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
154
155
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const cp = require("child_process");
const fs = require("fs");
const sleep = require("sleep-promise");
const PORT = 3000;
const BOT_USERNAME = process.env.BOT_USERNAME;
const BOT_PASSWORD = process.env.BOT_PASSWORD;
const BOT_DISPLAY_NAME = process.env.BOT_DISPLAY_NAME || "Puppet";
// Global state:
var attachedRooms = {};
async function attachToRoom(roomUrl) {
// Get uid:
const roomUid = roomUrl.split("/")[4];
// Make sure that no room get's attached to twice
if (attachedRooms[roomUid] !== undefined) return;
else attachedRooms[roomUid] = false;
// Subprocess setup
const subProcess = cp.fork(`${__dirname}/room_attacher.js`);
subProcess.send({
eventName: "attachTo",
data: {
username: BOT_USERNAME,
password: BOT_PASSWORD,
displayName: BOT_DISPLAY_NAME,
roomUrl,
screenShotPath: "room_previews/" + roomUid + ".jpeg",
},
});
const room = {
subProcess,
uid: roomUid,
url: roomUrl,
userCount: -1,
users: [],
name: "???",
detach: async () => {
try {
subProcess.send({ eventName: "detach", data: null });
await sleep(500);
subProcess.kill(0);
} catch (error) {
subProcess.kill(1);
}
},
sendMessage: (content) => {
subProcess.send({ eventName: "sendMessage", data: { content } });
},
};
subProcess.on("message", (m) => {
if (m.eventName === "roomInfo") {
room.userCount = m.data.count;
room.users = m.data.users;
room.name = m.data.name;
} else if (m.eventName === "sessionClosed") {
attachedRooms[roomUid] = undefined;
} else console.log("Room[" + roomUid + "]:", m);
});
// return a room
return room;
}
// Web server setup
const app = express();
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(cors());
app.get("/api/preview/:roomUid", (req, res) => {
res.setHeader("Cache-Control", "max-age=2");
res.sendFile(
path.join(
__dirname,
"room_previews",
req.params.roomUid.split("/")[0].split("\\")[0] + ".jpeg"
)
);
});
app.post("/api/attach", async (req, res) => {
const roomUrl = req.body.url;
console.debug("Received request to attach to room", roomUrl);
const room = await attachToRoom(roomUrl);
attachedRooms[room.uid] = room;
res.send({
uid: room.uid,
url: roomUrl,
});
});
app.post("/api/broadcast", (req, res) => {
const content = "📢 Broadcast 📢\n\n" + req.body.content;
console.debug("Received request to broadcast message", content);
for (const roomUid of Object.keys(attachedRooms)) {
if (
attachedRooms[roomUid] === undefined ||
attachedRooms[roomUid] === false
)
continue;
try {
attachedRooms[roomUid].sendMessage(content);
} catch (error) {
console.warn('Could not send message "' + content + '" to room', roomUid);
}
}
res.end({});
});
app.get("/api/attachedRooms", (req, res) => {
const roomList = [];
if (attachedRooms === undefined) {
console.error("attachedRooms is undefined");
process.exit(1);
}
for (const key of Object.keys(attachedRooms)) {
if (attachedRooms[key] === undefined) continue;
roomList.push({
uid: attachedRooms[key].uid,
url: attachedRooms[key].url,
name: attachedRooms[key].name,
userCount: attachedRooms[key].userCount,
users: attachedRooms[key].users,
});
}
res.send(roomList);
});
app.post("/api/detach", async (req, res) => {
const roomUid = req.body.uid;
if (attachedRooms[roomUid] !== undefined) {
await attachedRooms[roomUid].detach();
attachedRooms[roomUid] = undefined;
}
res.send({});
});
app.post("/api/bulkcreate", async (req, res) => {
const { prefix, amount } = req.body;
});
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`);
});