Skip to content

Commit

Permalink
新增令牌导出备份功能
Browse files Browse the repository at this point in the history
新增令牌数据保存在程序目录设置项
  • Loading branch information
rmbadmin committed Dec 28, 2020
1 parent e9d822b commit 95e834a
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 98 deletions.
20 changes: 19 additions & 1 deletion SteamTools/Models/Settings/AuthSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MetroTrilithon.Serialization;
using SteamTool.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
Expand All @@ -9,6 +11,22 @@ namespace SteamTools.Models.Settings
{
public static class AuthSettings
{
static AuthSettings()
{
Authenticators.ValueChanged += Authenticators_ValueChanged;
}

private static void Authenticators_ValueChanged(object sender, ValueChangedEventArgs<string> e)
{
if (e.NewValue != e.OldValue)
{
if (AuthSettings.IsCurrentDirectorySaveAuthData.Value)
{
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, Const.AUTHDATA_FILE), e.NewValue);
}
}
}

/// <summary>
/// 令牌数据(压缩存储)
/// </summary>
Expand All @@ -19,7 +37,7 @@ public static class AuthSettings
/// 是否将令牌数据存储在程序路径下
/// </summary>
public static SerializableProperty<bool> IsCurrentDirectorySaveAuthData { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Roaming) { AutoSave = true };
= new SerializableProperty<bool>(GetKey(), Providers.Roaming, false) { AutoSave = true };

private static string GetKey([CallerMemberName] string propertyName = "")
{
Expand Down
61 changes: 51 additions & 10 deletions SteamTools/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Security.Cryptography;
using System.Linq;
using MetroTrilithon.Mvvm;
using SteamTool.Model;

namespace SteamTools.Services
{
Expand Down Expand Up @@ -69,23 +70,58 @@ public BindingList<WinAuthAuthenticator> Authenticators

public void Initialize()
{
if (!string.IsNullOrEmpty(AuthSettings.Authenticators.Value))
try
{
try
var path = Path.Combine(AppContext.BaseDirectory, Const.AUTHDATA_FILE);
if (AuthSettings.IsCurrentDirectorySaveAuthData.Value && File.Exists(path))
{
var auths = AuthService.LoadJsonAuthenticator(AuthSettings.Authenticators.Value.DecompressString());
var text = File.ReadAllText(path);
var auths = AuthService.LoadJsonAuthenticator(text.DecompressString());
Authenticators = new BindingList<WinAuthAuthenticator>(auths);
if (string.IsNullOrEmpty(AuthSettings.Authenticators.Value))
{
AuthSettings.Authenticators.Value = text;
}
}
catch (Exception ex)
else
{
Logger.Error(ex);
WindowService.Current.ShowDialogWindow($"令牌同步服务器失败,错误信息:{ex.Message}");

var auths = AuthService.LoadJsonAuthenticator(AuthSettings.Authenticators.Value.DecompressString());
Authenticators = new BindingList<WinAuthAuthenticator>(auths);
if (AuthSettings.IsCurrentDirectorySaveAuthData.Value)
{
File.WriteAllText(path, AuthSettings.Authenticators.Value);
}
}
}
else
catch (Exception ex)
{
Authenticators = new BindingList<WinAuthAuthenticator>();
Logger.Error(ex);
WindowService.Current.ShowDialogWindow($"令牌同步服务器失败,错误信息:{ex.Message}");
}
}

/// <summary>
/// 导入Steam++导出的令牌数据文件
/// </summary>
public void ImportAuthenticatorsString(string str)
{
var auths = AuthService.LoadJsonAuthenticator(str);
foreach (var auth in auths)
{
Authenticators.Add(auth);
}
}

/// <summary>
/// 导入Steam++导出的令牌数据文件
/// </summary>
public void ImportAuthenticators(string file)
{
var text = File.ReadAllText(file, Encoding.UTF8);
var auths = AuthService.LoadJsonAuthenticator(text.DecompressString());
foreach (var auth in auths)
{
Authenticators.Add(auth);
}
}

Expand All @@ -95,7 +131,7 @@ public void Initialize()
/// <param name="parent">parent Form</param>
/// <param name="file">file name to import</param>
/// <returns>list of imported authenticators</returns>
public void ImportAuthenticators(string file)
public void ImportWinAuthenticators(string file)
{
StringBuilder lines = new StringBuilder();
bool retry;
Expand Down Expand Up @@ -555,5 +591,10 @@ public static IList<WinAuthAuthenticator> LoadJsonAuthenticator(string authStrin
}
return list;
}

public void SaveCurrentAuth()
{
AuthSettings.Authenticators.Value = ConvertJsonAuthenticator(Authenticators).CompressString();
}
}
}
2 changes: 1 addition & 1 deletion SteamTools/SteamTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<PackageId />

<Version>1.0.3.3</Version>
<Version>1.0.5</Version>

<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>
Expand Down
31 changes: 31 additions & 0 deletions SteamTools/ViewModels/Content/Settings/SettingsAuthViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,42 @@
using Newtonsoft.Json;
using SteamTools.Models.Settings;
using System.IO;
using SteamTools.Models;

namespace SteamTools.ViewModels
{
public class SettingsAuthViewModel : Livet.ViewModel
{
public void ImportOldVersionAuthData()
{
var settingPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
ProductInfo.Company,
ProductInfo.Product, Const.SETTINGS_FILE);

if (File.Exists(settingPath))
{
var text = File.ReadAllText(settingPath);
var data = text.Substring("<x:String x:Key=\"GeneralSettings.Authenticators\">", "</x:String>", false);
try
{
AuthService.Current.ImportAuthenticatorsString(data.DecompressString());
AuthService.Current.SaveCurrentAuth();
if (WindowService.Current.MainWindow.Dialog("导入旧版本令牌数据完成,\r\n是否删除Steam++旧版本数据文件?") == true)
{
File.Delete(settingPath);
StatusService.Current.Notify("旧版本数据文件删除完成");
}
}
catch (Exception ex)
{
WindowService.Current.MainWindow.Dialog("导入旧版本令牌数据出错:" + ex.Message);
}
}
else
{
StatusService.Current.Notify("旧版本令牌数据不存在");
}
}
}
}
2 changes: 1 addition & 1 deletion SteamTools/ViewModels/Page/CommunityProxyPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void OpenSelectFileDialog_Click()
{
var fileDialog = new OpenFileDialog
{
Filter = "脚本文件|*.js",
Filter = "JavaScript Files (*.js)|*.js",
Title = Resources.ImportScript,
//AddExtension = true,
Multiselect = true
Expand Down
66 changes: 59 additions & 7 deletions SteamTools/ViewModels/Window/AddAuthWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public AddAuthWindowViewModel()
public bool IsWinAuth { get; set; }
public bool IsSteamApp { get; set; } = true;
public bool IsSDA { get; set; }
public bool IsSpp { get; set; }

private string _WinAuthFileName;
public string WinAuthFileName
Expand All @@ -40,6 +41,20 @@ public string WinAuthFileName
}
}

private string _AuthFileName;
public string AuthFileName
{
get => this._AuthFileName;
set
{
if (this._AuthFileName != value)
{
this._AuthFileName = value;
this.RaisePropertyChanged();
}
}
}

private string _SDAFile;
public string SDAFile
{
Expand All @@ -53,6 +68,7 @@ public string SDAFile
}
}
}

private string _SDAPassword;
public string SDAPassword
{
Expand Down Expand Up @@ -123,6 +139,7 @@ public void OpenWinAuthFile()
{
WinAuthFileName = ofd.FileName;
this.Topmost = true;
this.Topmost = false;
}
}

Expand All @@ -141,6 +158,40 @@ public void OpenSDAFile()
{
SDAFile = ofd.FileName;
this.Topmost = true;
this.Topmost = false;
}
}

public void OpenSppAuthFile()
{
OpenFileDialog ofd = new OpenFileDialog
{
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
Filter = "AuthData Files (*.dat)|*.dat|All Files (*.*)|*.*",
RestoreDirectory = true,
Title = ProductInfo.Title
};
if (ofd.ShowDialog(App.Current.MainWindow) == true)
{
AuthFileName = ofd.FileName;
this.Topmost = true;
this.Topmost = false;
}
}

/// <summary>
/// 导入Steam++导出的令牌数据文件
/// </summary>
public void ImportAuthenticatorFile()
{
if (File.Exists(AuthFileName))
{
StatusService.Current.Set("导入Steam++令牌中...");
AuthService.Current.ImportAuthenticators(AuthFileName);
AuthService.Current.SaveCurrentAuth();
StatusService.Current.Set(Resources.Ready);
}
}

Expand All @@ -149,9 +200,8 @@ public void ImportWinAuthFile()
if (File.Exists(WinAuthFileName))
{
StatusService.Current.Set("导入WinAuth令牌中...");
AuthService.Current.ImportAuthenticators(WinAuthFileName);
AuthSettings.Authenticators.Value = AuthService.ConvertJsonAuthenticator(AuthService.Current.Authenticators).CompressString();
//(WindowService.Current.MainWindow as MainWindowViewModel).LocalAuthPage
AuthService.Current.ImportWinAuthenticators(WinAuthFileName);
AuthService.Current.SaveCurrentAuth();
StatusService.Current.Set(Resources.Ready);
}
}
Expand All @@ -161,7 +211,7 @@ public void ImportSteamGuard()
StatusService.Current.Notify("导入Steam App令牌中...");
if (AuthService.Current.ImportSteamGuard(AuthName, UUID, SteamGuard))
{
AuthSettings.Authenticators.Value = AuthService.ConvertJsonAuthenticator(AuthService.Current.Authenticators).CompressString();
AuthService.Current.SaveCurrentAuth();
}
else
{
Expand All @@ -174,7 +224,7 @@ public void ImportSDA()
StatusService.Current.Set("导入Steam Desktop Auth令牌中...");
if (AuthService.Current.ImportSDAFile(SDAFile, SDAPassword))
{
AuthSettings.Authenticators.Value = AuthService.ConvertJsonAuthenticator(AuthService.Current.Authenticators).CompressString();
AuthService.Current.SaveCurrentAuth();
StatusService.Current.Set(Resources.Ready);
}
else
Expand All @@ -184,14 +234,16 @@ public void ImportSDA()
}
}

public void Apply()
public void Apply()
{
if (IsWinAuth)
ImportWinAuthFile();
else if (IsSteamApp)
ImportSteamGuard();
else if(IsSDA)
else if (IsSDA)
ImportSDA();
else if (IsSpp)
ImportAuthenticatorFile();
}
}
}
27 changes: 18 additions & 9 deletions SteamTools/Views/Content/Settings/SettingsAuth.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,32 @@

<StackPanel Margin="20,0,0,0">
<TextBlock Margin="-20,0,0,10" Text="{Binding Resources.Settings_Auth, Source={x:Static services:ResourceService.Current}, Mode=OneWay}" Style="{DynamicResource SettingsHeaderTextStyleKey}" />

<TextBlock Text="令牌数据保存位置:"
Foreground="{DynamicResource ForegroundBrushKey}" />
<StackPanel Margin="20,0,0,0">
<StackPanel>
<Panel.Resources>
<Style TargetType="{x:Type RadioButton}"
BasedOn="{StaticResource {x:Type RadioButton}}">
<Setter Property="GroupName"
Value="Themes" />
</Style>
</Panel.Resources>
<RadioButton Content="保存到Windows AppData"
IsChecked="{Binding ThemeWindows}" />
<RadioButton Content="保存到Steam++当前目录下"
IsChecked="{Binding Dark}" />
<CheckBox Content="令牌数据保存到Steam++程序目录下"
IsChecked="{Binding Source={x:Static ms:AuthSettings.IsCurrentDirectorySaveAuthData}, Path=Value, Mode=TwoWay}" />
<TextBlock Style="{StaticResource DetailTextStyleKey}" Margin="0,6,0,0">
<Run Text="启用后Steam++的令牌数据文件将会保存在Steam++程序目录内,方便备份保存令牌数据。"></Run>
</TextBlock>
</StackPanel>

<Separator Opacity="0" Height="10"/>
<WrapPanel>
<metro2:CallMethodButton Content="读取并导入Steam++旧版本的令牌数据"
Width="300"
Height="30"
HorizontalAlignment="Left"
MethodName="ImportOldVersionAuthData"
MethodTarget="{Binding}"/>
<TextBlock Style="{StaticResource DetailTextStyleKey}" Margin="10,0,0,0">
<Run Text="*1.0.5版本之后修改了令牌的存储方式"></Run>
</TextBlock>
</WrapPanel>
<Border Height="1"
Background="{DynamicResource BorderBrushKey}"
Margin="-20,8" />
Expand Down
Loading

0 comments on commit 95e834a

Please sign in to comment.