Browse Source

fix: 启动逻辑优化

master
niushuai233 3 years ago
parent
commit
25a9a2d760
  1. 4
      Entity/Config.cs
  2. 12
      MainForm.Designer.cs
  3. 45
      MainForm.cs
  4. 4
      Program.cs
  5. 2
      UI/Set/GuardConfig.Designer.cs
  6. 53
      UI/Set/GuardConfig.cs
  7. 19
      Util/CommonUtil.cs
  8. 1
      Util/ConfigUtil.cs
  9. 16
      Util/SteamGuardCalcThread.cs
  10. 41
      Util/SteamTwoFactorToken.cs
  11. 1
      steam-token.csproj

4
Entity/Config.cs

@ -8,6 +8,10 @@ namespace steam_token.Entity @@ -8,6 +8,10 @@ namespace steam_token.Entity
{
public class Config
{
public Config()
{
SteamGuard = new SteamGuard();
}
public SteamGuard SteamGuard { get; set; }
}

12
MainForm.Designer.cs generated

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@

namespace steam_token
{
partial class mainForm
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
@ -30,7 +30,7 @@ namespace steam_token @@ -30,7 +30,7 @@ namespace steam_token
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm));
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.SettingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.GuardToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -71,14 +71,14 @@ namespace steam_token @@ -71,14 +71,14 @@ namespace steam_token
// GuardToolStripMenuItem
//
this.GuardToolStripMenuItem.Name = "GuardToolStripMenuItem";
this.GuardToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.GuardToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.GuardToolStripMenuItem.Text = "令牌配置";
this.GuardToolStripMenuItem.Click += new System.EventHandler(this.GuardToolStripMenuItem_Click);
//
// ProgramToolStripMenuItem
//
this.ProgramToolStripMenuItem.Name = "ProgramToolStripMenuItem";
this.ProgramToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.ProgramToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.ProgramToolStripMenuItem.Text = "程序设置";
this.ProgramToolStripMenuItem.Visible = false;
this.ProgramToolStripMenuItem.Click += new System.EventHandler(this.ProgramToolStripMenuItem_Click);
@ -164,7 +164,7 @@ namespace steam_token @@ -164,7 +164,7 @@ namespace steam_token
this.label_datetime.TabIndex = 6;
this.label_datetime.Text = " ";
//
// mainForm
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@ -183,7 +183,7 @@ namespace steam_token @@ -183,7 +183,7 @@ namespace steam_token
this.Margin = new System.Windows.Forms.Padding(5);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "mainForm";
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Steam令牌计算器";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.mainForm_FormClosed);

45
MainForm.cs

@ -16,21 +16,41 @@ using Util; @@ -16,21 +16,41 @@ using Util;
namespace steam_token
{
public partial class mainForm : Form
public partial class MainForm : Form
{
public mainForm()
public MainForm()
{
ConfigUtil.Init(new Config());
InitializeComponent();
this.timer_time.Start();
// 先计算一次
this.label_guard.Text = CalcOnce();
while (!ConfigUtil.INIT_SUCCESS)
{
CheckConfig();
}
// 新开一个线程去初始化数据
SteamGuardCalcThread.StartThread(this.label_guard, this.progressBar_refresh);
}
private void CheckConfig()
{
Console.WriteLine("Check Config Right Now.");
Config config = ConfigUtil.Read<Config>();
if (null == config || null == config.SteamGuard || !SteamTwoFactorToken.Verify(config.SteamGuard.shared_secret))
{
OpenGuardConfigForm();
}
else
{
// new SteamTwoFactorToken(steamGuard.shared_secret);
ConfigUtil.INIT_SUCCESS = true;
}
}
/// <summary>
/// 打开令牌配置页面
/// </summary>
@ -38,7 +58,12 @@ namespace steam_token @@ -38,7 +58,12 @@ namespace steam_token
/// <param name="e"></param>
private void GuardToolStripMenuItem_Click(object sender, EventArgs e)
{
GuardConfig guard = new GuardConfig();
OpenGuardConfigForm();
}
private void OpenGuardConfigForm()
{
GuardConfig guard = new GuardConfig(this.label_guard, this.progressBar_refresh);
guard.ShowDialog(this);
}
@ -56,19 +81,9 @@ namespace steam_token @@ -56,19 +81,9 @@ namespace steam_token
private void button_recalc_Click(object sender, EventArgs e)
{
this.label_guard.Text = CalcOnce();
SteamGuardCalcThread.StartThread(this.label_guard, this.progressBar_refresh);
}
private string CalcOnce()
{
Config config = ConfigUtil.Read<Config>();
string guard = SteamTwoFactorToken.GenerateSteamGuardCode(config.SteamGuard.shared_secret);
Console.WriteLine("guard = " + guard);
return guard;
}
private void button_copy_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(this.label_guard.Text);

4
Program.cs

@ -3,7 +3,6 @@ using System.Collections.Generic; @@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util;
namespace steam_token
{
@ -16,10 +15,9 @@ namespace steam_token @@ -16,10 +15,9 @@ namespace steam_token
static void Main()
{
Control.CheckForIllegalCrossThreadCalls = false;
ConfigUtil.Init(new steam_token.Entity.Config());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainForm());
Application.Run(new MainForm());
}
}
}

2
UI/Set/GuardConfig.Designer.cs generated

@ -105,6 +105,7 @@ namespace steam_token.UI.Set @@ -105,6 +105,7 @@ namespace steam_token.UI.Set
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(539, 132);
this.ControlBox = false;
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
@ -112,6 +113,7 @@ namespace steam_token.UI.Set @@ -112,6 +113,7 @@ namespace steam_token.UI.Set
this.Controls.Add(this.button_openfile);
this.Controls.Add(this.button_confirm);
this.Font = new System.Drawing.Font("微软雅黑", 12F);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.MaximizeBox = false;
this.MinimizeBox = false;

53
UI/Set/GuardConfig.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using Newtonsoft.Json;
using steam_token.Entity;
using steam_token.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -16,16 +17,18 @@ namespace steam_token.UI.Set @@ -16,16 +17,18 @@ namespace steam_token.UI.Set
{
public partial class GuardConfig : Form
{
private SteamGuard steamGuard;
public GuardConfig()
private Label labelGuard;
private ProgressBar progressBarRefresh;
public GuardConfig(Label label_guard, ProgressBar progressBar_refresh)
{
this.labelGuard = label_guard;
this.progressBarRefresh = progressBar_refresh;
InitializeComponent();
// 先初始化组件 再使用
Config config = ConfigUtil.Read<Config>();
steamGuard = config.SteamGuard;
if (null != steamGuard && !string.IsNullOrEmpty(steamGuard.shared_secret))
Config config = CommonUtil.GetConfig();
if (null != config && null != config.SteamGuard && !string.IsNullOrEmpty(config.SteamGuard.shared_secret))
{
this.textBox_shared_secret.Text = steamGuard.shared_secret;
this.textBox_shared_secret.Text = config.SteamGuard.shared_secret;
}
}
@ -55,18 +58,18 @@ namespace steam_token.UI.Set @@ -55,18 +58,18 @@ namespace steam_token.UI.Set
string fileStr = Encoding.UTF8.GetString(bytes);
ParseSteamGuard(fileStr);
}
private SteamGuard tmpSteamGuard;
private void ParseSteamGuard(string fileStr)
{
try
{
steamGuard = JsonConvert.DeserializeObject<SteamGuard>(fileStr);
Console.WriteLine(steamGuard);
tmpSteamGuard = JsonConvert.DeserializeObject<SteamGuard>(fileStr);
Console.WriteLine(tmpSteamGuard);
this.textBox_shared_secret.Text = tmpSteamGuard.shared_secret;
this.textBox_shared_secret.Text = steamGuard.shared_secret;
}
catch (JsonReaderException)
{
@ -80,16 +83,32 @@ namespace steam_token.UI.Set @@ -80,16 +83,32 @@ namespace steam_token.UI.Set
private void button_confirm_Click(object sender, EventArgs e)
{
if (null == steamGuard)
string text = this.textBox_shared_secret.Text;
if (string.IsNullOrEmpty(text))
{
MessageBox.Show("熬, 未知操作!!!");
MessageBox.Show("请输入shared_secret值");
return;
}
if (SteamTwoFactorToken.Verify(text))
{
Config config = ConfigUtil.Read<Config>();
if (null == config)
{
config = new Config();
}
if (null == tmpSteamGuard)
{
tmpSteamGuard = new SteamGuard();
tmpSteamGuard.shared_secret = text;
}
config.SteamGuard = tmpSteamGuard;
ConfigUtil.Save<Config>(config);
// 配置更新 重新计算值
SteamGuardCalcThread.StartThread(labelGuard, progressBarRefresh);
Config config = ConfigUtil.Read<Config>();
config.SteamGuard = steamGuard;
ConfigUtil.Save<Config>(config);
this.Dispose();
this.Dispose();
}
}
}
}

19
Util/CommonUtil.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
using steam_token.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util;
namespace steam_token.Util
{
public class CommonUtil
{
public static Config GetConfig()
{
return ConfigUtil.Read<Config>();
}
}
}

1
Util/ConfigUtil.cs

@ -12,6 +12,7 @@ namespace Util @@ -12,6 +12,7 @@ namespace Util
{
public class ConfigUtil
{
public static bool INIT_SUCCESS = false;
public static string CONFIG_NAME = steam_token.Properties.Resources.CONFIG_NAME;
public static string CONFIG_FOLDER = steam_token.Properties.Resources.CONFIG_FOLDER;

16
Util/SteamGuardCalcThread.cs

@ -50,20 +50,28 @@ namespace steam_token.Util @@ -50,20 +50,28 @@ namespace steam_token.Util
Console.WriteLine("未发现配置的秘钥");
return;
}
CalcOnce();
for (int i = 30; i >= 0; i--)
{
Thread.Sleep(1000);
if (i == 0)
{
string guard = SteamTwoFactorToken.GenerateSteamGuardCode(shared_secret);
label_guard.Text = guard;
CalcOnce();
i = 30;
Console.WriteLine("重新设值: " + guard);
}
progressBar_refresh.Value = i;
}
}
private static string CalcOnce()
{
Config config = ConfigUtil.Read<Config>();
string guard = SteamTwoFactorToken.GenerateSteamGuardCode(config.SteamGuard.shared_secret);
Console.WriteLine("calc once guard = " + guard);
label_guard.Text = guard;
return guard;
}
}
}

41
Util/SteamTwoFactorToken.cs

@ -1,10 +1,12 @@ @@ -1,10 +1,12 @@
using System;
using steam_token.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util;
namespace steam_token.Util
{
@ -13,19 +15,44 @@ namespace steam_token.Util @@ -13,19 +15,44 @@ namespace steam_token.Util
private static byte[] s_rgchSteamguardCodeChars = { 50, 51, 52, 53, 54, 55, 56, 57, 66, 67, 68, 70, 71, 72, 74, 75, 77, 78, 80, 81, 82, 84, 86, 87, 88, 89 };
private byte[] mSecret;
public SteamTwoFactorToken(String sharedSecret)
public SteamTwoFactorToken(string sharedSecret)
{
if (!string.IsNullOrEmpty(sharedSecret))
try
{
this.mSecret = Convert.FromBase64String(sharedSecret);
if (!string.IsNullOrEmpty(sharedSecret))
{
this.mSecret = Convert.FromBase64String(sharedSecret);
}
else
{
MessageBox.Show("请配置shared_secret值");
}
}
catch (Exception)
{
ConfigUtil.Init(new Config());
MessageBox.Show("shared_secret值不符合要求, 请重新设置");
}
ConfigUtil.INIT_SUCCESS = true;
}
public static bool Verify(string sharedSecret)
{
try
{
byte[] base64 = Convert.FromBase64String(sharedSecret);
Console.WriteLine("source: " + sharedSecret + ", base64 = " + base64);
return true;
}
else
catch (Exception)
{
MessageBox.Show("未知的shared_secret");
MessageBox.Show("shared_secret值校验失败, 请重新设置");
return false;
}
}
public static string GenerateSteamGuardCode(String sharedSecret)
public static string GenerateSteamGuardCode(string sharedSecret)
{
return new SteamTwoFactorToken(sharedSecret).GenerateSteamGuardCode();
}

1
steam-token.csproj

@ -217,6 +217,7 @@ @@ -217,6 +217,7 @@
<Compile Include="UI\Set\GuardConfig.Designer.cs">
<DependentUpon>GuardConfig.cs</DependentUpon>
</Compile>
<Compile Include="Util\CommonUtil.cs" />
<Compile Include="Util\SteamGuardCalcThread.cs" />
<Compile Include="Util\SteamTwoFactorToken.cs" />
<Compile Include="Util\ConfigUtil.cs" />

Loading…
Cancel
Save