0
0
Fork 0
mirror of https://github.com/VueTubeApp/VueTube synced 2024-11-13 23:15:07 +00:00
VueTube/NUXT/plugins/ryd.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-03-20 18:42:12 +00:00
//--- Modules/Imports ---//
2022-03-21 23:47:11 +00:00
import { Http } from "@capacitor-community/http";
import constants from "./constants";
2022-03-20 18:42:12 +00:00
function generateUserID(length = 36) {
2022-03-21 23:47:11 +00:00
const charset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
if (crypto && crypto.getRandomValues) {
const values = new Uint32Array(length);
crypto.getRandomValues(values);
for (let i = 0; i < length; i++) {
result += charset[values[i] % charset.length];
2022-03-20 18:42:12 +00:00
}
2022-03-21 23:47:11 +00:00
return result;
} else {
for (let i = 0; i < length; i++) {
result += charset[Math.floor(Math.random() * charset.length)];
}
return result;
2022-03-20 18:42:12 +00:00
}
2022-03-21 23:47:11 +00:00
}
2022-03-20 18:42:12 +00:00
function countLeadingZeroes(uInt8View, limit) {
2022-03-21 23:47:11 +00:00
let zeroes = 0;
let value = 0;
for (let i = 0; i < uInt8View.length; i++) {
value = uInt8View[i];
if (value === 0) {
zeroes += 8;
} else {
let count = 1;
if (value >>> 4 === 0) {
count += 4;
value <<= 4;
2022-03-20 18:42:12 +00:00
}
2022-03-21 23:47:11 +00:00
if (value >>> 6 === 0) {
count += 2;
value <<= 2;
2022-03-20 18:42:12 +00:00
}
2022-03-21 23:47:11 +00:00
zeroes += count - (value >>> 7);
break;
}
if (zeroes >= limit) {
break;
2022-03-20 18:42:12 +00:00
}
}
2022-03-21 23:47:11 +00:00
return zeroes;
}
2022-03-20 18:42:12 +00:00
//--- Puzzle Solver from Anarios --//
async function solvePuzzle(puzzle) {
2022-03-21 23:47:11 +00:00
let challenge = Uint8Array.from(atob(puzzle.challenge), (c) =>
c.charCodeAt(0)
);
let buffer = new ArrayBuffer(20);
let uInt8View = new Uint8Array(buffer);
let uInt32View = new Uint32Array(buffer);
let maxCount = Math.pow(2, puzzle.difficulty) * 5;
for (let i = 4; i < 20; i++) {
uInt8View[i] = challenge[i - 4];
2022-03-20 18:42:12 +00:00
}
2022-03-21 23:47:11 +00:00
for (let i = 0; i < maxCount; i++) {
uInt32View[0] = i;
let hash = await crypto.subtle.digest("SHA-512", buffer);
let hashUint8 = new Uint8Array(hash);
if (countLeadingZeroes(hashUint8) >= puzzle.difficulty) {
return {
solution: btoa(String.fromCharCode.apply(null, uInt8View.slice(0, 4))),
};
}
}
}
2022-03-20 18:42:12 +00:00
const rydModule = {
2022-03-21 23:47:11 +00:00
logs: new Array(),
2022-03-20 18:42:12 +00:00
2022-03-21 23:47:11 +00:00
//--- Get Dislikes ---//
getDislikes(id, callback) {
console.log("fetching ryd");
Http.request({
method: "GET",
url: `https://returnyoutubedislikeapi.com/votes`,
params: { videoId: id },
})
.then((res) => {
callback(res.data);
})
.catch((err) => {
callback(err);
});
},
};
2022-03-20 18:42:12 +00:00
//--- Start ---//
export default ({ app }, inject) => {
2022-03-21 23:47:11 +00:00
inject("ryd", { ...rydModule });
};