Skip to content

Commit

Permalink
新增下载流的https控制
Browse files Browse the repository at this point in the history
  • Loading branch information
leiurayer committed Jun 5, 2022
1 parent bfad734 commit 259271e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DownKyi.Core/Settings/Models/NetworkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class NetworkSettings
{
public AllowStatus IsLiftingOfRegion { get; set; } = AllowStatus.NONE;

public AllowStatus UseSSL { get; set; } = AllowStatus.NONE;

public Downloader Downloader { get; set; } = Downloader.NOT_SET;
public int MaxCurrentDownloads { get; set; } = -1;

Expand Down
30 changes: 30 additions & 0 deletions src/DownKyi.Core/Settings/SettingsManager.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public partial class SettingsManager
// 是否开启解除地区限制
private readonly AllowStatus isLiftingOfRegion = AllowStatus.YES;

// 启用https
private readonly AllowStatus useSSL = AllowStatus.YES;

// 下载器
private readonly Downloader downloader = Downloader.ARIA;

Expand Down Expand Up @@ -71,6 +74,33 @@ public bool IsLiftingOfRegion(AllowStatus isLiftingOfRegion)
return SetSettings();
}

/// <summary>
/// 获取是否启用https
/// </summary>
/// <returns></returns>
public AllowStatus UseSSL()
{
appSettings = GetSettings();
if (appSettings.Network.UseSSL == AllowStatus.NONE)
{
// 第一次获取,先设置默认值
UseSSL(useSSL);
return useSSL;
}
return appSettings.Network.UseSSL;
}

/// <summary>
/// 设置是否启用https
/// </summary>
/// <param name="useSSL"></param>
/// <returns></returns>
public bool UseSSL(AllowStatus useSSL)
{
appSettings.Network.UseSSL = useSSL;
return SetSettings();
}

/// <summary>
/// 获取下载器
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/DownKyi/Languages/Default.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@
<system:String x:Key="AutoDownloadAll">解析后自动下载已解析视频</system:String>

<system:String x:Key="Network">网络</system:String>
<system:String x:Key="UseSSL">启用https(若下载器提示SSL错误,则关闭此项)</system:String>
<system:String x:Key="SelectDownloader">选择下载器(重启生效):</system:String>
<system:String x:Key="BuiltinDownloader">内建下载器</system:String>
<system:String x:Key="BuiltinDownloader">内建下载器(测试)</system:String>
<system:String x:Key="Aria2cDownloader">Aria2下载器</system:String>
<system:String x:Key="AriaServerPort">Aria服务器端口:</system:String>
<system:String x:Key="AriaLogLevel">Aria日志等级:</system:String>
Expand Down
25 changes: 25 additions & 0 deletions src/DownKyi/Services/Download/AriaDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ private string DownloadVideo(DownloadingItem downloading, PlayUrlDashVideo downl
downloading.Downloading.Gid = null;
}

// 启用https
AllowStatus useSSL = SettingsManager.GetInstance().UseSSL();
if (useSSL == AllowStatus.YES)
{
for (int i = 0; i < urls.Count; i++)
{
string url = urls[i];
if (url.StartsWith("http://"))
{
urls[i] = url.Replace("http://", "https://");
}
}
}
else
{
for (int i = 0; i < urls.Count; i++)
{
string url = urls[i];
if (url.StartsWith("https://"))
{
urls[i] = url.Replace("https://", "http://");
}
}
}

// 开始下载
DownloadResult downloadStatus = DownloadByAria(downloading, urls, path, fileName);
switch (downloadStatus)
Expand Down
25 changes: 25 additions & 0 deletions src/DownKyi/Services/Download/BuiltinDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ private string DownloadVideo(DownloadingItem downloading, PlayUrlDashVideo downl
downloading.Downloading.Gid = null;
}

// 启用https
AllowStatus useSSL = SettingsManager.GetInstance().UseSSL();
if (useSSL == AllowStatus.YES)
{
for (int i = 0; i < urls.Count; i++)
{
string url = urls[i];
if (url.StartsWith("http://"))
{
urls[i] = url.Replace("http://", "https://");
}
}
}
else
{
for (int i = 0; i < urls.Count; i++)
{
string url = urls[i];
if (url.StartsWith("https://"))
{
urls[i] = url.Replace("https://", "http://");
}
}
}

// 开始下载
var downloadStatus = DownloadByBuiltin(downloading, urls, path, fileName);
if (downloadStatus)
Expand Down
26 changes: 26 additions & 0 deletions src/DownKyi/ViewModels/Settings/ViewNetworkViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public class ViewNetworkViewModel : BaseViewModel

#region 页面属性申明

private bool useSSL;
public bool UseSSL
{
get => useSSL;
set => SetProperty(ref useSSL, value);
}

private bool builtin;
public bool Builtin
{
Expand Down Expand Up @@ -236,6 +243,10 @@ public override void OnNavigatedTo(NavigationContext navigationContext)

isOnNavigatedTo = true;

// 启用https
AllowStatus useSSL = SettingsManager.GetInstance().UseSSL();
UseSSL = useSSL == AllowStatus.YES;

// 选择下载器
var downloader = SettingsManager.GetInstance().GetDownloader();
switch (downloader)
Expand Down Expand Up @@ -304,6 +315,21 @@ public override void OnNavigatedTo(NavigationContext navigationContext)

#region 命令申明

// 是否启用https事件
private DelegateCommand useSSLCommand;
public DelegateCommand UseSSLCommand => useSSLCommand ?? (useSSLCommand = new DelegateCommand(ExecuteUseSSLCommand));

/// <summary>
/// 是否启用https事件
/// </summary>
private void ExecuteUseSSLCommand()
{
AllowStatus useSSL = UseSSL ? AllowStatus.YES : AllowStatus.NO;

bool isSucceed = SettingsManager.GetInstance().UseSSL(useSSL);
PublishTip(isSucceed);
}

// 下载器选择事件
private DelegateCommand<string> selectDownloaderCommand;
public DelegateCommand<string> SelectDownloaderCommand => selectDownloaderCommand ?? (selectDownloaderCommand = new DelegateCommand<string>(ExecuteSelectDownloaderCommand));
Expand Down
12 changes: 12 additions & 0 deletions src/DownKyi/Views/Settings/ViewNetwork.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
Text="{DynamicResource Network}" />
</StackPanel>

<CheckBox
Name="nameUseSSL"
Margin="0,20,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding UseSSLCommand}"
Content="{DynamicResource UseSSL}"
FontSize="12"
Foreground="{DynamicResource BrushTextDark}"
IsChecked="{Binding UseSSL, Mode=TwoWay}"
Style="{StaticResource CheckBoxStyle}" />

<StackPanel Margin="0,20,0,0" Orientation="Vertical">
<TextBlock
FontSize="12"
Expand Down

0 comments on commit 259271e

Please sign in to comment.