mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1006 B
34 lines
1006 B
'use strict';
|
|
|
|
const { Ratelimits } = require(__dirname+'/../../db/')
|
|
, generateCaptcha = require(__dirname+'/../../helpers/captcha/captchagenerate.js')
|
|
, { secureCookies, rateLimitCost } = require(__dirname+'/../../configs/main.js')
|
|
, production = process.env.NODE_ENV === 'production';
|
|
|
|
module.exports = async (req, res, next) => {
|
|
|
|
if (!production && req.cookies['captchaid'] != null) {
|
|
return res.redirect(`/captcha/${req.cookies['captchaid']}.jpg`);
|
|
}
|
|
|
|
let captchaId;
|
|
try {
|
|
const ratelimit = await Ratelimits.incrmentQuota(res.locals.ip.hash, 'captcha', rateLimitCost.captcha);
|
|
if (ratelimit > 100) {
|
|
return res.status(429).redirect('/file/ratelimit.png');
|
|
}
|
|
const { id, text } = await generateCaptcha();
|
|
captchaId = id;
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
|
|
return res
|
|
.cookie('captchaid', captchaId.toString(), {
|
|
'maxAge': 5*60*1000, //5 minute cookie
|
|
'secure': production && secureCookies,
|
|
'sameSite': 'strict'
|
|
})
|
|
.redirect(`/captcha/${captchaId}.jpg`);
|
|
|
|
}
|
|
|