2021-04-12 18:40:39 +00:00
|
|
|
const got = require('got')
|
2022-04-27 15:21:02 +00:00
|
|
|
const fs = require('fs')
|
2021-07-20 12:44:19 +00:00
|
|
|
const {_md5, _ecbCrypt, _ecbDecrypt, generateBlowfishKey, decryptChunk} = require('./utils/crypto.js')
|
2021-08-02 19:33:58 +00:00
|
|
|
const { DownloadCanceled, DownloadEmpty} = require('./errors.js')
|
2021-04-12 18:52:15 +00:00
|
|
|
|
2021-07-20 12:56:31 +00:00
|
|
|
const { USER_AGENT_HEADER, pipeline } = require('./utils/index.js')
|
2021-04-09 16:45:57 +00:00
|
|
|
|
|
|
|
function generateStreamPath(sngID, md5, mediaVersion, format){
|
|
|
|
let urlPart = md5+"¤"+format+"¤"+sngID+"¤"+mediaVersion
|
|
|
|
let md5val = _md5(urlPart)
|
|
|
|
let step2 = md5val+"¤"+urlPart+"¤"
|
2021-06-08 17:35:56 +00:00
|
|
|
step2 += '.'.repeat(16 - (step2.length % 16))
|
2021-04-09 16:45:57 +00:00
|
|
|
urlPart = _ecbCrypt('jo6aey6haid2Teih', step2)
|
|
|
|
return urlPart
|
|
|
|
}
|
|
|
|
|
|
|
|
function reverseStreamPath(urlPart){
|
|
|
|
let step2 = _ecbDecrypt('jo6aey6haid2Teih', urlPart)
|
|
|
|
let [, md5, format, sngID, mediaVersion] = step2.split("¤")
|
|
|
|
return [sngID, md5, mediaVersion, format]
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:44:19 +00:00
|
|
|
function generateCryptedStreamURL(sngID, md5, mediaVersion, format){
|
|
|
|
let urlPart = generateStreamPath(sngID, md5, mediaVersion, format)
|
|
|
|
return "https://e-cdns-proxy-" + md5[0] + ".dzcdn.net/mobile/1/" + urlPart
|
|
|
|
}
|
|
|
|
|
2021-04-09 16:45:57 +00:00
|
|
|
function generateStreamURL(sngID, md5, mediaVersion, format){
|
|
|
|
let urlPart = generateStreamPath(sngID, md5, mediaVersion, format)
|
2021-07-19 16:42:43 +00:00
|
|
|
return "https://cdns-proxy-" + md5[0] + ".dzcdn.net/api/1/" + urlPart
|
2021-04-09 16:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reverseStreamURL(url){
|
2021-04-21 17:00:51 +00:00
|
|
|
let urlPart = url.slice(url.find("/1/")+3)
|
2021-04-09 16:45:57 +00:00
|
|
|
return reverseStreamPath(urlPart)
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:21:02 +00:00
|
|
|
async function streamTrack(writepath, track, downloadObject, listener){
|
2021-10-07 14:11:07 +00:00
|
|
|
if (downloadObject && downloadObject.isCanceled) throw new DownloadCanceled
|
2021-04-12 18:40:39 +00:00
|
|
|
let headers = {'User-Agent': USER_AGENT_HEADER}
|
2022-04-27 15:21:02 +00:00
|
|
|
let chunkLength = 0
|
2021-04-09 16:45:57 +00:00
|
|
|
let complete = 0
|
2021-07-25 10:19:57 +00:00
|
|
|
let isCryptedStream = track.downloadURL.includes("/mobile/") || track.downloadURL.includes("/media/")
|
2021-07-20 12:44:19 +00:00
|
|
|
let blowfishKey
|
2022-04-27 15:21:02 +00:00
|
|
|
let outputStream = fs.createWriteStream(writepath)
|
2021-04-09 16:45:57 +00:00
|
|
|
|
2021-06-07 18:14:41 +00:00
|
|
|
let itemData = {
|
|
|
|
id: track.id,
|
|
|
|
title: track.title,
|
|
|
|
artist: track.mainArtist.name
|
|
|
|
}
|
2021-05-16 18:01:56 +00:00
|
|
|
let error = ''
|
2021-04-29 19:14:38 +00:00
|
|
|
|
2021-07-20 12:44:19 +00:00
|
|
|
if (isCryptedStream) blowfishKey = generateBlowfishKey(String(track.id))
|
|
|
|
|
|
|
|
async function* decrypter(source){
|
|
|
|
let modifiedStream = Buffer.alloc(0)
|
|
|
|
for await (let chunk of source){
|
|
|
|
if (!isCryptedStream){
|
|
|
|
yield chunk
|
|
|
|
} else {
|
|
|
|
modifiedStream = Buffer.concat([modifiedStream, chunk])
|
|
|
|
while (modifiedStream.length >= 2048 * 3){
|
|
|
|
let decryptedChunks = Buffer.alloc(0)
|
|
|
|
let decryptingChunks = modifiedStream.slice(0, 2048 * 3)
|
|
|
|
modifiedStream = modifiedStream.slice(2048 * 3)
|
|
|
|
if (decryptingChunks.length >= 2048){
|
|
|
|
decryptedChunks = decryptChunk(decryptingChunks.slice(0, 2048), blowfishKey)
|
|
|
|
decryptedChunks = Buffer.concat([decryptedChunks, decryptingChunks.slice(2048)])
|
|
|
|
}
|
|
|
|
yield decryptedChunks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isCryptedStream){
|
|
|
|
let decryptedChunks = Buffer.alloc(0)
|
|
|
|
if (modifiedStream.length >= 2048){
|
|
|
|
decryptedChunks = decryptChunk(modifiedStream.slice(0, 2048), blowfishKey)
|
|
|
|
decryptedChunks = Buffer.concat([decryptedChunks, modifiedStream.slice(2048)])
|
2021-07-25 14:11:44 +00:00
|
|
|
yield decryptedChunks
|
|
|
|
}else{
|
|
|
|
yield modifiedStream
|
2021-07-20 12:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:21:58 +00:00
|
|
|
async function* depadder(source){
|
|
|
|
let isStart = true
|
|
|
|
for await (let chunk of source){
|
2022-01-04 21:06:44 +00:00
|
|
|
if (isStart && chunk[0] == 0 && chunk.slice(4, 8).toString() !== "ftyp"){
|
2021-09-23 12:21:58 +00:00
|
|
|
let i
|
|
|
|
for (i = 0; i < chunk.length; i++){
|
|
|
|
let byte = chunk[i]
|
|
|
|
if (byte !== 0) break
|
|
|
|
}
|
|
|
|
chunk = chunk.slice(i)
|
|
|
|
}
|
|
|
|
isStart = false
|
|
|
|
yield chunk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 18:01:56 +00:00
|
|
|
let request = got.stream(track.downloadURL, {
|
2021-04-09 16:45:57 +00:00
|
|
|
headers: headers,
|
2022-04-27 15:21:02 +00:00
|
|
|
timeout: 7000,
|
2021-09-30 17:00:36 +00:00
|
|
|
https: {rejectUnauthorized: false}
|
2021-04-09 16:45:57 +00:00
|
|
|
}).on('response', (response)=>{
|
2021-04-12 17:32:57 +00:00
|
|
|
complete = parseInt(response.headers["content-length"])
|
2021-05-16 18:01:56 +00:00
|
|
|
if (complete == 0) {
|
|
|
|
error = "DownloadEmpty"
|
|
|
|
request.destroy()
|
|
|
|
}
|
2022-04-27 15:21:02 +00:00
|
|
|
if (listener) listener.send('downloadInfo', {
|
|
|
|
uuid: downloadObject.uuid,
|
|
|
|
data: itemData,
|
|
|
|
state: "downloading",
|
|
|
|
alreadyStarted: false,
|
|
|
|
value: complete
|
|
|
|
})
|
2021-05-13 16:11:43 +00:00
|
|
|
}).on('data', function(chunk){
|
2021-05-16 18:01:56 +00:00
|
|
|
if (downloadObject.isCanceled) {
|
|
|
|
error = "DownloadCanceled"
|
|
|
|
request.destroy()
|
|
|
|
}
|
2021-05-13 16:11:43 +00:00
|
|
|
chunkLength += chunk.length
|
2021-04-12 18:40:39 +00:00
|
|
|
|
2021-05-13 16:11:43 +00:00
|
|
|
if (downloadObject){
|
2022-04-27 15:21:02 +00:00
|
|
|
downloadObject.progressNext += ((chunk.length / complete) / downloadObject.size) * 100
|
2021-05-13 16:11:43 +00:00
|
|
|
downloadObject.updateProgress(listener)
|
2021-04-09 16:45:57 +00:00
|
|
|
}
|
|
|
|
})
|
2021-04-12 18:40:39 +00:00
|
|
|
|
2021-04-29 19:14:38 +00:00
|
|
|
try {
|
2021-09-23 12:21:58 +00:00
|
|
|
await pipeline(request, decrypter, depadder, outputStream)
|
2021-04-29 19:14:38 +00:00
|
|
|
} catch (e){
|
2022-04-27 15:21:02 +00:00
|
|
|
outputStream.close()
|
|
|
|
if (e instanceof got.ReadError || e instanceof got.TimeoutError || ["ESOCKETTIMEDOUT", "ERR_STREAM_PREMATURE_CLOSE", "ETIMEDOUT"].includes(e.code)){
|
|
|
|
|
|
|
|
if (downloadObject && chunkLength != 0){
|
|
|
|
downloadObject.progressNext -= ((chunkLength / complete) / downloadObject.size) * 100
|
|
|
|
downloadObject.updateProgress(listener)
|
|
|
|
}
|
|
|
|
return await streamTrack(writepath, track, downloadObject, listener)
|
2021-05-16 18:01:56 +00:00
|
|
|
} else if (request.destroyed) {
|
|
|
|
switch (error) {
|
|
|
|
case 'DownloadEmpty': throw new DownloadEmpty
|
|
|
|
case 'DownloadCanceled': throw new DownloadCanceled
|
2021-05-19 21:03:47 +00:00
|
|
|
default: throw e
|
2021-05-16 18:01:56 +00:00
|
|
|
}
|
2022-04-27 15:21:02 +00:00
|
|
|
} else {
|
|
|
|
console.trace(e)
|
|
|
|
throw e
|
|
|
|
}
|
2021-04-29 19:14:38 +00:00
|
|
|
}
|
2022-04-27 15:21:02 +00:00
|
|
|
outputStream.close()
|
2021-04-09 16:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
generateStreamPath,
|
|
|
|
generateStreamURL,
|
2021-07-20 12:44:19 +00:00
|
|
|
generateCryptedStreamURL,
|
2021-04-09 16:45:57 +00:00
|
|
|
reverseStreamPath,
|
|
|
|
reverseStreamURL,
|
2021-08-02 19:33:58 +00:00
|
|
|
streamTrack
|
2021-04-09 16:45:57 +00:00
|
|
|
}
|