diff --git a/Entity/Config.cs b/Entity/Config.cs index ef10cdc..44fdad1 100644 --- a/Entity/Config.cs +++ b/Entity/Config.cs @@ -8,6 +8,10 @@ namespace steam_token.Entity { public class Config { + public Config() + { + SteamGuard = new SteamGuard(); + } public SteamGuard SteamGuard { get; set; } } diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 655446b..4faf45e 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -1,7 +1,7 @@  namespace steam_token { - partial class mainForm + partial class MainForm { /// /// 必需的设计器变量。 @@ -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 // 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 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 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); diff --git a/MainForm.cs b/MainForm.cs index 79eadf6..488147b 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -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(); + + if (null == config || null == config.SteamGuard || !SteamTwoFactorToken.Verify(config.SteamGuard.shared_secret)) + { + OpenGuardConfigForm(); + } + else + { + // new SteamTwoFactorToken(steamGuard.shared_secret); + ConfigUtil.INIT_SUCCESS = true; + } + + } + /// /// 打开令牌配置页面 /// @@ -38,7 +58,12 @@ namespace steam_token /// 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 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(); - 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); diff --git a/Program.cs b/Program.cs index e01e7ae..d7c0a23 100644 --- a/Program.cs +++ b/Program.cs @@ -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 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()); } } } diff --git a/UI/Set/GuardConfig.Designer.cs b/UI/Set/GuardConfig.Designer.cs index ae94ac0..45ff2b3 100644 --- a/UI/Set/GuardConfig.Designer.cs +++ b/UI/Set/GuardConfig.Designer.cs @@ -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 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; diff --git a/UI/Set/GuardConfig.cs b/UI/Set/GuardConfig.cs index b4f79af..2e477a5 100644 --- a/UI/Set/GuardConfig.cs +++ b/UI/Set/GuardConfig.cs @@ -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 { 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(); - 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 string fileStr = Encoding.UTF8.GetString(bytes); ParseSteamGuard(fileStr); - - } + private SteamGuard tmpSteamGuard; private void ParseSteamGuard(string fileStr) { try { - steamGuard = JsonConvert.DeserializeObject(fileStr); - Console.WriteLine(steamGuard); + tmpSteamGuard = JsonConvert.DeserializeObject(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 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(); + if (null == config) + { + config = new Config(); + } + if (null == tmpSteamGuard) + { + tmpSteamGuard = new SteamGuard(); + tmpSteamGuard.shared_secret = text; + } + config.SteamGuard = tmpSteamGuard; + ConfigUtil.Save(config); + + // 配置更新 重新计算值 + SteamGuardCalcThread.StartThread(labelGuard, progressBarRefresh); - Config config = ConfigUtil.Read(); - config.SteamGuard = steamGuard; - ConfigUtil.Save(config); - this.Dispose(); + this.Dispose(); + } } } } diff --git a/Util/CommonUtil.cs b/Util/CommonUtil.cs new file mode 100644 index 0000000..4f5ea4d --- /dev/null +++ b/Util/CommonUtil.cs @@ -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(); + } + } +} diff --git a/Util/ConfigUtil.cs b/Util/ConfigUtil.cs index 08938c9..55a0b31 100644 --- a/Util/ConfigUtil.cs +++ b/Util/ConfigUtil.cs @@ -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; diff --git a/Util/SteamGuardCalcThread.cs b/Util/SteamGuardCalcThread.cs index ebdbfd6..5ab83c9 100644 --- a/Util/SteamGuardCalcThread.cs +++ b/Util/SteamGuardCalcThread.cs @@ -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(); + string guard = SteamTwoFactorToken.GenerateSteamGuardCode(config.SteamGuard.shared_secret); + Console.WriteLine("calc once guard = " + guard); + label_guard.Text = guard; + return guard; + } } } diff --git a/Util/SteamTwoFactorToken.cs b/Util/SteamTwoFactorToken.cs index 64cff66..69b3f3e 100644 --- a/Util/SteamTwoFactorToken.cs +++ b/Util/SteamTwoFactorToken.cs @@ -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 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(); } diff --git a/steam-token.csproj b/steam-token.csproj index dbfef78..14df90a 100644 --- a/steam-token.csproj +++ b/steam-token.csproj @@ -217,6 +217,7 @@ GuardConfig.cs +