From 82da803809b9f8e61726326570c0758eef55b807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=82=85Fox?= Date: Sat, 18 Jul 2015 00:19:21 +0800 Subject: [PATCH 1/2] Fix UI thread block in authentication --- Auth/Program.cs | 122 ----------------------------- Auth/Properties/AssemblyInfo.cs | 4 +- Auth/Wait.Designer.cs | 33 +++++++- Auth/Wait.cs | 97 ++++++++++++++++++++++- Library/Properties/AssemblyInfo.cs | 4 +- 5 files changed, 131 insertions(+), 129 deletions(-) delete mode 100644 Auth/Program.cs diff --git a/Auth/Program.cs b/Auth/Program.cs deleted file mode 100644 index d292213..0000000 --- a/Auth/Program.cs +++ /dev/null @@ -1,122 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Windows.Forms; -using RapID.ClassLibrary; -using System.Net; -using System.Net.Sockets; -using System.IO; - -namespace RapID.Auth -{ - static class Program - { - /// - /// 应用程序的主入口点。 - /// - [STAThread] - static void Main(string[] args) - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - // callback& app - var callback = DecodeUrlString(args[0]); - var app = Crypt.Decrypt(Crypt.Decrypt(Crypt.Decrypt(args[1]))); - // ask for permission - var ifAuth = MessageBox.Show("是否同意应用" + app + "发起鉴权请求?", "Rap-ID", MessageBoxButtons.YesNo); - if (ifAuth != DialogResult.Yes) - { - // exit on denying - Application.Exit(); - } - // start user interface - var waitFrm = new Wait(); - waitFrm.Show(); - // start authentication - #region Authentication - const string auth_prefix = "AUTH"; - const string authok_prefix = "AUTHOK"; - const string authfail_prefix = "AUTHFAIL"; - - waitFrm.SetInfoText("正在读取配置文件..."); - - string cryptionKey, pairKey; - Device device; - using (var sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "pair")) - { - var name = Crypt.Decrypt( sr.ReadLine()); - pairKey = Crypt.Decrypt( sr.ReadLine()); - cryptionKey = Crypt.Decrypt( sr.ReadLine()); - device = WaitForIP(name); - } - waitFrm.SetInfoText("配置文件成功读取,正在建立连接..."); - using (var tcpClient = new TcpClient()) - { - tcpClient.Connect(device.IP, NetworkPorts.Auth); - waitFrm.SetInfoText("连接已建立,正在发送消息..."); - - using (var tcpStreamWriter = new StreamWriter(tcpClient.GetStream())) - { - tcpStreamWriter.WriteLine(Crypt.Encrypt(auth_prefix + pairKey, Crypt.GenerateKey(cryptionKey))); - tcpStreamWriter.Flush(); - waitFrm.SetInfoText("消息发送成功,正在等待手机端回应..."); - - using (var tcpStreamReader = new StreamReader(tcpClient.GetStream())) - { - var message = Crypt.Decrypt( tcpStreamReader.ReadLine(), Crypt.GenerateKey(cryptionKey)); -#if DEBUG - MessageBox.Show(message); -#endif - if (message.StartsWith(authok_prefix)) - { - waitFrm.SetInfoText("授权成功!"); - System.Threading.Thread.Sleep(1000); - if (callback != String.Empty) - { - System.Diagnostics.Process.Start(callback + message.Replace(authok_prefix, String.Empty)); - } - } - else if (message.StartsWith(authfail_prefix)) - { - waitFrm.SetInfoText("授权失败!"); - System.Threading.Thread.Sleep(1000); - } - } - - } - } - #endregion - Application.Exit(); - } - - /* - * (C) 2015 @ogi from StackOverflow - * Original Post: http://stackoverflow.com/questions/1405048/how-do-i-decode-a-url-parameter-using-c - */ - private static string DecodeUrlString(string url) - { - string newUrl; - while ((newUrl = Uri.UnescapeDataString(url)) != url) - url = newUrl; - return newUrl; - } - - private static Device WaitForIP(string name) - { - IPAddress ip = IPAddress.Any; - using (var udp = new UdpClient(new IPEndPoint(IPAddress.Any, NetworkPorts.Boradcast))) - { - string message = String.Empty; - while (message != name) - { - IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); - var result = udp.Receive(ref RemoteIpEndPoint); - message = Encodes.UTF8NoBOM.GetString(result); - ip = RemoteIpEndPoint.Address; - } - } - return new Device(name, ip); - } - } -} diff --git a/Auth/Properties/AssemblyInfo.cs b/Auth/Properties/AssemblyInfo.cs index fa4e111..99338c8 100644 --- a/Auth/Properties/AssemblyInfo.cs +++ b/Auth/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0.8")] -[assembly: AssemblyFileVersion("1.2.0.8")] +[assembly: AssemblyVersion("1.2.0.13")] +[assembly: AssemblyFileVersion("1.2.0.13")] diff --git a/Auth/Wait.Designer.cs b/Auth/Wait.Designer.cs index 24cabcc..c676868 100644 --- a/Auth/Wait.Designer.cs +++ b/Auth/Wait.Designer.cs @@ -30,6 +30,8 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Wait)); this.infoLabel = new System.Windows.Forms.Label(); + this.okButton = new System.Windows.Forms.Button(); + this.cancelButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // infoLabel @@ -40,16 +42,40 @@ private void InitializeComponent() this.infoLabel.Location = new System.Drawing.Point(14, 9); this.infoLabel.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); this.infoLabel.Name = "infoLabel"; - this.infoLabel.Size = new System.Drawing.Size(298, 42); + this.infoLabel.Size = new System.Drawing.Size(300, 78); this.infoLabel.TabIndex = 0; this.infoLabel.Text = "Rap-ID"; this.infoLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // + // okButton + // + this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.okButton.Location = new System.Drawing.Point(138, 90); + this.okButton.Name = "okButton"; + this.okButton.Size = new System.Drawing.Size(85, 29); + this.okButton.TabIndex = 1; + this.okButton.Text = "OK"; + this.okButton.UseVisualStyleBackColor = true; + this.okButton.Click += new System.EventHandler(this.okButton_Click); + // + // cancelButton + // + this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.cancelButton.Location = new System.Drawing.Point(229, 90); + this.cancelButton.Name = "cancelButton"; + this.cancelButton.Size = new System.Drawing.Size(85, 29); + this.cancelButton.TabIndex = 2; + this.cancelButton.Text = "Cancel"; + this.cancelButton.UseVisualStyleBackColor = true; + this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click); + // // Wait // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(326, 60); + this.ClientSize = new System.Drawing.Size(326, 131); + this.Controls.Add(this.cancelButton); + this.Controls.Add(this.okButton); this.Controls.Add(this.infoLabel); this.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); @@ -58,6 +84,7 @@ private void InitializeComponent() this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Wait"; this.TopMost = true; + this.Load += new System.EventHandler(this.Wait_Load); this.ResumeLayout(false); } @@ -65,5 +92,7 @@ private void InitializeComponent() #endregion private System.Windows.Forms.Label infoLabel; + private System.Windows.Forms.Button okButton; + private System.Windows.Forms.Button cancelButton; } } \ No newline at end of file diff --git a/Auth/Wait.cs b/Auth/Wait.cs index 7d1e0e9..85c72da 100644 --- a/Auth/Wait.cs +++ b/Auth/Wait.cs @@ -7,14 +7,22 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using RapID.ClassLibrary; +using System.Net; +using System.Net.Sockets; +using System.IO; namespace RapID.Auth { public partial class Wait : Form { - public Wait() + private string _callback; + private string _app; + public Wait(string callback, string app) { InitializeComponent(); + this._callback = callback; + this._app = app; } public void SetInfoText(string text) @@ -29,5 +37,92 @@ public void SetInfoText(string text) public delegate void DOverThread(); public DOverThread OverThread; + + private void cancelButton_Click(object sender, EventArgs e) + { + okButton.Enabled = false; + cancelButton.Enabled = false; + Application.Exit(); + } + + private void Wait_Load(object sender, EventArgs e) + { + this.infoLabel.Text = "应用" + this._app + "正在发起鉴权请求。"; + } + + private async void okButton_Click(object sender, EventArgs e) + { + okButton.Enabled = false; + cancelButton.Enabled = false; + + const string auth_prefix = "AUTH"; + const string authok_prefix = "AUTHOK"; + const string authfail_prefix = "AUTHFAIL"; + + this.SetInfoText("正在读取配置文件..."); + + string cryptionKey, pairKey; + Device device; + using (var sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "pair")) + { + var name = Crypt.Decrypt(sr.ReadLine()); + pairKey = Crypt.Decrypt(sr.ReadLine()); + cryptionKey = Crypt.Decrypt(sr.ReadLine()); + device = await WaitForIP(name); + } + this.SetInfoText("配置文件成功读取,正在建立连接..."); + using (var tcpClient = new TcpClient()) + { + tcpClient.Connect(device.IP, NetworkPorts.Auth); + this.SetInfoText("连接已建立,正在发送消息..."); + + using (var tcpStreamWriter = new StreamWriter(tcpClient.GetStream())) + { + await tcpStreamWriter.WriteLineAsync(Crypt.Encrypt(auth_prefix + pairKey, Crypt.GenerateKey(cryptionKey))); + await tcpStreamWriter.FlushAsync(); + this.SetInfoText("消息发送成功,正在等待手机端回应..."); + + using (var tcpStreamReader = new StreamReader(tcpClient.GetStream())) + { + var message = Crypt.Decrypt(await tcpStreamReader.ReadLineAsync(), Crypt.GenerateKey(cryptionKey)); +#if DEBUG + MessageBox.Show(message); +#endif + if (message.StartsWith(authok_prefix)) + { + this.SetInfoText("授权成功!"); + System.Threading.Thread.Sleep(1000); + if (_callback != String.Empty) + { + System.Diagnostics.Process.Start(_callback + message.Replace(authok_prefix, String.Empty)); + } + } + else if (message.StartsWith(authfail_prefix)) + { + this.SetInfoText("授权失败!"); + System.Threading.Thread.Sleep(1000); + } + } + + } + } + Application.Exit(); + } + + private async Task WaitForIP(string name) + { + IPAddress ip = IPAddress.Any; + using (var udp = new UdpClient(new IPEndPoint(IPAddress.Any, NetworkPorts.Boradcast))) + { + string message = String.Empty; + while (message != name) + { + var result = await udp.ReceiveAsync(); + message = Encodes.UTF8NoBOM.GetString(result.Buffer); + ip = result.RemoteEndPoint.Address; + } + } + return new Device(name, ip); + } } } diff --git a/Library/Properties/AssemblyInfo.cs b/Library/Properties/AssemblyInfo.cs index b5201f7..0c3700b 100644 --- a/Library/Properties/AssemblyInfo.cs +++ b/Library/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0.10")] -[assembly: AssemblyFileVersion("1.2.0.10")] +[assembly: AssemblyVersion("1.2.0.11")] +[assembly: AssemblyFileVersion("1.2.0.11")] From 4ea9b02aa64864890aa9c88cc849fa0dd763e146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=82=85Fox?= Date: Sat, 18 Jul 2015 00:19:53 +0800 Subject: [PATCH 2/2] Reset the Application Entry Point --- Auth/Program.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Auth/Program.cs diff --git a/Auth/Program.cs b/Auth/Program.cs new file mode 100644 index 0000000..a8b231a --- /dev/null +++ b/Auth/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; +using RapID.ClassLibrary; + +namespace RapID.Auth +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main(string[] args) + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + // callback& app + var callback = DecodeUrlString(args[0]); + var app = Crypt.Decrypt(Crypt.Decrypt(Crypt.Decrypt(args[1]))); + var waitFrm = new Wait(callback, app); + Application.Run(waitFrm); + } + + /* + * (C) 2015 @ogi from StackOverflow + * Original Post: http://stackoverflow.com/questions/1405048/how-do-i-decode-a-url-parameter-using-c + */ + private static string DecodeUrlString(string url) + { + string newUrl; + while ((newUrl = Uri.UnescapeDataString(url)) != url) + url = newUrl; + return newUrl; + } + } +}