fix: add missing cors and content-type headers

This commit is contained in:
Kentai Radiquum 2025-07-08 18:34:59 +05:00
parent 51c5bf01da
commit cc9a9c3a2c
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
14 changed files with 116 additions and 89 deletions

View file

@ -10,6 +10,5 @@ RUN npm ci
COPY *.ts ./
EXPOSE 7000
ENV PORT=7000
ENV HOSTNAME="0.0.0.0"
CMD ["npm", "run", "serve"]

View file

@ -5,42 +5,58 @@ import { getKodikURL } from "./kodik";
import express from "express";
const app = express();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", req.headers.origin || "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS");
next();
});
const host = "0.0.0.0";
const port = 7000;
const HOST = process.env.HOST || "0.0.0.0";
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 7001;
const allowedPlayers = ["kodik", "libria", "sibnet"];
app.get("/", (req, res) => {
const urlParams = new URLSearchParams(req.query)
const urlParams = new URLSearchParams(req.query);
const url = urlParams.get("url");
const player = urlParams.get("player");
if (!url) {
asJSON(res, { message: "no 'url' query provided" }, 400)
return
asJSON(req, res, { message: "no 'url' query provided" }, 400);
return;
}
if (!player) {
asJSON(res, { message: "no 'player' query provided" }, 400)
return
asJSON(req, res, { message: "no 'player' query provided" }, 400);
return;
}
switch (player) {
case "libria":
getAnilibriaURL(res, url)
return
getAnilibriaURL(req, res, url);
return;
case "sibnet":
getSibnetURL(res, url)
return
getSibnetURL(req, res, url);
return;
case "kodik":
getKodikURL(res, url)
return
getKodikURL(req, res, url);
return;
default:
asJSON(res, { message: `player '${player}' is not supported. choose one of: ${allowedPlayers.join(", ")}` }, 400)
return
asJSON(
req,
res,
{
message: `player '${player}' is not supported. choose one of: ${allowedPlayers.join(", ")}`,
},
400
);
return;
}
});
app.listen(port, host, function () {
console.log(`Server listens http://${host}:${port}`);
app.listen(PORT, HOST, function () {
console.log(`Server listens http://${HOST}:${PORT}`);
});

View file

@ -1,12 +1,12 @@
import { asJSON, randomUA } from "./shared";
const altDomains = ["kodik.info", "aniqit.com", "kodik.cc", "kodik.biz"];
export async function getKodikURL(res, url: string) {
export async function getKodikURL(req, res, url: string) {
const origDomain = url.replace("https://", "").split("/")[0];
let domain = url.replace("https://", "").split("/")[0];
if (!altDomains.includes(domain)) {
asJSON(res, { message: "Wrong url provided for player kodik" }, 400);
asJSON(req, res, { message: "Wrong url provided for player kodik" }, 400);
return;
}
@ -45,7 +45,7 @@ export async function getKodikURL(res, url: string) {
}
if (!pageRes.ok) {
asJSON(res, { message: "KODIK: failed to load page" }, 500);
asJSON(req, res, { message: "KODIK: failed to load page" }, 500);
return;
}
@ -54,7 +54,7 @@ export async function getKodikURL(res, url: string) {
const urlParamsMatch = urlParamsRe.exec(pageData);
if (!urlParamsMatch || urlParamsMatch.length == 0) {
asJSON(res, { message: `KODIK: failed to find data to parse` }, 500);
asJSON(req, res, { message: `KODIK: failed to find data to parse` }, 500);
return;
}
@ -86,7 +86,7 @@ export async function getKodikURL(res, url: string) {
});
if (!linksRes.ok) {
asJSON(res, { message: `KODIK: failed to get links` }, 500);
asJSON(req, res, { message: `KODIK: failed to get links` }, 500);
return;
}
@ -117,7 +117,7 @@ export async function getKodikURL(res, url: string) {
"thumb001.jpg"
);
asJSON(res, data, 200);
asJSON(req, res, data, 200);
return;
}

View file

@ -1,9 +1,9 @@
import { asJSON } from "./shared";
const API_URL = "https://api.anilibria.tv/v3/title"
export async function getAnilibriaURL(res, url: string) {
export async function getAnilibriaURL(req, res, url: string) {
if (!url.includes("libria")) {
asJSON(res, { message: "Wrong url provided for player libria" }, 400);
asJSON(req, res, { message: "Wrong url provided for player libria" }, 400);
return
}
@ -14,7 +14,7 @@ export async function getAnilibriaURL(res, url: string) {
let apiRes = await fetch(`${API_URL}?id=${releaseId}`);
if (!apiRes.ok) {
asJSON(res, { message: "LIBRIA: failed to get api response" }, 500);
asJSON(req, res, { message: "LIBRIA: failed to get api response" }, 500);
return
}
@ -26,7 +26,7 @@ export async function getAnilibriaURL(res, url: string) {
}
asJSON(res, data, 200);
asJSON(req, res, data, 200);
return
}

View file

@ -1,14 +1,10 @@
export const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Cache-Control": "no-cache",
};
export const resHeaders = {
...corsHeaders,
"Content-Type": "application/json",
};
export const USERAGENTS = [
"Mozilla/5.0 (Linux; Android 12.0; LG G8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.2.7124.71 Mobile Safari/537.36",
"Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.5.1269.13 Safari/537.36",
@ -37,7 +33,9 @@ export const USERAGENTS = [
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:128.0) Gecko/128.0 Firefox/128.0",
];
export function asJSON(res, object: any, status: number) {
export function asJSON(req, res, object: any, status: number) {
corsHeaders["Access-Control-Allow-Origin"] = req.headers.origin || "*";
res.status(status).type("application/json");
res.set(corsHeaders);
res.send(JSON.stringify(object));

View file

@ -1,9 +1,9 @@
import { asJSON, randomUA } from "./shared";
export async function getSibnetURL(res, url: string) {
export async function getSibnetURL(req, res, url: string) {
if (!url.includes("sibnet")) {
asJSON(res, { message: "Wrong url provided for player sibnet" }, 400);
asJSON(req, res, { message: "Wrong url provided for player sibnet" }, 400);
return
}
@ -15,7 +15,7 @@ export async function getSibnetURL(res, url: string) {
},
});
if (!pageRes.ok) {
asJSON(res, { message: `SIBNET:${pageRes.status}: failed to load page` }, 500)
asJSON(req, res, { message: `SIBNET:${pageRes.status}: failed to load page` }, 500)
return
}
const pageData = await pageRes.text();
@ -23,7 +23,7 @@ export async function getSibnetURL(res, url: string) {
const videoMatch = videoRe.exec(pageData);
if (!videoMatch || videoMatch.length == 0) {
asJSON(res, { message: `SIBNET: failed to find data to parse` }, 500)
asJSON(req, res, { message: `SIBNET: failed to find data to parse` }, 500)
return
}
@ -42,7 +42,7 @@ export async function getSibnetURL(res, url: string) {
);
if (!actualVideoRes.headers.get("location")) {
asJSON(res, { message: `SIBNET: failed to get video link` }, 500)
asJSON(req, res, { message: `SIBNET: failed to get video link` }, 500)
return
}
@ -54,6 +54,6 @@ export async function getSibnetURL(res, url: string) {
: null
: null;
asJSON(res, { manifest: video, poster }, 200)
asJSON(req, res, { manifest: video, poster }, 200)
return
}