From d5e418a13b0c1fb03292804511f3ea87a7169c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 19 Jan 2024 18:50:50 +0800 Subject: [PATCH 1/3] feat(hook): Add customize Netease Cookie --- README.md | 1 + src/hook.js | 15 ++++++++++++++- src/utilities.js | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 836ab6d9f1..cc404d53e3 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ node app.js -o bilibili ytdlp | SIGN_CERT | path | 自定义证书文件 | `SIGN_CERT="./server.crt"` | | SIGN_KEY | path | 自定义密钥文件 | `SIGN_KEY="./server.key"` | | SEARCH_ALBUM | bool | 在其他音源搜索歌曲时携带专辑名称(默认搜索条件 `歌曲名 - 歌手`,启用后搜索条件 `歌曲名 - 歌手 专辑名`) | `SEARCH_ALBUM=true` | +| NETEASE_COOKIE | str | 网易云 Cookie | `MUSIC_U=007554xxx` | #### 日志等级 (`LOG_LEVEL`) diff --git a/src/hook.js b/src/hook.js index 5fc4966c7f..51587918f7 100644 --- a/src/hook.js +++ b/src/hook.js @@ -3,7 +3,7 @@ const crypto = require('./crypto'); const request = require('./request'); const match = require('./provider/match'); const querystring = require('querystring'); -const { isHost } = require('./utilities'); +const { isHost, cookieToMap, mapToCookie } = require('./utilities'); const { getManagedCacheStorage } = require('./cache'); const { logScope } = require('./logger'); @@ -128,6 +128,19 @@ hook.request.before = (ctx) => { ) ) ctx.decision = 'proxy'; + + if (process.env.NETEASE_COOKIE && url.path.includes('url')) { + var cookies = cookieToMap(req.headers.cookie); + var new_cookies = cookieToMap(process.env.NETEASE_COOKIE); + + Object.entries(new_cookies).forEach(([key, value]) => { + cookies[key] = value; + }); + + req.headers.cookie = mapToCookie(cookies); + console.log('Replace netease cookie'); + } + if ( [url.hostname, req.headers.host].some((host) => hook.target.host.has(host) diff --git a/src/utilities.js b/src/utilities.js index d6f07a0415..3410ac75af 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -21,7 +21,22 @@ const isHost = (url, host) => { */ const isHostWrapper = (url) => (host) => isHost(url, host); +const cookieToMap = (cookie) => { + return cookie + .split(';') + .map((cookie) => cookie.trim().split('=')) + .reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {}); +}; + +const mapToCookie = (map) => { + return Object.entries(map) + .map(([key, value]) => `${key}=${value}`) + .join('; '); +}; + module.exports = { isHost, isHostWrapper, + cookieToMap, + mapToCookie, }; From bc375e43c17806ec9174804978955087dc52a3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 19 Jan 2024 19:00:04 +0800 Subject: [PATCH 2/3] chore(hook): Use console.debug replace console.log --- src/hook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hook.js b/src/hook.js index 51587918f7..d69ef47fc8 100644 --- a/src/hook.js +++ b/src/hook.js @@ -138,7 +138,7 @@ hook.request.before = (ctx) => { }); req.headers.cookie = mapToCookie(cookies); - console.log('Replace netease cookie'); + console.debug('Replace netease cookie'); } if ( From 87cd9d49f6a7f896eb26328d3d068cfbf4f18c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 19 Jan 2024 19:10:54 +0800 Subject: [PATCH 3/3] chore(hook): Use logger.debug replace console.debug --- src/hook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hook.js b/src/hook.js index d69ef47fc8..2e8c607c00 100644 --- a/src/hook.js +++ b/src/hook.js @@ -138,7 +138,7 @@ hook.request.before = (ctx) => { }); req.headers.cookie = mapToCookie(cookies); - console.debug('Replace netease cookie'); + logger.debug('Replace netease cookie'); } if (