diff --git a/DownKyi.Core/BiliApi/WebClient.cs b/DownKyi.Core/BiliApi/WebClient.cs index 520e5ef..d828c5d 100644 --- a/DownKyi.Core/BiliApi/WebClient.cs +++ b/DownKyi.Core/BiliApi/WebClient.cs @@ -1,5 +1,6 @@ using Brotli; using DownKyi.Core.BiliApi.Login; +using DownKyi.Core.Settings; using System; using System.Collections.Generic; using System.IO; @@ -50,11 +51,7 @@ namespace DownKyi.Core.BiliApi request.Method = method; request.Timeout = 30 * 1000; - // MacOS Safari - //string safari = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15"; - // Windows 10 Chrome - string chrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"; - request.UserAgent = chrome; + request.UserAgent = SettingsManager.GetInstance().GetUserAgent(); //request.ContentType = "application/json,text/html,application/xhtml+xml,application/xml;charset=UTF-8"; request.Headers["accept-language"] = "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"; diff --git a/DownKyi.Core/Settings/Models/NetworkSettings.cs b/DownKyi.Core/Settings/Models/NetworkSettings.cs index d745442..857952b 100644 --- a/DownKyi.Core/Settings/Models/NetworkSettings.cs +++ b/DownKyi.Core/Settings/Models/NetworkSettings.cs @@ -10,6 +10,7 @@ namespace DownKyi.Core.Settings.Models public AllowStatus IsLiftingOfRegion { get; set; } = AllowStatus.NONE; public AllowStatus UseSSL { get; set; } = AllowStatus.NONE; + public string UserAgent { get; set; } = string.Empty; public Downloader Downloader { get; set; } = Downloader.NOT_SET; public int MaxCurrentDownloads { get; set; } = -1; diff --git a/DownKyi.Core/Settings/SettingsManager.Network.cs b/DownKyi.Core/Settings/SettingsManager.Network.cs index 2e2a468..4e9d04e 100644 --- a/DownKyi.Core/Settings/SettingsManager.Network.cs +++ b/DownKyi.Core/Settings/SettingsManager.Network.cs @@ -10,6 +10,9 @@ namespace DownKyi.Core.Settings // 启用https private readonly AllowStatus useSSL = AllowStatus.YES; + // UserAgent + private readonly string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"; + // 下载器 private readonly Downloader downloader = Downloader.ARIA; @@ -21,7 +24,7 @@ namespace DownKyi.Core.Settings // HttpProxy代理 private readonly AllowStatus isHttpProxy = AllowStatus.NO; - private readonly string httpProxy = ""; + private readonly string httpProxy = string.Empty; private readonly int httpProxyListenPort = 0; // Aria服务器token @@ -50,7 +53,7 @@ namespace DownKyi.Core.Settings // Aria HttpProxy代理 private readonly AllowStatus isAriaHttpProxy = AllowStatus.NO; - private readonly string ariaHttpProxy = ""; + private readonly string ariaHttpProxy = string.Empty; private readonly int ariaHttpProxyListenPort = 0; /// @@ -107,6 +110,33 @@ namespace DownKyi.Core.Settings return SetSettings(); } + /// + /// 获取UserAgent + /// + /// + public string GetUserAgent() + { + appSettings = GetSettings(); + if (appSettings.Network.UserAgent == string.Empty) + { + // 第一次获取,先设置默认值 + SetUserAgent(userAgent); + return userAgent; + } + return appSettings.Network.UserAgent; + } + + /// + /// 设置UserAgent + /// + /// + /// + public bool SetUserAgent(string userAgent) + { + appSettings.Network.UserAgent = userAgent; + return SetSettings(); + } + /// /// 获取下载器 /// diff --git a/DownKyi/Languages/Default.xaml b/DownKyi/Languages/Default.xaml index d7aff13..f00384d 100644 --- a/DownKyi/Languages/Default.xaml +++ b/DownKyi/Languages/Default.xaml @@ -196,6 +196,7 @@ 网络 启用https(若下载器提示SSL错误,则关闭此项) + UserAgent: 选择下载器(重启生效): 内建下载器(测试) Aria2下载器 diff --git a/DownKyi/Services/Download/AriaDownloadService.cs b/DownKyi/Services/Download/AriaDownloadService.cs index d2954a7..bcfb703 100644 --- a/DownKyi/Services/Download/AriaDownloadService.cs +++ b/DownKyi/Services/Download/AriaDownloadService.cs @@ -315,7 +315,7 @@ namespace DownKyi.Services.Download $"Cookie: {LoginHelper.GetLoginInfoCookiesString()}", $"Origin: https://www.bilibili.com", $"Referer: https://www.bilibili.com", - $"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" + $"User-Agent: {SettingsManager.GetInstance().GetUserAgent()}" }; AriaConfig config = new AriaConfig() @@ -419,7 +419,7 @@ namespace DownKyi.Services.Download Out = localFileName, //Header = $"cookie: {LoginHelper.GetLoginInfoCookiesString()}\nreferer: https://www.bilibili.com", //UseHead = "true", - UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36", + UserAgent = SettingsManager.GetInstance().GetUserAgent(), }; // 如果设置了代理,则增加HttpProxy diff --git a/DownKyi/Services/Download/BuiltinDownloadService.cs b/DownKyi/Services/Download/BuiltinDownloadService.cs index 65eb72f..44b7035 100644 --- a/DownKyi/Services/Download/BuiltinDownloadService.cs +++ b/DownKyi/Services/Download/BuiltinDownloadService.cs @@ -301,7 +301,7 @@ namespace DownKyi.Services.Download mtd.Configure(req => { req.CookieContainer = LoginHelper.GetLoginInfoCookies(); - req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"; + req.UserAgent = SettingsManager.GetInstance().GetUserAgent(); req.Referer = "https://www.bilibili.com"; req.Headers.Add("Origin", "https://www.bilibili.com"); diff --git a/DownKyi/Services/Download/CustomAriaDownloadService.cs b/DownKyi/Services/Download/CustomAriaDownloadService.cs index 5dbf0c4..43fea1d 100644 --- a/DownKyi/Services/Download/CustomAriaDownloadService.cs +++ b/DownKyi/Services/Download/CustomAriaDownloadService.cs @@ -347,7 +347,7 @@ namespace DownKyi.Services.Download Out = localFileName, //Header = $"cookie: {LoginHelper.GetLoginInfoCookiesString()}\nreferer: https://www.bilibili.com", //UseHead = "true", - UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36", + UserAgent = SettingsManager.GetInstance().GetUserAgent(), }; //// 如果设置了代理,则增加HttpProxy diff --git a/DownKyi/ViewModels/Settings/ViewNetworkViewModel.cs b/DownKyi/ViewModels/Settings/ViewNetworkViewModel.cs index 98c40c3..eba6594 100644 --- a/DownKyi/ViewModels/Settings/ViewNetworkViewModel.cs +++ b/DownKyi/ViewModels/Settings/ViewNetworkViewModel.cs @@ -27,6 +27,13 @@ namespace DownKyi.ViewModels.Settings set => SetProperty(ref useSSL, value); } + private string userAgent; + public string UserAgent + { + get => userAgent; + set => SetProperty(ref userAgent, value); + } + private bool builtin; public bool Builtin { @@ -268,6 +275,9 @@ namespace DownKyi.ViewModels.Settings AllowStatus useSSL = SettingsManager.GetInstance().UseSSL(); UseSSL = useSSL == AllowStatus.YES; + // UserAgent + UserAgent = SettingsManager.GetInstance().GetUserAgent(); + // 选择下载器 var downloader = SettingsManager.GetInstance().GetDownloader(); switch (downloader) @@ -360,6 +370,19 @@ namespace DownKyi.ViewModels.Settings PublishTip(isSucceed); } + // 设置UserAgent事件 + private DelegateCommand userAgentCommand; + public DelegateCommand UserAgentCommand => userAgentCommand ?? (userAgentCommand = new DelegateCommand(ExecuteUserAgentCommand)); + + /// + /// 设置UserAgent事件 + /// + private void ExecuteUserAgentCommand() + { + bool isSucceed = SettingsManager.GetInstance().SetUserAgent(UserAgent); + PublishTip(isSucceed); + } + // 下载器选择事件 private DelegateCommand selectDownloaderCommand; public DelegateCommand SelectDownloaderCommand => selectDownloaderCommand ?? (selectDownloaderCommand = new DelegateCommand(ExecuteSelectDownloaderCommand)); diff --git a/DownKyi/Views/Settings/ViewNetwork.xaml b/DownKyi/Views/Settings/ViewNetwork.xaml index 884df8e..543e70f 100644 --- a/DownKyi/Views/Settings/ViewNetwork.xaml +++ b/DownKyi/Views/Settings/ViewNetwork.xaml @@ -28,6 +28,26 @@ IsChecked="{Binding UseSSL, Mode=TwoWay}" Style="{StaticResource CheckBoxStyle}" /> + + + + + + + +