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.
60 lines
2.1 KiB
60 lines
2.1 KiB
'use strict';
|
|
|
|
const { Ratelimits } = require(__dirname+'/../../../db/')
|
|
, checkCaptcha = require(__dirname+'/../../captcha/captcha.js')
|
|
, config = require(__dirname+'/../../misc/config.js')
|
|
, remove = require('fs-extra').remove
|
|
, dynamicResponse = require(__dirname+'/../../misc/dynamic.js')
|
|
, deleteTempFiles = require(__dirname+'/../../file/deletetempfiles.js')
|
|
, uploadDirectory = require(__dirname+'/../../file/uploaddirectory.js');
|
|
|
|
module.exports = async (req, res, next) => {
|
|
|
|
//already solved in pre stage for getting bypassID for "ip"
|
|
if (res.locals.anonymizer && res.locals.solvedCaptcha) {
|
|
return next();
|
|
}
|
|
|
|
//skip captcha if disabled on board for posts only
|
|
if (res.locals.board
|
|
&& req.path === `/board/${res.locals.board._id}/post`) {
|
|
if (res.locals.board.settings.captchaMode === 0 //if captcha disabled
|
|
|| (res.locals.board.settings.captchaMode === 1 && req.body.thread)) { //or if enabled for threads, and not a thread
|
|
return next(); //then skip checking captcha
|
|
}
|
|
}
|
|
|
|
const captchaInput = req.body.captcha || req.body['g-recaptcha-response'] || req.body['h-captcha-response'];
|
|
const captchaId = req.cookies.captchaid;
|
|
try {
|
|
await checkCaptcha(captchaInput, captchaId);
|
|
} catch (err) {
|
|
deleteTempFiles(req).catch(console.error);
|
|
if (err instanceof Error) {
|
|
return next(err);
|
|
}
|
|
const page = (req.body.minimal || req.path === '/blockbypass' ? 'bypass' : 'message');
|
|
return dynamicResponse(req, res, 403, page, {
|
|
'title': 'Forbidden',
|
|
'message': err,
|
|
'redirect': req.headers.referer,
|
|
});
|
|
}
|
|
|
|
//it was correct, so mark as solved for other middleware
|
|
res.locals.solvedCaptcha = true;
|
|
|
|
const { captchaOptions } = config.get;
|
|
if (captchaOptions.type !== 'google' && captchaOptions.type !== 'hcaptcha') {
|
|
//for builtin captchas, clear captchaid cookie, delete file and reset quota
|
|
res.clearCookie('captchaid');
|
|
await Promise.all([
|
|
!res.locals.anonymizer && Ratelimits.resetQuota(res.locals.ip.cloak, 'captcha'),
|
|
remove(`${uploadDirectory}/captcha/${captchaId}.jpg`)
|
|
]);
|
|
}
|
|
|
|
//completed captcha successfully, continue
|
|
return next();
|
|
|
|
};
|
|
|