Skip to content

Commit

Permalink
[UX] add port detector
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Jun 7, 2020
1 parent a8261b3 commit a7b4922
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
50 changes: 50 additions & 0 deletions NaiveSharp/MainWindow.cs
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion NaiveSharp/Module/NaiveCmdBuilder.cs
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;

namespace NaiveSharp.Module
{
Expand Down
24 changes: 24 additions & 0 deletions 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;
}
}
}

0 comments on commit a7b4922

Please sign in to comment.