5.4.7: added support for ancient vk videos & salt improvements

- now you can download 240p hardbass videos from 2008!
- implemented updated stream salt and ip salt properly
This commit is contained in:
wukko 2023-04-29 21:30:59 +06:00
parent 1f40cb9fab
commit 6e097de2db
7 changed files with 38 additions and 16 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "cobalt", "name": "cobalt",
"description": "save what you love", "description": "save what you love",
"version": "5.4.6", "version": "5.4.7",
"author": "wukko", "author": "wukko",
"exports": "./src/cobalt.js", "exports": "./src/cobalt.js",
"type": "module", "type": "module",

View file

@ -5,7 +5,7 @@ import cors from "cors";
import rateLimit from "express-rate-limit"; import rateLimit from "express-rate-limit";
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
process.env.streamSalt = randomBytes(64).toString('hex'); const ipSalt = randomBytes(64).toString('hex');
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
@ -24,7 +24,7 @@ import { changelogHistory } from "./modules/pageRender/onDemand.js";
import { sha256 } from "./modules/sub/crypto.js"; import { sha256 } from "./modules/sub/crypto.js";
import findRendered from "./modules/pageRender/findRendered.js"; import findRendered from "./modules/pageRender/findRendered.js";
if (process.env.selfURL && process.env.streamSalt && process.env.port) { if (process.env.selfURL && process.env.port) {
const commitHash = shortCommit(); const commitHash = shortCommit();
const branch = getCurrentBranch(); const branch = getCurrentBranch();
const app = express(); const app = express();
@ -38,7 +38,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
max: 25, max: 25,
standardHeaders: false, standardHeaders: false,
legacyHeaders: false, legacyHeaders: false,
keyGenerator: (req, res) => sha256(getIP(req), process.env.streamSalt), keyGenerator: (req, res) => sha256(getIP(req), ipSalt),
handler: (req, res, next, opt) => { handler: (req, res, next, opt) => {
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
return; return;
@ -49,7 +49,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
max: 28, max: 28,
standardHeaders: false, standardHeaders: false,
legacyHeaders: false, legacyHeaders: false,
keyGenerator: (req, res) => sha256(getIP(req), process.env.streamSalt), keyGenerator: (req, res) => sha256(getIP(req), ipSalt),
handler: (req, res, next, opt) => { handler: (req, res, next, opt) => {
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') }); res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
return; return;
@ -96,7 +96,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
app.post('/api/json', async (req, res) => { app.post('/api/json', async (req, res) => {
try { try {
let ip = sha256(getIP(req), process.env.streamSalt); let ip = sha256(getIP(req), ipSalt);
let lang = languageCode(req); let lang = languageCode(req);
let j = apiJSON(0, { t: "Bad request" }); let j = apiJSON(0, { t: "Bad request" });
try { try {
@ -122,7 +122,7 @@ if (process.env.selfURL && process.env.streamSalt && process.env.port) {
app.get('/api/:type', (req, res) => { app.get('/api/:type', (req, res) => {
try { try {
let ip = sha256(getIP(req), process.env.streamSalt); let ip = sha256(getIP(req), ipSalt);
switch (req.params.type) { switch (req.params.type) {
case 'stream': case 'stream':
if (req.query.p) { if (req.query.p) {

View file

@ -22,7 +22,7 @@ const representationMatch = {
} }
export default async function(o) { export default async function(o) {
let html; let html, url, filename = `vk_${o.userId}_${o.videoId}_`;
html = await fetch(`https://vk.com/video${o.userId}_${o.videoId}`, { html = await fetch(`https://vk.com/video${o.userId}_${o.videoId}`, {
headers: { "user-agent": genericUserAgent } headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false }); }).then((r) => { return r.text() }).catch(() => { return false });
@ -35,15 +35,25 @@ export default async function(o) {
if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' }; if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' };
if (js.mvData.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; if (js.mvData.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
let mpd = JSON.parse(xml2json(js.player.params[0]["manifest"], { compact: true, spaces: 4 })), if (js.player.params[0]["manifest"]) {
let mpd = JSON.parse(xml2json(js.player.params[0]["manifest"], { compact: true, spaces: 4 })),
repr = mpd.MPD.Period.AdaptationSet.Representation ? mpd.MPD.Period.AdaptationSet.Representation : mpd.MPD.Period.AdaptationSet[0]["Representation"], repr = mpd.MPD.Period.AdaptationSet.Representation ? mpd.MPD.Period.AdaptationSet.Representation : mpd.MPD.Period.AdaptationSet[0]["Representation"],
bestQuality = repr[repr.length - 1], bestQuality = repr[repr.length - 1],
resolutionPick = Number(bestQuality._attributes.width) > Number(bestQuality._attributes.height) ? 'width': 'height'; resolutionPick = Number(bestQuality._attributes.width) > Number(bestQuality._attributes.height) ? 'width': 'height';
if (Number(bestQuality._attributes.id) > Number(quality)) bestQuality = repr[quality];
if (bestQuality) return { if (Number(bestQuality._attributes.id) > Number(quality)) bestQuality = repr[quality];
urls: js.player.params[0][`url${resolutionMatch[bestQuality._attributes[resolutionPick]]}`],
filename: `vk_${o.userId}_${o.videoId}_${bestQuality._attributes.width}x${bestQuality._attributes.height}.mp4` url = js.player.params[0][`url${resolutionMatch[bestQuality._attributes[resolutionPick]]}`];
filename = `${bestQuality._attributes.width}x${bestQuality._attributes.height}.mp4`
} else if (js.player.params[0]["url240"]) { // fallback for when video is too old
url = js.player.params[0]["url240"];
filename += `320x240.mp4`
}
if (url && filename) return {
urls: url,
filename: filename
}; };
return { error: 'ErrorEmptyDownload' } return { error: 'ErrorEmptyDownload' }
} }

View file

@ -3,7 +3,7 @@ export const testers = {
|| (patternMatch["spaceId"] && patternMatch["spaceId"].length === 13), || (patternMatch["spaceId"] && patternMatch["spaceId"].length === 13),
"vk": (patternMatch) => (patternMatch["userId"] && patternMatch["videoId"] "vk": (patternMatch) => (patternMatch["userId"] && patternMatch["videoId"]
&& patternMatch["userId"].length <= 10 && patternMatch["videoId"].length === 9), && patternMatch["userId"].length <= 10 && patternMatch["videoId"].length <= 10),
"bilibili": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 12), "bilibili": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 12),

View file

@ -1,10 +1,12 @@
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { randomBytes } from "crypto";
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { sha256 } from "../sub/crypto.js"; import { sha256 } from "../sub/crypto.js";
import { streamLifespan } from "../config.js"; import { streamLifespan } from "../config.js";
const streamCache = new NodeCache({ stdTTL: streamLifespan/1000, checkperiod: 10, deleteOnExpire: true }); const streamCache = new NodeCache({ stdTTL: streamLifespan/1000, checkperiod: 10, deleteOnExpire: true });
const streamSalt = randomBytes(64).toString('hex');
streamCache.on("expired", (key) => { streamCache.on("expired", (key) => {
streamCache.del(key); streamCache.del(key);
@ -13,7 +15,7 @@ streamCache.on("expired", (key) => {
export function createStream(obj) { export function createStream(obj) {
let streamID = nanoid(), let streamID = nanoid(),
exp = Math.floor(new Date().getTime()) + streamLifespan, exp = Math.floor(new Date().getTime()) + streamLifespan,
ghmac = sha256(`${streamID},${obj.ip},${obj.service},${exp}`, process.env.streamSalt); ghmac = sha256(`${streamID},${obj.ip},${obj.service},${exp}`, streamSalt);
if (!streamCache.has(streamID)) { if (!streamCache.has(streamID)) {
streamCache.set(streamID, { streamCache.set(streamID, {
@ -46,7 +48,7 @@ export function verifyStream(ip, id, hmac, exp) {
let streamInfo = streamCache.get(id); let streamInfo = streamCache.get(id);
if (!streamInfo) return { error: 'this stream token does not exist', status: 400 }; if (!streamInfo) return { error: 'this stream token does not exist', status: 400 };
let ghmac = sha256(`${id},${ip},${streamInfo.service},${exp}`, process.env.streamSalt); let ghmac = sha256(`${id},${ip},${streamInfo.service},${exp}`, streamSalt);
if (String(hmac) === ghmac && String(exp) === String(streamInfo.exp) && ghmac === String(streamInfo.hmac) if (String(hmac) === ghmac && String(exp) === String(streamInfo.exp) && ghmac === String(streamInfo.hmac)
&& String(ip) === streamInfo.ip && Number(exp) > Math.floor(new Date().getTime())) { && String(ip) === streamInfo.ip && Number(exp) > Math.floor(new Date().getTime())) {
return streamInfo; return streamInfo;

View file

@ -65,6 +65,8 @@ export function cleanURL(url, host) {
let forbiddenChars = ['}', '{', '(', ')', '\\', '%', '>', '<', '^', '*', '!', '~', ';', ':', ',', '`', '[', ']', '#', '$', '"', "'", "@"] let forbiddenChars = ['}', '{', '(', ')', '\\', '%', '>', '<', '^', '*', '!', '~', ';', ':', ',', '`', '[', ']', '#', '$', '"', "'", "@"]
switch(host) { switch(host) {
case "vk": case "vk":
url = url.includes('clip') ? url.split('&')[0] : url.split('?')[0];
break;
case "youtube": case "youtube":
url = url.split('&')[0]; url = url.split('&')[0];
break; break;

View file

@ -475,6 +475,14 @@
"code": 200, "code": 200,
"status": "stream" "status": "stream"
} }
}, {
"name": "ancient video (fallback to 240p)",
"url": "https://vk.com/video-1959_28496479",
"params": {},
"expected": {
"code": 200,
"status": "stream"
}
}, { }, {
"name": "inexistent video", "name": "inexistent video",
"url": "https://vk.com/video-53333333_456233333", "url": "https://vk.com/video-53333333_456233333",