diff --git a/src/routes/api/v1/authed.ts b/src/routes/api/v1/authed.ts index db6a16a..f3a389b 100644 --- a/src/routes/api/v1/authed.ts +++ b/src/routes/api/v1/authed.ts @@ -2,6 +2,7 @@ import express, { Request, Response, NextFunction } from "express"; const router = express.Router(); import { getClicks, verifyKey } from "../../../functions.js"; import { rooms } from "../../ttt.js"; +import { time } from "console"; router.use(async (req: Request, res: Response, next: NextFunction) => { const { auth } = req.headers; @@ -64,4 +65,56 @@ router.get("/rooms", (req: Request, res: Response) => { } }); +router.post("/encode", (req: Request, res: Response) => { + let { text, times } = req.body; + let counter = 0; + + if (isNaN(Number(times))) { + return res.status(400).json({ + error: "Times is NaN" + }); + } + + try{ + while(counter < times){ + text = btoa(text); + counter++; + } + } + catch{ + res.sendStatus(500); + } + + + res.status(200).json({ + output: String(text) + }); +}); + +router.post("/decode", (req: Request, res: Response) => { + let { text, times } = req.body; + let counter = 0; + + if (isNaN(Number(times))) { + return res.status(400).json({ + error: "Times is NaN" + }); + } + + try{ + while(counter < times){ + text = atob(text); + counter++; + } + } + catch{ + res.sendStatus(500); + } + + + res.status(200).json({ + output: String(text) + }); +}); + export default router; \ No newline at end of file diff --git a/src/routes/api/v1/unauthed.ts b/src/routes/api/v1/unauthed.ts index a4f8f42..bcb1f05 100644 --- a/src/routes/api/v1/unauthed.ts +++ b/src/routes/api/v1/unauthed.ts @@ -57,31 +57,42 @@ router.post("/request", async (req: Request, res: Response) => { const { method, url, body, headers } = req.body; let resBody; - if(method && url){ - try{ + if (method && url) { + try { let safeHeaders: Record = {}; if (headers && typeof headers === "object" && !Array.isArray(headers)) { for (const [key, value] of Object.entries(headers)) { - if ( - typeof key === "string" && - typeof value !== "undefined" && - value !== null - ) { + if (typeof key === "string" && value != null) { safeHeaders[key] = String(value); } } } + const outgoingBody = + method === "GET" + ? undefined + : typeof body === "string" + ? body + : body !== undefined + ? JSON.stringify(body) + : undefined; + + const hasContentType = Object.keys(safeHeaders).some( + (h) => h.toLowerCase() === "content-type" + ); + if (outgoingBody && !hasContentType && typeof body !== "string") { + safeHeaders["Content-Type"] = "application/json"; + } + const response = await fetch(url, { method: method, headers: safeHeaders, - body: method == "GET" ? undefined : JSON.stringify(body) + body: outgoingBody }); const contenttype = await response.headers.get("content-type"); - if(contenttype?.includes("application/json")){ - resBody = await response.json() - } - else{ + if (contenttype?.includes("application/json")) { + resBody = await response.json(); + } else { resBody = await response.text(); } res.json({ @@ -89,15 +100,13 @@ router.post("/request", async (req: Request, res: Response) => { body: resBody, headers: Object.fromEntries(response.headers.entries()) }); - } - catch (error){ + } catch (error) { res.status(400).json({ error: "There was an error while sending the request. Make sure that the url is well formatted" }); console.error(error); } - } - else{ + } else { res.status(400).json({ error: "method and url are required in body" }); diff --git a/views/projects/request.ejs b/views/projects/request.ejs index fb97d64..b3b8d85 100644 --- a/views/projects/request.ejs +++ b/views/projects/request.ejs @@ -77,19 +77,30 @@ } sendBtn.addEventListener("click", async () => { - if(url.value){ - headers = JSON.parse(headersInp.value); - body = JSON.parse(bodyInp.value); + if (url.value) { + try { + headers = JSON.parse(headersInp.value); + } catch (e) { + result.innerText = "Headers must be valid JSON"; + return; + } + + const rawBody = bodyInp.value; + let bodyToSend; + try { + bodyToSend = JSON.parse(rawBody); + } catch (e) { + bodyToSend = rawBody; + } + const response = await fetch("/api/v1/request", { method: "POST", - headers: { - "Content-Type": "application/json" - }, + headers: { "Content-Type": "application/json" }, body: JSON.stringify({ method: method.value, url: url.value, headers: headers, - body: body + body: bodyToSend }) }); result.innerHTML = "";