fix(route): 通过在配置中预添加大量 Cookie 来绕过 Bilibili 反爬 (#13365)
* fix(route): allow to use the cookies which are in config * fix(route): get full cookie
This commit is contained in:
parent
2355b64f5c
commit
982dd67815
|
|
@ -1,24 +1,49 @@
|
|||
const got = require('@/utils/got');
|
||||
const utils = require('./utils');
|
||||
const cheerio = require('cheerio');
|
||||
const config = require('@/config').value;
|
||||
|
||||
module.exports = {
|
||||
getCookie: (ctx) => {
|
||||
if (Object.keys(config.bilibili.cookies).length > 0) {
|
||||
return config.bilibili.cookies[Object.keys(config.bilibili.cookies)[Math.floor(Math.random() * Object.keys(config.bilibili.cookies).length)]];
|
||||
}
|
||||
const key = 'bili-cookie';
|
||||
return ctx.cache.tryGet(key, async () => {
|
||||
// default Referer: https://www.bilibili.com is limited
|
||||
// Bilibili return cookies with multiple set-cookie
|
||||
const url = 'https://www.bilibili.com/';
|
||||
const response = await got(url, {
|
||||
headers: {
|
||||
Referer: url,
|
||||
},
|
||||
});
|
||||
const setCookies = response.headers['set-cookie'];
|
||||
if (typeof setCookies === 'undefined') {
|
||||
let response = await got('https://www.bilibili.com/');
|
||||
const setCookie = response.headers['set-cookie'];
|
||||
if (typeof setCookie === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
return setCookies.map((cookie) => cookie.split(';')[0]).join('; ');
|
||||
const cookie = setCookie.map((cookie) => cookie.split(';')[0]);
|
||||
cookie.push(['b_lsid', utils.lsid()].join('='));
|
||||
cookie.push(['_uuid', utils._uuid()].join('='));
|
||||
response = await got('https://api.bilibili.com/x/frontend/finger/spi', {
|
||||
headers: {
|
||||
Referer: 'https://www.bilibili.com/',
|
||||
Cookie: cookie.join('; '),
|
||||
}
|
||||
});
|
||||
cookie.push(['bvuid4', encodeURIComponent(response.data.data.b_4)].join('='));
|
||||
const e = Math.floor(Date.now() / 1000);;
|
||||
const hexsign = utils.hexsign(e);
|
||||
await got('https://space.bilibili.com/1', {
|
||||
headers: {
|
||||
Referer: 'https://www.bilibili.com/',
|
||||
Cookie: cookie.join('; '),
|
||||
}
|
||||
});
|
||||
response = await got.post(`https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket?key_id=ec02&hexsign=${hexsign}&context[ts]=${e}&csrf=`, {
|
||||
headers: {
|
||||
Referer: 'https://space.bilibili.com/1',
|
||||
Cookie: cookie.join('; '),
|
||||
}
|
||||
});
|
||||
cookie.push(['bili_ticket', response.data.data.ticket].join('='));
|
||||
cookie.push(['bili_ticket_expires', (parseInt(response.data.data.created_at) + parseInt(response.data.data.ttl)).toString()].join('='));
|
||||
return cookie.join('; ');
|
||||
});
|
||||
},
|
||||
getVerifyString: (ctx) => {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
const md5 = require('@/utils/md5');
|
||||
const CryptoJS = require('crypto-js');
|
||||
|
||||
function iframe(aid, page, bvid) {
|
||||
return `<iframe src="https://player.bilibili.com/player.html?${bvid ? `bvid=${bvid}` : `aid=${aid}`}${
|
||||
|
|
@ -6,7 +7,65 @@ function iframe(aid, page, bvid) {
|
|||
}&high_quality=1&autoplay=0" width="650" height="477" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>`;
|
||||
}
|
||||
|
||||
const addVerifyInfo = (params, verifyString) => {
|
||||
// a
|
||||
function randomHexStr(length) {
|
||||
let string = '';
|
||||
for (let r = 0; r < length; r++) {
|
||||
string += dec2HexUpper(16 * Math.random());
|
||||
}
|
||||
return padStringWithZeros(string, length);
|
||||
}
|
||||
|
||||
// o
|
||||
function dec2HexUpper(e) {
|
||||
return Math.ceil(e).toString(16).toUpperCase();
|
||||
}
|
||||
|
||||
// s
|
||||
function padStringWithZeros(string, length) {
|
||||
let padding = '';
|
||||
if (string.length < length) {
|
||||
for (let n = 0; n < length - string.length; n++) {
|
||||
padding += '0';
|
||||
}
|
||||
}
|
||||
return padding + string;
|
||||
}
|
||||
|
||||
function lsid() {
|
||||
const e = Date.now().toString(16).toUpperCase();
|
||||
const lsid = randomHexStr(8) + '_' + e;
|
||||
return lsid;
|
||||
}
|
||||
|
||||
function _uuid() {
|
||||
const e = randomHexStr(8);
|
||||
const t = randomHexStr(4);
|
||||
const r = randomHexStr(4);
|
||||
const n = randomHexStr(4);
|
||||
const o = randomHexStr(12);
|
||||
const i = Date.now();
|
||||
return e + '-' + t + '-' + r + '-' + n + '-' + o + padStringWithZeros((i % 100000).toString(), 5) + 'infoc';
|
||||
}
|
||||
|
||||
// P
|
||||
function shiftCharByOne(string) {
|
||||
let shiftedStr = '';
|
||||
for (let n = 0; n < string.length; n++) {
|
||||
shiftedStr += String.fromCharCode(string.charCodeAt(n) - 1);
|
||||
}
|
||||
return shiftedStr;
|
||||
}
|
||||
|
||||
// o
|
||||
function hexsign(e) {
|
||||
const n = 'YhxToH[2q';
|
||||
const r = CryptoJS.HmacSHA256('ts'.concat(e), shiftCharByOne(n));
|
||||
const o = CryptoJS.enc.Hex.stringify(r);
|
||||
return o;
|
||||
}
|
||||
|
||||
function addVerifyInfo(params, verifyString) {
|
||||
const searchParams = new URLSearchParams(params);
|
||||
searchParams.sort();
|
||||
const verifyParam = searchParams.toString();
|
||||
|
|
@ -17,6 +76,9 @@ const addVerifyInfo = (params, verifyString) => {
|
|||
|
||||
module.exports = {
|
||||
iframe,
|
||||
lsid,
|
||||
_uuid,
|
||||
hexsign,
|
||||
addVerifyInfo,
|
||||
bvidTime: 1589990400,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ const got = require('@/utils/got');
|
|||
const cache = require('./cache');
|
||||
const utils = require('./utils');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const config = require('@/config');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const uid = ctx.params.uid;
|
||||
|
|
@ -29,13 +28,11 @@ module.exports = async (ctx) => {
|
|||
const pageTotal = Math.ceil(response.data.data.page.count / response.data.data.page.ps);
|
||||
|
||||
const getPage = async (pageId) => {
|
||||
// A hack way to skip limit
|
||||
const userAgent = config.ua + `.${pageId}`;
|
||||
const cookie = await cache.getCookie(ctx);
|
||||
await got(`https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, {
|
||||
headers: {
|
||||
Referer: `https://space.bilibili.com/${uid}/`,
|
||||
Cookie: cookie,
|
||||
'User-Agent': userAgent,
|
||||
},
|
||||
});
|
||||
const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=${pageId}&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, verifyString);
|
||||
|
|
@ -43,7 +40,6 @@ module.exports = async (ctx) => {
|
|||
headers: {
|
||||
Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`,
|
||||
Cookie: cookie,
|
||||
'User-Agent': userAgent,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue