update: rename the captcha endpoint

This commit is contained in:
alikia2x (寒寒) 2025-05-11 19:14:19 +08:00
parent 137c19d74e
commit a063f2401b
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
7 changed files with 50 additions and 10 deletions

4
.gitignore vendored
View File

@ -39,4 +39,6 @@ redis/
dist/
build/
docker-compose.yml
docker-compose.yml
ucaptcha-config.yaml

View File

@ -0,0 +1,37 @@
import { Context } from "hono";
import { Bindings, BlankEnv, BlankInput } from "hono/types";
import { ErrorResponse } from "src/schema";
import { createHandlers } from "src/utils.ts";
const getChallengeVerificationResult = async (id: string, ans: string) => {
const baseURL = process.env["UCAPTCHA_URL"];
const url = new URL(baseURL);
url.pathname = `/challenge/${id}/validation`;
const res = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
y: ans
})
});
return res;
};
export const verifyChallengeHandler = createHandlers(
async (c: Context<BlankEnv & { Bindings: Bindings }, "/captcha/:id/result", BlankInput>) => {
const id = c.req.param("id");
const ans = c.req.query("ans");
if (!ans) {
const response: ErrorResponse<string> = {
message: "Missing required query parameter: ans",
code: "INVALID_QUERY_PARAMS"
};
return c.json<ErrorResponse<string>>(response, 400);
}
const res = await getChallengeVerificationResult(id, ans);
return res;
}
);

View File

@ -0,0 +1,2 @@
export * from "./session/POST.ts";
export * from "./[id]/result/GET.ts";

View File

@ -15,11 +15,10 @@ const createNewChallenge = async (difficulty: number) => {
difficulty: difficulty,
})
});
const data = await res.json();
return data;
return res;
}
export const createValidationSessionHandler = createHandlers(async (c) => {
const challenge = await createNewChallenge(DIFFICULTY);
return c.json(challenge);
export const createCaptchaSessionHandler = createHandlers(async (_c) => {
const res = await createNewChallenge(DIFFICULTY);
return res;
});

View File

@ -1 +0,0 @@
export * from "./POST.ts";

View File

@ -4,7 +4,7 @@ import { registerHandler } from "routes/user";
import { videoInfoHandler, getSnapshotsHanlder } from "routes/video";
import { Hono } from "hono";
import { Variables } from "hono/types";
import { createValidationSessionHandler } from "routes/validation/session";
import { createCaptchaSessionHandler, verifyChallengeHandler } from "routes/captcha";
export function configureRoutes(app: Hono<{ Variables: Variables }>) {
app.get("/", ...rootHandler);
@ -15,5 +15,6 @@ export function configureRoutes(app: Hono<{ Variables: Variables }>) {
app.get("/video/:id/info", ...videoInfoHandler);
app.post("/validation/session", ...createValidationSessionHandler)
app.post("/captcha/session", ...createCaptchaSessionHandler);
app.get("/captcha/:id/result", ...verifyChallengeHandler);
}

View File

@ -3,7 +3,7 @@ type ErrorCode = "INVALID_QUERY_PARAMS" | "UNKNOWN_ERR" | "INVALID_PAYLOAD" | "I
export interface ErrorResponse<E> {
code: ErrorCode;
message: string;
errors: E[];
errors?: E[];
}
export interface StatusResponse {