Skip to content

Commit

Permalink
Revert "Revert "暂时解决 因md5校验而无法下载的问题 resolved nondanee#98 (nondanee#101)…
Browse files Browse the repository at this point in the history
…" (nondanee#102)"

This reverts commit a4560ae.
  • Loading branch information
nondanee committed May 23, 2019
1 parent a4560ae commit b6182dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# IDE
.vscode
.idea

# Logs
logs
Expand Down
52 changes: 52 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const parse = require('url').parse
const crypto = require('./crypto')
const nativeCrypto = require('crypto')
const request = require('./request')
const match = require('./provider/match')
const cache = require('./cache')

const hook = {
request: {
Expand Down Expand Up @@ -244,6 +246,19 @@ const tryLike = ctx => {
.catch(() => {})
}

const getFileMD5 = url => {
return request('GET', url)
.then(response => {
return new Promise((resolve, reject) => {
let file_md5 = nativeCrypto.createHash('md5')
response
.on('data', chunk => file_md5.update(chunk))
.on('end', () => resolve(file_md5.digest('hex')))
.on('error', error => reject(error))
})
})
}

const tryMatch = ctx => {
const netease = ctx.netease
const jsonBody = netease.jsonBody
Expand All @@ -260,6 +275,43 @@ const tryMatch = ctx => {
item.code = 200
item.br = 320000
item.type = 'mp3'
if(!item.md5) {
const fakeMD5 = () => {
item.md5 = crypto.md5(song.url)
}
if(ctx.netease.path === '/api/song/enhance/download/url') {
// Android版 6.0.0以上 / Mac版 2.0.0以上 预下载 计算md5
const overVersion = (a, b) => {
const version_arr_a = a.split('.')
const version_arr_b = b.split('.')
return !(version_arr_a.find((single, i) => parseInt(single) < parseInt(version_arr_b[i])))
}
try {
let header = ctx.netease.param.header
if(typeof header === 'string') {
header = JSON.parse(ctx.netease.param.header)
}
const {os, appver} = header
console.log('Download device:', os, appver)
const limit = {
android: '6.0.0',
osx: '2.0.0'
}
if(limit[os] && overVersion(appver, limit[os])) {
// 设置7天的缓存 同一个文件地址 MD5应该不会频繁变化
return cache(getFileMD5, song.url, 7 * 24 * 60 * 60 * 1000)
.then(md5 => item.md5 = md5)
} else {
fakeMD5()
}
} catch (e) {
console.log('Unknow device')
fakeMD5()
}
} else {
fakeMD5()
}
}
})
.catch(() => {})
}
Expand Down
1 change: 0 additions & 1 deletion provider/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const check = url => {
song.md5 = response.headers['server-md5']
else if(url.includes('xiami.net') || url.includes('qianqian.com'))
song.md5 = response.headers['etag'].replace(/"/g, '').toLowerCase()
song.md5 = (song.md5) ? song.md5 : crypto.md5(url) //placeholder
song.size = parseInt(response.headers['content-length']) || 0
song.url = response.url.href
})
Expand Down

0 comments on commit b6182dc

Please sign in to comment.