更新 Controller类

改善错误输出
This commit is contained in:
ChsBuffer
2020-07-17 19:18:29 +08:00
parent a779295525
commit 281c67aced
17 changed files with 195 additions and 254 deletions

View File

@@ -20,7 +20,6 @@ namespace Netch.Controllers
public HTTPController()
{
Name = "HTTP";
Ready = true;
}
/// <summary>
@@ -31,8 +30,6 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public override bool Start(Server server, Mode mode)
{
if (!Ready) return false;
RecordPrevious();
try
{

View File

@@ -3,8 +3,6 @@ using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
using Netch.Utils;
using nfapinet;
@@ -15,14 +13,9 @@ namespace Netch.Controllers
{
private static readonly ServiceController NFService = new ServiceController("netfilter2");
private static readonly string BinDriver = "";
private static readonly string BinDriver = string.Empty;
private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
public static string DriverVersion(string file)
{
return File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : "";
}
static NFController()
{
switch ($"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}")
@@ -48,22 +41,24 @@ namespace Netch.Controllers
public NFController()
{
MainFile = "Redirector";
ExtFiles = new[] {Path.GetFileName(BinDriver)};
InitCheck();
if (!File.Exists(SystemDriver))
{
InstallDriver();
}
Name = "Redirector";
MainFile = "Redirector.exe";
StartedKeywords("Started");
StoppedKeywords("Failed", "Unable");
}
public override bool Start(Server server, Mode mode)
{
if (!CheckDriverReady())
Logging.Info("内置驱动版本" + DriverVersion(BinDriver));
Logging.Info("系统驱动版本" + DriverVersion(SystemDriver));
if (DriverVersion(SystemDriver) != DriverVersion(BinDriver))
{
if (File.Exists(SystemDriver))
{
Logging.Info("更新驱动");
UninstallDriver();
}
if (!InstallDriver())
return false;
}
@@ -73,7 +68,7 @@ namespace Netch.Controllers
processList += proc + ",";
processList += "NTT.exe";
Instance = GetProcess("bin\\Redirector.exe");
Instance = GetProcess();
if (server.Type != "Socks5")
{
Instance.StartInfo.Arguments += $"-r 127.0.0.1:{Global.Settings.Socks5LocalPort} -p \"{processList}\"";
@@ -110,7 +105,7 @@ namespace Netch.Controllers
if (State == State.Started) return true;
}
Logging.Error("NF 进程启动超时");
Logging.Error(Name + "启动超时");
Stop();
if (!RestartService()) return false;
}
@@ -155,17 +150,19 @@ namespace Netch.Controllers
return true;
}
private bool CheckDriverReady()
public static string DriverVersion(string file)
{
// 检查驱动是否存在
if (!File.Exists(SystemDriver)) return false;
// 检查驱动版本号
return DriverVersion(SystemDriver) == DriverVersion(BinDriver);
return File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : string.Empty;
}
/// <summary>
/// 卸载 NF 驱动
/// </summary>
/// <returns>是否成功卸载</returns>
public static bool UninstallDriver()
{
Global.MainForm.StatusText("Uninstall netfilter2");
Logging.Info("卸载NF驱动");
try
{
if (NFService.Status == ServiceControllerStatus.Running)
@@ -180,22 +177,28 @@ namespace Netch.Controllers
}
if (!File.Exists(SystemDriver)) return true;
try
{
NFAPI.nf_unRegisterDriver("netfilter2");
File.Delete(SystemDriver);
return true;
}
catch (Exception ex)
catch (Exception e)
{
throw ex;
Logging.Error(e.ToString());
return false;
}
File.Delete(SystemDriver);
return true;
}
/// <summary>
/// 安装 NF 驱动
/// </summary>
/// <returns>驱动是否安装成功</returns>
public static bool InstallDriver()
{
Logging.Info("安装驱动");
Logging.Info("安装NF驱动");
try
{
File.Copy(BinDriver, SystemDriver);
@@ -222,34 +225,34 @@ namespace Netch.Controllers
return true;
}
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)
State = State.Stopped;
else if (e.Data.Contains("Started"))
State = State.Started;
else if (e.Data.Contains("Failed") || e.Data.Contains("Unable")) State = State.Stopped;
}
else if (State == State.Started)
{
if (e.Data.StartsWith("[APP][Bandwidth]"))
{
var splited = e.Data.Replace("[APP][Bandwidth]", "").Trim().Split(',');
if (splited.Length == 2)
{
var uploadSplited = splited[0].Split(':');
var downloadSplited = splited[1].Split(':');
if (uploadSplited.Length == 2 && downloadSplited.Length == 2)
if (long.TryParse(uploadSplited[1], out var upload) && long.TryParse(downloadSplited[1], out var download))
Task.Run(() => OnBandwidthUpdated(upload, download));
}
}
}
}
// private new void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
// {
// if (!Write(e.Data)) return;
// if (State == State.Starting)
// {
// if (Instance.HasExited)
// State = State.Stopped;
// else if (e.Data.Contains("Started"))
// State = State.Started;
// else if (e.Data.Contains("Failed") || e.Data.Contains("Unable")) State = State.Stopped;
// }
// else if (State == State.Started)
// {
// if (e.Data.StartsWith("[APP][Bandwidth]"))
// {
// var splited = e.Data.Replace("[APP][Bandwidth]", "").Trim().Split(',');
// if (splited.Length == 2)
// {
// var uploadSplited = splited[0].Split(':');
// var downloadSplited = splited[1].Split(':');
//
// if (uploadSplited.Length == 2 && downloadSplited.Length == 2)
// if (long.TryParse(uploadSplited[1], out var upload) && long.TryParse(downloadSplited[1], out var download))
// Task.Run(() => OnBandwidthUpdated(upload, download));
// }
// }
// }
// }
public override void Stop()
{

View File

@@ -8,7 +8,6 @@ using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Netch.Forms;
using Netch.Models;
using Netch.Properties;
using Netch.Utils;
@@ -35,14 +34,16 @@ namespace Netch.Controllers
public TUNTAPController()
{
MainFile = "tun2socks";
InitCheck();
Name = "Tap";
MainFile = "tun2socks.exe";
StartedKeywords("Running");
StoppedKeywords("failed","invalid vconfig file");
}
/// <summary>
/// 配置 TUNTAP 适配器
/// </summary>
public bool Configure()
private bool Configure()
{
// 查询服务器 IP 地址
var destination = Dns.GetHostAddressesAsync(_savedServer.Hostname);
@@ -60,7 +61,7 @@ namespace Netch.Controllers
/// <summary>
/// 设置绕行规则
/// </summary>
public bool SetupBypass()
private bool SetupBypass()
{
Global.MainForm.StatusText(i18N.Translate("SetupBypass"));
Logging.Info("设置绕行规则 → 设置让服务器 IP 走直连");
@@ -178,7 +179,7 @@ namespace Netch.Controllers
Logging.Info("设置绕行规则 → 处理自定义 DNS 代理");
if (Global.Settings.TUNTAP.UseCustomDNS)
{
var dns = "";
var dns = string.Empty;
foreach (var value in Global.Settings.TUNTAP.DNS)
{
dns += value;
@@ -252,7 +253,7 @@ namespace Netch.Controllers
{
if (Global.Settings.TUNTAP.UseCustomDNS)
{
var dns = "";
var dns = string.Empty;
foreach (var value in Global.Settings.TUNTAP.DNS)
{
dns += value;
@@ -308,8 +309,6 @@ namespace Netch.Controllers
public override bool Start(Server server, Mode mode)
{
if (!Ready) return false;
Global.MainForm.StatusText(i18N.Translate("Starting Tap"));
_savedMode = mode;
@@ -321,7 +320,7 @@ namespace Netch.Controllers
SetupBypass();
Logging.Info("设置绕行规则完毕");
Instance = GetProcess("bin\\tun2socks.exe");
Instance = GetProcess();
var adapterName = TUNTAP.GetName(Global.TUNTAP.ComponentID);
Logging.Info($"tun2sock使用适配器{adapterName}");
@@ -331,7 +330,7 @@ namespace Netch.Controllers
//if (Global.Settings.TUNTAP.UseCustomDNS || server.Type.Equals("VMess"))
if (Global.Settings.TUNTAP.UseCustomDNS)
{
dns = "";
dns = string.Empty;
foreach (var value in Global.Settings.TUNTAP.DNS)
{
dns += value;
@@ -389,17 +388,6 @@ namespace Netch.Controllers
pDNSController.Stop();
}
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (e.Data.Contains("Running"))
State = State.Started;
else if (e.Data.Contains("failed") || e.Data.Contains("invalid vconfig file")) State = State.Stopped;
}
}
/// <summary>
/// 搜索出口
/// </summary>
@@ -431,7 +419,7 @@ namespace Netch.Controllers
// 通过索引查找对应适配器的 IPv4 地址
if (p.Index == Global.Adapter.Index)
{
var AdapterIPs = "";
var AdapterIPs = string.Empty;
foreach (var ip in adapterProperties.UnicastAddresses)
{