This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
5,206 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Compiled Files | ||
out | ||
|
||
## Assembly temp files | ||
AssemblyInfo.old | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
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 | ||
{ | ||
/// <summary> | ||
/// 应用程序的主入口点。 | ||
/// </summary> | ||
[STAThread] | ||
static async 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(); | ||
// 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(await sr.ReadLineAsync()); | ||
pairKey = Crypt.Decrypt(await sr.ReadLineAsync()); | ||
cryptionKey = Crypt.Decrypt(await sr.ReadLineAsync()); | ||
device = await WaitForIP(name); | ||
} | ||
waitFrm.SetInfoText("配置文件成功读取,正在建立连接..."); | ||
using (var tcpClient = new TcpClient()) | ||
{ | ||
await tcpClient.ConnectAsync(device.IP, NetworkPorts.Auth); | ||
waitFrm.SetInfoText("连接已建立,正在发送消息..."); | ||
|
||
using (var tcpStreamWriter = new StreamWriter(tcpClient.GetStream())) | ||
{ | ||
await tcpStreamWriter.WriteLineAsync(Crypt.Encrypt(auth_prefix + pairKey, Crypt.GenerateKey(cryptionKey))); | ||
await tcpStreamWriter.FlushAsync(); | ||
waitFrm.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)) | ||
{ | ||
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 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.