further benchmarking stuff
This commit is contained in:
7
example-app/server/api/pow/algorithm.get.ts
Normal file
7
example-app/server/api/pow/algorithm.get.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
return {
|
||||
algorithm: config.algorithm
|
||||
}
|
||||
})
|
||||
43
example-app/server/api/pow/algorithm.put.ts
Normal file
43
example-app/server/api/pow/algorithm.put.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
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}`
|
||||
};
|
||||
});
|
||||
@@ -27,11 +27,11 @@ export default defineEventHandler(async () => {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeAlgorithm.Argon2id:
|
||||
case ChallengeAlgorithm.Argon2:
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.Argon2id,
|
||||
algorithm: ChallengeAlgorithm.Argon2,
|
||||
strategy: ChallengeStrategy.LeadingZeroes,
|
||||
difficulty: config.leading_zeroes.difficulty,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
@@ -39,7 +39,7 @@ export default defineEventHandler(async () => {
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.Argon2id,
|
||||
algorithm: ChallengeAlgorithm.Argon2,
|
||||
strategy: ChallengeStrategy.TargetNumber,
|
||||
difficulty: config.target_number.max_number,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
|
||||
@@ -5,7 +5,7 @@ import { outstandingChallenges } from '~~/server/utils/pow';
|
||||
|
||||
const challengeSchema = z.object({
|
||||
salt: z.string(),
|
||||
// either a string if the algorithm is kCTF, or a number if the algorithm is Argon2id or SHA256
|
||||
// either a string if the algorithm is kCTF, or a number if the algorithm is Argon2 or SHA256
|
||||
solution: z.string().or(z.number()),
|
||||
})
|
||||
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
import { ChallengeStrategy } from '@impost/lib';
|
||||
import { ChallengeStrategy, ChallengeAlgorithm } from '@impost/lib';
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
let difficulty: number;
|
||||
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
difficulty = config.leading_zeroes.difficulty!;
|
||||
console.log("CONFIG", config);
|
||||
|
||||
switch (config.algorithm) {
|
||||
case ChallengeAlgorithm.SHA256:
|
||||
case ChallengeAlgorithm.Argon2:
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
difficulty = config.leading_zeroes.difficulty!;
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
difficulty = config.target_number.max_number!;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
difficulty = config.target_number.max_number!;
|
||||
case ChallengeAlgorithm.kCTF:
|
||||
difficulty = config.kctf.difficulty!;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,43 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
import { ChallengeStrategy } from '@impost/lib';
|
||||
import { ChallengeStrategy, ChallengeAlgorithm } from '@impost/lib';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
|
||||
let difficulty = body.difficulty;
|
||||
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
if (!difficulty || difficulty < 1 || difficulty > 64) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request',
|
||||
});
|
||||
}
|
||||
switch (config.algorithm) {
|
||||
case ChallengeAlgorithm.SHA256:
|
||||
case ChallengeAlgorithm.Argon2:
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
if (!difficulty || difficulty < 1 || difficulty > 64) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request',
|
||||
});
|
||||
}
|
||||
|
||||
config.leading_zeroes.difficulty = difficulty;
|
||||
config.leading_zeroes.difficulty = difficulty;
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
if (!difficulty || difficulty < 1 || difficulty > 100_000_000) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request',
|
||||
});
|
||||
}
|
||||
|
||||
config.target_number.max_number = difficulty;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
if (!difficulty || difficulty < 1 || difficulty > 100_000_000) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request',
|
||||
});
|
||||
}
|
||||
|
||||
config.target_number.max_number = difficulty;
|
||||
case ChallengeAlgorithm.kCTF:
|
||||
config.kctf.difficulty = difficulty;
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
message: 'Challenge difficulty set'
|
||||
message: `Challenge difficulty set to ${difficulty}`
|
||||
};
|
||||
});
|
||||
35
example-app/server/api/pow/index.get.ts
Normal file
35
example-app/server/api/pow/index.get.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { ChallengeAlgorithm } from '@impost/lib';
|
||||
import { defineEventHandler } from 'h3'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
let difficulty: number;
|
||||
|
||||
switch (config.algorithm) {
|
||||
case ChallengeAlgorithm.SHA256:
|
||||
case ChallengeAlgorithm.Argon2:
|
||||
switch (config.strategy) {
|
||||
case 'leading_zeroes':
|
||||
difficulty = config.leading_zeroes.difficulty!;
|
||||
break;
|
||||
case 'target_number':
|
||||
difficulty = config.target_number.max_number!;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeAlgorithm.kCTF:
|
||||
difficulty = config.kctf.difficulty!;
|
||||
break;
|
||||
default:
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Unknown algorithm',
|
||||
})
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
difficulty,
|
||||
algorithm: config.algorithm,
|
||||
strategy: config.strategy || undefined,
|
||||
}
|
||||
})
|
||||
14
example-app/server/api/pow/strategy.get.ts
Normal file
14
example-app/server/api/pow/strategy.get.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { defineEventHandler } from "h3";
|
||||
import { ChallengeAlgorithm } from "@impost/lib";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (config.algorithm === ChallengeAlgorithm.kCTF) {
|
||||
return {
|
||||
strategy: undefined,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
strategy: config.strategy,
|
||||
}
|
||||
});
|
||||
43
example-app/server/api/pow/strategy.put.ts
Normal file
43
example-app/server/api/pow/strategy.put.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
import { ChallengeAlgorithm, ChallengeStrategy } from '@impost/lib';
|
||||
import * as z from 'zod';
|
||||
|
||||
const strategySchema = z.object({
|
||||
strategy: z.enum(ChallengeStrategy),
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readValidatedBody(event, strategySchema.safeParse);
|
||||
|
||||
if (!body.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Validation failed'
|
||||
})
|
||||
}
|
||||
|
||||
switch (config.algorithm) {
|
||||
case ChallengeAlgorithm.SHA256:
|
||||
case ChallengeAlgorithm.Argon2:
|
||||
config.strategy = body.data.strategy;
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
config.leading_zeroes = config.leading_zeroes || {};
|
||||
config.leading_zeroes.difficulty = config.leading_zeroes.difficulty || 4;
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
config.target_number = config.target_number || {};
|
||||
config.target_number.max_number = config.target_number.max_number || 10_000;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeAlgorithm.kCTF:
|
||||
return {
|
||||
message: "Strategy cannot be set for kCTF"
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
message: `Strategy set to ${config.strategy}`
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user