Skip to content

Commit

Permalink
Merge pull request #344 from BlueHtml/main
Browse files Browse the repository at this point in the history
[bug fix] 本地文件被多次解密导致报错 The input data is not a complete block
  • Loading branch information
nilaoda authored Jun 22, 2024
2 parents 8fdb6bc + 21f2e59 commit a29c8b9
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions src/N_m3u8DL-RE/Downloader/SimpleDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using N_m3u8DL_RE.Common.Entity;
using N_m3u8DL_RE.Common.Entity;
using N_m3u8DL_RE.Common.Enum;
using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Config;
Expand Down Expand Up @@ -33,50 +33,57 @@ public SimpleDownloader(DownloaderConfig config)
public async Task<DownloadResult?> DownloadSegmentAsync(MediaSegment segment, string savePath, SpeedContainer speedContainer, Dictionary<string, string>? headers = null)
{
var url = segment.Url;
var dResult = await DownClipAsync(url, savePath, speedContainer, segment.StartRange, segment.StopRange, headers, DownloaderConfig.MyOptions.DownloadRetryCount);
if (dResult != null && dResult.Success && segment.EncryptInfo != null)
var (des, dResult) = await DownClipAsync(url, savePath, speedContainer, segment.StartRange, segment.StopRange, headers, DownloaderConfig.MyOptions.DownloadRetryCount);
if (dResult != null && dResult.Success && dResult.ActualFilePath != des)
{
if (segment.EncryptInfo.Method == EncryptMethod.AES_128)
if (segment.EncryptInfo != null)
{
var key = segment.EncryptInfo.Key;
var iv = segment.EncryptInfo.IV;
AESUtil.AES128Decrypt(dResult.ActualFilePath, key!, iv!);
}
else if (segment.EncryptInfo.Method == EncryptMethod.AES_128_ECB)
{
var key = segment.EncryptInfo.Key;
var iv = segment.EncryptInfo.IV;
AESUtil.AES128Decrypt(dResult.ActualFilePath, key!, iv!, System.Security.Cryptography.CipherMode.ECB);
}
else if (segment.EncryptInfo.Method == EncryptMethod.CHACHA20)
{
var key = segment.EncryptInfo.Key;
var nonce = segment.EncryptInfo.IV;
if (segment.EncryptInfo.Method == EncryptMethod.AES_128)
{
var key = segment.EncryptInfo.Key;
var iv = segment.EncryptInfo.IV;
AESUtil.AES128Decrypt(dResult.ActualFilePath, key!, iv!);
}
else if (segment.EncryptInfo.Method == EncryptMethod.AES_128_ECB)
{
var key = segment.EncryptInfo.Key;
var iv = segment.EncryptInfo.IV;
AESUtil.AES128Decrypt(dResult.ActualFilePath, key!, iv!, System.Security.Cryptography.CipherMode.ECB);
}
else if (segment.EncryptInfo.Method == EncryptMethod.CHACHA20)
{
var key = segment.EncryptInfo.Key;
var nonce = segment.EncryptInfo.IV;

var fileBytes = File.ReadAllBytes(dResult.ActualFilePath);
var decrypted = ChaCha20Util.DecryptPer1024Bytes(fileBytes, key!, nonce!);
await File.WriteAllBytesAsync(dResult.ActualFilePath, decrypted);
}
else if (segment.EncryptInfo.Method == EncryptMethod.SAMPLE_AES_CTR)
{
//throw new NotSupportedException("SAMPLE-AES-CTR");
}
var fileBytes = File.ReadAllBytes(dResult.ActualFilePath);
var decrypted = ChaCha20Util.DecryptPer1024Bytes(fileBytes, key!, nonce!);
await File.WriteAllBytesAsync(dResult.ActualFilePath, decrypted);
}
else if (segment.EncryptInfo.Method == EncryptMethod.SAMPLE_AES_CTR)
{
//throw new NotSupportedException("SAMPLE-AES-CTR");
}

//Image头处理
if (dResult.ImageHeader)
{
await ImageHeaderUtil.ProcessAsync(dResult.ActualFilePath);
}
//Gzip解压
if (dResult.GzipHeader)
{
await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath);
//Image头处理
if (dResult.ImageHeader)
{
await ImageHeaderUtil.ProcessAsync(dResult.ActualFilePath);
}
//Gzip解压
if (dResult.GzipHeader)
{
await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath);
}
}

//处理完成后改名
File.Move(dResult.ActualFilePath, des);
dResult.ActualFilePath = des;
}
return dResult;
}

private async Task<DownloadResult?> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3)
private async Task<(string des, DownloadResult? dResult)> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3)
{
CancellationTokenSource? cancellationTokenSource = null;
retry:
Expand All @@ -89,15 +96,15 @@ public SimpleDownloader(DownloaderConfig config)
if (File.Exists(des))
{
speedContainer.Add(new FileInfo(des).Length);
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des };
return (des, new DownloadResult() { ActualContentLength = 0, ActualFilePath = des });
}

//已解密跳过
var dec = Path.Combine(Path.GetDirectoryName(des)!, Path.GetFileNameWithoutExtension(des) + "_dec" + Path.GetExtension(des));
if (File.Exists(dec))
{
speedContainer.Add(new FileInfo(dec).Length);
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = dec };
return (dec, new DownloadResult() { ActualContentLength = 0, ActualFilePath = dec });
}

//另起线程进行监控
Expand All @@ -118,14 +125,8 @@ public SimpleDownloader(DownloaderConfig config)

//调用下载
var result = await DownloadUtil.DownloadToFileAsync(url, path, speedContainer, cancellationTokenSource, headers, fromPosition, toPosition);

//下载完成后改名
if (result.Success || !DownloaderConfig.CheckContentLength)
{
File.Move(path, des);
result.ActualFilePath = des;
return result;
}
return (des, result);

throw new Exception("please retry");
}
catch (Exception ex)
Expand All @@ -144,7 +145,7 @@ public SimpleDownloader(DownloaderConfig config)
Logger.WarnMarkUp($"[grey]{ex.Message.EscapeMarkup()}[/]");
}
//throw new Exception("download failed", ex);
return null;
return default;
}
finally
{
Expand Down

0 comments on commit a29c8b9

Please sign in to comment.