优化解密逻辑

This commit is contained in:
nilaoda 2022-12-19 15:29:18 +08:00
parent 44cc97357f
commit fa7ff57fae
3 changed files with 33 additions and 8 deletions

View File

@ -22,6 +22,8 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS
private static partial Regex OrderFixRegex(); private static partial Regex OrderFixRegex();
[GeneratedRegex("#EXT-X-MAP.*\\.apple\\.com/")] [GeneratedRegex("#EXT-X-MAP.*\\.apple\\.com/")]
private static partial Regex ATVRegex(); private static partial Regex ATVRegex();
[GeneratedRegex("(#EXT-X-KEY:[\\s\\S]*?)(#EXT-X-DISCONTINUITY|#EXT-X-ENDLIST)")]
private static partial Regex ATVRegex2();
public override bool CanProcess(ExtractorType extractorType, string rawText, ParserConfig parserConfig) => extractorType == ExtractorType.HLS; public override bool CanProcess(ExtractorType extractorType, string rawText, ParserConfig parserConfig) => extractorType == ExtractorType.HLS;
@ -80,7 +82,7 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS
if (m3u8Content.Contains("#EXT-X-DISCONTINUITY") && m3u8Content.Contains("#EXT-X-MAP") && (m3u8Url.Contains(".apple.com/") || ATVRegex().IsMatch(m3u8Content))) if (m3u8Content.Contains("#EXT-X-DISCONTINUITY") && m3u8Content.Contains("#EXT-X-MAP") && (m3u8Url.Contains(".apple.com/") || ATVRegex().IsMatch(m3u8Content)))
{ {
//只取加密部分即可 //只取加密部分即可
Regex ykmap = DNSPRegex(); Regex ykmap = ATVRegex2();
if (ykmap.IsMatch(m3u8Content)) if (ykmap.IsMatch(m3u8Content))
{ {
m3u8Content = "#EXTM3U\r\n" + ykmap.Match(m3u8Content).Groups[1].Value + "\r\n#EXT-X-ENDLIST"; m3u8Content = "#EXTM3U\r\n" + ykmap.Match(m3u8Content).Groups[1].Value + "\r\n#EXT-X-ENDLIST";

View File

@ -85,12 +85,19 @@ namespace N_m3u8DL_RE.Downloader
cancellationTokenSource = new(); cancellationTokenSource = new();
var des = Path.ChangeExtension(path, null); var des = Path.ChangeExtension(path, null);
//已下载跳过 //已下载跳过
if (File.Exists(des)) if (File.Exists(des))
{ {
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des }; return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des };
} }
//已解密跳过
var dec = Path.Combine(Path.GetDirectoryName(des)!, Path.GetFileNameWithoutExtension(des) + "_dec" + Path.GetExtension(des));
if (File.Exists(dec))
{
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = dec };
}
//另起线程进行监控 //另起线程进行监控
using var watcher = Task.Factory.StartNew(async () => using var watcher = Task.Factory.StartNew(async () =>
{ {

View File

@ -7,17 +7,26 @@ namespace N_m3u8DL_RE.Util
{ {
internal class MP4DecryptUtil internal class MP4DecryptUtil
{ {
private static string ZeroKid = "00000000000000000000000000000000";
public static async Task<bool> DecryptAsync(bool shakaPackager, string bin, string[]? keys, string source, string dest, string? kid, string init = "") public static async Task<bool> DecryptAsync(bool shakaPackager, string bin, string[]? keys, string source, string dest, string? kid, string init = "")
{ {
if (keys == null || keys.Length == 0) return false; if (keys == null || keys.Length == 0) return false;
var keyPair = keys.First(); string? keyPair = null;
string? trackId = null;
if (!string.IsNullOrEmpty(kid)) if (!string.IsNullOrEmpty(kid))
{ {
var test = keys.Where(k => k.StartsWith(kid)); var test = keys.Where(k => k.StartsWith(kid));
if (test.Any()) keyPair = test.First(); if (test.Any()) keyPair = test.First();
} }
//Apple
if (kid == ZeroKid)
{
keyPair = keys.First();
trackId = "1";
}
if (keyPair == null) return false; if (keyPair == null) return false;
//shakaPackager 无法单独解密init文件 //shakaPackager 无法单独解密init文件
@ -37,12 +46,19 @@ namespace N_m3u8DL_RE.Util
enc = tmpFile; enc = tmpFile;
} }
cmd = $"--enable_raw_key_decryption input=\"{enc}\",stream=0,output=\"{dest}\" " + cmd = $"--quiet --enable_raw_key_decryption input=\"{enc}\",stream=0,output=\"{dest}\" " +
$"--keys key_id={keyPair.Split(':')[0]}:key={keyPair.Split(':')[1]}"; $"--keys {(trackId != null ? $"label={trackId}:" : "")}key_id={ZeroKid}:key={keyPair.Split(':')[1]}";
} }
else else
{ {
cmd = string.Join(" ", keys.Select(k => $"--key {k}")); if (trackId == null)
{
cmd = string.Join(" ", keys.Select(k => $"--key {k}"));
}
else
{
cmd = string.Join(" ", keys.Select(k => $"--key {trackId}:{k.Split(':')[1]}"));
}
if (init != "") if (init != "")
{ {
cmd += $" --fragments-info \"{init}\" "; cmd += $" --fragments-info \"{init}\" ";
@ -69,8 +85,8 @@ namespace N_m3u8DL_RE.Util
{ {
FileName = name, FileName = name,
Arguments = arg, Arguments = arg,
RedirectStandardOutput = true, //RedirectStandardOutput = true,
RedirectStandardError = true, //RedirectStandardError = true,
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = false UseShellExecute = false
})!.WaitForExitAsync(); })!.WaitForExitAsync();