From 9cd6cda6962fac2c2e9e9697d84d6f5deefcaaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=99=BD-=E7=99=BD?= Date: Sun, 14 Feb 2021 17:55:45 +0800 Subject: [PATCH] fix: [breaking] [config] pixiv support refresh token login (#6911) * fix: support refresh token login (#6883) * docs: update pixiv login * docs: update pixiv login --- docs/en/install/README.md | 4 +-- docs/install/README.md | 4 +-- lib/config.js | 3 +- lib/routes/pixiv/bookmarks.js | 2 +- lib/routes/pixiv/illustfollow.js | 2 +- lib/routes/pixiv/ranking.js | 2 +- lib/routes/pixiv/search.js | 2 +- lib/routes/pixiv/token.js | 54 +++++++++----------------------- lib/routes/pixiv/user.js | 2 +- 9 files changed, 22 insertions(+), 53 deletions(-) diff --git a/docs/en/install/README.md b/docs/en/install/README.md index b0ec4ea209..d88c310d2a 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -453,9 +453,7 @@ See docs of specified route and `lib/config.js` for detail information. - pixiv: [Registration](https://accounts.pixiv.net/signup) - - `PIXIV_USERNAME`: Pixiv username - - - `PIXIV_PASSWORD`: Pixiv password + - `PIXIV_PIXIV_REFRESHTOKEN`: Please refer to [this article](https://gist.github.com/ZipFile/c9ebedb224406f4f11845ab700124362) to get a `refresh_token` - `PIXIV_BYPASS_CDN`: bypass Cloudflare bot check by directly accessing Pixiv source server, defaults to disable, set `true` or `1` to enable diff --git a/docs/install/README.md b/docs/install/README.md index 03b6218688..77a5d6a314 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -479,9 +479,7 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行 - pixiv 全部路由:[注册地址](https://accounts.pixiv.net/signup) - - `PIXIV_USERNAME`: Pixiv 用户名 - - - `PIXIV_PASSWORD`: Pixiv 密码 + - `PIXIV_PIXIV_REFRESHTOKEN`: Pixiv Refresh Token, 请参考 [此文](https://gist.github.com/ZipFile/c9ebedb224406f4f11845ab700124362) 获取,或自行对客户端抓包获取 - `PIXIV_BYPASS_CDN`: 绕过 Pixiv 前置的 Cloudflare CDN, 使用`PIXIV_BYPASS_HOSTNAME`指示的 IP 地址访问 Pixiv API, 可以解决因 Cloudflare 机器人验证导致的登录失败问题,默认关闭,设置 true 或 1 开启 diff --git a/lib/config.js b/lib/config.js index 0ebad15129..3dbf731e76 100644 --- a/lib/config.js +++ b/lib/config.js @@ -50,8 +50,7 @@ const calculateValue = () => { nodeName: envs.NODE_NAME, suffix: envs.SUFFIX, pixiv: { - username: envs.PIXIV_USERNAME, - password: envs.PIXIV_PASSWORD, + refreshToken: envs.PIXIV_REFRESHTOKEN, bypassCdn: envs.PIXIV_BYPASS_CDN !== '0' && envs.PIXIV_BYPASS_CDN !== 'false', bypassCdnHostname: envs.PIXIV_BYPASS_HOSTNAME || 'public-api.secure.pixiv.net', bypassCdnDoh: envs.PIXIV_BYPASS_DOH || 'https://1.1.1.1/dns-query', diff --git a/lib/routes/pixiv/bookmarks.js b/lib/routes/pixiv/bookmarks.js index e47bec3a89..eb7ab8cc97 100644 --- a/lib/routes/pixiv/bookmarks.js +++ b/lib/routes/pixiv/bookmarks.js @@ -4,7 +4,7 @@ const getUserDetail = require('./api/getUserDetail'); const config = require('@/config').value; module.exports = async (ctx) => { - if (!config.pixiv || !config.pixiv.username || !config.pixiv.password) { + if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; } diff --git a/lib/routes/pixiv/illustfollow.js b/lib/routes/pixiv/illustfollow.js index 40af1393b5..3c817de8f6 100644 --- a/lib/routes/pixiv/illustfollow.js +++ b/lib/routes/pixiv/illustfollow.js @@ -2,7 +2,7 @@ const getToken = require('./token'); const getIllustFollows = require('./api/getIllustFollows'); const config = require('@/config').value; module.exports = async (ctx) => { - if (!config.pixiv || !config.pixiv.username || !config.pixiv.password) { + if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; } if (!getToken()) { diff --git a/lib/routes/pixiv/ranking.js b/lib/routes/pixiv/ranking.js index abffa7e704..837318e202 100644 --- a/lib/routes/pixiv/ranking.js +++ b/lib/routes/pixiv/ranking.js @@ -33,7 +33,7 @@ const links = { }; module.exports = async (ctx) => { - if (!config.pixiv || !config.pixiv.username || !config.pixiv.password) { + if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; } diff --git a/lib/routes/pixiv/search.js b/lib/routes/pixiv/search.js index 1e52201179..174d24bb1d 100644 --- a/lib/routes/pixiv/search.js +++ b/lib/routes/pixiv/search.js @@ -4,7 +4,7 @@ const searchIllust = require('./api/searchIllust'); const config = require('@/config').value; module.exports = async (ctx) => { - if (!config.pixiv || !config.pixiv.username || !config.pixiv.password) { + if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; } diff --git a/lib/routes/pixiv/token.js b/lib/routes/pixiv/token.js index 5975ec7583..e51700a2ff 100644 --- a/lib/routes/pixiv/token.js +++ b/lib/routes/pixiv/token.js @@ -3,43 +3,16 @@ const logger = require('@/utils/logger'); const wait = require('@/utils/wait'); const got = require('./pixiv-got'); const maskHeader = require('./constants').maskHeader; -const md5 = require('@/utils/md5'); - -const pixivConfig = config.pixiv; let token = null; const authorizationInfo = { client_id: 'MOBrBDS8blbauoSck0ZfDbtuzpyT', client_secret: 'lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj', - hash_secret: '28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c', - username: pixivConfig.username, - password: pixivConfig.password, + hash_secret: '28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c' }; -async function getToken() { - const localTime = new Date().toISOString().replace(/\..+/, '') + '+00:00'; - const response = await got({ - method: 'post', - url: 'https://oauth.secure.pixiv.net/auth/token', - form: { - ...authorizationInfo, - get_secure_url: 1, - grant_type: 'password', - }, - headers: { - 'X-Client-Time': localTime, - 'X-Client-Hash': md5(localTime + authorizationInfo.hash_secret), - ...maskHeader, - }, - }).catch(function (e) { - logger.error('Pixiv login fail.'); - logger.debug(e); - }); - return response && response.data && response.data.response; -} - -async function refreshToken(refresh_token) { +async function refreshToken() { const response = await got({ method: 'post', url: 'https://oauth.secure.pixiv.net/auth/token', @@ -47,41 +20,42 @@ async function refreshToken(refresh_token) { ...authorizationInfo, get_secure_url: 1, grant_type: 'refresh_token', - refresh_token: refresh_token, + refresh_token: config.pixiv.refreshToken, }, headers: { ...maskHeader, }, + }).catch(function (e) { + logger.error('Pixiv refresh token failed.'); + logger.debug(e); }); - return response.data.response; + return response && response.data && response.data.response; } async function tokenLoop() { - const res = await getToken(); - if (res) { + const res = await refreshToken(); + if (res && res.access_token) { logger.info('Pixiv login success.'); token = res.access_token; - let refresh_token = res.refresh_token; let expires_in = res.expires_in * 0.9; /* eslint-disable no-constant-condition, no-await-in-loop */ while (true) { await wait(expires_in * 1000); - try { - const refresh_res = await refreshToken(refresh_token); + const refresh_res = await refreshToken(); + if (refresh_res && refresh_res.access_token) { logger.debug('Pixiv refresh token success.'); token = refresh_res.access_token; - refresh_token = refresh_res.refresh_token; expires_in = refresh_res.expires_in * 0.9; - } catch (err) { + } else { expires_in = 30; - logger.error(`Pixiv refresh token failed, retry in ${expires_in} seconds.`, err); + logger.error(`Pixiv refresh token failed, retry in ${expires_in} seconds.`); } } /* eslint-enable no-await-in-loop */ } } -if (config.pixiv && config.pixiv.username && config.pixiv.password) { +if (config.pixiv && config.pixiv.refreshToken) { tokenLoop(); } diff --git a/lib/routes/pixiv/user.js b/lib/routes/pixiv/user.js index a7dbfa2297..cbb5cda820 100644 --- a/lib/routes/pixiv/user.js +++ b/lib/routes/pixiv/user.js @@ -3,7 +3,7 @@ const getIllusts = require('./api/getIllusts'); const config = require('@/config').value; module.exports = async (ctx) => { - if (!config.pixiv || !config.pixiv.username || !config.pixiv.password) { + if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; }