From d6b01d83bd4df976dce3e35989de0af3ee248c05 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Fri, 22 Jul 2022 11:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96KEY=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 --- .../Extractor/HLSExtractor.cs | 25 ++++++----------- .../Processor/HLS/DefaultHLSKeyProcessor.cs | 28 +++++++++++++------ .../Processor/KeyProcessor.cs | 4 +-- src/N_m3u8DL-RE/Processor/DemoProcessor2.cs | 8 +++--- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs index 1605f33..2ef28be 100644 --- a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs +++ b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs @@ -308,6 +308,7 @@ namespace N_m3u8DL_RE.Parser.Extractor //对于IV,没自定义且当前行有IV的话 就用 if (ParserConfig.CustomeKey != null) { + currentEncryptInfo.Key = ParserConfig.CustomeKey; if (ParserConfig.CustomeIV == null && line.Contains("IV=0x")) currentEncryptInfo.IV = HexUtil.HexToBytes(ParserUtil.GetAttribute(line, "IV")); continue; @@ -321,21 +322,11 @@ namespace N_m3u8DL_RE.Parser.Extractor //如果KEY URL相同,不进行重复解析 if (uri != uri_last) { - //加密方式 - currentEncryptInfo.Method = EncryptInfo.ParseMethod(method); - //IV - if (!string.IsNullOrEmpty(iv)) - { - currentEncryptInfo.IV = HexUtil.HexToBytes(iv); - } - //KEY - var parsedInfo = ParseKey(method, uri); + //调用处理器进行解析 + var parsedInfo = ParseKey(line); + currentEncryptInfo.Method = parsedInfo.Method; currentEncryptInfo.Key = parsedInfo.Key; - //加密方式被处理器更改 - if (currentEncryptInfo.Method != parsedInfo.Method) - { - currentEncryptInfo.Method = parsedInfo.Method; - } + currentEncryptInfo.IV = parsedInfo.IV; } lastKeyLine = line; } @@ -453,14 +444,14 @@ namespace N_m3u8DL_RE.Parser.Extractor return playlist; } - private EncryptInfo ParseKey(string method, string uriText) + private EncryptInfo ParseKey(string keyLine) { foreach (var p in ParserConfig.KeyProcessors) { - if (p.CanProcess(ExtractorType, method, uriText, M3u8Content, ParserConfig)) + if (p.CanProcess(ExtractorType, keyLine, M3u8Content, ParserConfig)) { //匹配到对应处理器后不再继续 - return p.Process(method, uriText, M3u8Content, ParserConfig); + return p.Process(keyLine, 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 016124a..a04f497 100644 --- a/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs +++ b/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs @@ -13,24 +13,34 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS { public class DefaultHLSKeyProcessor : KeyProcessor { - public override bool CanProcess(ExtractorType extractorType, string method, string keyUriText, string m3u8Content, ParserConfig paserConfig) => extractorType == ExtractorType.HLS; + public override bool CanProcess(ExtractorType extractorType, string keyLine, string m3u8Content, ParserConfig paserConfig) => extractorType == ExtractorType.HLS; - public override EncryptInfo Process(string method, string keyUriText, string m3u8Content, ParserConfig parserConfig) + public override EncryptInfo Process(string keyLine, string m3u8Content, ParserConfig parserConfig) { - var encryptInfo = new EncryptInfo(method); + var iv = ParserUtil.GetAttribute(keyLine, "IV"); + var method = ParserUtil.GetAttribute(keyLine, "METHOD"); + var uri = ParserUtil.GetAttribute(keyLine, "URI"); - if (keyUriText.ToLower().StartsWith("base64:")) + var encryptInfo = new EncryptInfo(method); + //IV + if (!string.IsNullOrEmpty(iv)) { - encryptInfo.Key = Convert.FromBase64String(keyUriText[7..]); + encryptInfo.IV = HexUtil.HexToBytes(iv); } - else if (keyUriText.ToLower().StartsWith("data:text/plain;base64,")) + + //KEY + if (uri.ToLower().StartsWith("base64:")) { - encryptInfo.Key = Convert.FromBase64String(keyUriText[23..]); + encryptInfo.Key = Convert.FromBase64String(uri[7..]); } - else if (!string.IsNullOrEmpty(keyUriText)) + else if (uri.ToLower().StartsWith("data:text/plain;base64,")) { - var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, keyUriText), parserConfig); + encryptInfo.Key = Convert.FromBase64String(uri[23..]); + } + else if (!string.IsNullOrEmpty(uri)) + { + var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, uri), parserConfig); var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result; encryptInfo.Key = bytes; } diff --git a/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs b/src/N_m3u8DL-RE.Parser/Processor/KeyProcessor.cs index 0cecd25..13d1c4e 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 keyUriText, string m3u8Content, ParserConfig parserConfig); - public abstract EncryptInfo Process(string method, string keyUriText, string m3u8Content, ParserConfig parserConfig); + public abstract bool CanProcess(ExtractorType extractorType, string keyLine, string m3u8Content, ParserConfig parserConfig); + public abstract EncryptInfo Process(string keyLine, string m3u8Content, ParserConfig parserConfig); } } diff --git a/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs b/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs index eff7fba..de6032e 100644 --- a/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs +++ b/src/N_m3u8DL-RE/Processor/DemoProcessor2.cs @@ -15,15 +15,15 @@ namespace N_m3u8DL_RE.Processor { internal class DemoProcessor2 : KeyProcessor { - public override bool CanProcess(ExtractorType extractorType, string method, string uriText, string m3u8Content, ParserConfig parserConfig) + public override bool CanProcess(ExtractorType extractorType, string keyLine, string m3u8Content, ParserConfig parserConfig) { return extractorType == ExtractorType.HLS && parserConfig.Url.Contains("playertest.longtailvideo.com"); } - public override EncryptInfo Process(string method, string uriText, string m3u8Content, ParserConfig parserConfig) + public override EncryptInfo Process(string keyLine, string m3u8Content, ParserConfig parserConfig) { - Logger.InfoMarkUp($"[white on green]My Key Processor => {uriText}[/]"); - var info = new DefaultHLSKeyProcessor().Process(method, uriText, m3u8Content, parserConfig); + Logger.InfoMarkUp($"[white on green]My Key Processor => {keyLine}[/]"); + var info = new DefaultHLSKeyProcessor().Process(keyLine, m3u8Content, parserConfig); Logger.InfoMarkUp("[red]" + HexUtil.BytesToHex(info.Key!, " ") + "[/]"); return info; }