43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { defineEventHandler } from 'h3'
|
|
import { ChallengeAlgorithm } from '@impost/lib';
|
|
import * as z from 'zod';
|
|
|
|
const algorithmSchema = z.object({
|
|
algorithm: z.enum(ChallengeAlgorithm),
|
|
});
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readValidatedBody(event, algorithmSchema.safeParse);
|
|
|
|
if (!body.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Validation failed'
|
|
})
|
|
}
|
|
|
|
switch (body.data.algorithm) {
|
|
case 'sha256':
|
|
case 'argon2':
|
|
config.algorithm = body.data.algorithm;
|
|
config.strategy = config.strategy || 'leading_zeroes';
|
|
switch (config.strategy) {
|
|
case 'leading_zeroes':
|
|
config.leading_zeroes.difficulty = config.leading_zeroes.difficulty || 4;
|
|
break;
|
|
case 'target_number':
|
|
config.target_number.max_number = config.target_number.max_number || 10_000;
|
|
break;
|
|
}
|
|
break;
|
|
case 'kctf':
|
|
config.algorithm = body.data.algorithm;
|
|
config.kctf = config.kctf || {};
|
|
config.kctf.difficulty = config.kctf.difficulty || 100;
|
|
break;
|
|
}
|
|
|
|
return {
|
|
message: `Algorithm set to ${config.algorithm}`
|
|
};
|
|
}); |