优化KEY处理器

This commit is contained in:
nilaoda 2022-07-22 11:58:38 +08:00
parent 8bfc8eabf8
commit d6b01d83bd
4 changed files with 33 additions and 32 deletions

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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;
}