From 13f404027878a92772e6a47eefae0a3b3c6279bb Mon Sep 17 00:00:00 2001 From: ArthurHeitmann <37270165+ArthurHeitmann@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:12:00 +0100 Subject: [PATCH] debug resolve url endpoint --- src/serverScripts/photonApi.ts | 56 ++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/serverScripts/photonApi.ts b/src/serverScripts/photonApi.ts index 00feffd5..0e291b55 100644 --- a/src/serverScripts/photonApi.ts +++ b/src/serverScripts/photonApi.ts @@ -1,6 +1,6 @@ import express from "express"; import RateLimit from "express-rate-limit"; -import fetch from "node-fetch"; +import fetch, { Response } from "node-fetch"; import youtube_dl from "youtube-dl-exec"; import { basicRateLimitConfig, @@ -177,7 +177,7 @@ photonApiRouter.get("/resolveRedditUrl", RateLimit(tokenRequestRateLimitConfig), if (url.startsWith("/")) url = "https://www.reddit.com" + url; - let response; + let response: Response; let isRedirect = false; const supportedRedditUrl = /^https:\/\/(\w+\.)?reddit.com\/(r|u|user)\/[^/#?]+\//; do { @@ -208,3 +208,55 @@ photonApiRouter.get("/resolveRedditUrl", RateLimit(tokenRequestRateLimitConfig), res.json({ url, path: urlObj.pathname }); })); + + +photonApiRouter.get("/resolveRedditUrlDebug", RateLimit(tokenRequestRateLimitConfig), safeExcAsync(async (req, res) => { + let url = req.query["url"].toString(); + if (!url) { + res.status(400).json({ error: "¯\\_(ツ)_/¯" }); + return; + } + if (url.startsWith("https://reddit.com/")) + url = url.replace("https://reddit.com/", "https://www.reddit.com/"); + if (url.startsWith("/")) + url = "https://www.reddit.com" + url; + + let response: Response; + let isRedirect = false; + const supportedRedditUrl = /^https:\/\/(\w+\.)?reddit.com\/(r|u|user)\/[^/#?]+\//; + do { + response = await fetch(url, { + redirect: "manual", + headers: { + "User-Agent": `web_backend:photon-reddit.com:v${photonVersion} (by /u/RaiderBDev)` + } + }); + let newUrl = response.headers.get("location"); + if (!newUrl) + break; + if (!newUrl.startsWith("http")) + newUrl = new URL(newUrl, response.url).href; + url = newUrl; + isRedirect = response.status >= 300 && response.status < 400; + } + while (isRedirect && !supportedRedditUrl.test(url)); + + if (!supportedRedditUrl.test(url)) { + res.status(400).json({ error: "Failed to resolve url" }); + return; + } + const urlObj = new URL(url); + urlObj.search = ""; + urlObj.hash = ""; + url = urlObj.toString(); + + res.json({ + url, + path: urlObj.pathname, + response: { + status: response.status, + headers: response.headers.raw(), + text: await response.text() + } + }); +}));