Skip to content

Commit

Permalink
add ability to verify the requests
Browse files Browse the repository at this point in the history
fixes #9
  • Loading branch information
unixfox committed Dec 13, 2024
1 parent 100e272 commit c446f33
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ port = 8282
host = "127.0.0.1"
secret_key = "CHANGE_ME"
base_url = "http://localhost:8282"
verify_requests = false

[cache]
enabled = true
Expand Down
39 changes: 39 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/lib/helpers/verifyRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Store } from "@willsoto/node-konfig-core";
import { decodeBase64 } from "jsr:@std/encoding/base64";
import { Aes } from "https://deno.land/x/[email protected]/aes.ts";
import {
Ecb,
Padding,
} from "https://deno.land/x/[email protected]/block-modes.ts";

export const verifyRequest = (
stringToCheck: string,
videoId: string,
konfigStore: Store,
): boolean => {
try {
const decipher = new Ecb(
Aes,
new TextEncoder().encode(
Deno.env.get("SERVER_SECRET_KEY") ||
konfigStore.get("server.secret_key") as string,
),
Padding.PKCS7,
);

const encryptedData = new TextDecoder().decode(
decipher.decrypt(decodeBase64(stringToCheck)),
);
const [parsedTimestamp, parsedVideoId] = encryptedData.split("|");
const parsedTimestampInt = parseInt(parsedTimestamp);
const timestampNow = Math.round(+new Date() / 1000);
if (parsedVideoId !== videoId) {
return false;
}
// only allow ID to live for 6 hours
if ((timestampNow + 6 * 60 * 60) - parsedTimestampInt < 0) {
console.log("sup lol")
return false;
}
} catch (_) {
return false;
}
return true;
};
18 changes: 17 additions & 1 deletion src/routes/invidious_routes/dashManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {
youtubePlayerParsing,
youtubeVideoInfo,
} from "../../lib/helpers/youtubePlayerHandling.ts";
import {
verifyRequest
} from "../../lib/helpers/verifyRequest.ts";
import { HTTPException } from "hono/http-exception";

const dashManifest = new Hono<{ Variables: HonoVariables }>();

dashManifest.get("/:videoId", async (c) => {
const { videoId } = c.req.param();
const { local } = c.req.query();
const { check, local } = c.req.query();
c.header("access-control-allow-origin", "*");

const innertubeClient = await c.get("innertubeClient") as Innertube;
Expand All @@ -20,6 +24,18 @@ dashManifest.get("/:videoId", async (c) => {
Record<string, unknown>
>;

if (konfigStore.get("server.verify_requests") && check == undefined) {
throw new HTTPException(400, {
res: new Response("No check ID."),
});
} else if (konfigStore.get("server.verify_requests") && check) {
if (verifyRequest(check, videoId, konfigStore) === false) {
throw new HTTPException(400, {
res: new Response("ID incorrect."),
});
}
}

const youtubePlayerResponseJson = await youtubePlayerParsing(
innertubeClient,
videoId,
Expand Down
17 changes: 16 additions & 1 deletion src/routes/invidious_routes/latestVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
youtubePlayerParsing,
youtubeVideoInfo,
} from "../../lib/helpers/youtubePlayerHandling.ts";
import {
verifyRequest
} from "../../lib/helpers/verifyRequest.ts";

const latestVersion = new Hono<{ Variables: HonoVariables }>();

latestVersion.get("/", async (c) => {
const { itag, id, local } = c.req.query();
const { check, itag, id, local } = c.req.query();
c.header("access-control-allow-origin", "*");

if (!id || !itag) {
Expand All @@ -26,6 +29,18 @@ latestVersion.get("/", async (c) => {
Record<string, unknown>
>;

if (konfigStore.get("server.verify_requests") && check == undefined) {
throw new HTTPException(400, {
res: new Response("No check ID."),
});
} else if (konfigStore.get("server.verify_requests") && check) {
if (verifyRequest(check, id, konfigStore) === false) {
throw new HTTPException(400, {
res: new Response("ID incorrect."),
});
}
}

const youtubePlayerResponseJson = await youtubePlayerParsing(
innertubeClient,
id,
Expand Down

0 comments on commit c446f33

Please sign in to comment.