mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-10-24 00:21:23 +08:00
fix: 修复 cookie 解析器并也尝试从请求头读 cookie
This commit is contained in:
+24
-4
@@ -94,10 +94,12 @@ module.exports = {
|
||||
// 优化:使用for循环替代forEach,性能更好
|
||||
for (let i = 0, len = cookieArr.length; i < len; i++) {
|
||||
let item = cookieArr[i]
|
||||
let arr = item.split('=')
|
||||
// 优化:使用严格等于
|
||||
if (arr.length === 2) {
|
||||
obj[arr[0].trim()] = arr[1].trim()
|
||||
let separator = item.indexOf('=')
|
||||
// 只按第一个等号拆分,保留 Cookie 值中的等号
|
||||
if (separator > 0) {
|
||||
obj[item.slice(0, separator).trim()] = item
|
||||
.slice(separator + 1)
|
||||
.trim()
|
||||
}
|
||||
}
|
||||
return obj
|
||||
@@ -112,6 +114,24 @@ module.exports = {
|
||||
)
|
||||
},
|
||||
|
||||
getCookieHeader(headers) {
|
||||
if (!headers || typeof headers !== 'object') return ''
|
||||
|
||||
if (typeof headers.get === 'function') {
|
||||
const cookie = headers.get('cookie')
|
||||
if (typeof cookie === 'string') return cookie
|
||||
}
|
||||
|
||||
const headerName = Object.keys(headers).find(
|
||||
(name) => name.toLowerCase() === 'cookie',
|
||||
)
|
||||
if (!headerName) return ''
|
||||
|
||||
const cookie = headers[headerName]
|
||||
if (Array.isArray(cookie)) return cookie.join('; ')
|
||||
return typeof cookie === 'string' ? cookie : ''
|
||||
},
|
||||
|
||||
cookieObjToString(cookie) {
|
||||
// 优化:使用预绑定的keys函数和for循环
|
||||
const cookieKeys = keys(cookie)
|
||||
|
||||
+6
-2
@@ -1,11 +1,15 @@
|
||||
const { hasCookie } = require('./index')
|
||||
const { getCookieHeader, hasCookie } = require('./index')
|
||||
|
||||
const createOption = (query, crypto = '', checkToken = false) => {
|
||||
const headerCookie = getCookieHeader(query.headers)
|
||||
|
||||
return {
|
||||
crypto: query.crypto || crypto || '',
|
||||
cookie: hasCookie(query.cookie)
|
||||
? query.cookie
|
||||
: process.env.NETEASE_COOKIE,
|
||||
: hasCookie(headerCookie)
|
||||
? headerCookie
|
||||
: process.env.NETEASE_COOKIE,
|
||||
ua: query.ua || '',
|
||||
proxy: query.proxy,
|
||||
realIP: query.realIP,
|
||||
|
||||
+9
-1
@@ -13,6 +13,8 @@ const tmpPath = require('os').tmpdir()
|
||||
const {
|
||||
cookieToJson,
|
||||
cookieObjToString,
|
||||
getCookieHeader,
|
||||
hasCookie,
|
||||
toBoolean,
|
||||
generateRandomChineseIP,
|
||||
} = require('./index')
|
||||
@@ -243,13 +245,19 @@ const createRequest = async (uri, data, options) => {
|
||||
headers['X-Forwarded-For'] = ip
|
||||
}
|
||||
|
||||
let cookie = options.cookie || {}
|
||||
let cookie = hasCookie(options.cookie)
|
||||
? options.cookie
|
||||
: getCookieHeader(options.headers)
|
||||
if (!hasCookie(cookie)) cookie = {}
|
||||
if (typeof cookie === 'string') {
|
||||
cookie = cookieToJson(cookie)
|
||||
}
|
||||
|
||||
if (typeof cookie === 'object') {
|
||||
cookie = processCookieObject(cookie, crypto)
|
||||
Object.keys(headers).forEach((name) => {
|
||||
if (name.toLowerCase() === 'cookie') delete headers[name]
|
||||
})
|
||||
headers['Cookie'] = cookieObjToString(cookie)
|
||||
}
|
||||
let url = ''
|
||||
|
||||
Reference in New Issue
Block a user