- Netease-sync/server.js

- 新增 buildDownloadHeadersForUrl(url, source)
      - 当直链域名包含 qqmusic 时,自动带上
          - Referer: https://y.qq.com/
          - Origin: https://y.qq.com
          - Range: bytes=0-
      - 直链下载时使用该头部
This commit is contained in:
史悦
2026-01-13 11:34:16 +08:00
parent ad972dd2a5
commit 0ff4769eb0

View File

@@ -271,6 +271,33 @@ function buildDownloadHeaders(source) {
return headers;
}
function buildDownloadHeadersForUrl(url, source) {
const headers = buildDownloadHeaders(source);
if (!url) return headers;
try {
const parsed = new URL(url);
const host = parsed.host || '';
if (host.includes('qqmusic')) {
headers['Referer'] = 'https://y.qq.com/';
headers['Origin'] = 'https://y.qq.com';
headers['Accept'] = '*/*';
headers['Range'] = 'bytes=0-';
} else if (host.includes('music.163.com')) {
headers['Referer'] = 'https://music.163.com/';
headers['Origin'] = 'https://music.163.com';
} else if (host.includes('kuwo')) {
headers['Referer'] = 'https://www.kuwo.cn/';
headers['Origin'] = 'https://www.kuwo.cn';
}
} catch (e) {
return headers;
}
return headers;
}
function getPreferredQualities(types) {
const preference = ['flac24bit', 'flac', '320k', '128k'];
if (!Array.isArray(types) || types.length === 0) {
@@ -383,18 +410,19 @@ async function tryDownloadWithCandidates(song, source, baseName, candidates, dow
const finalFile = path.join(MUSIC_DIR, `${baseName}.${ext}`);
try {
await downloadFile(directUrl, tempFile, downloadHeaders);
const resolvedHeaders = buildDownloadHeadersForUrl(directUrl, source);
await downloadFile(directUrl, tempFile, resolvedHeaders);
try {
// Try to download cover image
let coverPath = null;
try {
const coverUrl = `${TUNEHUB_API_URL}/api/?source=${source}&id=${song.id}&type=pic`;
coverPath = path.join(MUSIC_DIR, `temp_cover_${Date.now()}_${song.id}.jpg`);
await downloadFile(coverUrl, coverPath, downloadHeaders);
} catch (e) {
console.warn(`[Sync] Failed to download cover for ${song.name}:`, e.message);
}
try {
const coverUrl = `${TUNEHUB_API_URL}/api/?source=${source}&id=${song.id}&type=pic`;
coverPath = path.join(MUSIC_DIR, `temp_cover_${Date.now()}_${song.id}.jpg`);
await downloadFile(coverUrl, coverPath, downloadHeaders);
} catch (e) {
console.warn(`[Sync] Failed to download cover for ${song.name}:`, e.message);
}
// Write Metadata (Title, Artist, Album, Cover)
await writeMetadata(tempFile, finalFile, {