-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Public API: Implement a public server proxy.
Hopefully this will be fine...
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |