支持自定义http代理

This commit is contained in:
nilaoda 2022-09-25 12:30:14 +08:00
parent 9211e185fd
commit e5dc06c64b
5 changed files with 50 additions and 13 deletions

View File

@ -61,6 +61,7 @@ namespace N_m3u8DL_RE.Common.Resource
public static string cmd_useShakaPackager { get => GetText("cmd_useShakaPackager"); } public static string cmd_useShakaPackager { get => GetText("cmd_useShakaPackager"); }
public static string cmd_concurrentDownload { get => GetText("cmd_concurrentDownload"); } public static string cmd_concurrentDownload { get => GetText("cmd_concurrentDownload"); }
public static string cmd_useSystemProxy { get => GetText("cmd_useSystemProxy"); } public static string cmd_useSystemProxy { get => GetText("cmd_useSystemProxy"); }
public static string cmd_customProxy { get => GetText("cmd_customProxy"); }
public static string cmd_liveKeepSegments { get => GetText("cmd_liveKeepSegments"); } public static string cmd_liveKeepSegments { get => GetText("cmd_liveKeepSegments"); }
public static string cmd_liveRecordLimit { get => GetText("cmd_liveRecordLimit"); } public static string cmd_liveRecordLimit { get => GetText("cmd_liveRecordLimit"); }
public static string cmd_liveRealTimeMerge { get => GetText("cmd_liveRealTimeMerge"); } public static string cmd_liveRealTimeMerge { get => GetText("cmd_liveRealTimeMerge"); }

View File

@ -244,6 +244,12 @@ namespace N_m3u8DL_RE.Common.Resource
zhTW: "錄製直播時即時合併", zhTW: "錄製直播時即時合併",
enUS: "Real-time merge into file when recording live" enUS: "Real-time merge into file when recording live"
), ),
["cmd_customProxy"] = new TextContainer
(
zhCN: "设置请求代理",
zhTW: "設置請求代理",
enUS: "Set web request proxy"
),
["cmd_useSystemProxy"] = new TextContainer ["cmd_useSystemProxy"] = new TextContainer
( (
zhCN: "使用系统默认代理", zhCN: "使用系统默认代理",

View File

@ -10,6 +10,7 @@ using System.CommandLine;
using System.CommandLine.Binding; using System.CommandLine.Binding;
using System.CommandLine.Parsing; using System.CommandLine.Parsing;
using System.Globalization; using System.Globalization;
using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace N_m3u8DL_RE.CommandLine namespace N_m3u8DL_RE.CommandLine
@ -52,6 +53,8 @@ namespace N_m3u8DL_RE.CommandLine
//代理选项 //代理选项
private readonly static Option<bool> UseSystemProxy = new(new string[] { "--use-system-proxy" }, description: ResString.cmd_useSystemProxy, getDefaultValue: () => true); private readonly static Option<bool> UseSystemProxy = new(new string[] { "--use-system-proxy" }, description: ResString.cmd_useSystemProxy, getDefaultValue: () => true);
private readonly static Option<WebProxy?> CustomProxy = new(new string[] { "--custom-proxy" }, description: ResString.cmd_customProxy, parseArgument: ParseProxy) { ArgumentHelpName = "URL" };
//morehelp //morehelp
private readonly static Option<string?> MoreHelp = new(new string[] { "--morehelp" }, description: ResString.cmd_moreHelp) { ArgumentHelpName = "OPTION" }; private readonly static Option<string?> MoreHelp = new(new string[] { "--morehelp" }, description: ResString.cmd_moreHelp) { ArgumentHelpName = "OPTION" };
@ -76,6 +79,30 @@ namespace N_m3u8DL_RE.CommandLine
private readonly static Option<StreamFilter?> AudioFilter = new(new string[] { "-sa", "--select-audio" }, description: ResString.cmd_selectAudio, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" }; private readonly static Option<StreamFilter?> AudioFilter = new(new string[] { "-sa", "--select-audio" }, description: ResString.cmd_selectAudio, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" };
private readonly static Option<StreamFilter?> SubtitleFilter = new(new string[] { "-ss", "--select-subtitle" }, description: ResString.cmd_selectSubtitle, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" }; private readonly static Option<StreamFilter?> SubtitleFilter = new(new string[] { "-ss", "--select-subtitle" }, description: ResString.cmd_selectSubtitle, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" };
/// <summary>
/// 解析用户代理
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
private static WebProxy? ParseProxy(ArgumentResult result)
{
var input = result.Tokens.First().Value;
try
{
if (string.IsNullOrEmpty(input))
return null;
if (!input.StartsWith("http"))
throw new ArgumentException("url must starts with http");
return new WebProxy(new Uri(input));
}
catch (Exception ex)
{
result.ErrorMessage = $"error in parse {input}: " + ex.Message;
return null;
}
}
/// <summary> /// <summary>
/// 解析自定义KEY /// 解析自定义KEY
/// </summary> /// </summary>
@ -340,20 +367,12 @@ namespace N_m3u8DL_RE.CommandLine
LiveRecordLimit = bindingContext.ParseResult.GetValueForOption(LiveRecordLimit), LiveRecordLimit = bindingContext.ParseResult.GetValueForOption(LiveRecordLimit),
LivePerformAsVod = bindingContext.ParseResult.GetValueForOption(LivePerformAsVod), LivePerformAsVod = bindingContext.ParseResult.GetValueForOption(LivePerformAsVod),
UseSystemProxy = bindingContext.ParseResult.GetValueForOption(UseSystemProxy), UseSystemProxy = bindingContext.ParseResult.GetValueForOption(UseSystemProxy),
CustomProxy = bindingContext.ParseResult.GetValueForOption(CustomProxy),
}; };
if (bindingContext.ParseResult.HasOption(CustomHLSMethod)) if (bindingContext.ParseResult.HasOption(CustomHLSMethod)) option.CustomHLSMethod = bindingContext.ParseResult.GetValueForOption(CustomHLSMethod);
{ if (bindingContext.ParseResult.HasOption(CustomHLSKey)) option.CustomHLSKey = bindingContext.ParseResult.GetValueForOption(CustomHLSKey);
option.CustomHLSMethod = bindingContext.ParseResult.GetValueForOption(CustomHLSMethod); if (bindingContext.ParseResult.HasOption(CustomHLSIv)) option.CustomHLSIv = bindingContext.ParseResult.GetValueForOption(CustomHLSIv);
}
if (bindingContext.ParseResult.HasOption(CustomHLSKey))
{
option.CustomHLSKey = bindingContext.ParseResult.GetValueForOption(CustomHLSKey);
}
if (bindingContext.ParseResult.HasOption(CustomHLSIv))
{
option.CustomHLSIv = bindingContext.ParseResult.GetValueForOption(CustomHLSIv);
}
var parsedHeaders = bindingContext.ParseResult.GetValueForOption(Headers); var parsedHeaders = bindingContext.ParseResult.GetValueForOption(Headers);
if (parsedHeaders != null) if (parsedHeaders != null)
@ -413,7 +432,7 @@ namespace N_m3u8DL_RE.CommandLine
FFmpegBinaryPath, FFmpegBinaryPath,
LogLevel, UILanguage, UrlProcessorArgs, Keys, KeyTextFile, DecryptionBinaryPath, UseShakaPackager, MP4RealTimeDecryption, LogLevel, UILanguage, UrlProcessorArgs, Keys, KeyTextFile, DecryptionBinaryPath, UseShakaPackager, MP4RealTimeDecryption,
MuxAfterDone, MuxAfterDone,
CustomHLSMethod, CustomHLSKey, CustomHLSIv, UseSystemProxy, CustomHLSMethod, CustomHLSKey, CustomHLSIv, UseSystemProxy, CustomProxy,
LivePerformAsVod, LiveRealTimeMerge, LiveKeepSegments, LiveRecordLimit, LivePerformAsVod, LiveRealTimeMerge, LiveKeepSegments, LiveRecordLimit,
MuxImports, VideoFilter, AudioFilter, SubtitleFilter, MoreHelp MuxImports, VideoFilter, AudioFilter, SubtitleFilter, MoreHelp
}; };

View File

@ -2,6 +2,7 @@
using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Entity; using N_m3u8DL_RE.Entity;
using N_m3u8DL_RE.Enum; using N_m3u8DL_RE.Enum;
using System.Net;
namespace N_m3u8DL_RE.CommandLine namespace N_m3u8DL_RE.CommandLine
{ {
@ -191,6 +192,10 @@ namespace N_m3u8DL_RE.CommandLine
/// See: <see cref="CommandInvoker.CustomHLSIv"/>. /// See: <see cref="CommandInvoker.CustomHLSIv"/>.
/// </summary> /// </summary>
public byte[]? CustomHLSIv { get; set; } public byte[]? CustomHLSIv { get; set; }
/// <summary>
/// See: <see cref="CommandInvoker.CustomProxy"/>.
/// </summary>
public WebProxy? CustomProxy { get; set; }
public bool MuxKeepFiles { get; set; } public bool MuxKeepFiles { get; set; }
} }
} }

View File

@ -63,6 +63,12 @@ namespace N_m3u8DL_RE
HTTPUtil.HttpClientHandler.UseProxy = false; HTTPUtil.HttpClientHandler.UseProxy = false;
} }
if (option.CustomProxy != null)
{
HTTPUtil.HttpClientHandler.Proxy = option.CustomProxy;
HTTPUtil.HttpClientHandler.UseProxy = true;
}
try try
{ {
//检查互斥的选项 //检查互斥的选项