mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-10-20 14:41:23 +08:00
feat(server): 支持多域名CORS配置
- 实现了逗号分隔的多域名CORS配置解析功能 - 新增parseCorsAllowOrigins函数处理域名列表解析 - 新增getCorsAllowOrigin函数实现动态域名匹配逻辑 - 更新CORS响应头设置逻辑支持多域名验证 - 添加Vary: Origin响应头优化缓存策略 - 修改README文档说明多域名配置方式
This commit is contained in:
@@ -127,6 +127,39 @@ async function checkVersion() {
|
||||
})
|
||||
}
|
||||
|
||||
function parseCorsAllowOrigins(corsAllowOrigin) {
|
||||
if (!corsAllowOrigin) {
|
||||
return null
|
||||
}
|
||||
|
||||
const origins = corsAllowOrigin
|
||||
.split(',')
|
||||
.map((origin) => origin.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
return origins.length > 0 ? origins : null
|
||||
}
|
||||
|
||||
function getCorsAllowOrigin(allowOrigins, requestOrigin) {
|
||||
if (!allowOrigins) {
|
||||
return requestOrigin || '*'
|
||||
}
|
||||
|
||||
if (allowOrigins.includes('*')) {
|
||||
return '*'
|
||||
}
|
||||
|
||||
if (requestOrigin && allowOrigins.includes(requestOrigin)) {
|
||||
return requestOrigin
|
||||
}
|
||||
|
||||
if (!requestOrigin) {
|
||||
return allowOrigins[0] || null
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the server of NCM API.
|
||||
*
|
||||
@@ -136,6 +169,7 @@ async function checkVersion() {
|
||||
async function constructServer(moduleDefs) {
|
||||
const app = express()
|
||||
const { CORS_ALLOW_ORIGIN } = process.env
|
||||
const allowOrigins = parseCorsAllowOrigins(CORS_ALLOW_ORIGIN)
|
||||
app.set('trust proxy', true)
|
||||
|
||||
/**
|
||||
@@ -147,10 +181,21 @@ async function constructServer(moduleDefs) {
|
||||
*/
|
||||
app.use((req, res, next) => {
|
||||
if (req.path !== '/' && !req.path.includes('.')) {
|
||||
const corsAllowOrigin = getCorsAllowOrigin(
|
||||
allowOrigins,
|
||||
req.headers.origin,
|
||||
)
|
||||
const shouldSetVaryHeader =
|
||||
allowOrigins &&
|
||||
!allowOrigins.includes('*') &&
|
||||
req.headers.origin &&
|
||||
corsAllowOrigin
|
||||
res.set({
|
||||
'Access-Control-Allow-Credentials': true,
|
||||
'Access-Control-Allow-Origin':
|
||||
CORS_ALLOW_ORIGIN || req.headers.origin || '*',
|
||||
...(corsAllowOrigin
|
||||
? { 'Access-Control-Allow-Origin': corsAllowOrigin }
|
||||
: {}),
|
||||
...(shouldSetVaryHeader ? { Vary: 'Origin' } : {}),
|
||||
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
|
||||
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
|
||||
Reference in New Issue
Block a user