新增自定义UserAgent

pull/671/head
leiurayer 1 year ago
parent e4f1f9dce2
commit 8d744eb106

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

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

@ -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;
/// <summary>
@ -107,6 +110,33 @@ namespace DownKyi.Core.Settings
return SetSettings();
}
/// <summary>
/// 获取UserAgent
/// </summary>
/// <returns></returns>
public string GetUserAgent()
{
appSettings = GetSettings();
if (appSettings.Network.UserAgent == string.Empty)
{
// 第一次获取,先设置默认值
SetUserAgent(userAgent);
return userAgent;
}
return appSettings.Network.UserAgent;
}
/// <summary>
/// 设置UserAgent
/// </summary>
/// <param name="userAgent"></param>
/// <returns></returns>
public bool SetUserAgent(string userAgent)
{
appSettings.Network.UserAgent = userAgent;
return SetSettings();
}
/// <summary>
/// 获取下载器
/// </summary>

@ -196,6 +196,7 @@
<system:String x:Key="Network">网络</system:String>
<system:String x:Key="UseSSL">启用https若下载器提示SSL错误则关闭此项</system:String>
<system:String x:Key="UserAgent">UserAgent</system:String>
<system:String x:Key="SelectDownloader">选择下载器(重启生效):</system:String>
<system:String x:Key="BuiltinDownloader">内建下载器(测试)</system:String>
<system:String x:Key="Aria2cDownloader">Aria2下载器</system:String>

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

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

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

@ -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));
/// <summary>
/// 设置UserAgent事件
/// </summary>
private void ExecuteUserAgentCommand()
{
bool isSucceed = SettingsManager.GetInstance().SetUserAgent(UserAgent);
PublishTip(isSucceed);
}
// 下载器选择事件
private DelegateCommand<string> selectDownloaderCommand;
public DelegateCommand<string> SelectDownloaderCommand => selectDownloaderCommand ?? (selectDownloaderCommand = new DelegateCommand<string>(ExecuteSelectDownloaderCommand));

@ -28,6 +28,26 @@
IsChecked="{Binding UseSSL, Mode=TwoWay}"
Style="{StaticResource CheckBoxStyle}" />
<TextBlock
Margin="0,20,0,0"
FontSize="12"
Foreground="{DynamicResource BrushTextDark}"
Text="{DynamicResource UserAgent}" />
<TextBox
Name="nameUserAgent"
Height="20"
Margin="0,10,0,0"
HorizontalAlignment="Stretch"
Text="{Binding UserAgent}">
<TextBox.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding UserAgentCommand}"
CommandParameter="{Binding ElementName=nameUserAgent, Path=Text}" />
</TextBox.InputBindings>
</TextBox>
<StackPanel Margin="0,20,0,0" Orientation="Vertical">
<TextBlock
FontSize="12"

Loading…
Cancel
Save