applied feedback from ciarands

This commit is contained in:
Jorrin 2024-01-05 17:01:54 +01:00
parent 605b9d78d1
commit 1970e11443
2 changed files with 33 additions and 59 deletions

View File

@ -1,36 +1,11 @@
import { makeFullUrl } from '@/fetchers/common'; import { makeFullUrl } from '@/fetchers/common';
import { decodeData } from '@/providers/sources/vidsrcto/common';
import { EmbedScrapeContext } from '@/utils/context'; import { EmbedScrapeContext } from '@/utils/context';
export const vidplayBase = 'https://vidplay.site'; export const vidplayBase = 'https://vidplay.site';
// This file is based on https://github.com/Ciarands/vidsrc-to-resolver/blob/960afb11c30aa6497804b4691fb1c401e539cfe7/vidsrc.py#L10 // This file is based on https://github.com/Ciarands/vidsrc-to-resolver/blob/dffa45e726a4b944cb9af0c9e7630476c93c0213/vidsrc.py#L16
// Full credits to @Ciarands! // Full credits to @Ciarands!
export function keyPermutation(key: string, data: any) {
const state = Array.from(Array(256).keys());
let index1 = 0;
for (let i = 0; i < 256; i += 1) {
index1 = (index1 + state[i] + key.charCodeAt(i % key.length)) % 256;
const temp = state[i];
state[i] = state[index1];
state[index1] = temp;
}
index1 = 0;
let index2 = 0;
let finalKey = '';
for (let char = 0; char < data.length; char += 1) {
index1 = (index1 + 1) % 256;
index2 = (index2 + state[index1]) % 256;
const temp = state[index1];
state[index1] = state[index2];
state[index2] = temp;
if (typeof data[char] === 'string') {
finalKey += String.fromCharCode(data[char].charCodeAt(0) ^ state[(state[index1] + state[index2]) % 256]);
} else if (typeof data[char] === 'number') {
finalKey += String.fromCharCode(data[char] ^ state[(state[index1] + state[index2]) % 256]);
}
}
return finalKey;
}
export const getDecryptionKeys = async (ctx: EmbedScrapeContext): Promise<string[]> => { export const getDecryptionKeys = async (ctx: EmbedScrapeContext): Promise<string[]> => {
const res = await ctx.fetcher<string>( const res = await ctx.fetcher<string>(
@ -44,10 +19,10 @@ export const getEncodedId = async (ctx: EmbedScrapeContext) => {
const id = url.pathname.replace('/e/', ''); const id = url.pathname.replace('/e/', '');
const keyList = await getDecryptionKeys(ctx); const keyList = await getDecryptionKeys(ctx);
const decodedId = keyPermutation(keyList[0], id); const decodedId = decodeData(keyList[0], id);
const encodedResult = keyPermutation(keyList[1], decodedId); const encodedResult = decodeData(keyList[1], decodedId);
const base64 = btoa(encodedResult); const b64encoded = btoa(encodedResult);
return base64.replace('/', '_'); return b64encoded.replace('/', '_');
}; };
export const getFuTokenKey = async (ctx: EmbedScrapeContext) => { export const getFuTokenKey = async (ctx: EmbedScrapeContext) => {

View File

@ -1,3 +1,6 @@
// This file is based on https://github.com/Ciarands/vidsrc-to-resolver/blob/dffa45e726a4b944cb9af0c9e7630476c93c0213/vidsrc.py#L16
// Full credits to @Ciarands!
const DECRYPTION_KEY = '8z5Ag5wgagfsOuhz'; const DECRYPTION_KEY = '8z5Ag5wgagfsOuhz';
export const decodeBase64UrlSafe = (str: string) => { export const decodeBase64UrlSafe = (str: string) => {
@ -12,39 +15,35 @@ export const decodeBase64UrlSafe = (str: string) => {
return bytes; return bytes;
}; };
export const decode = (str: Uint8Array) => { export const decodeData = (key: string, data: any) => {
const keyBytes = new TextEncoder().encode(DECRYPTION_KEY); const state = Array.from(Array(256).keys());
let index1 = 0;
let j = 0;
const s = new Uint8Array(256);
for (let i = 0; i < 256; i += 1) { for (let i = 0; i < 256; i += 1) {
s[i] = i; index1 = (index1 + state[i] + key.charCodeAt(i % key.length)) % 256;
const temp = state[i];
state[i] = state[index1];
state[index1] = temp;
} }
index1 = 0;
for (let i = 0, k = 0; i < 256; i += 1) { let index2 = 0;
j = (j + s[i] + keyBytes[k % keyBytes.length]) & 0xff; let finalKey = '';
[s[i], s[j]] = [s[j], s[i]]; for (let char = 0; char < data.length; char += 1) {
k += 1; index1 = (index1 + 1) % 256;
index2 = (index2 + state[index1]) % 256;
const temp = state[index1];
state[index1] = state[index2];
state[index2] = temp;
if (typeof data[char] === 'string') {
finalKey += String.fromCharCode(data[char].charCodeAt(0) ^ state[(state[index1] + state[index2]) % 256]);
} else if (typeof data[char] === 'number') {
finalKey += String.fromCharCode(data[char] ^ state[(state[index1] + state[index2]) % 256]);
}
} }
return finalKey;
const decoded = new Uint8Array(str.length);
let i = 0;
let k = 0;
for (let index = 0; index < str.length; index += 1) {
i = (i + 1) & 0xff;
k = (k + s[i]) & 0xff;
[s[i], s[k]] = [s[k], s[i]];
const t = (s[i] + s[k]) & 0xff;
decoded[index] = str[index] ^ s[t];
}
return decoded;
}; };
export const decryptSourceUrl = (sourceUrl: string) => { export const decryptSourceUrl = (sourceUrl: string) => {
const encoded = decodeBase64UrlSafe(sourceUrl); const encoded = decodeBase64UrlSafe(sourceUrl);
const decoded = decode(encoded); const decoded = decodeData(DECRYPTION_KEY, encoded);
const decodedText = new TextDecoder().decode(decoded); return decodeURIComponent(decodeURIComponent(decoded));
return decodeURIComponent(decodeURIComponent(decodedText));
}; };