Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/routes/api/v1/authed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
41 changes: 25 additions & 16 deletions src/routes/api/v1/unauthed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,56 @@ 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<string, string> = {};
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({
status: response.status,
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"
});
Expand Down
25 changes: 18 additions & 7 deletions views/projects/request.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down