-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
209 lines (165 loc) · 5.58 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { decrementRoomCount, incrementRoomCount } from "./utils/roomCount";
import type { Socket } from "socket.io";
import { config } from "dotenv";
config();
import express = require("express");
const app = express();
const server = require("http").Server(app);
import compression = require("compression");
import minify = require("express-minify");
import Client from "./database/redis";
import doesRoomExist from "./utils/roomExists";
import createRoom from "./utils/createRoom";
import storeSocketId from "./utils/storeSocket";
import { keepRoomAlive } from "./utils/keepAlive";
import deleteSocket from "./utils/deleteSocket";
import "./database/sqlite";
import deleteOldFiles from "./tasks/deleteOldFiles";
app.use(compression());
app.use(minify());
interface options {
cors: { origin: string; methods: string[]; credentials: boolean };
}
let socketioOptions: options = {} as options;
if (process.env.USE_CORS === "true") {
socketioOptions.cors = {
origin: "*",
methods: ["GET", "POST"],
credentials: true,
};
app.use(require("cors")());
}
const io = require("socket.io")(server, socketioOptions);
interface encryptionData {
iv: string;
data: string;
}
io.on("connection", (socket: Socket) => {
socket.on(
"join",
async (data: {
roomName: string;
username: { iv: string; data: string };
}) => {
// Listens for join events from clients
if (typeof data === "object") {
// Make sure the data received is a JS object
var now = new Date();
if (!data.roomName || !data.username) {
// Make sure the data received is a JS object
console.log(`${now} - Event had invalid fields.`);
return;
}
// Join the socket to the requested room
socket.join(data.roomName);
io.to(data.roomName).emit("join response", {
// Broadcasts the username as a join response to the Socket room
username: data.username,
id: socket.id,
});
if (!(await doesRoomExist(data.roomName))) {
// If the room doesn't exist, create it
await createRoom(data.roomName);
} else {
await incrementRoomCount(data.roomName);
}
await storeSocketId(
socket.id,
data.roomName,
JSON.stringify(data.username)
);
return;
}
console.log(`${now} - Event was rejected: ${data}`);
}
);
socket.on(
"chat event",
async (data: {
roomName: string;
username: encryptionData;
message: encryptionData;
key: string;
}) => {
if (typeof data !== "object") {
return;
}
if (!data.roomName || !data.username || !data.message) {
return;
}
await keepRoomAlive(data.roomName);
io.to(data.roomName).emit("chat response", {
username: data.username,
message: data.message,
key: data.key,
uid: socket.id,
});
}
);
socket.on("disconnect", async () => {
// on disconnect, check if the socket is in a room
// if so, decrement the room count and broadcast leave
const socketRoom: string = (await Client.get(socket.id)) as string;
if (!socketRoom) {
return;
}
const clientUsername: string = (await Client.get(
`${socket.id}_username`
)) as string;
io.to(socketRoom).emit("leave response", {
id: socket.id,
username: clientUsername,
});
await deleteSocket(socket.id);
await decrementRoomCount(socketRoom);
});
socket.on(
"file event",
async (data: {
roomName: string;
username: { iv: string; data: string };
id: string;
iv: string;
name: { iv: string; data: string };
type: { iv: string; data: string };
key: { iv: string; data: string };
}) => {
if (
!data.roomName ||
!data.username ||
!data.id ||
!data.iv ||
!data.name ||
!data.type ||
!data.key
) {
return;
}
if (!(await doesRoomExist(data.roomName))) {
return;
}
await keepRoomAlive(data.roomName);
io.to(data.roomName).emit("file response", {
username: data.username,
id: data.id,
iv: data.iv,
name: data.name,
type: data.type,
key: data.key,
uid: socket.id,
});
}
);
});
import UploadEndpoint from "./routes/files/upload";
const multer = require("multer");
const storage = multer.memoryStorage();
const upload = multer({ storage });
const cors = require("cors");
app.post("/upload", cors(), upload.single("efile"), UploadEndpoint);
app.use("/efile", express.static("public"));
// call deleteOldFiles every 1m
setInterval(deleteOldFiles, 60 * 1000);
server.listen(process.env.PORT, process.env.HOST, () => {
console.log(`listening on ${process.env.HOST}:${process.env.PORT}`);
});