Implement kCTF strategy

This implementation is pretty scuffed, but its more exploratory than anything else.
This commit is contained in:
Zoe
2025-11-21 16:20:07 +00:00
parent cfab3d0b8f
commit 570531fe32
22 changed files with 1090 additions and 1007 deletions

View File

@@ -6,19 +6,28 @@ 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.LeadingZeroes:
case ChallengeStrategy.kCTF:
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,
difficulty: config.kctf.difficulty,
};
break;
}
@@ -31,10 +40,10 @@ export default defineEventHandler(async () => {
});
}
outstandingChallenges.set(challenge.salt, {
outstandingChallenges.set(challenge.challenge, {
challenge, timeout: setTimeout(() => {
console.log("Challenge timed out:", challenge.salt);
outstandingChallenges.delete(challenge.salt);
console.log("Challenge timed out:", challenge.challenge);
outstandingChallenges.delete(challenge.challenge);
}, CHALLENGE_TIMEOUT_MS)
});

View File

@@ -4,12 +4,13 @@ import * as z from 'zod';
import { outstandingChallenges } from '~~/server/utils/pow';
const challengeSchema = z.object({
challenge: z.string(),
nonce: z.string()
challenge: z.string().startsWith("s."),
solution: z.string().startsWith("s.")
})
// post handler that takes in the challenge, and the nonce
export default defineEventHandler(async (event) => {
console.log(await readBody(event));
const body = await readValidatedBody(event, challengeSchema.safeParse);
if (!body.success) {
@@ -19,19 +20,25 @@ export default defineEventHandler(async (event) => {
})
}
let target = body.data.challenge;
let nonce = body.data.nonce;
let { challenge, solution } = body.data;
const outstanding_challenge = outstandingChallenges.get(challenge);
if (outstanding_challenge === undefined) {
throw createError({
statusCode: 400,
statusMessage: 'Challenge not found'
})
}
// check if the challenge is valid
let challenge_valid = await validate_challenge(outstandingChallenges.get(target)!.challenge, {
challenge: target,
nonce: nonce
});
const challenge_valid = await validate_challenge(outstanding_challenge.challenge, solution);
console.log("CHALLENGE VALID", challenge_valid);
if (challenge_valid) {
// clear the challenge
clearTimeout(outstandingChallenges.get(target)!.timeout);
outstandingChallenges.delete(target);
clearTimeout(outstandingChallenges.get(challenge)!.timeout);
outstandingChallenges.delete(challenge);
return {
message: 'Challenge solved'