细节修缮

This commit is contained in:
ChsBuffer
2020-07-16 17:20:06 +08:00
parent aa581b2586
commit d27c7c016c
19 changed files with 239 additions and 257 deletions

View File

@@ -8,7 +8,7 @@ namespace Netch.Controllers
{
public DNSController()
{
AkaName = "dns Service";
Name = "dns Service";
MainFile = "unbound";
ExtFiles = new[] {"unbound-service.conf", "forward-zone.conf"};
InitCheck();
@@ -44,7 +44,7 @@ namespace Netch.Controllers
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
WriteLog(e);
Write(e.Data);
}
public override void Stop()

View File

@@ -92,7 +92,7 @@ namespace Netch.Controllers
public override void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)

View File

@@ -18,7 +18,6 @@ namespace Netch.Controllers
if (!Ready) return false;
Instance = GetProcess("bin\\ShadowsocksR.exe");
Instance.StartInfo.FileName = "bin\\ShadowsocksR.exe";
Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;
@@ -69,7 +68,7 @@ namespace Netch.Controllers
public override void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)

View File

@@ -63,7 +63,7 @@ namespace Netch.Controllers
public override void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)

View File

@@ -205,7 +205,7 @@ namespace Netch.Controllers
public override void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)

View File

@@ -13,7 +13,7 @@ namespace Netch.Controllers
/// <param />
/// 未赋值会在 <see cref="InitCheck" /> 赋值为 <see cref="MainFile" />
/// </summary>
public string AkaName;
public string Name;
/// <summary>
/// 其他需要文件
@@ -66,7 +66,7 @@ namespace Netch.Controllers
/// <returns></returns>
protected void InitCheck()
{
if (string.IsNullOrEmpty(AkaName)) AkaName = MainFile;
if (string.IsNullOrEmpty(Name)) Name = MainFile;
var result = false;
// 杀残留
@@ -74,7 +74,7 @@ namespace Netch.Controllers
// 清日志
try
{
if (File.Exists($"logging\\{AkaName}.log")) File.Delete($"logging\\{AkaName}.log");
if (File.Exists($"logging\\{Name}.log")) File.Delete($"logging\\{Name}.log");
}
catch (Exception)
{
@@ -90,38 +90,37 @@ namespace Netch.Controllers
Logging.Error($"主程序 bin\\{MainFile}.exe 不存在");
}
if (ExtFiles == null)
extResult = true;
else
foreach (var f in ExtFiles)
if (!File.Exists($"bin\\{f}"))
if (ExtFiles != null)
{
foreach (var file in ExtFiles)
if (!File.Exists($"bin\\{file}"))
{
extResult = false;
Logging.Error($"附加文件 bin\\{f} 不存在");
Logging.Error($"附加文件 bin\\{file} 不存在");
}
}
result = extResult && mainResult;
if (!result)
Logging.Error(AkaName + " 未就绪");
Logging.Error(Name + " 未就绪");
Ready = result;
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="std"></param>
/// <returns><see cref="std" />是否为空</returns>
protected bool WriteLog(DataReceivedEventArgs std)
/// <param name="s"></param>
/// <returns><see cref="s" />是否为空</returns>
protected bool Write(string s)
{
if (string.IsNullOrWhiteSpace(std.Data)) return false;
if (string.IsNullOrWhiteSpace(s)) return false;
try
{
File.AppendAllText($"logging\\{AkaName}.log", $@"{std.Data}{Global.EOF}");
File.AppendAllText($"logging\\{Name}.log", s + Global.EOF);
}
catch (Exception e)
{
Logging.Error($"写入{AkaName}日志错误:\n" + e);
Logging.Error($"写入{Name}日志错误:\n" + e);
}
return true;

View File

@@ -18,7 +18,7 @@ namespace Netch.Controllers
/// <summary>
/// NTT 控制器
/// </summary>
public NTTController pNTTController;
public NTTController pNTTController = new NTTController();
[DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")]
public static extern uint FlushDNSResolverCache();
@@ -31,7 +31,6 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
pNTTController ??= new NTTController();
FlushDNSResolverCache();
var result = false;
@@ -57,12 +56,13 @@ namespace Netch.Controllers
break;
}
MainForm.Instance.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.AkaName));
Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
if (pEncryptedProxyController.Ready) result = pEncryptedProxyController.Start(server, mode);
}
if (result)
{
Logging.Info("加密代理已启动");
// 加密代理已启动
switch (mode.Type)
{
@@ -84,26 +84,29 @@ namespace Netch.Controllers
if (pModeController != null && pModeController.Ready)
{
MainForm.Instance.StatusText(i18N.Translate("Starting ", pModeController.AkaName));
Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name));
result = pModeController.Start(server, mode);
}
switch (mode.Type)
if (result)
{
case 0:
case 1:
case 2:
if (result)
Logging.Info("模式已启动");
switch (mode.Type)
{
case 0:
case 1:
case 2:
Task.Run(() =>
{
MainForm.Instance.NatTypeStatusText(i18N.Translate("Starting NatTester"));
Global.MainForm.NatTypeStatusText(i18N.Translate("Starting NatTester"));
// Thread.Sleep(1000);
var (nttResult, natType, localEnd, publicEnd) = pNTTController.Start();
var country = Utils.Utils.GetCityCode(publicEnd);
if (nttResult) MainForm.Instance.NatTypeStatusText(natType, country);
if (nttResult) Global.MainForm.NatTypeStatusText(natType, country);
});
break;
break;
}
}
}

View File

@@ -19,7 +19,7 @@ namespace Netch.Controllers
public HTTPController()
{
AkaName = "HTTP";
Name = "HTTP";
Ready = true;
}

View File

@@ -25,9 +25,7 @@ namespace Netch.Controllers
static NFController()
{
// 生成系统版本
var winNTver = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}";
switch (winNTver)
switch ($"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}")
{
case "10.0":
BinDriver = "Win-10.sys";
@@ -41,7 +39,7 @@ namespace Netch.Controllers
BinDriver = "Win-7.sys";
break;
default:
Logging.Error($"不支持的系统版本:{winNTver}");
Logging.Error($"不支持的系统版本:{Environment.OSVersion.Version}");
return;
}
@@ -131,11 +129,11 @@ namespace Netch.Controllers
// 防止其他程序占用 重置 NF 百万连接数限制
NFService.Stop();
NFService.WaitForStatus(ServiceControllerStatus.Stopped);
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
Global.MainForm.StatusText(i18N.Translate("Starting netfilter2 Service"));
NFService.Start();
break;
case ServiceControllerStatus.Stopped:
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
Global.MainForm.StatusText(i18N.Translate("Starting netfilter2 Service"));
NFService.Start();
break;
}
@@ -208,12 +206,12 @@ namespace Netch.Controllers
return false;
}
MainForm.Instance.StatusText(i18N.Translate("Register driver"));
Global.MainForm.StatusText(i18N.Translate("Register driver"));
// 注册驱动文件
var result = NFAPI.nf_registerDriver("netfilter2");
if (result == NF_STATUS.NF_STATUS_SUCCESS)
{
Logging.Info($"驱动安装成功,当前驱动版本:{DriverVersion(DriverVersion(SystemDriver))}");
Logging.Info($"驱动安装成功,当前驱动版本:{DriverVersion(SystemDriver)}");
}
else
{
@@ -226,7 +224,7 @@ namespace Netch.Controllers
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (Instance.HasExited)

View File

@@ -62,7 +62,7 @@ namespace Netch.Controllers
/// </summary>
public bool SetupBypass()
{
MainForm.Instance.StatusText(i18N.Translate("SetupBypass"));
Global.MainForm.StatusText(i18N.Translate("SetupBypass"));
Logging.Info("设置绕行规则 → 设置让服务器 IP 走直连");
// 让服务器 IP 走直连
foreach (var address in _serverAddresses)
@@ -310,7 +310,7 @@ namespace Netch.Controllers
{
if (!Ready) return false;
MainForm.Instance.StatusText(i18N.Translate("Starting Tap"));
Global.MainForm.StatusText(i18N.Translate("Starting Tap"));
_savedMode = mode;
_savedServer = server;
@@ -391,7 +391,7 @@ namespace Netch.Controllers
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!WriteLog(e)) return;
if (!Write(e.Data)) return;
if (State == State.Starting)
{
if (e.Data.Contains("Running"))