mirror of
https://github.com/Radiquum/AniX.git
synced 2025-09-07 06:53:54 +05:00
feat/api-prox: add GET proxy
This commit is contained in:
parent
1a439f0a77
commit
32e830cae1
3 changed files with 49 additions and 8 deletions
|
@ -2,7 +2,18 @@
|
|||
// {EPOCH * 1000 + MAJOR}.MINOR.PATCH
|
||||
export const appVersion = "0000.0.0";
|
||||
|
||||
export const baseUrls = [
|
||||
export const BASE_URLS = [
|
||||
"https://api-s.anixsekai.com/",
|
||||
"https://api.anixsekai.com/",
|
||||
];
|
||||
|
||||
export type ANIXART_HEADERST = {
|
||||
"User-Agent": string;
|
||||
"Content-Type": string;
|
||||
"Api-Version"?: string;
|
||||
};
|
||||
export const ANIXART_UA = "AnixartApp/9.0 BETA 5-25062213 (Android 9; SDK 28; arm64-v8a; samsung SM-G975N; en)";
|
||||
export const ANIXART_HEADERS: ANIXART_HEADERST = {
|
||||
"User-Agent": ANIXART_UA,
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
};
|
|
@ -1,12 +1,11 @@
|
|||
import { Hono } from "hono";
|
||||
import { logger } from "hono/logger";
|
||||
import { RouteLogger } from "./utils/logger.js";
|
||||
import { trimTrailingSlash } from "hono/trailing-slash";
|
||||
import { asciiHTML, separatorHTML } from "./utils/info.js";
|
||||
import { appVersion } from "./config.js";
|
||||
import { ANIXART_HEADERS, appVersion, BASE_URLS } from "./config.js";
|
||||
import { tryCatchAPI } from "./utils/tryCatch.js";
|
||||
|
||||
const app = new Hono({ strict: true });
|
||||
app.use(trimTrailingSlash());
|
||||
const app = new Hono({ strict: false });
|
||||
app.use(logger(RouteLogger));
|
||||
|
||||
app.get("/", (c) => {
|
||||
|
@ -62,4 +61,35 @@ app.get("/health/json", (c) => {
|
|||
return c.json({ status: "OK", version: appVersion });
|
||||
});
|
||||
|
||||
app.get("/favicon.ico", (c) => {
|
||||
return c.text("", 404);
|
||||
})
|
||||
|
||||
app.get("/*", async (c) => {
|
||||
console.log("--- Trying to proxy `GET` request")
|
||||
|
||||
const url = new URL(c.req.url);
|
||||
const currentBaseURL = new URL(BASE_URLS[Math.floor(Math.random() * BASE_URLS.length)]);
|
||||
url.protocol = currentBaseURL.protocol;
|
||||
url.host = currentBaseURL.host;
|
||||
url.port = currentBaseURL.port;
|
||||
if (url.searchParams.get("API-Version") == "v2" || c.req.header("API-Version") == "v2") {
|
||||
ANIXART_HEADERS["Api-Version"] = "v2";
|
||||
url.searchParams.delete("API-Version");
|
||||
}
|
||||
|
||||
console.log("--- URL:", `${url.protocol}//${url.host}${url.pathname}`);
|
||||
const { data, error } = await tryCatchAPI(fetch(url.toString(), {
|
||||
method: "GET",
|
||||
headers: ANIXART_HEADERS
|
||||
}));
|
||||
|
||||
if (error) {
|
||||
return c.json(error);
|
||||
}
|
||||
|
||||
//@ts-ignore
|
||||
return c.json(data);
|
||||
})
|
||||
|
||||
export default app;
|
||||
|
|
|
@ -4,16 +4,16 @@ export const RouteLogger = (message: string) => {
|
|||
const method = args[1];
|
||||
const url = new URL("http://example.com" + args[2]);
|
||||
if (url.searchParams.get("token")) {
|
||||
url.searchParams.set("token", "*********");
|
||||
url.searchParams.set("token", "***");
|
||||
}
|
||||
|
||||
if (direction == "<--") {
|
||||
console.log(`REQ | ${method} ${url.pathname}${url.search}`);
|
||||
console.log(`--> REQ | ${method} ${url.pathname}${url.search}`);
|
||||
} else {
|
||||
const status = args[3];
|
||||
const time = args[4];
|
||||
console.log(
|
||||
`RES | ${method} ${url.pathname}${url.search} ${status} ${time}`
|
||||
`<-- RES | ${method} ${url.pathname}${url.search} ${status} ${time}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue