Skip to content

Commit

Permalink
Public API: Implement a public server proxy.
Browse files Browse the repository at this point in the history
Hopefully this will be fine...
  • Loading branch information
e3ndr committed Sep 21, 2023
1 parent 5bb87d3 commit 4d4da10
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions public-api/src/routes/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class JsonResponse extends Response {
const router = Router();

(await import("./search.mjs")).default(router);
(await import("./server-proxy.mjs")).default(router);

router.get('/', () => {
return new Response(`🍿 @ ${Date.now()}`);
Expand Down
58 changes: 58 additions & 0 deletions public-api/src/routes/server-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { JsonResponse } from "./index.mjs";

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(searchParams.get("url"));

try {
switch (url.protocol) {
case "http":
case "https": {
if ((await (await fetch(`${url.protocol}${url.hostname}/.well-known/x-athena`)).text()).trim() != "yes") {
return new JsonResponse({
data: null,
error: `The server url "${url.protocol}${url.hostname}" does not appear to be an Athena server.`,
rel: null,
}, { status: 400 });
}

const response = await fetch(url, {
headers: new Headers({
...Object.fromEntries(request.headers.entries()),
"X-Athena-Proxy-Limits": "NO_STREAMING"
})
});

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

return response;
}

default:
return new JsonResponse({
data: null,
error: `Unknown or unsupported protocol "${url.protocol}"`,
rel: null,
}, { status: 400 });
}
} catch (e) {
console.error("Internal error:", e.toString(), e.stack);
return new JsonResponse({
data: null,
error: `An internal error occurred whilst satisfying your request.`,
rel: null,
}, { status: 500 });
}
});

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class AthenaSoraAdapter extends SoraPlugin {

@Override
public void onInit(Sora sora) {
sora.addProvider(this, new MetaRoutes());
sora.addProvider(this, new MediaRoutes());
sora.addProvider(this, new StreamRoutes());
sora.addProvider(this, new SessionRoutes());
Expand Down
18 changes: 18 additions & 0 deletions server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package xyz.e3ndr.athena.server.http;

import co.casterlabs.rakurai.io.http.StandardHttpStatus;
import co.casterlabs.rakurai.io.http.server.HttpResponse;
import co.casterlabs.sora.api.http.HttpProvider;
import co.casterlabs.sora.api.http.SoraHttpSession;
import co.casterlabs.sora.api.http.annotations.HttpEndpoint;

class MetaRoutes implements HttpProvider {

@HttpEndpoint(uri = "/.well-known/x-athena")
public HttpResponse onIngest(SoraHttpSession session) {
return HttpResponse
.newFixedLengthResponse(StandardHttpStatus.OK, "yes")
.setMimeType("text/plain");
}

}

0 comments on commit 4d4da10

Please sign in to comment.