Merge pull request #311 from chsbuffer/master

修 Bug (大概)
This commit is contained in:
AmazingDM
2020-07-14 17:48:04 +08:00
committed by GitHub
4 changed files with 112 additions and 116 deletions

View File

@@ -13,65 +13,58 @@ namespace Netch.Controllers
{
public class NFController : ModeController
{
/// <summary>
/// 流量变动处理器
/// </summary>
/// <param name="upload">上传</param>
/// <param name="download">下载</param>
public delegate void BandwidthUpdateHandler(long upload, long download);
private static readonly ServiceController NFService = new ServiceController("netfilter2");
private readonly string _binDriverPath;
private static readonly string BinDriver = "";
private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
private readonly string _driverPath = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
private readonly ServiceController _service = new ServiceController("netfilter2");
private string _systemDriverVersion;
public static string DriverVersion(string file)
{
return File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : "";
}
static NFController()
{
// 生成系统版本
var winNTver = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}";
switch (winNTver)
{
case "10.0":
BinDriver = "Win-10.sys";
break;
case "6.3":
case "6.2":
BinDriver = "Win-8.sys";
break;
case "6.1":
case "6.0":
BinDriver = "Win-7.sys";
break;
default:
Logging.Error($"不支持的系统版本:{winNTver}");
return;
}
BinDriver = "bin\\" + BinDriver;
}
public NFController()
{
MainFile = "Redirector";
ExtFiles = new[] {Path.GetFileName(BinDriver)};
InitCheck();
// 生成系统版本
var winNTver = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}";
var driverName = "";
switch (winNTver)
{
case "10.0":
driverName = "Win-10.sys";
break;
case "6.3":
case "6.2":
driverName = "Win-8.sys";
break;
case "6.1":
case "6.0":
driverName = "Win-7.sys";
break;
default:
Logging.Error($"不支持的系统版本:{winNTver}");
Ready = false;
return;
}
_binDriverPath = "bin\\" + driverName;
if (!File.Exists(_driverPath))
if (!File.Exists(SystemDriver))
{
InstallDriver();
}
// 驱动版本
_systemDriverVersion = FileVersionInfo.GetVersionInfo(_driverPath).FileVersion;
}
/// <summary>
/// 流量变动事件
/// </summary>
public event BandwidthUpdateHandler OnBandwidthUpdated;
public override bool Start(Server server, Mode mode)
{
if (!CheckDriverReady())
{
if (File.Exists(_driverPath))
if (File.Exists(SystemDriver))
UninstallDriver();
if (!InstallDriver())
return false;
@@ -131,19 +124,19 @@ namespace Netch.Controllers
{
try
{
switch (_service.Status)
switch (NFService.Status)
{
// 启动驱动服务
case ServiceControllerStatus.Running:
// 防止其他程序占用 重置 NF 百万连接数限制
_service.Stop();
_service.WaitForStatus(ServiceControllerStatus.Stopped);
NFService.Stop();
NFService.WaitForStatus(ServiceControllerStatus.Stopped);
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
_service.Start();
NFService.Start();
break;
case ServiceControllerStatus.Stopped:
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
_service.Start();
NFService.Start();
break;
}
}
@@ -167,22 +160,20 @@ namespace Netch.Controllers
private bool CheckDriverReady()
{
// 检查驱动是否存在
if (!File.Exists(_driverPath)) return false;
if (!File.Exists(SystemDriver)) return false;
// 检查驱动版本号
var binVersion = FileVersionInfo.GetVersionInfo(_binDriverPath).FileVersion;
return _systemDriverVersion.Equals(binVersion);
return DriverVersion(SystemDriver) == DriverVersion(BinDriver);
}
public bool UninstallDriver()
public static bool UninstallDriver()
{
try
{
var service = new ServiceController("netfilter2");
if (service.Status == ServiceControllerStatus.Running)
if (NFService.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
NFService.Stop();
NFService.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
catch (Exception)
@@ -190,13 +181,12 @@ namespace Netch.Controllers
// ignored
}
if (!File.Exists(_driverPath)) return true;
if (!File.Exists(SystemDriver)) return true;
try
{
NFAPI.nf_unRegisterDriver("netfilter2");
File.Delete(_driverPath);
_systemDriverVersion = "";
File.Delete(SystemDriver);
return true;
}
catch (Exception ex)
@@ -205,13 +195,12 @@ namespace Netch.Controllers
}
}
public bool InstallDriver()
public static bool InstallDriver()
{
if (!Ready) return false;
Logging.Info("安装驱动中");
try
{
File.Copy(_binDriverPath, _driverPath);
File.Copy(BinDriver, SystemDriver);
}
catch (Exception e)
{
@@ -224,8 +213,7 @@ namespace Netch.Controllers
var result = NFAPI.nf_registerDriver("netfilter2");
if (result == NF_STATUS.NF_STATUS_SUCCESS)
{
_systemDriverVersion = FileVersionInfo.GetVersionInfo(_driverPath).FileVersion;
Logging.Info($"驱动安装成功,当前驱动版本:{_systemDriverVersion}");
Logging.Info($"驱动安装成功,当前驱动版本:{DriverVersion(DriverVersion(SystemDriver))}");
}
else
{
@@ -269,5 +257,17 @@ namespace Netch.Controllers
{
StopInstance();
}
/// <summary>
/// 流量变动事件
/// </summary>
public event BandwidthUpdateHandler OnBandwidthUpdated;
/// <summary>
/// 流量变动处理器
/// </summary>
/// <param name="upload">上传</param>
/// <param name="download">下载</param>
public delegate void BandwidthUpdateHandler(long upload, long download);
}
}

View File

@@ -52,18 +52,18 @@ namespace Netch.Forms
{
Task.Run(() =>
{
UpdateStatus(State.Started);
StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + PortText(server.Type,mode.Type));
LastUploadBandwidth = 0;
//LastDownloadBandwidth = 0;
//UploadSpeedLabel.Text = "↑: 0 KB/s";
DownloadSpeedLabel.Text = "↑↓: 0 KB/s";
UsedBandwidthLabel.Text = $"{i18N.Translate("Used",": ")}0 KB";
UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true;
UploadSpeedLabel.Visible = false;
Bandwidth.NetTraffic(server, mode, MainController);
});
//MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated;
// 如果勾选启动后最小化
if (Global.Settings.MinimizeWhenStarted)
@@ -86,45 +86,6 @@ namespace Netch.Forms
Hide();
}
// TODO 是否需要移到一个函数中
var text = new StringBuilder(" (");
text.Append(Global.Settings.LocalAddress == "0.0.0.0"
? i18N.Translate("Allow other Devices to connect") + " "
: "");
if (server.Type == "Socks5")
{
// 不可控Socks5
if (mode.Type == 3 || mode.Type == 5)
{
// 可控HTTP
text.Append(
$"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
{
// 不可控HTTP
text.Clear();
}
}
else
{
// 可控Socks5
text.Append(
$"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (mode.Type == 3 || mode.Type == 5)
{
//有HTTP
text.Append(
$" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}
if (text.Length > 0)
{
text.Append(")");
}
UpdateStatus(State.Started);
StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + text);
if (Global.Settings.StartedTcping)
{
// 自动检测延迟
@@ -169,6 +130,47 @@ namespace Netch.Forms
}
}
private string PortText(string serverType,int modeType)
{
var text = new StringBuilder(" (");
text.Append(Global.Settings.LocalAddress == "0.0.0.0"
? i18N.Translate("Allow other Devices to connect") + " "
: "");
if (serverType == "Socks5")
{
// 不可控Socks5
if (modeType == 3 || modeType == 5)
{
// 可控HTTP
text.Append(
$"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
{
// 不可控HTTP
text.Clear();
}
}
else
{
// 可控Socks5
text.Append(
$"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (modeType == 3 || modeType == 5)
{
//有HTTP
text.Append(
$" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}
if (text.Length > 0)
{
text.Append(")");
}
return text.ToString();
}
public void OnBandwidthUpdated(long download)
{
try

View File

@@ -274,7 +274,7 @@ namespace Netch.Forms
{
try
{
if (new NFController().UninstallDriver())
if (NFController.UninstallDriver())
{
MessageBoxX.Show(i18N.Translate("Service has been uninstalled"), owner: this);
}
@@ -286,6 +286,7 @@ namespace Netch.Forms
throw;
}
StatusText(i18N.Translate(StateExtension.GetStatusString(State.Waiting)));
Enabled = true;
});
}

View File

@@ -41,16 +41,9 @@
{
public static string GetStatusString(State state)
{
return state switch
{
State.Started => state.ToString(),
State.Stopping => state.ToString(),
State.Stopped => state.ToString(),
State.Terminating => state.ToString(),
State.Starting => state.ToString(),
State.Waiting => "Waiting for command",
_ => ""
};
if (state == State.Waiting)
return "Waiting for command";
return state.ToString();
}
}
}