优化KEY处理器
This commit is contained in:
parent
8bfc8eabf8
commit
d6b01d83bd
|
@ -308,6 +308,7 @@ namespace N_m3u8DL_RE.Parser.Extractor
|
||||||
//对于IV,没自定义且当前行有IV的话 就用
|
//对于IV,没自定义且当前行有IV的话 就用
|
||||||
if (ParserConfig.CustomeKey != null)
|
if (ParserConfig.CustomeKey != null)
|
||||||
{
|
{
|
||||||
|
currentEncryptInfo.Key = ParserConfig.CustomeKey;
|
||||||
if (ParserConfig.CustomeIV == null && line.Contains("IV=0x"))
|
if (ParserConfig.CustomeIV == null && line.Contains("IV=0x"))
|
||||||
currentEncryptInfo.IV = HexUtil.HexToBytes(ParserUtil.GetAttribute(line, "IV"));
|
currentEncryptInfo.IV = HexUtil.HexToBytes(ParserUtil.GetAttribute(line, "IV"));
|
||||||
continue;
|
continue;
|
||||||
|
@ -321,21 +322,11 @@ namespace N_m3u8DL_RE.Parser.Extractor
|
||||||
//如果KEY URL相同,不进行重复解析
|
//如果KEY URL相同,不进行重复解析
|
||||||
if (uri != uri_last)
|
if (uri != uri_last)
|
||||||
{
|
{
|
||||||
//加密方式
|
//调用处理器进行解析
|
||||||
currentEncryptInfo.Method = EncryptInfo.ParseMethod(method);
|
var parsedInfo = ParseKey(line);
|
||||||
//IV
|
|
||||||
if (!string.IsNullOrEmpty(iv))
|
|
||||||
{
|
|
||||||
currentEncryptInfo.IV = HexUtil.HexToBytes(iv);
|
|
||||||
}
|
|
||||||
//KEY
|
|
||||||
var parsedInfo = ParseKey(method, uri);
|
|
||||||
currentEncryptInfo.Key = parsedInfo.Key;
|
|
||||||
//加密方式被处理器更改
|
|
||||||
if (currentEncryptInfo.Method != parsedInfo.Method)
|
|
||||||
{
|
|
||||||
currentEncryptInfo.Method = parsedInfo.Method;
|
currentEncryptInfo.Method = parsedInfo.Method;
|
||||||
}
|
currentEncryptInfo.Key = parsedInfo.Key;
|
||||||
|
currentEncryptInfo.IV = parsedInfo.IV;
|
||||||
}
|
}
|
||||||
lastKeyLine = line;
|
lastKeyLine = line;
|
||||||
}
|
}
|
||||||
|
@ -453,14 +444,14 @@ namespace N_m3u8DL_RE.Parser.Extractor
|
||||||
return playlist;
|
return playlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
private EncryptInfo ParseKey(string method, string uriText)
|
private EncryptInfo ParseKey(string keyLine)
|
||||||
{
|
{
|
||||||
foreach (var p in ParserConfig.KeyProcessors)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,24 +13,34 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS
|
||||||
{
|
{
|
||||||
public class DefaultHLSKeyProcessor : KeyProcessor
|
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 iv = ParserUtil.GetAttribute(keyLine, "IV");
|
||||||
|
var method = ParserUtil.GetAttribute(keyLine, "METHOD");
|
||||||
|
var uri = ParserUtil.GetAttribute(keyLine, "URI");
|
||||||
|
|
||||||
var encryptInfo = new EncryptInfo(method);
|
var encryptInfo = new EncryptInfo(method);
|
||||||
|
//IV
|
||||||
|
if (!string.IsNullOrEmpty(iv))
|
||||||
|
{
|
||||||
|
encryptInfo.IV = HexUtil.HexToBytes(iv);
|
||||||
|
}
|
||||||
|
|
||||||
if (keyUriText.ToLower().StartsWith("base64:"))
|
//KEY
|
||||||
|
if (uri.ToLower().StartsWith("base64:"))
|
||||||
{
|
{
|
||||||
encryptInfo.Key = Convert.FromBase64String(keyUriText[7..]);
|
encryptInfo.Key = Convert.FromBase64String(uri[7..]);
|
||||||
}
|
}
|
||||||
else if (keyUriText.ToLower().StartsWith("data:text/plain;base64,"))
|
else if (uri.ToLower().StartsWith("data:text/plain;base64,"))
|
||||||
{
|
{
|
||||||
encryptInfo.Key = Convert.FromBase64String(keyUriText[23..]);
|
encryptInfo.Key = Convert.FromBase64String(uri[23..]);
|
||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(keyUriText))
|
else if (!string.IsNullOrEmpty(uri))
|
||||||
{
|
{
|
||||||
var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, keyUriText), parserConfig);
|
var segUrl = PreProcessUrl(ParserUtil.CombineURL(parserConfig.BaseUrl, uri), parserConfig);
|
||||||
var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result;
|
var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result;
|
||||||
encryptInfo.Key = bytes;
|
encryptInfo.Key = bytes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace N_m3u8DL_RE.Parser.Processor
|
||||||
{
|
{
|
||||||
public abstract class KeyProcessor
|
public abstract class KeyProcessor
|
||||||
{
|
{
|
||||||
public abstract bool CanProcess(ExtractorType extractorType, 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 method, string keyUriText, string m3u8Content, ParserConfig parserConfig);
|
public abstract EncryptInfo Process(string keyLine, string m3u8Content, ParserConfig parserConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,15 +15,15 @@ namespace N_m3u8DL_RE.Processor
|
||||||
{
|
{
|
||||||
internal class DemoProcessor2 : KeyProcessor
|
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");
|
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}[/]");
|
Logger.InfoMarkUp($"[white on green]My Key Processor => {keyLine}[/]");
|
||||||
var info = new DefaultHLSKeyProcessor().Process(method, uriText, m3u8Content, parserConfig);
|
var info = new DefaultHLSKeyProcessor().Process(keyLine, m3u8Content, parserConfig);
|
||||||
Logger.InfoMarkUp("[red]" + HexUtil.BytesToHex(info.Key!, " ") + "[/]");
|
Logger.InfoMarkUp("[red]" + HexUtil.BytesToHex(info.Key!, " ") + "[/]");
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue