-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (161 loc) · 5.53 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
const app = require('express')();
//const app = express()
const mongoClient = require('mongodb').MongoClient;
const http = require('http').Server(app);
const io = require('socket.io')(http);
const formidable = require('formidable');
const bCrypt = require('bcrypt');
const crypto = require('crypto');
require('google-closure-library');
//goog.require('goog.html.sanitizer.HtmlSanitizer');
var mongoUrl = "mongodb://localhost:27017/";
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use("/public", express.static("pages/public"));
var userList = []
io.on('connection', (socket) => {
console.log("A user connected!");
socket.on('disconnect', () => {
console.log("A user disconnected...");
})
socket.on('login', (userData) => {
userList.push(userData.username);
})
socket.on('chat message', (data) => {
data["timestamp"] = new Date().getTime();
mongoClient.connect(mongoUrl, (err, db) => {
if (err) throw err;
var dbo = db.db("mostWanted");
dbo.collection("chatMessages").insertOne(data);
})
io.emit('update');
console.log(data);
})
socket.on('getUpdate', (timestamp) => {
mongoClient.connect(mongoUrl, (err, db) => {
if (err) throw err;
var dbo = db.db("mostWanted");
dbo.collection("chatMessages").find({ timestamp: { $gt: parseInt(timestamp) } }).toArray().then((result) => {
if (result.length > 0) {
var uidList = []
for (var i = 0; i < result.length; i++) {
uidList.push({ uid: result[i].uid });
}
uidList.filter((item, index) => { return uidList.indexOf(item) >= index; });
dbo.collection("users").find({ $or: uidList }).toArray().then((users) => {
var chat = Array.from(result);
var userRef = {};
for (var i = 0; i < users.length; i++) {
userRef[users[i].uid] = users[i];
}
for (var i = 0; i < chat.length; i++) {
// io.emit("debug", [userRef, chat]);
// console.log(userRef[chat[i].uid])
chat[i].nickname = userRef[chat[i].uid].nickname
}
io.emit("sendChatMessage", chat);
})
}
});
});
})
setInterval(() => {
io.emit("update");
io.emit("checkOnline");
// console.log(userList);
userList = [];
}, 10000);
})
app.get('/', (req, res) => {
res.sendFile(__dirname + "/pages/blankPage.html");
});
app.get('/testing', (req, res) => {
res.sendFile(__dirname + "/pages/test.html");
});
app.get('/loadPage.js', (req, res) => {
res.sendFile(__dirname + "/pages/public/loadPage.js");
});
app.get('/loadTest.js', (req, res) => {
res.sendFile(__dirname + "/pages/public/loadTest.js");
});
// app.get('/socket.io/socket.io.js', (req, res) => {
// res.sendFile(__dirname + "/node_modules/socket.io-client/dist/socket.io.js");
// });
app.get('/closure-library/:a/:b/:c', (req, res) => {
res.sendFile(__dirname + "/pages/public/closure-library/" + req.params.a + "/" + req.params.b + "/" + req.params.c);
});
app.get('/closure-library/:a/:b/:c/:d', (req, res) => {
res.sendFile(__dirname + "/pages/public/closure-library/" + req.params.a + "/" + req.params.b + "/" + req.params.c + "/" + req.params.d);
});
app.get('/closure-library/:a/:b/:c/:d/:e', (req, res) => {
res.sendFile(__dirname + "/pages/public/closure-library/" + req.params.a + "/" + req.params.b + "/" + req.params.c + "/" + req.params.d + "/" + req.params.e);
});
app.post('/api/register', (req, res) => {
var form = new formidable.IncomingForm();
var userData = { "admin": 0, "pfp": null };
var pw;
form.parse(req)
.on('field', function (name, value) {
userData[name] = value;
})
.on('end', function () {
pw = userData.password;
userData["nickname"] = userData.username;
register(userData).then(() => {
login(userData.username, pw).then((data) => {
res.send(data);
})
});
})
})
app.post('/api/online', (req, res) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, db) => {
if (err) throw err;
var dbo = db.db("mostWanted");
dbo.collection("users").findOne({ uid: req.body.uid, token: req.body.token }, (err, data) => {
if (err) throw err;
if (data == null) {
res.send("Oh no! You probably messed with your local storage. Logging in hasn't been implemented yet, so you can make a new user by deleting your uid.");
} else {
res.send(data);
}
})
})
})
async function register(userData) {
return new Promise((resolve, reject) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("mostWanted");
userData["uid"] = crypto.randomBytes(32).toString('hex');
bCrypt.hash(userData["password"], 10, (err, hash) => {
if (err) throw err;
userData["password"] = hash;
dbo.collection("users").insertOne(userData);
resolve("User created!");
});
})
})
}
async function login(username, password) {
return new Promise((resolve, reject) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("mostWanted");
dbo.collection("users").findOne({ username: username }).then(function (json) {
bCrypt.compare(password, json["password"], (err, result) => {
if (err) throw err;
if (result) {
var token = crypto.randomBytes(32).toString('hex');
dbo.collection("users").updateOne({ _id: json["_id"] }, { $set: { token: token } });
resolve({ status: "logged in", uid: json["uid"], token: token });
} else {
resolve({ status: "incorrect username or password" });
}
})
})
})
});
}
http.listen(3001, () => console.log('App listening on port 3001!'));