more flexible
This commit is contained in:
parent
044f4094f0
commit
7ab9d70d47
|
@ -1,6 +1,7 @@
|
||||||
FROM node:22-bullseye
|
FROM node:22-bullseye
|
||||||
ARG APP_PORT
|
ARG APP_PORT
|
||||||
ENV APP_PORT $APP_PORT
|
ENV APP_PORT $APP_PORT
|
||||||
|
WORKDIR /app
|
||||||
COPY migrations migrations
|
COPY migrations migrations
|
||||||
COPY public public
|
COPY public public
|
||||||
COPY views views
|
COPY views views
|
||||||
|
|
|
@ -5,18 +5,18 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- ${APP_PORT}:${APP_PORT}
|
- ${APP_PORT}:${APP_PORT}
|
||||||
depends_on:
|
depends_on:
|
||||||
- database
|
database:
|
||||||
|
condition: service_healthy
|
||||||
restart: on-failure
|
restart: on-failure
|
||||||
volumes:
|
volumes:
|
||||||
- ./data/uploads:${DATA_DIR}
|
- ./data/uploads:${DATA_DIR}
|
||||||
|
- ./.env:/app/.env:ro
|
||||||
database:
|
database:
|
||||||
hostname: database
|
hostname: database
|
||||||
image: 'postgres:15'
|
image: 'postgres:15'
|
||||||
volumes:
|
volumes:
|
||||||
- ./data/db:/var/lib/postgresql/data
|
- ./data/db:/var/lib/postgresql/data
|
||||||
env_file: .env
|
env_file: .env
|
||||||
ports:
|
|
||||||
- 5432:5432
|
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: ${DBUSER}
|
POSTGRES_USER: ${DBUSER}
|
||||||
POSTGRES_PASSWORD: ${DBPASS}
|
POSTGRES_PASSWORD: ${DBPASS}
|
||||||
|
|
|
@ -54,6 +54,19 @@ const show_stats = async () => {
|
||||||
statsText.innerHTML += `You have solved ${stats.my_count} captcha(s)`
|
statsText.innerHTML += `You have solved ${stats.my_count} captcha(s)`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const validate_solution = (settings, solution) => {
|
||||||
|
if (solution.length != settings.captcha_length) return false;
|
||||||
|
for (let i = 0; i < solution.length; i++) {
|
||||||
|
let char = solution[i];
|
||||||
|
if (!char.match(settings.captcha_regex)) {
|
||||||
|
console.log("Illegal symbol: " + char);
|
||||||
|
alert(`Illegal symbol ${char} at position ${i + 1}`);
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
window.onload = async () => {
|
window.onload = async () => {
|
||||||
const inputField = document.getElementById("captcha");
|
const inputField = document.getElementById("captcha");
|
||||||
inputField.focus();
|
inputField.focus();
|
||||||
|
@ -62,8 +75,9 @@ window.onload = async () => {
|
||||||
if (!document.cookie.includes('JWT')) {
|
if (!document.cookie.includes('JWT')) {
|
||||||
document.location.href = "/login";
|
document.location.href = "/login";
|
||||||
}
|
}
|
||||||
|
const settings = await (await fetch("/api/settings")).json();
|
||||||
const response = await fetch("https://check.ofd.ru/api/captcha/common/img");
|
const captcha_source_url = settings.captcha_source_url;
|
||||||
|
const response = await fetch(captcha_source_url);
|
||||||
captcha = await response.blob();
|
captcha = await response.blob();
|
||||||
|
|
||||||
const url = URL.createObjectURL(captcha);
|
const url = URL.createObjectURL(captcha);
|
||||||
|
@ -72,6 +86,10 @@ window.onload = async () => {
|
||||||
|
|
||||||
form.addEventListener('submit', async (e) => {
|
form.addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (!validate_solution(settings, inputField.value)) {
|
||||||
|
alert("You must specify valid solution!")
|
||||||
|
return;
|
||||||
|
}
|
||||||
// if (!await check_solution(inputField.value)) {
|
// if (!await check_solution(inputField.value)) {
|
||||||
// alert("Капча решена неверно")
|
// alert("Капча решена неверно")
|
||||||
// returnl
|
// returnl
|
||||||
|
|
|
@ -13,3 +13,4 @@ ADMIN_TOKEN=GENERATE_A_STRONG_TOKEN_HERE
|
||||||
SECRET=GENERATE_A_STRONG_SECRET_HERE
|
SECRET=GENERATE_A_STRONG_SECRET_HERE
|
||||||
#Allowed symbols. Must be a regex
|
#Allowed symbols. Must be a regex
|
||||||
ALPHABET="[123456789ABCDEFGHIJKLMNPQRSTUVWXZY]"
|
ALPHABET="[123456789ABCDEFGHIJKLMNPQRSTUVWXZY]"
|
||||||
|
CAPTCHA_LENGTH=6
|
|
@ -17,7 +17,8 @@ const config = {
|
||||||
data_dir: process.env.DATA_DIR,
|
data_dir: process.env.DATA_DIR,
|
||||||
admin_token: process.env.ADMIN_TOKEN,
|
admin_token: process.env.ADMIN_TOKEN,
|
||||||
secret: process.env.SECRET,
|
secret: process.env.SECRET,
|
||||||
alphabet: process.env.ALPHABET
|
alphabet: process.env.ALPHABET,
|
||||||
|
captcha_length: Number.parseInt(process.env.CAPTCHA_LENGTH)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default config;
|
export default config;
|
|
@ -6,7 +6,8 @@ class CaptchaController {
|
||||||
async submit(req, res) {
|
async submit(req, res) {
|
||||||
const { image, solution } = req.body;
|
const { image, solution } = req.body;
|
||||||
if (!image) return res.status(400).send({ "message": "You must send image blob" });
|
if (!image) return res.status(400).send({ "message": "You must send image blob" });
|
||||||
if (!solution || solution.length != 6) return res.status(400).send({"message":"You must send a valid solution"});
|
|
||||||
|
if (!solution || solution.length != config.captcha_length) return res.status(400).send({ "message": "You must send a valid solution" });
|
||||||
for (let i = 0; i < solution.length; i++) {
|
for (let i = 0; i < solution.length; i++) {
|
||||||
let char = solution[i];
|
let char = solution[i];
|
||||||
if (!char.match(config.alphabet)) {
|
if (!char.match(config.alphabet)) {
|
||||||
|
|
10
src/index.js
10
src/index.js
|
@ -15,6 +15,16 @@ app.use(express.urlencoded({ extended: false }));
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
|
app.get('/api/settings', (req, res) => {
|
||||||
|
return res.status(200).send(
|
||||||
|
{
|
||||||
|
"captcha_source_url": config.captcha_source_url,
|
||||||
|
"captcha_regex": config.captcha_regex,
|
||||||
|
"captcha_length": config.captcha_length
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
app.set('view engine', 'pug');
|
app.set('view engine', 'pug');
|
||||||
|
|
||||||
app.use('/api', CaptchaRouter);
|
app.use('/api', CaptchaRouter);
|
||||||
|
|
|
@ -15,7 +15,7 @@ function base64ToArrayBuffer(base64) {
|
||||||
|
|
||||||
class CaptchaService {
|
class CaptchaService {
|
||||||
async new(image, solution, submitter) {
|
async new(image, solution, submitter) {
|
||||||
const b64 = image.replace(/^data:image\/jpeg;base64,/, "");
|
const b64 = image.replace(/^.*,/, "");
|
||||||
const arrayBuffer = base64ToArrayBuffer(b64);
|
const arrayBuffer = base64ToArrayBuffer(b64);
|
||||||
const buffer = Buffer.from(arrayBuffer);
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
const hash = createHash('md5').update(buffer).digest('hex');
|
const hash = createHash('md5').update(buffer).digest('hex');
|
||||||
|
|
Loading…
Reference in New Issue