Skip to content

Commit

Permalink
update aes decrypt ,update to 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
HeiSir committed Jun 13, 2020
1 parent a3348c6 commit 7a7429f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 109 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" href="css/style.css">
</head>
<body class="heisir">
<div class="header"><span class="title">HLS Downloader v1.0.2</span>
<div class="header"><span class="title">HLS Downloader v1.0.3</span>
<div class="space"></div>
<a class="home" href="https://tools.heisir.cn/HLSDownload/" target="_blank">官网</a>
<a class="qqgroup" href="https://jq.qq.com/?_wv=1027&k=nhFrZBS0">点击加群</a>
Expand Down
132 changes: 62 additions & 70 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ var fs = require('fs');
var async = require('async');
var dateFormat = require('dateformat');
var download = require('download');
var AES = require("crypto-js/aes");
var crypto = require('crypto');
var isdelts = true;
var mainWindow = null;
Expand Down Expand Up @@ -210,8 +209,7 @@ var QueueObject = /** @class */ (function () {
}
QueueObject.prototype.callback = function (_callback) {
return __awaiter(this, void 0, void 0, function () {
var partent_uri, segment, uri_ts, filename, filpath, _loop_1, this_1, index, state_1;
var _this = this;
var partent_uri, segment, uri_ts, filename, filpath, filpath_dl, index, that, stat, aes_path, key_uri, key_, iv_, cipher, inputData, outputData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -226,79 +224,73 @@ var QueueObject = /** @class */ (function () {
}
filename = ((this.idx + 1) + '').padStart(6, '0') + ".ts";
filpath = path.join(this.dir, filename);
filpath_dl = path.join(this.dir, filename + ".dl");
console.log("2 " + segment.uri, "" + filename);
_loop_1 = function (index) {
var that_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!fs.existsSync(filpath)) return [3 /*break*/, 2];
that_1 = this_1;
return [4 /*yield*/, download(uri_ts, that_1.dir, { filename: filename + ".dl" }).then(function () { return __awaiter(_this, void 0, void 0, function () {
var key_uri;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(segment.key != null && segment.key.method != null)) return [3 /*break*/, 2];
key_uri = segment.key.uri;
if (!/^http.*/.test(segment.key.uri)) {
key_uri = partent_uri + segment.key.uri;
}
return [4 /*yield*/, download(key_uri, that_1.dir, { filename: "aes.key" }).then(function () {
var key_ = fs.readFileSync(path.join(that_1.dir, "aes.key"));
var iv_ = Buffer.from(segment.key.iv.buffer);
var cipher = crypto.createCipheriv((segment.key.method + "-cbc").toLowerCase(), key_, iv_);
var input = fs.createReadStream(path.join(that_1.dir, filename + ".dl"));
var output = fs.createWriteStream(path.join(that_1.dir, filename));
input.pipe(cipher).pipe(output);
})["catch"](function (err_) {
console.error(err_);
console.log('download key error');
})];
case 1:
_a.sent();
fs.unlinkSync(filpath + ".dl");
return [3 /*break*/, 3];
case 2:
fs.renameSync(filpath + ".dl", filpath);
_a.label = 3;
case 3: return [2 /*return*/];
}
});
}); })["catch"](function () {
try {
fs.unlinkSync(filpath + ".dl");
}
catch (derror) {
console.log(derror);
}
})];
case 1:
_a.sent();
_a.label = 2;
case 2:
if (fs.existsSync(filpath)) {
return [2 /*return*/, "break"];
}
return [2 /*return*/];
}
});
};
this_1 = this;
index = 0;
_a.label = 1;
case 1:
if (!(index < 3)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_1(index)];
if (!(index < 3)) return [3 /*break*/, 10];
if (!!fs.existsSync(filpath)) return [3 /*break*/, 8];
that = this;
return [4 /*yield*/, download(uri_ts, that.dir, { filename: filename + ".dl" })["catch"](function (err) {
console.log(err);
if (fs.existsSync(filpath_dl))
fs.unlinkSync(filpath_dl);
})];
case 2:
state_1 = _a.sent();
if (state_1 === "break")
return [3 /*break*/, 4];
_a.label = 3;
_a.sent();
if (!fs.existsSync(filpath_dl)) return [3 /*break*/, 8];
stat = fs.statSync(filpath_dl);
if (!(stat.size > 0)) return [3 /*break*/, 7];
if (!(segment.key != null && segment.key.method != null)) return [3 /*break*/, 5];
aes_path = path.join(this.dir, "aes.key");
if (!!fs.existsSync(aes_path)) return [3 /*break*/, 4];
key_uri = segment.key.uri;
if (!/^http.*/.test(segment.key.uri)) {
key_uri = partent_uri + segment.key.uri;
}
return [4 /*yield*/, download(key_uri, that.dir, { filename: "aes.key" })["catch"](console.error)];
case 3:
_a.sent();
_a.label = 4;
case 4:
if (fs.existsSync(aes_path)) {
try {
key_ = fs.readFileSync(aes_path);
iv_ = segment.key.iv != null ? Buffer.from(segment.key.iv.buffer)
: Buffer.from(that.idx.toString(16).padStart(32, '0'), 'hex');
cipher = crypto.createDecipheriv((segment.key.method + "-cbc").toLowerCase(), key_, iv_);
cipher.on('error', console.error);
inputData = fs.readFileSync(filpath_dl);
outputData = Buffer.concat([cipher.update(inputData), cipher.final()]);
fs.writeFileSync(filpath, outputData);
fs.unlinkSync(filpath_dl);
that.then && that.then();
_callback();
return [2 /*return*/];
}
catch (error) {
console.error(error);
fs.unlinkSync(filpath_dl);
}
}
return [3 /*break*/, 6];
case 5:
fs.renameSync(filpath_dl, filpath);
_a.label = 6;
case 6: return [3 /*break*/, 8];
case 7:
fs.unlinkSync(filpath_dl);
_a.label = 8;
case 8:
if (fs.existsSync(filpath)) {
return [3 /*break*/, 10];
}
_a.label = 9;
case 9:
index++;
return [3 /*break*/, 1];
case 4:
case 10:
if (fs.existsSync(filpath)) {
this.then && this.then();
}
Expand All @@ -322,8 +314,8 @@ function startDownload(url, parser) {
console.log(dir);
var filesegments = [];
fs.mkdirSync(dir, { recursive: true });
//并发 3 个线程下载
var tsQueues = async.queue(queue_callback, 3);
//并发 6 个线程下载
var tsQueues = async.queue(queue_callback, 6);
var count_seg = parser.manifest.segments.length;
var count_downloaded = 0;
var video = {
Expand Down
89 changes: 55 additions & 34 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const fs = require('fs');
var async = require('async');
const dateFormat = require('dateformat');
const download = require('download');
var AES = require("crypto-js/aes");
const crypto = require('crypto');

let isdelts = true;
Expand Down Expand Up @@ -113,7 +112,9 @@ app.on('ready', () => {
configVideos = JSON.parse(fs.readFileSync("config.data"));
} catch (error) {

}
}


});

// 当全部窗口关闭时退出。
Expand Down Expand Up @@ -214,6 +215,7 @@ class QueueObject {
}
let filename = `${ ((this.idx + 1) +'').padStart(6,'0')}.ts`;
let filpath = path.join(this.dir, filename);
let filpath_dl = path.join(this.dir, filename+".dl");

console.log(`2 ${segment.uri}`,`${filename}`);
//检测文件是否存在
Expand All @@ -222,40 +224,59 @@ class QueueObject {
{
// 下载的时候使用.dl后缀的文件名,下载完成后重命名
let that = this;
await download (uri_ts, that.dir, {filename: filename + ".dl"}).then(async ()=>{

//标准解密TS流
if(segment.key != null && segment.key.method != null)
await download (uri_ts, that.dir, {filename: filename + ".dl"}).catch((err)=>{
console.log(err);
if(fs.existsSync(filpath_dl))
fs.unlinkSync( filpath_dl);
});
if( fs.existsSync(filpath_dl) )
{
let stat = fs.statSync(filpath_dl);
if(stat.size > 0)
{
let key_uri = segment.key.uri;
if (! /^http.*/.test(segment.key.uri)) {
key_uri = partent_uri + segment.key.uri;
//标准解密TS流
if(segment.key != null && segment.key.method != null)
{
let aes_path = path.join(this.dir, "aes.key" );
if(!fs.existsSync(aes_path))
{
let key_uri = segment.key.uri;
if (! /^http.*/.test(segment.key.uri)) {
key_uri = partent_uri + segment.key.uri;
}
await download (key_uri, that.dir, { filename: "aes.key" }).catch(console.error);
}
if(fs.existsSync(aes_path ))
{
try {
let key_ = fs.readFileSync( aes_path );
let iv_ = segment.key.iv != null ? Buffer.from(segment.key.iv.buffer)
:Buffer.from(that.idx.toString(16).padStart(32,'0') ,'hex' );
let cipher = crypto.createDecipheriv((segment.key.method+"-cbc").toLowerCase(), key_, iv_);
cipher.on('error', console.error);
let inputData = fs.readFileSync( filpath_dl );
let outputData =Buffer.concat([cipher.update(inputData),cipher.final()]);
fs.writeFileSync(filpath,outputData);

fs.unlinkSync(filpath_dl);
that.then && that.then();
_callback();
return;
} catch (error) {
console.error(error);
fs.unlinkSync(filpath_dl);
}
}
}
else
{
fs.renameSync(filpath_dl,filpath);
}
await download (key_uri, that.dir, {filename: "aes.key"}).then(()=>{
let key_ = fs.readFileSync( path.join( that.dir,"aes.key" ) );
let iv_ = Buffer.from(segment.key.iv.buffer);
const cipher = crypto.createCipheriv((segment.key.method+"-cbc").toLowerCase(), key_, iv_);
const input = fs.createReadStream(path.join(that.dir,filename + ".dl"));
const output = fs.createWriteStream(path.join(that.dir,filename));
input.pipe(cipher).pipe(output);
})
.catch((err_)=>{
console.error(err_);
console.log('download key error');
});
fs.unlinkSync(filpath+".dl");
}
else
{
fs.renameSync(filpath+".dl",filpath);
}
}).catch(()=>{
try {
fs.unlinkSync(filpath+".dl");
} catch (derror) {
console.log(derror);
else{
fs.unlinkSync(filpath_dl);
}
});
}
}
if(fs.existsSync(filpath))
{
Expand Down Expand Up @@ -287,8 +308,8 @@ function startDownload(url:string, parser:any) {
let filesegments = [];
fs.mkdirSync(dir, { recursive: true });

//并发 3 个线程下载
var tsQueues = async.queue(queue_callback, 3 );
//并发 6 个线程下载
var tsQueues = async.queue(queue_callback, 6 );

let count_seg = parser.manifest.segments.length;
let count_downloaded = 0;
Expand Down
32 changes: 30 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "m3u8-downloader",
"version": "1.0.2",
"version": "1.0.3",
"description": "hls 下载器,用于下载主流网站的视频和直播。",
"main": "main.js",
"dependencies": {
Expand All @@ -13,6 +13,7 @@
"https": "^1.0.0",
"m3u8-parser": "^4.4.0",
"queue": "^6.0.1",
"update-electron-app": "^1.5.0",
"url": "^0.11.0"
},
"devDependencies": {
Expand All @@ -23,7 +24,7 @@
"copyffmpeg": "xcopy /D /E /I /F /Y .\\ffmpeg.exe ..\\dist\\m3u8-downloader-win32-x64",
"start": "electron .",
"start2": "npm run complie && electron .",
"package": "npm run complie && electron-packager . m3u8-downloader --asar --platform=win32 --arch=x64 --ignore=^/.git -unpack=ffmpeg.exe --icon=.\\icon\\logo.ico --out ../dist --electron-version=9.0.3 --overwrite && npm run copyffmpeg"
"package": "npm run complie && electron-packager . m3u8-downloader --asar --platform=win32 --arch=x64 --ignore=^/.git --ignore=^/download --ignore=^/config.data -unpack=ffmpeg.exe --icon=.\\icon\\logo.ico --out ../dist --electron-version=9.0.3 --overwrite && npm run copyffmpeg"
},
"author": "heisir qq:1586462",
"license": "ISC"
Expand Down

0 comments on commit 7a7429f

Please sign in to comment.