improved captchas fix #1

merge-requests/208/head
fatchan 5 years ago
parent 6ea3f29d17
commit e075b26228
  1. 30
      helpers/captchagenerate.js
  2. 5
      models/pages/captcha.js

@ -17,16 +17,30 @@ module.exports = (text, captchaId) => {
text = text.split(''); //array of chars
const x = gm(200, 80, '#fff')
.fill('#000')
.fontSize(80)
.fontSize(70)
let lastx = 0;
for (let i = 0; i <6; i++) {
x.drawText(i*30, 55+rr(0,10), text[i])
}
for (let i = 0; i <4; i++) {
const shape = getShape();
x.strokeWidth(rr(3, 5))
x.drawCircle(shape.x1, shape.y1, shape.x2, shape.y1)
x.drawText(lastx, 50+rr(0,10), text[i])
switch (text[i]) {
case 'w':
case 'm':
lastx += 40;
break;
case 'i':
case 'f':
case 'l':
case 'j':
case 't':
lastx += 15;
break;
default:
lastx += 30;
break;
}
}
x.wave(10, rr(50,80))
const recy1 = rr(30,50)
x.drawRectangle(rr(5,10), recy1, rr(190,195), recy1+5)
.wave(10, rr(80,120))
.blur(1, 2)
.crop(200, 80, 0, 0)
.quality(30)

@ -1,6 +1,6 @@
'use strict';
const crypto = require('crypto')
const { randomBytes } = require('crypto')
, Captchas = require(__dirname+'/../../db/captchas.js')
, generateCaptcha = require(__dirname+'/../../helpers/captchagenerate.js');
@ -8,9 +8,10 @@ module.exports = async (req, res, next) => {
// if we got here, they dont have a cookie so we need to
// gen a captcha, set their cookie and redirect to the captcha
const text = crypto.randomBytes(20).toString('hex').substring(0,6);
let captchaId;
try {
// const text = await randomBytes(3).toString('hex').substring(0,6);
const text = Math.random().toString(36).substr(2,6);
captchaId = await Captchas.insertOne(text).then(r => r.insertedId); //get id of document as filename and captchaid
await generateCaptcha(text, captchaId);
} catch (err) {

Loading…
Cancel
Save