Skip to content

Commit

Permalink
改进已启动时设置端口的占用检查处理
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Jul 18, 2020
1 parent 8a56dd4 commit 54a263a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
15 changes: 12 additions & 3 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
using Netch.Utils;

namespace Netch.Controllers
{
public class MainController
{
/// <summary>
/// 记录当前使用的端口
/// <see cref="MainForm.LocalPortText"/>
/// </summary>
public static readonly List<int> UsingPorts = new List<int>();

public EncryptedProxy pEncryptedProxyController;

public ModeController pModeController;
Expand Down Expand Up @@ -60,19 +68,19 @@ public bool Start(Server server, Mode mode)
// 检查端口是否被占用
if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
{
MessageBoxX.Show("Socks5" + i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "Socks5"));
return false;
}

if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
{
MessageBoxX.Show("HTTP" + i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "HTTP"));
return false;
}

if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
{
MessageBoxX.Show("RedirectorTCP" + i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "Redirector TCP"));
return false;
}

Expand Down Expand Up @@ -139,6 +147,7 @@ public bool Start(Server server, Mode mode)
public void Stop()
{
Task.Run(() => pEncryptedProxyController?.Stop());
Task.Run(() => UsingPorts.Clear());
pModeController?.Stop();
}

Expand Down
15 changes: 8 additions & 7 deletions Netch/Controllers/Mode/NFController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public NFController()

public override bool Start(Server server, Mode mode)
{
Logging.Info("内置驱动版本" + DriverVersion(BinDriver));
Logging.Info("系统驱动版本" + DriverVersion(SystemDriver));
Logging.Info("内置驱动版本: " + DriverVersion(BinDriver));
if (DriverVersion(SystemDriver) != DriverVersion(BinDriver))
{
if (File.Exists(SystemDriver))
{
Logging.Info("系统驱动版本: " + DriverVersion(SystemDriver));
Logging.Info("更新驱动");
UninstallDriver();
}
Expand Down Expand Up @@ -108,13 +108,14 @@ public override bool Start(Server server, Mode mode)

//备份并替换系统DNS
_sysDns = DNS.getSystemDns();
string[] dns = { "1.1.1.1", "8.8.8.8" };
DNS.SetDNS(dns);

return true; }
string[] dns = {"1.1.1.1", "8.8.8.8"};
DNS.SetDNS(dns);

return true;
}
}

Logging.Error(Name + "启动超时");
Logging.Error(Name + " 启动超时");
Stop();
if (!RestartService()) return false;
}
Expand Down
6 changes: 6 additions & 0 deletions Netch/Forms/MainForm.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static string LocalPortText(string serverType, int modeType)
if (modeType == 3 || modeType == 5)
{
// 可控HTTP
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
Expand All @@ -136,14 +137,19 @@ private static string LocalPortText(string serverType, int modeType)
else
{
// 可控Socks5
MainController.UsingPorts.Add(Global.Settings.Socks5LocalPort);
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (modeType == 3 || modeType == 5)
{
// 有HTTP
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}

if (modeType == 0)
MainController.UsingPorts.Add(Global.Settings.RedirectorTCPPort);

text.Append(")");
return text.ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion Netch/Forms/SettingForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private bool CheckPortText(string portName, ref TextBox portTextBox, ref int ori

if (PortHelper.PortInUse(port, portType))
{
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", portName));
MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", portName));
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Netch/Resources/zh-CN
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"Exit": "退出",
"Unable to start? Click me to download": "无法启动?点我下载依赖",

"port is in use.": "端口已被占用",
"The {0} port is in use.": "{0} 端口已被占用",

"Bypass LAN": "[网页代理] 绕过局域网",
"Bypass LAN (Non System Proxy)": "[网页代理] 绕过局域网(不设置系统代理)",
Expand Down
6 changes: 3 additions & 3 deletions Netch/Utils/PortHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Net.NetworkInformation;
using Netch.Controllers;

namespace Netch.Utils
{
Expand All @@ -14,9 +15,8 @@ public static class PortHelper
public static bool PortInUse(int port, PortType type = PortType.Both)
{
var netInfo = IPGlobalProperties.GetIPGlobalProperties();

var tcpResult = type != PortType.UDP && netInfo.GetActiveTcpListeners().Any(ipEndPoint => ipEndPoint.Port == port);
var udpResult = type != PortType.TCP && netInfo.GetActiveUdpListeners().Any(ipEndPoint => ipEndPoint.Port == port);
var tcpResult = type != PortType.UDP && netInfo.GetActiveTcpListeners().Any(ipEndPoint => !MainController.UsingPorts.Contains(ipEndPoint.Port) && ipEndPoint.Port == port);
var udpResult = type != PortType.TCP && netInfo.GetActiveUdpListeners().Any(ipEndPoint => !MainController.UsingPorts.Contains(ipEndPoint.Port) && ipEndPoint.Port == port);

return tcpResult || udpResult;
}
Expand Down
29 changes: 20 additions & 9 deletions Netch/Utils/i18N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class i18N
public static void Load(string langCode)
{
LangCode = langCode;

var text = "";
if (langCode.Equals("System"))
{
Expand Down Expand Up @@ -67,19 +67,30 @@ public static void Load(string langCode)
/// </summary>
/// <param name="text">需要翻译的文本</param>
/// <returns>翻译完毕的文本</returns>
public static string Translate(string text)
{
return Data.Contains(text) ? Data[text].ToString() : text;
}

public static string Translate(params string[] text)
public static string Translate(params object[] text)
{
var a = new StringBuilder();
foreach (var t in text)
a.Append(Data.Contains(t) ? Data[t].ToString() : t);
if (t is string)
a.Append(Data.Contains(t) ? Data[t].ToString() : t);
else
a.Append(t);
return a.ToString();
}


public static string TranslateFormat(string format, params object[] args)
{
for (var i = 0; i < args.Length; i++)
{
if (args[i] is string)
{
args[i] = Translate((string) args[i]);
}
}

return string.Format(Translate(format), args);
}

public static List<string> GetTranslateList()
{
var translateFile = new List<string> {"System", "zh-CN", "en-US"};
Expand Down

0 comments on commit 54a263a

Please sign in to comment.