Skip to content

Commit

Permalink
Public API: Clean up the server proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Sep 21, 2023
1 parent 4d4da10 commit d0ad40d
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions public-api/src/routes/server-proxy.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { JsonResponse } from "./index.mjs";

async function isValidAthenaServer(url) {
const response = await (await fetch(`${url.protocol}${url.hostname}/.well-known/x-athena`)).text();
return response.trim() == "yes";
}

function isAcceptableToProxy(response) {
const contentType = (response.headers.get("Content-Type") || "").split(";")[0];
if (!["text/plain", "application/json"].includes(contentType)) {
throw "You may only use this proxy to make non-media requests.";
}
}

export default (/** @type {import("itty-router").RouterType<import("itty-router").Route, any[]} */router) => {

router.get("/server-proxy", async (request) => {
const searchParams = new URL(request.url).searchParams;
const url = new URL(new URL(request.url).searchParams.get("url"));

const url = new URL(searchParams.get("url"));
console.debug("Proxying request:", url);

try {
switch (url.protocol) {
case "http":
case "https": {
if ((await (await fetch(`${url.protocol}${url.hostname}/.well-known/x-athena`)).text()).trim() != "yes") {
if (!isValidAthenaServer(url)) {
console.debug("Not a valid Athena server.");
return new JsonResponse({
data: null,
error: `The server url "${url.protocol}${url.hostname}" does not appear to be an Athena server.`,
Expand All @@ -22,23 +35,27 @@ export default (/** @type {import("itty-router").RouterType<import("itty-router"
const response = await fetch(url, {
headers: new Headers({
...Object.fromEntries(request.headers.entries()),
"X-Athena-Proxy-Limits": "NO_STREAMING"
"X-Athena-Proxy": "used"
})
});

const contentType = (response.headers.get("Content-Type") || "").split(";")[0];
if (!["text/plain", "application/json"].includes(contentType)) {
try {
isAcceptableToProxy(response);
} catch (message) {
console.debug("Unacceptable to proxy:", message);
return new JsonResponse({
data: null,
error: `You may only use this proxy to make non-media requests.`,
error: message,
rel: null,
}, { status: 400 });
}

console.debug("Success!");
return response;
}

default:
console.debug("Invalid protocol.");
return new JsonResponse({
data: null,
error: `Unknown or unsupported protocol "${url.protocol}"`,
Expand All @@ -49,7 +66,7 @@ export default (/** @type {import("itty-router").RouterType<import("itty-router"
console.error("Internal error:", e.toString(), e.stack);
return new JsonResponse({
data: null,
error: `An internal error occurred whilst satisfying your request.`,
error: "An internal error occurred whilst satisfying your request.",
rel: null,
}, { status: 500 });
}
Expand Down

0 comments on commit d0ad40d

Please sign in to comment.