diff --git a/api-prox/src/config.ts b/api-prox/src/config.ts index 0cac298..bdc8c99 100644 --- a/api-prox/src/config.ts +++ b/api-prox/src/config.ts @@ -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", +}; \ No newline at end of file diff --git a/api-prox/src/index.ts b/api-prox/src/index.ts index 244cdc2..7e2db64 100644 --- a/api-prox/src/index.ts +++ b/api-prox/src/index.ts @@ -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; diff --git a/api-prox/src/utils/logger.ts b/api-prox/src/utils/logger.ts index 903f2ae..fcc3283 100644 --- a/api-prox/src/utils/logger.ts +++ b/api-prox/src/utils/logger.ts @@ -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}` ); } };