From a7b4922df66902e6d272aea68785971d32c0989e Mon Sep 17 00:00:00 2001 From: KevinZonda <33132228+KevinZonda@users.noreply.github.com> Date: Sun, 7 Jun 2020 14:09:17 +0800 Subject: [PATCH] [UX] add port detector --- NaiveSharp/MainWindow.cs | 50 ++++++++++++++++++++++++++++ NaiveSharp/Module/NaiveCmdBuilder.cs | 1 - NaiveSharp/Module/Net.cs | 24 +++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 NaiveSharp/Module/Net.cs diff --git a/NaiveSharp/MainWindow.cs b/NaiveSharp/MainWindow.cs index 8985dd9..f312022 100644 --- a/NaiveSharp/MainWindow.cs +++ b/NaiveSharp/MainWindow.cs @@ -142,6 +142,56 @@ private void lblSave_Click(object sender, EventArgs e) private void btnRun_Click(object sender, EventArgs e) { + /* + * 0 -> Ok + * 1 -> 1080 + * 2 -> 1081 + * 3 -> 1080 & 1081 + */ + int status = 0; + + if (Net.IsPortUsed(1080)) + { + status = 1; + } + if (Net.IsPortUsed(1081)) + { + if (status == 1) + { + status = 3; + } + else + { + status = 2; + } + } + DialogResult result = DialogResult.OK; + switch (status) + { + case 1: + result = MessageBox.Show("Port 1080 is in used! NaiveProxy may not work normally!\n" + + "Do you still want to continue?", "Port is in used", + MessageBoxButtons.YesNo, + MessageBoxIcon.Warning); + break; + case 2: + result = MessageBox.Show("Port 1081 is in used! HTTP proxy and padding may not work normally!\n" + + "Do you still want to continue?", "Port is in used", + MessageBoxButtons.YesNo, + MessageBoxIcon.Warning); + break; + case 3: + result = MessageBox.Show("Port 1080 is in used! NaiveProxy may not work normally!\n" + + "Port 1081 is in used! HTTP proxy and padding may not work normally!\n" + + "Do you still want to continue?", "Port is in used", + MessageBoxButtons.YesNo, + MessageBoxIcon.Warning); + break; + } + if (result == DialogResult.No) + { + return; + } Operation.Run(); MessageBox.Show("NaiveProxy runs successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); diff --git a/NaiveSharp/Module/NaiveCmdBuilder.cs b/NaiveSharp/Module/NaiveCmdBuilder.cs index fcf090c..c10a377 100644 --- a/NaiveSharp/Module/NaiveCmdBuilder.cs +++ b/NaiveSharp/Module/NaiveCmdBuilder.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; namespace NaiveSharp.Module { diff --git a/NaiveSharp/Module/Net.cs b/NaiveSharp/Module/Net.cs new file mode 100644 index 0000000..8b421d5 --- /dev/null +++ b/NaiveSharp/Module/Net.cs @@ -0,0 +1,24 @@ +using System.Net; +using System.Net.NetworkInformation; + +namespace NaiveSharp.Module +{ + class Net + { + public static bool IsPortUsed(int port) + { + bool isPortUsed = false; + IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); + foreach (IPEndPoint endPoint in ipEndPoints) + { + if (endPoint.Port == port) + { + isPortUsed = true; + break; + } + } + return isPortUsed; + } + } +}