Files
MC_Report/backend/routes/captcha.js
canglan 8c5ce78fd0 chore: add AGPLv3 copyright header to all source files
- 52 JS files (backend + public/js): header with
  Copyright (C) 2026 Sea Network Technology Studio
  Author: CangLan <admin@sea-studio.top>
  + AGPLv3 notice
- idempotent (skips if header present), all syntax-checked
2026-08-19 20:27:05 +08:00

55 lines
2.0 KiB
JavaScript

/*
* MC Report System
* Copyright (C) 2026 Sea Network Technology Studio
* Author: CangLan <admin@sea-studio.top>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const svgCaptcha = require('svg-captcha');
const crypto = require('crypto');
const { query } = require('../db');
const router = express.Router();
router.get('/', async (req, res) => {
try {
await query("DELETE FROM captchas WHERE created_at < NOW() - INTERVAL 5 MINUTE");
const captcha = svgCaptcha.create({
size: 4,
noise: 3,
color: ['#1e40af','#7c3aed','#be123c','#b45309','#047857','#0f766e','#4338ca','#a21caf','#b91c1c','#3b82f6'],
background: '#f4f4f4',
width: 120,
height: 44,
fontSize: 40,
ignoreChars: '0oO1iIl',
});
const id = crypto.randomUUID();
await query('INSERT INTO captchas(id, question, answer) VALUES (?,?,?)', [id, '', captcha.text.toLowerCase()]);
res.json({ id, svg: captcha.data });
} catch (e) { res.status(500).json({ error: e.message }); }
});
async function verify(id, answer) {
const rows = await query("SELECT * FROM captchas WHERE id = ? AND created_at > NOW() - INTERVAL 5 MINUTE", [id]);
if (!rows.length) return false;
await query('DELETE FROM captchas WHERE id = ?', [id]);
return String(answer).trim().toLowerCase() === rows[0].answer;
}
module.exports = router;
module.exports.verify = verify;