From 0045e3ebc856928fb53d499e6234b2d1230231ae Mon Sep 17 00:00:00 2001 From: QiYueYiya Date: Sat, 28 Jun 2025 16:37:09 +0800 Subject: [PATCH 1/2] Auto renew Cookie --- src/provider/qq.js | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/provider/qq.js b/src/provider/qq.js index 20a9ced22a..3f8bc7cfb2 100644 --- a/src/provider/qq.js +++ b/src/provider/qq.js @@ -3,10 +3,49 @@ const select = require('./select'); const request = require('../request'); const { getManagedCacheStorage } = require('../cache'); +let COOKIE = process.env.NEW_QQ_COOKIE || process.env.QQ_COOKIE; const headers = { origin: 'http://y.qq.com/', referer: 'http://y.qq.com/', - cookie: process.env.QQ_COOKIE || null, // 'uin=; qm_keyst=', + cookie: COOKIE || null, // 'uin=; qm_keyst=', +}; + +const refresh = () => { + const refresh_token = (COOKIE.match(/qqrefresh_token=([^;]*)/) || [])[1] || ''; + const musickey = (COOKIE.match(/qqmusic_key=([^;]*)/) || [])[1] || ''; + const musicid = parseInt((COOKIE.match(/uin=(\d+)/) || [])[1] || 0, 10); + + const body = { + "req": { + "module": "music.login.LoginServer", + "method": "Login", + "param": { + "refresh_token": refresh_token, + "musickey": musickey, + "musicid": musicid + } + } + }; + const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'; + return request("POST", url, { + 'Cookie': COOKIE, + 'Content-Type': 'application/json' + }, JSON.stringify(body)) + .then(response => response.json()) + .then(jsonBody => { + const data = jsonBody.req?.data || {}; + const newMusickey = data.musickey; + const newMusickeyCreateTime = data.musickeyCreateTime; + if (!newMusickey || !newMusickeyCreateTime) return; + let newCookie = COOKIE; + newCookie = newCookie + .replace(/(qqmusic_key=)[^;]*/, `$1${newMusickey}`) + .replace(/(qm_keyst=)[^;]*/, `$1${newMusickey}`) + .replace(/(psrf_musickey_createtime=)[^;]*/, `$1${newMusickeyCreateTime}`); + process.env.NEW_QQ_COOKIE = newCookie; + headers.cookie = newCookie; + COOKIE = newCookie; + }) }; const format = (song) => ({ @@ -109,6 +148,12 @@ const track = (id) => { }; const cs = getManagedCacheStorage('provider/qq'); -const check = (info) => cs.cache(info, () => search(info)).then(track); +const check = (info) => { + const now = Math.floor(Date.now() / 1000); + const musickey_createtime = ((COOKIE || '').match(/musickey_createtime=(\d+)/) || [])[1] || ''; + const createTime = parseInt(musickey_createtime, 10) + 259200; + if (musickey_createtime && (createTime - now < 600)) refresh(); + return cs.cache(info, () => search(info)).then(track); +}; module.exports = { check, track }; From 8623164b3aadfa9b28339fa6a86e10eff4429289 Mon Sep 17 00:00:00 2001 From: QiYueYiya Date: Sat, 28 Jun 2025 21:07:43 +0800 Subject: [PATCH 2/2] Auto renew Cookie --- src/provider/qq.js | 86 ++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/src/provider/qq.js b/src/provider/qq.js index 3f8bc7cfb2..0e1a80a3da 100644 --- a/src/provider/qq.js +++ b/src/provider/qq.js @@ -11,41 +11,50 @@ const headers = { }; const refresh = () => { - const refresh_token = (COOKIE.match(/qqrefresh_token=([^;]*)/) || [])[1] || ''; - const musickey = (COOKIE.match(/qqmusic_key=([^;]*)/) || [])[1] || ''; - const musicid = parseInt((COOKIE.match(/uin=(\d+)/) || [])[1] || 0, 10); + const refresh_token = + (COOKIE.match(/qqrefresh_token=([^;]*)/) || [])[1] || ''; + const musickey = (COOKIE.match(/qqmusic_key=([^;]*)/) || [])[1] || ''; + const musicid = parseInt((COOKIE.match(/uin=(\d+)/) || [])[1] || 0, 10); - const body = { - "req": { - "module": "music.login.LoginServer", - "method": "Login", - "param": { - "refresh_token": refresh_token, - "musickey": musickey, - "musicid": musicid - } - } - }; - const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'; - return request("POST", url, { - 'Cookie': COOKIE, - 'Content-Type': 'application/json' - }, JSON.stringify(body)) - .then(response => response.json()) - .then(jsonBody => { - const data = jsonBody.req?.data || {}; - const newMusickey = data.musickey; - const newMusickeyCreateTime = data.musickeyCreateTime; - if (!newMusickey || !newMusickeyCreateTime) return; - let newCookie = COOKIE; - newCookie = newCookie - .replace(/(qqmusic_key=)[^;]*/, `$1${newMusickey}`) - .replace(/(qm_keyst=)[^;]*/, `$1${newMusickey}`) - .replace(/(psrf_musickey_createtime=)[^;]*/, `$1${newMusickeyCreateTime}`); - process.env.NEW_QQ_COOKIE = newCookie; - headers.cookie = newCookie; - COOKIE = newCookie; - }) + const body = { + req: { + module: 'music.login.LoginServer', + method: 'Login', + param: { + refresh_token: refresh_token, + musickey: musickey, + musicid: musicid, + }, + }, + }; + const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'; + return request( + 'POST', + url, + { + Cookie: COOKIE, + 'Content-Type': 'application/json', + }, + JSON.stringify(body) + ) + .then((response) => response.json()) + .then((jsonBody) => { + const data = jsonBody.req?.data || {}; + const newMusickey = data.musickey; + const newMusickeyCreateTime = data.musickeyCreateTime; + if (!newMusickey || !newMusickeyCreateTime) return; + let newCookie = COOKIE; + newCookie = newCookie + .replace(/(qqmusic_key=)[^;]*/, `$1${newMusickey}`) + .replace(/(qm_keyst=)[^;]*/, `$1${newMusickey}`) + .replace( + /(psrf_musickey_createtime=)[^;]*/, + `$1${newMusickeyCreateTime}` + ); + process.env.NEW_QQ_COOKIE = newCookie; + headers.cookie = newCookie; + COOKIE = newCookie; + }); }; const format = (song) => ({ @@ -149,10 +158,11 @@ const track = (id) => { const cs = getManagedCacheStorage('provider/qq'); const check = (info) => { - const now = Math.floor(Date.now() / 1000); - const musickey_createtime = ((COOKIE || '').match(/musickey_createtime=(\d+)/) || [])[1] || ''; - const createTime = parseInt(musickey_createtime, 10) + 259200; - if (musickey_createtime && (createTime - now < 600)) refresh(); + const now = Math.floor(Date.now() / 1000); + const musickey_createtime = + ((COOKIE || '').match(/musickey_createtime=(\d+)/) || [])[1] || ''; + const createTime = parseInt(musickey_createtime, 10) + 259200; + if (musickey_createtime && createTime - now < 600) refresh(); return cs.cache(info, () => search(info)).then(track); };