-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
345 lines (293 loc) · 11 KB
/
index.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
'use strict'
// Modules
const UTIL = require('./api/helper/utilities');
const CONSTANTS = require('./api/helper/constants');
const HTTP = require('http');
const HTTPS = require('https');
const EXPRESS = require('express');
const ROUTES = require('./api/router');
const BODY_PARSER = require('body-parser');
const BLUEBIRD = require('bluebird');
const FS = BLUEBIRD.promisifyAll(require('fs'));
const HELMET = require('helmet');
const HASHMAP = require('hashmap');
const USER_MIDDLEWARE = require('./api/user_middleware');
const MUSIC_MIDDLEWARE = require('./api/music_middleware');
// const SEED_MUSIC = require('./Mp3_Dump/seed_music.js');
const { spawn } = require('child_process');
const DGRAM = require('dgram');
const LINK_MIDDLEWARE = require('./api/link_middleware');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
// // Setup Express routes
const HTTPAPP = EXPRESS();
const HTTPSAPP = EXPRESS();
const PEER_LINK_SOCKET = DGRAM.createSocket('udp4');
// // File Constants
const ONE_YEAR = 31536000000;
const HTTP_PORT = 80;
const SECURE_PORT = 443;
const SPAWN_PEER_PORT = 40000;
const SERVER_GRPC_PORT = 50002;
const CERT_LOC = '/etc/letsencrypt/live/www.dotify.online/';
const ROUTER = ROUTES(EXPRESS.Router());
const NODE_PROTO_PATH = __dirname + '/api/Proto/NodePb.proto';
const nodePackageDefinition = protoLoader.loadSync(
NODE_PROTO_PATH,
{
keepCase : true,
longs : String,
enums : String,
defaults : true,
oneofs : true
}
);
const nodeProto = grpc.loadPackageDefinition(nodePackageDefinition).NodePb;
// Incrase server listener count
require('events').EventEmitter.defaultMaxListeners = 30;
// Before anything, make sure to run any method that hasn't finished
// running because of a server crash
async function crashRecover(){
// Open the request logs json file
let requestLogs = await FS.readFileAsync(CONSTANTS.REQUEST_LOG_FILEPATH);
// Turn the request log into a hash map object
requestLogs = new HASHMAP(JSON.parse(requestLogs));
// Retrieve all of the values from the hashmap
let uuidKeys = requestLogs.keys();
// Do each request
for (let uuid of uuidKeys){
let request = requestLogs.get(uuid);
// Retrieve the request type of the logged request
let requestType = request.requestType;
// Depending on the request type, send the request the the appropriate
// location
switch(requestType) {
case CONSTANTS.CREATE_ACCOUNT_REQUEST:{
USER_MIDDLEWARE.createUser(request, null);
}
break;
case CONSTANTS.UPDATE_USER_PASSWORD_REQUEST:{
USER_MIDDLEWARE.updateUser(request, null);
}
break;
case CONSTANTS.DELETE_PLAYLIST_REQUEST:{
MUSIC_MIDDLEWARE.deletePlaylist(request, null);
}
break;
case CONSTANTS.CREATE_PLAYLIST_REQUEST:{
MUSIC_MIDDLEWARE.createPlaylist(request, null);
}
break;
case CONSTANTS.ADD_SONG_TO_PLAYLIST_REQUEST:{
MUSIC_MIDDLEWARE.addSongToPlaylist(request, null);
}
break;
case CONSTANTS.DELETE_SONG_FROM_PLAYSLIST_REQUEST:{
MUSIC_MIDDLEWARE.deleteSongFromPlaylist(request, null);
}
break;
case CONSTANTS.SAVE_USER_PROFILE_IMAGE_REQUEST: {
USER_MIDDLEWARE.saveUserProfileImage(request, null);
}
break;
}
// Remove all of the uuid values from the logged hashmap
UTIL.removeRequestLog(...uuidKeys);
}
}
crashRecover();
// SSL Cipher Keys
let cipher = ['ECDHE-ECDSA-AES256-GCM-SHA384',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-RSA-AES256-CBC-SHA384',
'ECDHE-RSA-AES256-CBC-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'DHE-RSA-AES128-GCM-SHA256',
'DHE-RSA-AES256-GCM-SHA384',
'!aNULL',
'!MD5',
'!DSS'].join(':');
// Redirect HTTP Connections to HTTPS
HTTPAPP.get('*', function(req, res, next) {
if(req.headers.host === "dotify.online"){
res.redirect('https://www.' + req.headers.host + req.originalUrl);
}
else{
res.redirect('https://'+ req.headers.host + req.originalUrl);
}
});
// Setup Helmet for HTTPS
HTTPSAPP.use(HELMET.hsts({
maxAge:ONE_YEAR,
includeSubdomains: true,
force: true
}));
HTTPSAPP.use(BODY_PARSER.urlencoded({limit: '40mb', extended: true}));
HTTPSAPP.use(BODY_PARSER.json({limit: '40mb', extended: true}));
HTTPSAPP.use('/', ROUTER);
HTTPSAPP.use(EXPRESS.static(__dirname + '/public'));
let options = {
key: FS.readFileSync(CERT_LOC + 'privkey.pem'),
cert: FS.readFileSync(CERT_LOC + 'fullchain.pem'),
ciphers: cipher
};
HTTPS.createServer(options, HTTPSAPP).listen(SECURE_PORT);
HTTP.createServer(HTTPAPP).listen(HTTP_PORT);
// Spawn a peer object for the user, currently testing right now
// spawn('node',['./Mp3_Dump/seed_music.js']);
// UDP Requests to Initialize Peer proceses
PEER_LINK_SOCKET.on('message', function(msg, rinfo) {
UTIL.logAsync(`Server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
// Check whether the message is an open peer message
if (msg == "open"){
UTIL.logAsync(`Creating a peer object for ${rinfo.address}:${rinfo.port}`);
LINK_MIDDLEWARE.createPeer().then((port) => {
let bufferUtil = Buffer.allocUnsafe(8);
bufferUtil.writeInt32BE(port);
// .send(Uint8Array, Offset, Byte Count, port, address
PEER_LINK_SOCKET.send(bufferUtil, 0, bufferUtil.byteLength, rinfo.port, rinfo.address,(err) => {
if(err) {
UTIL.logAsync("Error sending a request to the client");
}
});
}).catch((err) => {
UTIL.logAsync(`Error initializing peer object.\nError Message: ${err.message}`);
});
}
});
// Specifies that the server is listening on specified port
PEER_LINK_SOCKET.on('listening', ()=>{
const address = PEER_LINK_SOCKET.address();
UTIL.logAsync(`Server listening ${address.address} : ${address.port}`);
});
PEER_LINK_SOCKET.bind(SPAWN_PEER_PORT);
// GRPC for node communication
// Create a list that the server keeps track of for all of the current nodes that are alive
const aliveNodePorts = [];
// GRPC Server
function sort(call, callback) {
UTIL.logAsync("Server starting the sort implementation");
let songFile = FS.readFileSync(CONSTANTS.SONG_DATABASE_FILE);
let mapList = createKeyValuePair(JSON.parse(songFile));
// let albumFile = FS.readFileSync(CONSTANTS.SONG_DATABASE_FILE);
// let mapList = createKeyValuePair(JSON.parse(albumFile));
// let artistFile = FS.readFileSync(CONSTANTS.ARTIST_DATABASE_FILE);
// let mapList = createKeyValuePair(JSON.parse(artistFile));
// let guidToInfoFile = FS.readFileSync(CONSTANTS.GUID_MAPPING_FILE);
// let mapList = createKeyValuePair(JSON.parse(guidToInfoFile));
UTIL.logAsync("Server finished splitting the files");
let alphabetStartNum = 65;
let incrVal = Math.ceil(26/aliveNodePorts.length);
let alphabetEndNum = alphabetStartNum + incrVal;
let alphabetRangePortMapDict = {};
for(let i = 0; i < aliveNodePorts.length; i++) {
let currNodePort = aliveNodePorts[i];
UTIL.logAsync(`Sending map request to ${currNodePort}`);
alphabetRangePortMapDict[`${alphabetStartNum}, ${alphabetEndNum}`] = currNodePort;
alphabetStartNum = alphabetEndNum + 1;
alphabetEndNum = alphabetStartNum + incrVal;
}
alphabetStartNum = 65;
alphabetEndNum = alphabetStartNum + incrVal;
for(let i = 0; i < aliveNodePorts.length; i++) {
let currNodePort = aliveNodePorts[i];
let currToNode = new nodeProto.NodeRequests(`0.0.0.0:${currNodePort}`, grpc.credentials.createInsecure());
currToNode.map({
keyVal : mapList[i],
alphabetRangeStart : alphabetStartNum,
alphabetRangeStop : alphabetEndNum,
alphabetRangePortMap : alphabetRangePortMapDict
}, function(err, response) {
if(err) {
UTIL.logAsync(err);
} else {
UTIL.logAsync(`Successfully send map request node`);
}
});
alphabetStartNum = alphabetEndNum + 1;
alphabetEndNum = alphabetStartNum + incrVal;
}
// Do something
callback(null);
}
// Create a list that contains the current file's key, value mapping into their respective
// member node lists
function createKeyValuePair(currFile){
let keys = Object.keys(currFile);
let memberCount = aliveNodePorts.length;
// Create a map for each member and into their map as appropriate
let mapList = new Array(memberCount);
// Repeatedly add items to each member's maps in order
for (let i = 0; i < keys.length; i++) {
// Index is equal to the i value mod the number of members so that we can
// add in to the maps in consecutive orders
let index = i % memberCount;
// The current index is a new map object if it hasn't been initialized
if(!mapList[index]) {
mapList[index] = {}
}
// Add into the current mapList[index]
// Retrieve the current key and value associated with the key
let currKey = keys[i];
let currVal = JSON.stringify(currFile[currKey]);
mapList[index][currKey] = currVal;
}
return mapList;
}
// Nodes telling the server that they are alive
function creation(call, callback) {
let nodePort = call.request.portVal;
aliveNodePorts.push(nodePort);
UTIL.logAsync(`Server Received alive node ${nodePort}`);
callback(null);
}
let reduceFinishedCounter = 0;
let sortedStringList;
// Nodes telling the server that they have finished reducing
function reduceFinished(call, callback) {
UTIL.logAsync("Finished");
// There was a single node, print it out
if (call.request.alphabetRangeStart == 0) {
console.log(call.request.keyVal);
}else {
// Increment the finished Counter
reduceFinishedCounter += 1;
// Initialize the sortedStringList if it hasn't been initialized
if(!sortedStringList) {
sortedStringList = new Array(aliveNodePorts.length);
}
let storeIndex = (call.request.alphabetRangeStart - 65) / (Math.ceil(26 / aliveNodePorts.length) + 1);
// There was multiple nodes, concat, then print it out
if(Object.keys(call.request.keyVal) != 0) {
sortedStringList[storeIndex] = JSON.stringify(call.request.keyVal) + "\n";
}
if (reduceFinishedCounter == aliveNodePorts.length) {
// Concatenate the strings together
let printString = "";
for(let i = 0; i < aliveNodePorts.length; i++) {
if (sortedStringList[i]) {
// ------------- If anything broke delete code between lines -----------
console.log(JSON.parse(sortedStringList[i]));
let sortedJSONList = JSON.parse(sortedStringList[i]);
Object.entries(sortedJSONList).forEach(element =>{
printString += element[0] +"\n" + element[1] +"\n";
});
// ---------------------------------------------------------------------
//printString += sortedStringList[i] + "\n";
}
}
FS.writeFileSync('text.json', printString);
reduceFinishedCounter = 0;
}
}
callback(null);
}
const grpcNodeServer = new grpc.Server();
grpcNodeServer.addService(nodeProto.ServerRequests.service, {
sort : sort,
creation : creation,
reduceFinished : reduceFinished
});
grpcNodeServer.bind(`0.0.0.0:${SERVER_GRPC_PORT}`, grpc.ServerCredentials.createInsecure());
grpcNodeServer.start();