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

Commit bbec204

Browse files
committed
v1.3.1 Fix UI Thread Blocking
2 parents e546212 + 4ea9b02 commit bbec204

File tree

5 files changed

+134
-93
lines changed

5 files changed

+134
-93
lines changed

Auth/Program.cs

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using System.Threading.Tasks;
55
using System.Windows.Forms;
66
using RapID.ClassLibrary;
7-
using System.Net;
8-
using System.Net.Sockets;
9-
using System.IO;
107

118
namespace RapID.Auth
129
{
@@ -16,78 +13,15 @@ static class Program
1613
/// 应用程序的主入口点。
1714
/// </summary>
1815
[STAThread]
19-
static void Main(string[] args)
16+
static void Main(string[] args)
2017
{
2118
Application.EnableVisualStyles();
2219
Application.SetCompatibleTextRenderingDefault(false);
2320
// callback& app
2421
var callback = DecodeUrlString(args[0]);
2522
var app = Crypt.Decrypt(Crypt.Decrypt(Crypt.Decrypt(args[1])));
26-
// ask for permission
27-
var ifAuth = MessageBox.Show("是否同意应用" + app + "发起鉴权请求?", "Rap-ID", MessageBoxButtons.YesNo);
28-
if (ifAuth != DialogResult.Yes)
29-
{
30-
// exit on denying
31-
Application.Exit();
32-
}
33-
// start user interface
34-
var waitFrm = new Wait();
35-
waitFrm.Show();
36-
// start authentication
37-
#region Authentication
38-
const string auth_prefix = "AUTH";
39-
const string authok_prefix = "AUTHOK";
40-
const string authfail_prefix = "AUTHFAIL";
41-
42-
waitFrm.SetInfoText("正在读取配置文件...");
43-
44-
string cryptionKey, pairKey;
45-
Device device;
46-
using (var sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "pair"))
47-
{
48-
var name = Crypt.Decrypt( sr.ReadLine());
49-
pairKey = Crypt.Decrypt( sr.ReadLine());
50-
cryptionKey = Crypt.Decrypt( sr.ReadLine());
51-
device = WaitForIP(name);
52-
}
53-
waitFrm.SetInfoText("配置文件成功读取,正在建立连接...");
54-
using (var tcpClient = new TcpClient())
55-
{
56-
tcpClient.Connect(device.IP, NetworkPorts.Auth);
57-
waitFrm.SetInfoText("连接已建立,正在发送消息...");
58-
59-
using (var tcpStreamWriter = new StreamWriter(tcpClient.GetStream()))
60-
{
61-
tcpStreamWriter.WriteLine(Crypt.Encrypt(auth_prefix + pairKey, Crypt.GenerateKey(cryptionKey)));
62-
tcpStreamWriter.Flush();
63-
waitFrm.SetInfoText("消息发送成功,正在等待手机端回应...");
64-
65-
using (var tcpStreamReader = new StreamReader(tcpClient.GetStream()))
66-
{
67-
var message = Crypt.Decrypt( tcpStreamReader.ReadLine(), Crypt.GenerateKey(cryptionKey));
68-
#if DEBUG
69-
MessageBox.Show(message);
70-
#endif
71-
if (message.StartsWith(authok_prefix))
72-
{
73-
waitFrm.SetInfoText("授权成功!");
74-
System.Threading.Thread.Sleep(1000);
75-
if (callback != String.Empty)
76-
{
77-
System.Diagnostics.Process.Start(callback + message.Replace(authok_prefix, String.Empty));
78-
}
79-
}
80-
else if (message.StartsWith(authfail_prefix))
81-
{
82-
waitFrm.SetInfoText("授权失败!");
83-
System.Threading.Thread.Sleep(1000);
84-
}
85-
}
86-
87-
}
88-
}
89-
#endregion
90-
Application.Exit();
23+
var waitFrm = new Wait(callback, app);
24+
Application.Run(waitFrm);
9125
}
9226

9327
/*
@@ -101,22 +35,5 @@ private static string DecodeUrlString(string url)
10135
url = newUrl;
10236
return newUrl;
10337
}
104-
105-
private static Device WaitForIP(string name)
106-
{
107-
IPAddress ip = IPAddress.Any;
108-
using (var udp = new UdpClient(new IPEndPoint(IPAddress.Any, NetworkPorts.Boradcast)))
109-
{
110-
string message = String.Empty;
111-
while (message != name)
112-
{
113-
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
114-
var result = udp.Receive(ref RemoteIpEndPoint);
115-
message = Encodes.UTF8NoBOM.GetString(result);
116-
ip = RemoteIpEndPoint.Address;
117-
}
118-
}
119-
return new Device(name, ip);
120-
}
12138
}
12239
}

Auth/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
3333
// 方法是按如下所示使用“*”:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.0.8")]
36-
[assembly: AssemblyFileVersion("1.2.0.8")]
35+
[assembly: AssemblyVersion("1.2.0.13")]
36+
[assembly: AssemblyFileVersion("1.2.0.13")]

Auth/Wait.Designer.cs

Lines changed: 31 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Auth/Wait.cs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using System.Windows.Forms;
10+
using RapID.ClassLibrary;
11+
using System.Net;
12+
using System.Net.Sockets;
13+
using System.IO;
1014

1115
namespace RapID.Auth
1216
{
1317
public partial class Wait : Form
1418
{
15-
public Wait()
19+
private string _callback;
20+
private string _app;
21+
public Wait(string callback, string app)
1622
{
1723
InitializeComponent();
24+
this._callback = callback;
25+
this._app = app;
1826
}
1927

2028
public void SetInfoText(string text)
@@ -29,5 +37,92 @@ public void SetInfoText(string text)
2937

3038
public delegate void DOverThread();
3139
public DOverThread OverThread;
40+
41+
private void cancelButton_Click(object sender, EventArgs e)
42+
{
43+
okButton.Enabled = false;
44+
cancelButton.Enabled = false;
45+
Application.Exit();
46+
}
47+
48+
private void Wait_Load(object sender, EventArgs e)
49+
{
50+
this.infoLabel.Text = "应用" + this._app + "正在发起鉴权请求。";
51+
}
52+
53+
private async void okButton_Click(object sender, EventArgs e)
54+
{
55+
okButton.Enabled = false;
56+
cancelButton.Enabled = false;
57+
58+
const string auth_prefix = "AUTH";
59+
const string authok_prefix = "AUTHOK";
60+
const string authfail_prefix = "AUTHFAIL";
61+
62+
this.SetInfoText("正在读取配置文件...");
63+
64+
string cryptionKey, pairKey;
65+
Device device;
66+
using (var sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "pair"))
67+
{
68+
var name = Crypt.Decrypt(sr.ReadLine());
69+
pairKey = Crypt.Decrypt(sr.ReadLine());
70+
cryptionKey = Crypt.Decrypt(sr.ReadLine());
71+
device = await WaitForIP(name);
72+
}
73+
this.SetInfoText("配置文件成功读取,正在建立连接...");
74+
using (var tcpClient = new TcpClient())
75+
{
76+
tcpClient.Connect(device.IP, NetworkPorts.Auth);
77+
this.SetInfoText("连接已建立,正在发送消息...");
78+
79+
using (var tcpStreamWriter = new StreamWriter(tcpClient.GetStream()))
80+
{
81+
await tcpStreamWriter.WriteLineAsync(Crypt.Encrypt(auth_prefix + pairKey, Crypt.GenerateKey(cryptionKey)));
82+
await tcpStreamWriter.FlushAsync();
83+
this.SetInfoText("消息发送成功,正在等待手机端回应...");
84+
85+
using (var tcpStreamReader = new StreamReader(tcpClient.GetStream()))
86+
{
87+
var message = Crypt.Decrypt(await tcpStreamReader.ReadLineAsync(), Crypt.GenerateKey(cryptionKey));
88+
#if DEBUG
89+
MessageBox.Show(message);
90+
#endif
91+
if (message.StartsWith(authok_prefix))
92+
{
93+
this.SetInfoText("授权成功!");
94+
System.Threading.Thread.Sleep(1000);
95+
if (_callback != String.Empty)
96+
{
97+
System.Diagnostics.Process.Start(_callback + message.Replace(authok_prefix, String.Empty));
98+
}
99+
}
100+
else if (message.StartsWith(authfail_prefix))
101+
{
102+
this.SetInfoText("授权失败!");
103+
System.Threading.Thread.Sleep(1000);
104+
}
105+
}
106+
107+
}
108+
}
109+
Application.Exit();
110+
}
111+
112+
private async Task<Device> WaitForIP(string name)
113+
{
114+
IPAddress ip = IPAddress.Any;
115+
using (var udp = new UdpClient(new IPEndPoint(IPAddress.Any, NetworkPorts.Boradcast)))
116+
{
117+
string message = String.Empty;
118+
while (message != name)
119+
{
120+
var result = await udp.ReceiveAsync();
121+
message = Encodes.UTF8NoBOM.GetString(result.Buffer);
122+
ip = result.RemoteEndPoint.Address;
123+
}
124+
}
125+
return new Device(name, ip);
126+
}
32127
}
33128
}

Library/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
3333
// 方法是按如下所示使用“*”:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.0.10")]
36-
[assembly: AssemblyFileVersion("1.2.0.10")]
35+
[assembly: AssemblyVersion("1.2.0.11")]
36+
[assembly: AssemblyFileVersion("1.2.0.11")]

0 commit comments

Comments
 (0)