mirror of
https://github.com/netchx/netch.git
synced 2026-04-03 19:35:10 +08:00
新增端口占用检查 #307
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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
59
Netch/Utils/PortHelper.cs
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user