更改KEY处理器

This commit is contained in:
nilaoda 2022-07-21 19:14:27 +08:00
parent c34fb94af4
commit 8bfc8eabf8
5 changed files with 51 additions and 30 deletions

View File

@ -16,5 +16,28 @@ namespace N_m3u8DL_RE.Common.Entity
public byte[]? Key { get; set; }
public byte[]? IV { get; set; }
public EncryptInfo() { }
/// <summary>
/// 创建EncryptInfo并尝试自动解析Method
/// </summary>
/// <param name="method"></param>
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;
}
}
}
}

View File

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

View File

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

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

View File

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