-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
server.js
286 lines (252 loc) · 10.3 KB
/
server.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require('dotenv').config();
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const passport = require('passport');
const helmet = require("helmet");
const SQLiteStore = require('connect-sqlite3')(session);
const limit = require('express-limit').limit;
const { createServer } = require("http");
const { Server } = require("socket.io");
const filter = require('leo-profanity');
const { getSources, getShowInfo, getVideoSourcesGogoanime, getVideoSourcesZoro } = require('./utils/getSources');
const app = express();
const port = process.env.PORT || 3000;
const ejs = require('ejs')
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"]
} });
const roomData = {};
io.on("connection", (socket) => {
let room;
setInterval(() => {
checkRoomMembers(roomData);
}, 1000);
function createRoom(roomID) {
room = roomID
socket.join(room)
roomData[room] = {
users: {},
hostID: socket.id,
currentTime: 0,
roomID: roomID,
isPublic: true,
roomName: "Untitled Room",
roomDescription: "No description provided.",
playing: false,
currentManifest: "",
isVideoCurrentlyPlaying: false,
}
roomData[room].users[socket.id] = {
ping: -1,
id: socket.id,
isHost: true,
}
}
function joinRoom(roomID) {
room = roomID
socket.join(room)
roomData[room].users[socket.id] = {
ping: -1,
id: socket.id,
isHost: false,
}
console.log("send video down to user")
if (roomData[room].isVideoCurrentlyPlaying == true) {
if (roomData[room].currentManifest == "") return console.log("No manifest to send to user");
socket.emit("receiveUserVideoFromHost", {videoData: roomData[room].currentManifest, currentTime: roomData[room].currentTime});
}
}
socket.on("init", (data) => {
if(io.sockets.adapter.rooms.get(data.roomID) != undefined && io.sockets.adapter.rooms.get(data.roomID).size > 0){
joinRoom(data.roomID)
} else {
createRoom(data.roomID)
}
})
socket.on('disconnect', () => {
let idToRemove = socket.id
// Update the user information to make sure they are not a host, because it will be recycled to another user and we want to avoid edge cases :)
if (roomData[room] !== undefined) {
roomData[room].users[socket.id] = {
ping: -1,
id: socket.id,
isHost: false,
}
socket.disconnect()
delete roomData[room].users[idToRemove]
}
});
function checkRoomMembers() {
if (roomData[room] == undefined) return
if (Object.keys(roomData[room].users).length < 1) {
delete roomData[room]
}
}
socket.on("ping", (callback) => {
callback();
});
socket.on("updateUserPing", (data) => {
if (roomData[room] === undefined) return;
roomData[room].users[socket.id].ping = data.ping;
socket.emit("receiveNewUserList", {users: roomData[room].users})
let shouldRotateHost = true;
for (let userId in roomData[room].users) {
if (roomData[room].users.hasOwnProperty(userId)) {
const user = roomData[room].users[userId];
if (user.id == roomData[room].hostID) shouldRotateHost = false;
}
}
if (shouldRotateHost) {
const potentialHostList = Object.keys(roomData[room].users);
const newHostId = potentialHostList[Math.floor(Math.random() * potentialHostList.length)];
roomData[room].hostID = newHostId;
const newHostSocket = io.sockets.sockets.get(newHostId);
roomData[room].users[newHostId].isHost = true;
newHostSocket.emit("receiveNewHostMessage", {message: "You are now the host!"});
}
});
socket.on("updateRoomName", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
let cleanRoomName = filter.clean(data.newRoomName);
roomData[room].roomName = cleanRoomName;
})
socket.on("updateRoomDescription", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
let cleanRoomDescription = filter.clean(data.newRoomDescription);
roomData[room].roomDescription = cleanRoomDescription;
});
socket.on("playVideo", async (data) => {
try {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
if (data.videoID == undefined) return;
let videoID = data.videoID;
let showInfo = await getShowInfo(videoID);
let malID = showInfo.malId;
let videoInfo = await getSources(malID);
socket.emit("receiveNewVideo", {videoData: videoInfo, showInfo: showInfo});
} catch {
return socket.emit("permissionDenied", {message: "An error occured while trying to obtain video info!"});
}
});
socket.on("serverGetNewSource", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
console.log(data)
})
socket.on("playNewVideo", async (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
if (data.showID == undefined) return socket.emit("permissionDenied", {message: "No video ID provided!"});
let videoID = data.showID;
roomData[room].isVideoCurrentlyPlaying = false;
if (data.source == "gogoanime") {
try {
let sources = await getVideoSourcesGogoanime(data.episodeID);
roomData[room].isVideoCurrentlyPlaying = true;
return io.to(data.roomID).emit("sendNewVideo", { source: sources })
} catch {
return io.to(data.roomID).emit("permissionDenied", {message: "An error occured while trying to obtain video info!"});
}
}
if (data.source == "zoro") {
try {
let sources = await getVideoSourcesZoro(data.zoroID, data.episodeNumber);
roomData[room].isVideoCurrentlyPlaying = true;
return io.to(data.roomID).emit("sendNewVideoZoro", { source: sources })
} catch {
return io.to(data.roomID).emit("permissionDenied", {message: "An error occured while trying to obtain video info!"});
}
}
})
socket.on("serverUpdateManifest", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
roomData[room].currentManifest = data.currentManifest;
console.log(roomData[room].currentManifest)
})
socket.on("getCurrentTime", (data) => {
if (roomData[room] === undefined) return;
socket.emit("receiveCurrentTime", {currentTime: roomData[room].currentTime})
})
socket.on("updateVideoStatus", async (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
roomData[room].playing = data.status;
if (roomData[room].playing == true) {
io.in(data.roomID).emit("receiveVideoStatus", {status: "play"})
} else {
io.in(data.roomID).emit("receiveVideoStatus", {status: "pause"})
}
console.log(roomData[room].playing)
})
socket.on("updateCurrentTime", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
roomData[room].currentTime = data.currentTime;
io.in(data.roomID).emit("receiveCurrentTime", {currentTime: roomData[room].currentTime})
});
socket.on("amIHost", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].users[socket.id].isHost == false) return socket.emit("permissionDenied", {message: "You are not the host!"});
socket.emit("receiveHostStatus", {isHost: true})
});
socket.on("getVideoPlayState", (data) => {
if (roomData[room] === undefined) return;
if (roomData[room].playing == undefined) return socket.emit("permissionDenied", {message: "An error occured while trying to obtain video info!"})
socket.emit("receiveVideoPlayState", {isVideoCurrentlyPlaying: roomData[room].playing})
})
});
io.listen(8000);
app.locals.pluralize = require('pluralize');
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
originAgentCluster: false,
frameguard: false,
}))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
name : '.HKSECURITY',
secret: process.env.AUTH_SECRET || "tacocat", // absolutely set a AUTH_SECRET for production...
resave: false, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
store: new SQLiteStore({ db: 'sessions.db', dir: './var/db' })
}));
app.use(flash());
app.use(limit({max: 10, period: 5 * 1000, message: "Request Limit Exceeded!" }), passport.authenticate('session'));
app.use(express.static('public'))
app.engine('ejs', ejs.renderFile);
app.set('views', 'public')
app.listen(port);
function getRoomData() {
return roomData;
}
module.exports = {
getRoomData
};
// Set up app routes
app.use('/', require('./routers/index.js'));
app.use('/', require('./routers/auth.js'));
app.use('/w2g', require('./routers/w2g.js'));
app.use('/api', require('./routers/api.js'));
app.use('/ajax', require('./routers/ajax.js'));
app.use('/watchlist', require('./routers/watchlist.js'));
app.use('/search', require('./routers/search.js'));
app.use('/trending', require('./routers/trending.js'));
app.use('/releases', require('./routers/releases.js'));
app.use('/genres', require('./routers/genre/genres.js'));
app.use('/genre/', require('./routers/genre/genre.js'));
app.use("/status", require("./routers/status/status.js"));
app.use('/watch', require("./routers/watch/watch.js"));