Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
v1.3.1 Fix UI Thread Blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
xfoxfu committed Jul 17, 2015
2 parents e546212 + 4ea9b02 commit bbec204
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 93 deletions.
89 changes: 3 additions & 86 deletions Auth/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using RapID.ClassLibrary;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace RapID.Auth
{
Expand All @@ -16,78 +13,15 @@ static class Program
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
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();
var waitFrm = new Wait(callback, app);
Application.Run(waitFrm);
}

/*
Expand All @@ -101,22 +35,5 @@ private static string DecodeUrlString(string 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);
}
}
}
4 changes: 2 additions & 2 deletions Auth/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
33 changes: 31 additions & 2 deletions Auth/Wait.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 96 additions & 1 deletion Auth/Wait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<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)
{
var result = await udp.ReceiveAsync();
message = Encodes.UTF8NoBOM.GetString(result.Buffer);
ip = result.RemoteEndPoint.Address;
}
}
return new Device(name, ip);
}
}
}
4 changes: 2 additions & 2 deletions Library/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit bbec204

Please sign in to comment.