-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
244 lines (216 loc) · 7.07 KB
/
main.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
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const cookieParser = require('cookie-parser');
app.use(cookieParser());
const allowedOrigins = JSON.parse(process.env.FRONTEND_HOSTS);
const cors = require('cors');
app.use(cors({
origin: (origin, callback) => {
if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
}));
let server = null;
if (process.env.NODE_ENV === 'production') {
const fs = require('fs');
var privateKey = fs.readFileSync( '/etc/letsencrypt/live/api0.brettselby.xyz/privkey.pem' );
var certificate = fs.readFileSync( '/etc/letsencrypt/live/api0.brettselby.xyz/cert.pem' );
const https = require('https');
server = https.createServer({
key: privateKey,
cert: certificate
}, app);
} else {
const http = require('http');
server = http.createServer(app);
}
let db;
if (process.env.NODE_ENV === 'production' || process.env.USE_POSTGRES_DEV === 'true') {
const DB = require("./db");
db = new DB(
process.env.PG_USER,
process.env.PG_URL,
process.env.PG_DATABASE,
process.env.PG_PASSWORD,
process.env.PG_PORT
);
db.init();
} else {
const MockDB = require("./mockdb");
db = new MockDB();
}
const io = require("socket.io")(server, {
cors: {
origin: allowedOrigins,
methods: ["GET", "POST"]
}
});
const jwt = require('jsonwebtoken');
const bcrypt = require("bcrypt");
const {randomChoice, randAdd, randSub, randMul, randDiv} = require('./quiz');
const ops = {
'+': randAdd,
'-': randSub,
'*': randMul,
'/': randDiv,
};
const defaultConfig = {
'+': {
active: true,
max: 100,
negative: true,
},
'-': {
active: true,
max: 100,
negative: true,
},
'*': {
active: true,
max: 30,
negative: true,
},
'/': {
active: true,
max: 30,
negative: true,
},
};
let hasAuth = {};
let users = {};
let configs = {};
app.post('/signup', async (req, res) => {
try {
console.log(req.body);
const {userid, password, email} = req.body;
if (userid == null || userid == "" || userid.length > 32) {
console.log("Invalid username");
return res.status(400).json({message: "Invalid username"});
}
if (password == null || password == "" || password.length < 6) {
console.log("Invalid password");
return res.status(400).json({message: "Invalid password"});
}
if (email == null || email.length < 3 || email.indexOf("@") < 1 || email.indexOf("@") == email.length - 1) {
console.log("Invalid email");
return res.status(400).json({message: "Invalid email"});
}
let user = await db.getUser(userid);
if (user != null) {
res.status(401).json({message: "User already exists"});
return;
}
const saltRounds = 10;
bcrypt.hash(password, saltRounds, async (err, hash) => {
if (err) throw new Error("Internal Server Error");
let newUser = await db.createUser(userid, hash, email);
if (newUser != null) {
res.json({ message: "User created successfully", user: userid });
} else {
res.status(500).json({message: "Couldn't create user"});
}
});
} catch (err) {
return res.status(401).json({message: err.message});
}
});
app.post('/login', async (req, res) => {
try {
const {userid, password} = req.body;
console.log(req.body);
let user = await db.getUser(userid);
if (user == null) {
return res.status(401).json({ message: "Invalid Credentials" });
}
bcrypt.compare(password, user.password, (err, result) => {
if (result) {
const token = jwt.sign({sub: userid}, process.env.SECRET_KEY, { expiresIn: '7d' });
// res.cookie('token', token, { httpOnly: true });
return res.status(200).json({ message: "User Logged in Successfully", userid: userid, token: token, config: defaultConfig });
}
console.log(err);
return res.status(401).json({ message: "Invalid Credentials" });
});
} catch (err) {
return res.status(401).json({message: err.message});
}
});
const nextQuestion = (socket, userid, config) => {
let op = randomChoice(Object.keys(config).filter(k => config[k].active));
let question = ops[op](config[op]);
socket.emit('question', {question: question.str})
return {sent: Date.now(), ping: null, attempts: 0, question: question};
}
io.on('connection', (socket) => {
console.log('a user connected');
hasAuth[socket.id] = false;
const authTimeout = setTimeout(() => {
socket.emit('error', {error: 'auth timeout'});
socket.disconnect();
}, 5000);
socket.on('disconnect', () => {
delete hasAuth[socket]
console.log('user disconnected');
});
socket.on('auth', (data) => {
console.log(data);
let {userid, token} = data;
jwt.verify(token, process.env.SECRET_KEY, { subject: userid }, function(err, decoded) {
clearTimeout(authTimeout);
if (err) {
console.log(err.message)
socket.emit('error', {error: "invalid credentials"});
socket.disconnect();
} else {
console.log(decoded)
hasAuth[socket.id] = true;
configs[socket.id] = defaultConfig;
socket.emit('auth', decoded);
}
});
});
socket.on('start', (data) => {
let config = data.config || {};
let {userid} = data;
users[userid] = nextQuestion(socket, userid, config);
configs[userid] = config;
});
socket.on('ping', (data) => {
let {userid} = data;
if (!(userid in users)) {
socket.emit('error', {error: "invalid user"});
return;
}
users[userid].ping = Date.now() - users[userid].sent;
console.log(users);
});
socket.on('answer', async (data) => {
let {answer, userid} = data;
if (!(userid in users)) {
socket.emit('error', {error: "invalid user"});
return;
}
users[userid].attempts++;
if (Number(answer) === users[userid].question.result) {
const elapsed = Date.now() - users[userid].sent - users[userid].ping;
await db.createBenchmark(userid, users[userid].sent, users[userid].question.op, users[userid].question.l, users[userid].question.r, elapsed, users[userid].attempts);
socket.emit('correctAnswer');
users[userid] = nextQuestion(socket, userid, configs[userid]);
} else {
console.log(Number(answer))
console.log('!=')
console.log(users[userid].question.result)
socket.emit('incorrectAnswer', {response: answer});
}
console.log(users);
});
});
server.listen(process.env.PORT, () => {
console.log(`listening on *:${process.env.PORT}`);
});