新增端口占用检查 #307

This commit is contained in:
橘子皮
2020-07-17 00:03:06 +08:00
parent b5c1cdaf83
commit a6fd0764e1
4 changed files with 110 additions and 2 deletions

View File

@@ -20,7 +20,7 @@ namespace Netch.Forms
SaveConfigs();
if (State == State.Waiting || State == State.Stopped)
{
// 服务器、模式 需选
#region
if (ServerComboBox.SelectedIndex == -1)
{
MessageBoxX.Show(i18N.Translate("Please select a server first"));
@@ -32,8 +32,30 @@ namespace Netch.Forms
MessageBoxX.Show(i18N.Translate("Please select an mode first"));
return;
}
#endregion
//MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
#region
if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
{
MessageBoxX.Show("The Socks5 port is in use. Click OK to modify it.");
SettingsButton.PerformClick();
return;
}
if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
{
MessageBoxX.Show("The HTTP port is in use. Click OK to modify it.");
SettingsButton.PerformClick();
return;
}
if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
{
MessageBoxX.Show("The RedirectorTCP port is in use. Click OK to modify it.");
SettingsButton.PerformClick();
return;
}
#endregion
UpdateStatus(State.Starting);

View File

@@ -398,6 +398,26 @@ namespace Netch.Forms
Global.Settings.TUNTAP.ProxyDNS = ProxyDNSCheckBox.Checked;
Global.Settings.TUNTAP.UseFakeDNS = UseFakeDNSCheckBox.Checked;
#region
if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
{
MessageBoxX.Show("The Socks5 port is in use. Please reset.");
return;
}
if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
{
MessageBoxX.Show("The HTTP port is in use. Please reset.");
return;
}
if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
{
MessageBoxX.Show("The RedirectorTCP port is in use. Please reset.");
return;
}
#endregion
Configuration.Save();
MessageBoxX.Show(i18N.Translate("Saved"));
Close();

View File

@@ -187,6 +187,13 @@
"Exit": "退出",
"Unable to start? Click me to download": "无法启动?点我下载依赖",
"The Socks5 port is in use. Click OK to modify it.": "Socks5端口已被占用点击确定前往修改",
"The Socks5 port is in use. Please reset.": "Socks5端口已被占用请重新设置",
"The HTTP port is in use. Click OK to modify it.": "HTTP端口已被占用点击确定前往修改",
"The HTTP port is in use. Please reset.": "HTTP端口已被占用请重新设置",
"The RedirectorTCP port is in use. Click OK to modify it.": "RedirectorTCP端口已被占用点击确定前往修改",
"The RedirectorTCP port is in use. Please reset.": "RedirectorTCP端口已被占用请重新设置",
"Bypass LAN": "[网页代理] 绕过局域网",
"Bypass LAN (Non System Proxy)": "[网页代理] 绕过局域网(不设置系统代理)",
"Bypass LAN (TUN/TAP)": "[TUN/TAP] 绕过局域网",

59
Netch/Utils/PortHelper.cs Normal file
View File

@@ -0,0 +1,59 @@
using System.Net;
using System.Net.NetworkInformation;
namespace Netch.Utils
{
class PortHelper
{
/// <summary>
/// 端口是否被使用
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public static bool PortInUse(int port)
{
return PortInUse(port, PortType.TCP) || PortInUse(port, PortType.UDP);
}
/// <summary>
/// 指定类型的端口是否已经被使用了
/// </summary>
/// <param name="port">端口号</param>
/// <param name="type">端口类型</param>
/// <returns></returns>
public static bool PortInUse(int port, PortType type)
{
bool flag = false;
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipendpoints = null;
if (type == PortType.TCP)
{
ipendpoints = properties.GetActiveTcpListeners();
}
else
{
ipendpoints = properties.GetActiveUdpListeners();
}
foreach (IPEndPoint ipendpoint in ipendpoints)
{
if (ipendpoint.Port == port)
{
flag = true;
break;
}
}
ipendpoints = null;
properties = null;
return flag;
}
}
/// <summary>
/// 端口类型
/// </summary>
enum PortType
{
TCP,
UDP
}
}