diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index 100a6925..3b93bdfa 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -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); diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 99aad7dd..fcd5a85a 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -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(); diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index 79be3937..7627f31f 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -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] 绕过局域网", diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs new file mode 100644 index 00000000..8c57d370 --- /dev/null +++ b/Netch/Utils/PortHelper.cs @@ -0,0 +1,59 @@ +using System.Net; +using System.Net.NetworkInformation; + +namespace Netch.Utils +{ + class PortHelper + { + /// + /// 端口是否被使用 + /// + /// + /// + public static bool PortInUse(int port) + { + return PortInUse(port, PortType.TCP) || PortInUse(port, PortType.UDP); + } + + /// + /// 指定类型的端口是否已经被使用了 + /// + /// 端口号 + /// 端口类型 + /// + 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; + } + } + + /// + /// 端口类型 + /// + enum PortType + { + TCP, + UDP + } +} \ No newline at end of file