diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.cs b/src/N_m3u8DL-RE.Common/Resource/ResString.cs index 01b7f9b..31eff18 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.cs +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.cs @@ -61,6 +61,7 @@ namespace N_m3u8DL_RE.Common.Resource public static string cmd_useShakaPackager { get => GetText("cmd_useShakaPackager"); } public static string cmd_concurrentDownload { get => GetText("cmd_concurrentDownload"); } 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_liveRecordLimit { get => GetText("cmd_liveRecordLimit"); } public static string cmd_liveRealTimeMerge { get => GetText("cmd_liveRealTimeMerge"); } diff --git a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs index 7d5f8f5..f8ec5fe 100644 --- a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs +++ b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs @@ -244,6 +244,12 @@ namespace N_m3u8DL_RE.Common.Resource zhTW: "錄製直播時即時合併", enUS: "Real-time merge into file when recording live" ), + ["cmd_customProxy"] = new TextContainer + ( + zhCN: "设置请求代理", + zhTW: "設置請求代理", + enUS: "Set web request proxy" + ), ["cmd_useSystemProxy"] = new TextContainer ( zhCN: "使用系统默认代理", diff --git a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs index 5de0b16..9047b6a 100644 --- a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs +++ b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs @@ -10,6 +10,7 @@ using System.CommandLine; using System.CommandLine.Binding; using System.CommandLine.Parsing; using System.Globalization; +using System.Net; using System.Text.RegularExpressions; namespace N_m3u8DL_RE.CommandLine @@ -52,6 +53,8 @@ namespace N_m3u8DL_RE.CommandLine //代理选项 private readonly static Option UseSystemProxy = new(new string[] { "--use-system-proxy" }, description: ResString.cmd_useSystemProxy, getDefaultValue: () => true); + private readonly static Option CustomProxy = new(new string[] { "--custom-proxy" }, description: ResString.cmd_customProxy, parseArgument: ParseProxy) { ArgumentHelpName = "URL" }; + //morehelp private readonly static Option MoreHelp = new(new string[] { "--morehelp" }, description: ResString.cmd_moreHelp) { ArgumentHelpName = "OPTION" }; @@ -76,6 +79,30 @@ namespace N_m3u8DL_RE.CommandLine private readonly static Option AudioFilter = new(new string[] { "-sa", "--select-audio" }, description: ResString.cmd_selectAudio, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" }; private readonly static Option SubtitleFilter = new(new string[] { "-ss", "--select-subtitle" }, description: ResString.cmd_selectSubtitle, parseArgument: ParseStreamFilter) { ArgumentHelpName = "OPTIONS" }; + /// + /// 解析用户代理 + /// + /// + /// + /// + 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; + } + } + /// /// 解析自定义KEY /// @@ -340,20 +367,12 @@ namespace N_m3u8DL_RE.CommandLine LiveRecordLimit = bindingContext.ParseResult.GetValueForOption(LiveRecordLimit), LivePerformAsVod = bindingContext.ParseResult.GetValueForOption(LivePerformAsVod), UseSystemProxy = bindingContext.ParseResult.GetValueForOption(UseSystemProxy), + CustomProxy = bindingContext.ParseResult.GetValueForOption(CustomProxy), }; - if (bindingContext.ParseResult.HasOption(CustomHLSMethod)) - { - option.CustomHLSMethod = bindingContext.ParseResult.GetValueForOption(CustomHLSMethod); - } - if (bindingContext.ParseResult.HasOption(CustomHLSKey)) - { - option.CustomHLSKey = bindingContext.ParseResult.GetValueForOption(CustomHLSKey); - } - if (bindingContext.ParseResult.HasOption(CustomHLSIv)) - { - option.CustomHLSIv = bindingContext.ParseResult.GetValueForOption(CustomHLSIv); - } + if (bindingContext.ParseResult.HasOption(CustomHLSMethod)) option.CustomHLSMethod = bindingContext.ParseResult.GetValueForOption(CustomHLSMethod); + 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); if (parsedHeaders != null) @@ -413,7 +432,7 @@ namespace N_m3u8DL_RE.CommandLine FFmpegBinaryPath, LogLevel, UILanguage, UrlProcessorArgs, Keys, KeyTextFile, DecryptionBinaryPath, UseShakaPackager, MP4RealTimeDecryption, MuxAfterDone, - CustomHLSMethod, CustomHLSKey, CustomHLSIv, UseSystemProxy, + CustomHLSMethod, CustomHLSKey, CustomHLSIv, UseSystemProxy, CustomProxy, LivePerformAsVod, LiveRealTimeMerge, LiveKeepSegments, LiveRecordLimit, MuxImports, VideoFilter, AudioFilter, SubtitleFilter, MoreHelp }; diff --git a/src/N_m3u8DL-RE/CommandLine/MyOption.cs b/src/N_m3u8DL-RE/CommandLine/MyOption.cs index 1b23b58..a4ea8de 100644 --- a/src/N_m3u8DL-RE/CommandLine/MyOption.cs +++ b/src/N_m3u8DL-RE/CommandLine/MyOption.cs @@ -2,6 +2,7 @@ using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Entity; using N_m3u8DL_RE.Enum; +using System.Net; namespace N_m3u8DL_RE.CommandLine { @@ -191,6 +192,10 @@ namespace N_m3u8DL_RE.CommandLine /// See: . /// public byte[]? CustomHLSIv { get; set; } + /// + /// See: . + /// + public WebProxy? CustomProxy { get; set; } public bool MuxKeepFiles { get; set; } } } \ No newline at end of file diff --git a/src/N_m3u8DL-RE/Program.cs b/src/N_m3u8DL-RE/Program.cs index d4730b7..2ddd54d 100644 --- a/src/N_m3u8DL-RE/Program.cs +++ b/src/N_m3u8DL-RE/Program.cs @@ -63,6 +63,12 @@ namespace N_m3u8DL_RE HTTPUtil.HttpClientHandler.UseProxy = false; } + if (option.CustomProxy != null) + { + HTTPUtil.HttpClientHandler.Proxy = option.CustomProxy; + HTTPUtil.HttpClientHandler.UseProxy = true; + } + try { //检查互斥的选项