Skip to content

fix(getOnlineUrl): 修复网页端登录后获取url的CORS问题#472

Merged
imsyy merged 1 commit into
SPlayer-Dev:devfrom
MoeFurina:dev
Sep 25, 2025
Merged

fix(getOnlineUrl): 修复网页端登录后获取url的CORS问题#472
imsyy merged 1 commit into
SPlayer-Dev:devfrom
MoeFurina:dev

Conversation

@MoeFurina
Copy link
Copy Markdown
Contributor

为什么要修改这里?

简单来说,CORS(跨域资源共享)是浏览器的一种安全机制。它要求网站A想从网站B加载资源时,网站B的服务器必须明确表示“我允许网站A来拿我的资源”。如果服务器没有给出这个“许可”,浏览器就会拦截这个请求。

网易云音乐的音乐链接有个特点:

  • 未登录时获取的链接(通常是 m801 或 m701 开头的域名)是配置好了这个“许可”的。

  • 登录后获取的链接(通常是 m804 或 m704 开头的域名)反而没有配置这个“许可”。

这就导致了一个问题:当我们直接使用登录后的链接去请求音乐时,浏览器会因为安全规则(CORS)而阻止。

解决方案很简单:

既然未登录的链接有“许可”,而登录前后的链接只有域名不同,那我们只需要把登录后链接里的域名(m804/m704)替换成未登录的域名(m801/m701)就可以了。这样,我们就能绕过浏览器的CORS限制,正常播放音乐。

@HelloCodeFlow
Copy link
Copy Markdown

@MonkeyCode-AI review

Copy link
Copy Markdown

@MonkeyCode-AI MonkeyCode-AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是 MonkeyCode AI 编程助手,你可以在 GitHub 任意仓库的任意 Issue 或 PR 中 at @MonkeyCode-AI 来呼唤我。

Pull Request 概述

  • 本次提交修改了播放器中获取歌曲播放地址的逻辑,主要为了解决网页端音乐链接的CORS问题。通过替换URL中的域名来绕过限制,并添加了日志输出便于调试。总体来看,修改是为了解决一个具体的播放问题,提高了用户体验。

Pull Request 变更详情

  • 本次PR仅修改了一个文件:src/utils/player.ts,主要是在获取播放链接时增加了域名替换逻辑和日志输出。
变更详情
文件路径 变更类型 变更内容
src/utils/player.ts 修改 在获取歌曲播放地址时增加域名替换逻辑以解决CORS问题,并添加日志输出

Comment thread src/utils/player.ts
// 客户端直接返回,网页端转 https
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:");
// 客户端直接返回,网页端转 https, 并转换url以便解决音乐链接cors问题
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接通过字符串替换来解决CORS问题虽然能快速生效,但不够灵活和稳定。建议使用更通用的代理方式或配置CORS策略,以提高代码的健壮性和可维护性。

Suggested change
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
// 客户端直接返回,网页端转 https, 并通过代理解决音乐链接cors问题
const url = isElectron
? songData.url
: songData.url
.replace(/^http:/, "https:")
.replace(/m\d+\.music\.126\.net/g, 'proxy.music.126.net'); // 使用更通用的正则表达式和代理域名

Comment thread src/utils/player.ts
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:");
// 客户端直接返回,网页端转 https, 并转换url以便解决音乐链接cors问题
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
console.log(`🎧 ${id} music url:`, url);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在生产环境中,应避免打印敏感信息(如完整的音乐URL)到控制台,以防止潜在的安全风险。

Suggested change
console.log(`🎧 ${id} music url:`, url);
// 可以考虑仅在开发模式下打印详细日志
if (process.env.NODE_ENV === 'development') {
console.log(`🎧 ${id} music url:`, url);
}

Copy link
Copy Markdown

@MonkeyCode-AI MonkeyCode-AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是 MonkeyCode AI 编程助手,你可以在 GitHub 任意仓库的任意 Issue 或 PR 中 at @MonkeyCode-AI 来呼唤我。

Pull Request 概述

  • 本次 PR 修改了播放器中获取在线歌曲 URL 的逻辑,主要目的是解决音乐链接的 CORS 问题。

Pull Request 变更详情

  • 修改了 src/utils/player.ts 文件中获取在线播放链接的方法,增加对音乐链接域名的替换处理以解决 CORS 问题,并增加了调试日志输出。
变更详情
文件路径 变更类型 变更内容
src/utils/player.ts 修改 在获取在线播放链接时,增加了对音乐链接域名的替换处理和日志输出,以解决 CORS 问题

Comment thread src/utils/player.ts
// 客户端直接返回,网页端转 https
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:");
// 客户端直接返回,网页端转 https, 并转换url以便解决音乐链接cors问题
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接通过字符串替换来解决CORS问题虽然能快速生效,但不够灵活和稳定。建议使用更通用的代理方式或配置CORS策略,以提高代码的健壮性和可维护性。

Suggested change
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
// 客户端直接返回,网页端转 https, 并通过代理解决音乐链接cors问题
const url = isElectron
? songData.url
: songData.url
.replace(/^http:/, "https:")
.replace(/m\d+\.music\.126\.net/g, 'proxy.music.126.net'); // 使用更通用的正则表达式和代理域名

Comment thread src/utils/player.ts
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:");
// 客户端直接返回,网页端转 https, 并转换url以便解决音乐链接cors问题
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
console.log(`🎧 ${id} music url:`, url);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在生产环境中,应避免打印敏感信息(如完整的音乐URL)到控制台,以防止潜在的安全风险。

Suggested change
console.log(`🎧 ${id} music url:`, url);
// 可以考虑仅在开发模式下打印详细日志
if (process.env.NODE_ENV === 'development') {
console.log(`🎧 ${id} music url:`, url);
}

Comment thread src/utils/player.ts
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:");
// 客户端直接返回,网页端转 https, 并转换url以便解决音乐链接cors问题
const url = isElectron ? songData.url : songData.url.replace(/^http:/, "https:").replace(/m804\.music\.126\.net/g, 'm801.music.126.net').replace(/m704\.music\.126\.net/g, 'm701.music.126.net');
console.log(`🎧 ${id} music url:`, url);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处添加的 console.log 会在生产环境中输出调试信息,可能会暴露敏感数据或影响性能。

Suggested change
console.log(`🎧 ${id} music url:`, url);
// 🎧 ${id} music url: ${url}

@imsyy imsyy merged commit 00e6f7b into SPlayer-Dev:dev Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants