-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfddrace-web.js
713 lines (659 loc) · 23.7 KB
/
fddrace-web.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
const fetch = require('node-fetch')
const { exec } = require('child_process')
const express = require('express')
const session = require('express-session')
const fs = require('fs')
const redis = require('redis')
const { v4: uuidv4 } = require('uuid')
const app = express()
const dotenv = require('dotenv')
const redisStore = require('connect-redis')(session)
const redisClient = redis.createClient()
dotenv.config()
const { sendMailPassword, sendMailVerify } = require('./src/mail')
const { loginAccount, getAccsByEmail } = require('./src/account')
const { execCmd, testEcon } = require('./src/api')
const { insertSurvey, updateSurvey, getDb } = require('./src/survey')
const logger = require('./src/logger')
const port = 5690
// Add headers
// https://stackoverflow.com/a/18311469
app.use(function (req, res, next) {
// TODO: make this more dynamic and decide on a front end port (9090 for now)
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9090')
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
// Set to true if you need the website to include cookies in the requests sent
// res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next()
})
app.use(
express.urlencoded({
extended: true
})
)
const isCaptcha = process.env.CAPTCHA_BACKEND && process.env.CAPTCHA_BACKEND !== ''
const captchaData = {}
const SCORE_HUMAN = 1
app.use(session({
secret: process.env.SESSION_SECRET,
/* eslint-disable new-cap */
store: new redisStore({ host: 'localhost', port: 6379, client: redisClient, ttl: 260 }),
/* eslint-enable new-cap */
saveUninitialized: true,
resave: true
}))
app.set('view engine', 'ejs')
const sanitizeGmail = email => {
if (email.endsWith('gmail.com')) {
return email.toLowerCase().replace(/@gmail.com$/, '').replaceAll('.', '') + '@gmail.com'
} else if (email.endsWith('googlemail.com')) {
return email.toLowerCase().replace(/@googlemail.com$/, '').replaceAll('.', '') + '@googlemail.com'
}
return email
}
const whitelist = (context, ipAddr) => {
const ipv4Regex = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/gm
let message = ''
let success = false
if (!ipv4Regex.test(ipAddr)) {
message = `<h1 style="color: red">Your ip <span style="color: rgb(130, 130, 130)">${ipAddr}</span> is not a valid ipv4!</h1>`
} else {
success = true
message = `<h1>Your ip <span style="color: rgb(130, 130, 130)">${ipAddr}</span> is now whitelisted!</h1>`
exec(`./wl.sh ${ipAddr}`, (err, stdout, stderr) => {
if (err) {
logger.logAndThrow(err)
}
if (stdout) {
logger.log(context, `stdout: ${stdout}`)
}
if (stderr) {
logger.log(context, `stderr: ${stderr}`)
}
})
}
return { message, success }
}
app.get('/', (req, res) => {
// const ipAddr = (req.header('x-forwarded-for') || req.socket.remoteAddress).split(',')[0]
// const message = whitelist('index', req, ipAddr)
if (!req.session.verified) {
res.redirect('/verify')
return
}
const message = ''
res.render('index', {
whitelistMessage: message,
serverIp: process.env.IP_ADDR,
data: req.session.data,
token: process.env.CAPTCHA_TOKEN,
hostname: process.env.HOSTNAME,
captchaBackend: process.env.CAPTCHA_BACKEND
})
})
app.get('/verify', (req, res) => {
const token = uuidv4()
let errMsg = false
if (req.query.verify === 'fail') {
errMsg = 'Failed to verify.'
} else if (req.query.verify === 'robot') {
errMsg = 'Failed to verify. Are you a robot?'
} else if (req.query.verify === 'token') {
errMsg = 'Failed to verify. Invalid token.'
}
res.render('verify', {
messageGreen: req.query.password === 'success' ? 'Password reset successfully' : false,
messageRed: errMsg,
token: token,
isCaptcha: isCaptcha,
hostname: process.env.HOSTNAME,
serverIp: process.env.IP_ADDR,
captchaBackend: process.env.CAPTCHA_BACKEND
})
})
const verifyCaptchaPassed = async (req, res) => {
// tokens are one use only
delete captchaData[req.body.token]
const ipAddr = (req.header('x-forwarded-for') || req.socket.remoteAddress).split(',')[0]
const wlistResult = whitelist('verify', ipAddr)
const message = wlistResult.message
if (wlistResult.success) {
req.session.verified = true
}
res.render('index', {
whitelistMessage: message,
serverIp: process.env.IP_ADDR,
data: req.session.data,
token: process.env.CAPTCHA_TOKEN,
hostname: process.env.HOSTNAME,
captchaBackend: process.env.CAPTCHA_BACKEND
})
}
app.post('/verify', async (req, res) => {
if (!req.body.token) {
res.redirect('/verify?verify=robot')
return
}
const hexKey = Buffer.from(process.env.IP_ADDR + process.env.HOSTNAME + req.body.token, 'utf8').toString('hex')
const captchaUrl = `${process.env.CAPTCHA_BACKEND}/score/${hexKey}`
if (!isCaptcha) {
res.end('<html>Missing captcha. Please contact an admin<a href="/">back</a></html>')
return
}
if (captchaData[req.body.token] === SCORE_HUMAN) {
verifyCaptchaPassed(req, res)
return
}
fetch(captchaUrl)
.then(data => data.text())
.then(text => {
logger.log('verify', 'captcha data:')
logger.log('verify', text)
const result = JSON.parse(text)
if (result.score !== SCORE_HUMAN) {
res.redirect('/verify?verify=robot')
} else {
verifyCaptchaPassed(req, res)
}
})
})
app.get('/login', (req, res) => {
const token = uuidv4()
let errMsg = false
if (req.query.login === 'fail') {
errMsg = 'Failed to login.'
} else if (req.query.login === 'robot') {
errMsg = 'Failed to login. Are you a robot?'
} else if (req.query.login === 'token') {
errMsg = 'Failed to login. Invalid alpha token.'
}
res.render('login', {
messageGreen: req.query.password === 'success' ? 'Password reset successfully' : false,
messageRed: errMsg,
token: token,
isCaptcha: isCaptcha,
hostname: process.env.HOSTNAME,
serverIp: process.env.IP_ADDR,
captchaBackend: process.env.CAPTCHA_BACKEND
})
})
app.get('/account', (req, res) => {
if (req.session.data) {
getDb().get('SELECT * FROM Answers WHERE username = ?', req.session.data.username, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
if (rows) {
res.render('account', {
voted: true,
data: req.session.data,
messageGreen: req.query.mail === 'success' ? 'E-Mail verified' : false
})
} else {
res.render('account', {
voted: false,
data: req.session.data,
messageGreen: req.query.mail === 'success' ? 'E-Mail verified' : false
})
}
})
} else {
res.redirect('/login')
}
})
app.post('/account', (req, res) => {
if (!req.session.data || !req.session.data.username || req.session.data.username === '') {
res.redirect('/login')
return
}
res.writeHead(200, { 'Content-Type': 'text/html' })
const token = uuidv4()
if (!req.body.email || req.body.email === '' || !req.body.email.match(/^[a-zA-Z0-9.\-@]+$/)) {
res.end('<html>Invalid mail.<a href="account">back</a></html>')
return
}
if (!req.session.data.security_pin.match(/[0-9]+/)) {
res.end('<html>Please set a pin. Check /pin in game<a href="account">back</a></html>')
return
}
const email = req.body.email.trim().toLowerCase()
redisClient.get(sanitizeGmail(email), (err, reply) => {
if (err) logger.logAndThrow(err)
if (reply !== null) {
const emailData = JSON.parse(reply)
if (req.body.pin !== req.session.data.security_pin) {
if (Object.prototype.hasOwnProperty.call(emailData, 'pinExpire')) {
const today = new Date().toISOString().split('T')[0]
if (emailData.pinExpire > today) {
res.end('<html>Ratelimited pin attempts. Try again tomorrow.<a href="account">back</a></html>')
return
}
}
if (Object.prototype.hasOwnProperty.call(emailData, 'pinAttempts')) {
emailData.pinAttempts += 1
} else {
emailData.pinAttempts = 0
}
if (emailData.pinAttempts >= 3) {
emailData.pinAttempts = 0
// ban 1 day after 3 attempts
const expireDate = new Date()
expireDate.setTime(expireDate.getTime() + 1 * 86400000)
emailData.pinExpire = expireDate.toISOString().split('T')[0]
redisClient.set(sanitizeGmail(email), JSON.stringify(emailData), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('email-update', `email='${email}' pin attempts=${emailData.pinAttempts} (banned) redis response: ${reply}`)
})
}
redisClient.set(sanitizeGmail(email), JSON.stringify(emailData), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('email-update', `email='${email}' pin attempts=${emailData.pinAttempts} redis response: ${reply}`)
})
res.end('<html>Invalid pin. Check /pin in game<a href="account">back</a></html>')
return
}
}
if (req.body.pin !== req.session.data.security_pin) {
redisClient.set(sanitizeGmail(email), JSON.stringify({ pinAttempts: 0 }), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('email-update', `email='${email}' pin attempts=0 redis response: ${reply}`)
})
res.end('<html>Invalid pin. Check /pin in game<a href="account">back</a></html>')
return
}
const acc = getAccsByEmail(email)[0]
if (acc) {
res.end('<html>Email already in use.<a href="account">back</a></html>')
return
}
redisClient.get(sanitizeGmail(email), (err, reply) => {
if (err) logger.logAndThrow(err)
if (reply !== null) {
const emailData = JSON.parse(reply)
if (Object.prototype.hasOwnProperty.call(emailData, 'expire')) {
const today = new Date().toISOString().split('T')[0]
if (emailData.expire > today) {
logger.log('email-update', `Error: email ratelimit email=${email} expire=${emailData.expire} today=${today}`)
res.end('<html>Email already pending. Try again later.<a href="account">back</a></html>')
return
}
}
}
const username = req.session.data.username
const expireDate = new Date()
expireDate.setTime(expireDate.getTime() + 3 * 86400000)
redisClient.set(token, JSON.stringify({ username: username, email: email, expire: expireDate.toISOString().split('T')[0] }), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('email-update', `email='${email}' username='${username}' redis response: ${reply}`)
})
sendMailVerify(email, token)
res.end('<html>Check your mail. (also in spam folder -.-)<a href="account">back</a></html>')
})
})
})
app.get('/verify-email', async (req, res) => {
const { token } = req.query
redisClient.get(token, async (err, reply) => {
if (err || reply === null) {
res.end('Invalid token.')
return
}
const data = JSON.parse(reply)
const today = new Date().toISOString().split('T')[0]
if (data.expire <= today) {
redisClient.del(token, (err, reply) => {
if (err) logger.logAndThrow(err)
})
logger.log('verify-email', `Error: expired token username=${data.username} expire=${data.expire} today=${today}`)
res.end('Expired token')
return
}
const username = data.username
const email = data.email
if (!username || !email) {
res.end('Something went wrong.')
return
}
const loggedIn = await loginAccount(username, null)
if (!loggedIn) {
res.end('Something went horribly wrong')
return
}
execCmd('econ', `acc_edit ${username} email "${email}"`)
req.session.data = loggedIn
req.session.data.email = email
redisClient.del(token, (err, reply) => {
if (err) logger.logAndThrow(err)
})
res.redirect('/account?mail=success')
})
})
app.get('/logout', (req, res) => {
req.session.destroy(err => {
if (err) {
logger.log('logout', err)
} else {
res.redirect('/')
}
})
})
const loginCaptchaPassed = async (req, res) => {
if (process.env.ALPHA_TOKEN && req.body.alphatoken !== process.env.ALPHA_TOKEN) {
res.redirect('/login?login=fail-token')
return
}
// tokens are one use only
delete captchaData[req.body.token]
const loggedIn = await loginAccount(req.body.username, req.body.password)
if (typeof loggedIn === 'string' || loggedIn instanceof String) {
res.end(`<html>${loggedIn} <a href="login">back</a></html>`)
} else if (loggedIn) {
req.session.data = loggedIn
logger.log('login', `'${req.body.username}' logged in addr=${req.header('x-forwarded-for') || req.socket.remoteAddress}`)
res.redirect('/account')
} else {
res.redirect('/login?login=fail')
}
}
app.post('/login', async (req, res) => {
if (!req.body.token) {
res.redirect('/login?login=robot')
return
}
const hexKey = Buffer.from(process.env.IP_ADDR + process.env.HOSTNAME + req.body.token, 'utf8').toString('hex')
const captchaUrl = `${process.env.CAPTCHA_BACKEND}/score/${hexKey}`
if (isCaptcha) {
if (captchaData[req.body.token] !== 1) {
fetch(captchaUrl)
.then(data => data.text())
.then(text => {
logger.log('login', 'captcha data:')
logger.log('login', text)
const result = JSON.parse(text)
if (result.score !== SCORE_HUMAN) {
res.redirect('/login?login=robot')
} else {
loginCaptchaPassed(req, res)
}
})
return
}
}
loginCaptchaPassed(req, res)
})
app.post('/new-password', (req, res) => {
const { password, password2, token } = req.body
if (password !== password2) {
res.end('Passwords do not match')
return
}
if (typeof token !== 'string') {
res.end('Invalid token.')
return
}
logger.log('new-password', `get token='${token}'`)
redisClient.get(token, (err, reply) => {
if (err || reply === null) {
res.end('Invalid token.')
return
}
const data = JSON.parse(reply)
logger.log('new-pasword', data)
const today = new Date().toISOString().split('T')[0]
if (data.expire <= today) {
redisClient.del(token, (err, reply) => {
if (err) logger.logAndThrow(err)
})
logger.log('new-password', `Error: expired token username=${data.username} expire=${data.expire} today=${today}`)
res.end('Expired token')
return
}
execCmd('econ', `acc_edit ${data.username} password "${password}"`)
redisClient.del(token, (err, reply) => {
if (err) logger.logAndThrow(err)
})
res.redirect('/login?password=success')
})
})
app.get('/new-password', (req, res) => {
const { token } = req.query
redisClient.get(token, (err, reply) => {
if (err || reply === null) {
res.end('Invalid token.')
return
}
logger.log('new-password', reply)
res.render('new-password', { username: JSON.parse(reply).username, token: token })
})
})
app.get('/survey', async (req, res) => {
if (!req.session.data) {
res.redirect('/login')
return
}
if (req.session.data.level < 10) {
res.end('<html>You have to be at least level 10 to take part in the survey.<a href="/">okay</a></html>')
return
}
const questions = JSON.parse(fs.readFileSync('survey.json', 'UTF-8'))
const userIp = req.header('x-forwarded-for') || req.socket.remoteAddress
getDb().get('SELECT * FROM Answers WHERE username = ?', req.session.data.username, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
if (rows) {
logger.log('survey', `'${req.session.data.username}' started editing the survey`)
res.render('survey', { data: req.session.data, questions: questions, answers: rows, isEdit: true })
} else {
getDb().get('SELECT COUNT(*) AS count FROM Answers WHERE ip = ?', userIp, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
if (rows && rows.count > 2) {
logger.log('survey', `'${req.session.data.username}' could not vote due to ip limit reach`)
res.end('<html>You already voted with another account.<br><a href="/">back</a><br><a href="/survey_result">results</a></html>')
} else {
if (rows) {
logger.log('survey', `'${req.session.data.username}' ip_votes=${rows.count} started doing the survey`)
} else {
logger.log('survey', `'${req.session.data.username}' started doing the survey`)
}
res.render('survey', { data: req.session.data, questions: questions, answers: [], isEdit: false })
}
})
}
})
})
const getSurveyResult = (index) => {
return new Promise(resolve => {
getDb().get(`
SELECT question${index}, COUNT(question${index}) AS c
FROM Answers
WHERE question${index} != ''
GROUP BY question${index}
ORDER BY c DESC;
`, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
resolve(rows)
})
})
}
app.get('/survey_result', async (req, res) => {
const questions = JSON.parse(fs.readFileSync('survey.json', 'UTF-8'))
const results = []
for (let i = 0; i < questions.length; i++) {
const row = await getSurveyResult(i)
if (row) {
results.push(row)
}
}
res.render('survey_result', { data: req.session.data, results: results, questions: questions })
})
app.post('/survey', async (req, res) => {
if (!req.session.data) {
res.redirect('/login')
return
}
if (req.session.data.level < 10) {
res.end('<html>You have to be at least level 10 to take part in the survey.<a href="/">okay</a></html>')
return
}
const userIp = req.header('x-forwarded-for') || req.socket.remoteAddress
getDb().get('SELECT * FROM Answers WHERE username = ?', req.session.data.username, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
if (rows) {
logger.log('survey', `'${req.session.data.username}' updated his vote: ${req.body.questions}`)
updateSurvey(
req.session.data.username,
req.body.questions
)
res.redirect('/survey_result')
} else {
if (req.body.questions.every(q => q === '' || !q)) {
logger.log('survey', `'${req.session.data.username}' skipping empty vote`)
res.redirect('/survey_result')
return
}
getDb().get('SELECT COUNT(*) AS count FROM Answers WHERE ip = ?', userIp, (err, rows) => {
if (err) {
logger.logAndThrow(err)
}
if (rows && rows.count > 2) {
logger.log('survey', `'${req.session.data.username}' could not vote due to ip limit reach`)
res.end('<html>You already voted with another account.<a href="/">okay</a></html>')
} else {
if (rows) {
console.log(rows.count)
logger.log('survey', `'${req.session.data.username}' ip_votes=${rows.count} voted: ${req.body.questions}`)
} else {
logger.log('survey', `'${req.session.data.username}' voted: ${req.body.questions}`)
}
insertSurvey(
req.session.data.username,
userIp,
req.body.questions
)
res.redirect('/survey_result')
}
})
}
})
})
app.get('/reset', (req, res) => {
res.render('reset')
})
app.post('/reset', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' })
const token = uuidv4()
if (!req.body.email || req.body.email === '' || !req.body.email.match(/^[a-zA-Z0-9.\-@]+$/)) {
res.end('<html>Invalid mail.<a href="reset">back</a></html>')
return
}
const email = req.body.email.trim().toLowerCase()
const acc = getAccsByEmail(email)[0]
if (!acc) {
// keep same message here as in happy path
// to not leak emails
// but tbh when setting the email it shows a error if others already use it so ye...
res.end('<html>Check your mail. (also in spam folder -.-)<a href="reset">back</a></html>')
return
}
redisClient.get(sanitizeGmail(email), (err, reply) => {
if (err) logger.logAndThrow(err)
if (reply !== null) {
const emailData = JSON.parse(reply)
if (Object.prototype.hasOwnProperty.call(emailData, 'expire')) {
const today = new Date().toISOString().split('T')[0]
if (emailData.expire > today) {
logger.log('password-reset', `Error: email ratelimit email=${email} expire=${emailData.expire} today=${today}`)
res.end('<html>Password reset already pending. Try again later.<a href="reset">back</a></html>')
return
}
}
}
const expireDate = new Date()
expireDate.setTime(expireDate.getTime() + 3 * 86400000)
const username = acc.username
redisClient.set(token, JSON.stringify({ username: username, expire: expireDate.toISOString().split('T')[0] }), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('password-reset', `token email='${email}' username='${username}' redis response: ${reply}`)
})
redisClient.set(sanitizeGmail(email), JSON.stringify({ expire: expireDate.toISOString().split('T')[0] }), (err, reply) => {
if (err) logger.logAndThrow(err)
logger.log('password-reset', `email email='${email}' username='${username}' redis response: ${reply}`)
})
sendMailPassword(email, token)
res.end('<html>Check your mail. (also in spam folder -.-)<a href="reset">back</a></html>')
})
})
app.use(express.json())
app.set('trust proxy', true)
app.post('/', (req, res) => {
const reqHost = `${req.protocol}://${req.header('Host')}`
const reqAddr = `${req.headers['x-forwarded-for'] || req.socket.remoteAddress}`.split(',')[0]
const isOwnAddr = reqAddr === process.env.IP_ADDR
const isCaptchaAddr = reqAddr === process.env.CAPTCHA_BACKEND_IP
if (reqHost !== process.env.CAPTCHA_BACKEND && !isOwnAddr && !isCaptchaAddr) {
logger.log('captcha', `blocked post from invalid host='${reqHost}' addr='${reqAddr}' expected='${process.env.CAPTCHA_BACKEND}'`)
res.end('ERROR')
return
}
const score = req.body.score
if (score === SCORE_HUMAN) {
// do not save robot scores to save memory
captchaData[req.body.token] = score
logger.log('captcha', `result=hooman ip=${req.ip}`)
} else {
logger.log('captcha', `result=robot ip=${req.ip}`)
}
res.end('OK')
})
/*
/players/:player
seves a text file like players.txt as an array
using the url parameter :player to search for entries
the file is expected to be in the unix 'sort -nr' format
the file does not have to be sorted
so the importance of the player followed by its name:
100 nameless tee
65 brainless tee
10 (1)nameless tee
200 unsorted_also_works
This file can be generated from server logs using this script
https://github.com/lib-crash/lib-teeworlds/blob/master/bin/tw_get_unique_names
*/
app.get('/api/players/:player', (req, res) => {
const player = decodeURIComponent(req.params.player)
const players = []
if (!process.env.PLAYER_NAMES_PATH) {
res.end('[]')
return []
}
if (!fs.existsSync(process.env.PLAYER_NAMES_PATH)) {
res.end('[]')
return []
}
fs.readFileSync(process.env.PLAYER_NAMES_PATH, 'UTF-8')
.split(/\r?\n/)
.filter(data => data.toLowerCase().includes(player.toLowerCase()))
.forEach(line => {
const data = line.trim().split(' ')
players.push([parseInt(data[0], 10), data.slice(1).join(' ').trim()])
})
players.sort((p1, p2) => p2[0] - p1[0])
res.send(JSON.stringify(players.map(player => player[1]).slice(0, 10)))
})
app.use(express.static('static'))
app.listen(port, '0.0.0.0', () => {
logger.log('server', `App running on http://localhost:${port}.`)
testEcon()
})