53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { defineEventHandler } from 'h3'
|
|
import { config } from '~~/server/utils/config';
|
|
import { generate_challenge } from '@impost/lib/validator';
|
|
import { ChallengeStrategy } from '@impost/lib';
|
|
import { CHALLENGE_TIMEOUT_MS, outstandingChallenges } from '~~/server/utils/pow';
|
|
|
|
export default defineEventHandler(async () => {
|
|
let challenge_config;
|
|
// switch (config.strategy) {
|
|
// case ChallengeStrategy.LeadingZeroes:
|
|
// challenge_config = {
|
|
// parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
|
// strategy: config.strategy,
|
|
// difficulty: config.leading_zeroes?.difficulty!,
|
|
// };
|
|
// break;
|
|
// case ChallengeStrategy.TargetNumber:
|
|
// challenge_config = {
|
|
// parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
|
// strategy: config.strategy,
|
|
// max_number: config.target_number.max_number,
|
|
// };
|
|
// break;
|
|
// }
|
|
switch (config.strategy) {
|
|
case ChallengeStrategy.kCTF:
|
|
challenge_config = {
|
|
parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
|
strategy: config.strategy,
|
|
difficulty: config.kctf.difficulty,
|
|
};
|
|
break;
|
|
}
|
|
|
|
let challenge = await generate_challenge(challenge_config);
|
|
if (challenge === null) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to generate challenge'
|
|
});
|
|
}
|
|
|
|
outstandingChallenges.set(challenge.challenge, {
|
|
challenge, timeout: setTimeout(() => {
|
|
console.log("Challenge timed out:", challenge.challenge);
|
|
outstandingChallenges.delete(challenge.challenge);
|
|
}, CHALLENGE_TIMEOUT_MS)
|
|
});
|
|
|
|
return {
|
|
challenge,
|
|
}
|
|
}) |