From 8bfc8eabf8fd22d09aac8d488e5b3ad281714774 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Thu, 21 Jul 2022 19:14:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9KEY=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Common/Entity/EncryptInfo.cs | 23 +++++++++++++++++++ .../Extractor/HLSExtractor.cs | 23 +++++++++---------- .../Processor/HLS/DefaultHLSKeyProcessor.cs | 21 ++++++++--------- .../Processor/KeyProcessor.cs | 4 ++-- src/N_m3u8DL-RE/Processor/DemoProcessor2.cs | 10 ++++---- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Entity/EncryptInfo.cs b/src/N_m3u8DL-RE.Common/Entity/EncryptInfo.cs index a4eca05..dedfee8 100644 --- a/src/N_m3u8DL-RE.Common/Entity/EncryptInfo.cs +++ b/src/N_m3u8DL-RE.Common/Entity/EncryptInfo.cs @@ -16,5 +16,28 @@ namespace N_m3u8DL_RE.Common.Entity public byte[]? Key { get; set; } public byte[]? IV { get; set; } + + public EncryptInfo() { } + + /// + /// 创建EncryptInfo并尝试自动解析Method + /// + /// + public EncryptInfo(string method) + { + Method = ParseMethod(method); + } + + public static EncryptMethod ParseMethod(string? method) + { + if (method != null && System.Enum.TryParse(method.Replace("-", "_"), out EncryptMethod m)) + { + return m; + } + else + { + return EncryptMethod.UNKNOWN; + } + } } } diff --git a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs index 19a6f02..1605f33 100644 --- a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs +++ b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs @@ -322,21 +322,20 @@ namespace N_m3u8DL_RE.Parser.Extractor if (uri != uri_last) { //加密方式 - if (Enum.TryParse(method.Replace("-", "_"), out EncryptMethod m)) - { - currentEncryptInfo.Method = m; - } - else - { - currentEncryptInfo.Method = EncryptMethod.UNKNOWN; - } + currentEncryptInfo.Method = EncryptInfo.ParseMethod(method); //IV if (!string.IsNullOrEmpty(iv)) { currentEncryptInfo.IV = HexUtil.HexToBytes(iv); } //KEY - currentEncryptInfo.Key = ParseKey(method, uri); + var parsedInfo = ParseKey(method, uri); + currentEncryptInfo.Key = parsedInfo.Key; + //加密方式被处理器更改 + if (currentEncryptInfo.Method != parsedInfo.Method) + { + currentEncryptInfo.Method = parsedInfo.Method; + } } lastKeyLine = line; } @@ -454,14 +453,14 @@ namespace N_m3u8DL_RE.Parser.Extractor return playlist; } - private byte[]? ParseKey(string method, string uriText) + private EncryptInfo ParseKey(string method, string uriText) { foreach (var p in ParserConfig.KeyProcessors) { - if (p.CanProcess(ExtractorType, method, uriText, ParserConfig)) + if (p.CanProcess(ExtractorType, method, uriText, M3u8Content, ParserConfig)) { //匹配到对应处理器后不再继续 - return p.Process(method, uriText, ParserConfig); + return p.Process(method, uriText, M3u8Content, ParserConfig); } } diff --git a/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs b/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs index c7cee1d..016124a 100644 --- a/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs +++ b/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs @@ -13,30 +13,29 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS { public class DefaultHLSKeyProcessor : KeyProcessor { - public override bool CanProcess(ExtractorType extractorType, string method, string uriText, ParserConfig paserConfig) => extractorType == ExtractorType.HLS; + public override bool CanProcess(ExtractorType extractorType, string method, string keyUriText, string m3u8Content, ParserConfig paserConfig) => extractorType == ExtractorType.HLS; - public override byte[]? Process(string method, string uriText, ParserConfig parserConfig) + public override EncryptInfo Process(string method, string keyUriText, string m3u8Content, ParserConfig parserConfig) { - var encryptInfo = new EncryptInfo(); + var encryptInfo = new EncryptInfo(method); - - if (uriText.ToLower().StartsWith("base64:")) + if (keyUriText.ToLower().StartsWith("base64:")) { - encryptInfo.Key = Convert.FromBase64String(uriText.Substring(7)); + encryptInfo.Key = Convert.FromBase64String(keyUriText[7..]); } - else if (uriText.ToLower().StartsWith("data:text/plain;base64,")) + else if (keyUriText.ToLower().StartsWith("data:text/plain;base64,")) { - encryptInfo.Key = Convert.FromBase64String(uriText.Substring(23)); + encryptInfo.Key = Convert.FromBase64String(keyUriText[23..]); } - else if (!string.IsNullOrEmpty(uriText)) + else if (!string.IsNullOrEmpty(keyUriText)) { - var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, uriText), parserConfig); + var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, keyUriText), parserConfig); var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result; encryptInfo.Key = bytes; } - return encryptInfo.Key; + return encryptInfo; } /// diff --git a/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs b/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs index 1d77666..0cecd25 100644 --- a/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs +++ b/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs @@ -11,7 +11,7 @@ namespace N_m3u8DL_RE.Parser.Processor { public abstract class KeyProcessor { - public abstract bool CanProcess(ExtractorType extractorType, string method, string uriText, ParserConfig parserConfig); - public abstract byte[]? Process(string method, string uriText, ParserConfig parserConfig); + public abstract bool CanProcess(ExtractorType extractorType, string method, string keyUriText, string m3u8Content, ParserConfig parserConfig); + public abstract EncryptInfo Process(string method, string keyUriText, string m3u8Content, ParserConfig parserConfig); } } diff --git a/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs b/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs index 120881c..eff7fba 100644 --- a/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs +++ b/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs @@ -15,17 +15,17 @@ namespace N_m3u8DL_RE.Processor { internal class DemoProcessor2 : KeyProcessor { - public override bool CanProcess(ExtractorType extractorType, string method, string uriText, ParserConfig parserConfig) + public override bool CanProcess(ExtractorType extractorType, string method, string uriText, string m3u8Content, ParserConfig parserConfig) { return extractorType == ExtractorType.HLS && parserConfig.Url.Contains("playertest.longtailvideo.com"); } - public override byte[]? Process(string method, string uriText, ParserConfig parserConfig) + public override EncryptInfo Process(string method, string uriText, string m3u8Content, ParserConfig parserConfig) { Logger.InfoMarkUp($"[white on green]My Key Processor => {uriText}[/]"); - var key = new DefaultHLSKeyProcessor().Process(method, uriText, parserConfig); - Logger.InfoMarkUp("[red]" + HexUtil.BytesToHex(key!, " ") + "[/]"); - return key; + var info = new DefaultHLSKeyProcessor().Process(method, uriText, m3u8Content, parserConfig); + Logger.InfoMarkUp("[red]" + HexUtil.BytesToHex(info.Key!, " ") + "[/]"); + return info; } } }