From 281c67acedf62b6f1f8aeed008dffef26644b4ff Mon Sep 17 00:00:00 2001
From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com>
Date: Fri, 17 Jul 2020 19:18:29 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Controller=E7=B1=BB=20?=
=?UTF-8?q?=E6=94=B9=E5=96=84=E9=94=99=E8=AF=AF=E8=BE=93=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Netch/Controllers/DNSController.cs | 16 +--
.../EncryptedProxy/SSController.cs | 25 +---
.../EncryptedProxy/SSRController.cs | 26 +---
.../EncryptedProxy/TrojanController.cs | 23 +---
.../EncryptedProxy/VMessController.cs | 33 ++---
Netch/Controllers/Interface/Controller.cs | 126 ++++++++++--------
Netch/Controllers/Interface/EncryptedProxy.cs | 5 +-
Netch/Controllers/MainController.cs | 9 +-
Netch/Controllers/Mode/HTTPController.cs | 3 -
Netch/Controllers/Mode/NFController.cs | 121 +++++++++--------
Netch/Controllers/Mode/TUNTAPController.cs | 34 ++---
Netch/Controllers/NTTController.cs | 9 +-
Netch/Controllers/PrivoxyController.cs | 10 +-
Netch/Controllers/UpdateChecker.cs | 2 +-
Netch/Forms/MainForm.MenuStrip.cs | 2 +-
Netch/Forms/MainForm.Profile.cs | 2 +-
Netch/Netch.cs | 3 +
17 files changed, 195 insertions(+), 254 deletions(-)
diff --git a/Netch/Controllers/DNSController.cs b/Netch/Controllers/DNSController.cs
index b58cd538..72d7a8ee 100644
--- a/Netch/Controllers/DNSController.cs
+++ b/Netch/Controllers/DNSController.cs
@@ -1,5 +1,4 @@
using System;
-using System.Diagnostics;
using Netch.Utils;
namespace Netch.Controllers
@@ -8,10 +7,8 @@ namespace Netch.Controllers
{
public DNSController()
{
- Name = "dns Service";
- MainFile = "unbound";
- ExtFiles = new[] {"unbound-service.conf", "forward-zone.conf"};
- InitCheck();
+ Name = "DNS Service";
+ MainFile = "unbound.exe";
}
///
@@ -20,9 +17,7 @@ namespace Netch.Controllers
///
public bool Start()
{
- if (!Ready) return false;
-
- Instance = GetProcess("bin\\unbound.exe");
+ Instance = GetProcess();
Instance.StartInfo.Arguments = "-c unbound-service.conf -v";
Instance.OutputDataReceived += OnOutputDataReceived;
@@ -42,11 +37,6 @@ namespace Netch.Controllers
}
}
- private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
- {
- Write(e.Data);
- }
-
public override void Stop()
{
StopInstance();
diff --git a/Netch/Controllers/EncryptedProxy/SSController.cs b/Netch/Controllers/EncryptedProxy/SSController.cs
index 4780c2ce..794c25a4 100644
--- a/Netch/Controllers/EncryptedProxy/SSController.cs
+++ b/Netch/Controllers/EncryptedProxy/SSController.cs
@@ -1,5 +1,4 @@
-using System.Diagnostics;
-using System.Text;
+using System.Text;
using System.Threading;
using Netch.Models;
using Netch.Utils;
@@ -10,8 +9,10 @@ namespace Netch.Controllers
{
public SSController()
{
- MainFile = "Shadowsocks";
- InitCheck();
+ Name = "Shadowsocks";
+ MainFile = "Shadowsocks.exe";
+ StartedKeywords("listening at");
+ StoppedKeywords("Invalid config path","usage","plugin service exit unexpectedly");
}
public override bool Start(Server server, Mode mode)
@@ -45,8 +46,7 @@ namespace Netch.Controllers
return true;
}
- if (!Ready) return false;
- Instance = GetProcess("bin\\Shadowsocks.exe");
+ Instance = GetProcess();
Instance.StartInfo.Arguments = $"-s {server.Hostname} -p {server.Port} -b {Global.Settings.LocalAddress} -l {Global.Settings.Socks5LocalPort} -m {server.EncryptMethod} -k \"{server.Password}\" -u";
if (!string.IsNullOrWhiteSpace(server.Plugin) && !string.IsNullOrWhiteSpace(server.PluginOption))
@@ -89,18 +89,5 @@ namespace Netch.Controllers
else
StopInstance();
}
-
- public override 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("listening at"))
- State = State.Started;
- else if (e.Data.Contains("Invalid config path") || e.Data.Contains("usage") || e.Data.Contains("plugin service exit unexpectedly")) State = State.Stopped;
- }
- }
}
}
\ No newline at end of file
diff --git a/Netch/Controllers/EncryptedProxy/SSRController.cs b/Netch/Controllers/EncryptedProxy/SSRController.cs
index 6ef77874..a93ebf73 100644
--- a/Netch/Controllers/EncryptedProxy/SSRController.cs
+++ b/Netch/Controllers/EncryptedProxy/SSRController.cs
@@ -1,5 +1,4 @@
-using System.Diagnostics;
-using System.Threading;
+using System.Threading;
using Netch.Models;
using Netch.Utils;
@@ -9,15 +8,15 @@ namespace Netch.Controllers
{
public SSRController()
{
- MainFile = "ShadowsocksR";
- InitCheck();
+ Name = "ShadowsocksR";
+ MainFile = "ShadowsocksR.exe";
+ StartedKeywords("listening at");
+ StoppedKeywords("Invalid config path","usage");
}
public override bool Start(Server server, Mode mode)
{
- if (!Ready) return false;
-
- Instance = GetProcess("bin\\ShadowsocksR.exe");
+ Instance = GetProcess();
Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;
@@ -66,19 +65,6 @@ namespace Netch.Controllers
return false;
}
- public override 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("listening at"))
- State = State.Started;
- else if (e.Data.Contains("Invalid config path") || e.Data.Contains("usage")) State = State.Stopped;
- }
- }
-
public override void Stop()
{
StopInstance();
diff --git a/Netch/Controllers/EncryptedProxy/TrojanController.cs b/Netch/Controllers/EncryptedProxy/TrojanController.cs
index 5b75b0ff..9589884e 100644
--- a/Netch/Controllers/EncryptedProxy/TrojanController.cs
+++ b/Netch/Controllers/EncryptedProxy/TrojanController.cs
@@ -1,5 +1,4 @@
using System.Collections.Generic;
-using System.Diagnostics;
using System.IO;
using System.Threading;
using Netch.Models;
@@ -12,13 +11,14 @@ namespace Netch.Controllers
{
public TrojanController()
{
- MainFile = "Trojan";
- InitCheck();
+ Name = "Trojan";
+ MainFile = "Trojan.exe";
+ StartedKeywords("started");
+ StoppedKeywords("exiting");
}
public override bool Start(Server server, Mode mode)
{
- if (!Ready) return false;
File.WriteAllText("data\\last.json", JsonConvert.SerializeObject(new Trojan
{
@@ -32,7 +32,7 @@ namespace Netch.Controllers
}
}));
- Instance = GetProcess("bin\\Trojan.exe");
+ Instance = GetProcess();
Instance.StartInfo.Arguments = "-c ..\\data\\last.json";
Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;
@@ -61,19 +61,6 @@ namespace Netch.Controllers
return false;
}
- public override 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("exiting")) State = State.Stopped;
- }
- }
-
public override void Stop()
{
StopInstance();
diff --git a/Netch/Controllers/EncryptedProxy/VMessController.cs b/Netch/Controllers/EncryptedProxy/VMessController.cs
index 19365fab..5bd91802 100644
--- a/Netch/Controllers/EncryptedProxy/VMessController.cs
+++ b/Netch/Controllers/EncryptedProxy/VMessController.cs
@@ -1,5 +1,4 @@
using System.Collections.Generic;
-using System.Diagnostics;
using System.IO;
using System.Threading;
using Netch.Models;
@@ -13,13 +12,14 @@ namespace Netch.Controllers
{
public VMessController()
{
- MainFile = "v2ray";
- InitCheck();
+ Name = "v2ray";
+ MainFile = "v2ray.exe";
+ StartedKeywords("started");
+ StoppedKeywords("config file not readable", "failed to");
}
public override bool Start(Server server, Mode mode)
{
- if (!Ready) return false;
File.WriteAllText("data\\last.json", JsonConvert.SerializeObject(new VMess.Config
{
inbounds = new List
@@ -58,14 +58,14 @@ namespace Netch.Controllers
streamSettings = new VMess.StreamSettings
{
network = server.TransferProtocol,
- security = server.TLSSecure ? "tls" : "",
+ security = server.TLSSecure ? "tls" : string.Empty,
wsSettings = server.TransferProtocol == "ws"
? new VMess.WebSocketSettings
{
- path = server.Path == "" ? "/" : server.Path,
+ path = server.Path == string.Empty ? "/" : server.Path,
headers = new VMess.WSHeaders
{
- Host = server.Host == "" ? server.Hostname : server.Host
+ Host = server.Host == string.Empty ? server.Hostname : server.Host
}
}
: null,
@@ -77,10 +77,10 @@ namespace Netch.Controllers
type = server.FakeType,
request = new VMess.TCPRequest
{
- path = server.Path == "" ? "/" : server.Path,
+ path = server.Path == string.Empty ? "/" : server.Path,
headers = new VMess.TCPRequestHeaders
{
- Host = server.Host == "" ? server.Hostname : server.Host
+ Host = server.Host == string.Empty ? server.Hostname : server.Host
}
}
}
@@ -168,7 +168,7 @@ namespace Netch.Controllers
}
}));
- Instance = GetProcess("bin\\v2ray.exe");
+ Instance = GetProcess();
Instance.StartInfo.Arguments = "-config ..\\data\\last.json";
Instance.OutputDataReceived += OnOutputDataReceived;
@@ -203,19 +203,6 @@ namespace Netch.Controllers
return false;
}
- public override 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("config file not readable") || e.Data.Contains("failed to")) State = State.Stopped;
- }
- }
-
public override void Stop()
{
StopInstance();
diff --git a/Netch/Controllers/Interface/Controller.cs b/Netch/Controllers/Interface/Controller.cs
index f551094d..d6a42b6f 100644
--- a/Netch/Controllers/Interface/Controller.cs
+++ b/Netch/Controllers/Interface/Controller.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Netch.Models;
@@ -10,16 +11,9 @@ namespace Netch.Controllers
{
///
/// 控制器名
- ///
- /// 未赋值会在 赋值为
///
public string Name;
- ///
- /// 其他需要文件
- ///
- protected string[] ExtFiles;
-
///
/// 进程实例
///
@@ -30,10 +24,29 @@ namespace Netch.Controllers
///
public string MainFile;
- ///
- /// 运行检查, 由 赋值
- ///
- public bool Ready;
+ private List _startedKeywords = new List();
+
+ private List _stoppedKeywords = new List();
+
+ protected bool RedirectStd = true;
+
+ protected void StartedKeywords(params string[] texts)
+ {
+ foreach (var text in texts)
+ {
+ _startedKeywords.Add(text);
+ }
+ }
+
+
+ protected void StoppedKeywords(params string[] texts)
+ {
+ foreach (var text in texts)
+ {
+ _stoppedKeywords.Add(text);
+ }
+ }
+
///
/// 当前状态
@@ -55,23 +68,13 @@ namespace Netch.Controllers
}
catch (Exception e)
{
- Logging.Error($"停止 {MainFile}.exe 错误:\n" + e);
+ Logging.Error($"停止 {MainFile} 错误:\n" + e);
}
}
- ///
- /// 杀残留进程,清除日志,检查文件
- ///
- ///
- protected void InitCheck()
+ public void ClearLog()
{
- if (string.IsNullOrEmpty(Name)) Name = MainFile;
-
- var result = false;
- // 杀残留
- MainController.KillProcessByName(MainFile);
- // 清日志
try
{
if (File.Exists($"logging\\{Name}.log")) File.Delete($"logging\\{Name}.log");
@@ -80,30 +83,6 @@ namespace Netch.Controllers
{
// ignored
}
-
- // 检查文件
- var mainResult = true;
- var extResult = true;
- if (!string.IsNullOrEmpty(MainFile) && !File.Exists($"bin\\{MainFile}.exe"))
- {
- mainResult = false;
- Logging.Error($"主程序 bin\\{MainFile}.exe 不存在");
- }
-
- if (ExtFiles != null)
- {
- foreach (var file in ExtFiles)
- if (!File.Exists($"bin\\{file}"))
- {
- extResult = false;
- Logging.Error($"附加文件 bin\\{file} 不存在");
- }
- }
-
- result = extResult && mainResult;
- if (!result)
- Logging.Error(Name + " 未就绪");
- Ready = result;
}
///
@@ -126,24 +105,65 @@ namespace Netch.Controllers
return true;
}
- public static Process GetProcess(string path = null, bool redirectStd = true)
+ public Process GetProcess()
{
var p = new Process
{
StartInfo =
{
- Arguments = "",
+ FileName = Path.GetFullPath($"bin\\{MainFile}"),
WorkingDirectory = $"{Global.NetchDir}\\bin",
CreateNoWindow = true,
- RedirectStandardError = redirectStd,
- RedirectStandardInput = redirectStd,
- RedirectStandardOutput = redirectStd,
+ RedirectStandardError = RedirectStd,
+ RedirectStandardInput = RedirectStd,
+ RedirectStandardOutput = RedirectStd,
UseShellExecute = false
},
EnableRaisingEvents = true
};
- if (path != null) p.StartInfo.FileName = Path.GetFullPath(path);
return p;
}
+
+ ///
+ /// 接收输出数据
+ ///
+ /// 发送者
+ /// 数据
+ protected void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
+ {
+ // 写入日志
+ if (!Write(e.Data)) return;
+
+ // 检查启动
+ if (State == State.Starting)
+ {
+ if (Instance.HasExited)
+ {
+ State = State.Stopped;
+
+ return;
+ }
+
+ foreach (var s in _startedKeywords)
+ {
+ if (e.Data.Contains(s))
+ {
+ State = State.Started;
+
+ return;
+ }
+ }
+
+ foreach (var s in _stoppedKeywords)
+ {
+ if (e.Data.Contains(s))
+ {
+ State = State.Stopped;
+
+ return;
+ }
+ }
+ }
+ }
}
}
\ No newline at end of file
diff --git a/Netch/Controllers/Interface/EncryptedProxy.cs b/Netch/Controllers/Interface/EncryptedProxy.cs
index e036c346..f1ec9dc6 100644
--- a/Netch/Controllers/Interface/EncryptedProxy.cs
+++ b/Netch/Controllers/Interface/EncryptedProxy.cs
@@ -1,5 +1,4 @@
-using System.Diagnostics;
-using Netch.Models;
+using Netch.Models;
namespace Netch.Controllers
{
@@ -12,7 +11,5 @@ namespace Netch.Controllers
/// 模式
/// 是否启动成功
public abstract bool Start(Server server, Mode mode);
-
- public abstract void OnOutputDataReceived(object sender, DataReceivedEventArgs e);
}
}
\ No newline at end of file
diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs
index ae144998..d76208c8 100644
--- a/Netch/Controllers/MainController.cs
+++ b/Netch/Controllers/MainController.cs
@@ -3,7 +3,6 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
-using Netch.Forms;
using Netch.Models;
using Netch.Utils;
@@ -56,13 +55,14 @@ namespace Netch.Controllers
break;
}
+ KillProcessByName(pEncryptedProxyController.MainFile);
+
Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
- if (pEncryptedProxyController.Ready) result = pEncryptedProxyController.Start(server, mode);
+ result = pEncryptedProxyController.Start(server, mode);
}
if (result)
{
- Logging.Info("加密代理已启动");
// 加密代理已启动
switch (mode.Type)
{
@@ -82,7 +82,7 @@ namespace Netch.Controllers
break;
}
- if (pModeController != null && pModeController.Ready)
+ if (pModeController != null)
{
Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name));
result = pModeController.Start(server, mode);
@@ -90,7 +90,6 @@ namespace Netch.Controllers
if (result)
{
- Logging.Info("模式已启动");
switch (mode.Type)
{
case 0:
diff --git a/Netch/Controllers/Mode/HTTPController.cs b/Netch/Controllers/Mode/HTTPController.cs
index 99565bc0..d80df22f 100644
--- a/Netch/Controllers/Mode/HTTPController.cs
+++ b/Netch/Controllers/Mode/HTTPController.cs
@@ -20,7 +20,6 @@ namespace Netch.Controllers
public HTTPController()
{
Name = "HTTP";
- Ready = true;
}
///
@@ -31,8 +30,6 @@ namespace Netch.Controllers
/// 是否启动成功
public override bool Start(Server server, Mode mode)
{
- if (!Ready) return false;
-
RecordPrevious();
try
{
diff --git a/Netch/Controllers/Mode/NFController.cs b/Netch/Controllers/Mode/NFController.cs
index 16c7184e..5fc0ddbc 100644
--- a/Netch/Controllers/Mode/NFController.cs
+++ b/Netch/Controllers/Mode/NFController.cs
@@ -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;
}
+ ///
+ /// 卸载 NF 驱动
+ ///
+ /// 是否成功卸载
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;
}
+ ///
+ /// 安装 NF 驱动
+ ///
+ /// 驱动是否安装成功
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()
{
diff --git a/Netch/Controllers/Mode/TUNTAPController.cs b/Netch/Controllers/Mode/TUNTAPController.cs
index c6dd0b64..b1c3f495 100644
--- a/Netch/Controllers/Mode/TUNTAPController.cs
+++ b/Netch/Controllers/Mode/TUNTAPController.cs
@@ -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");
}
///
/// 配置 TUNTAP 适配器
///
- public bool Configure()
+ private bool Configure()
{
// 查询服务器 IP 地址
var destination = Dns.GetHostAddressesAsync(_savedServer.Hostname);
@@ -60,7 +61,7 @@ namespace Netch.Controllers
///
/// 设置绕行规则
///
- 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;
- }
- }
-
///
/// 搜索出口
///
@@ -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)
{
diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs
index 661938bf..c2568af2 100644
--- a/Netch/Controllers/NTTController.cs
+++ b/Netch/Controllers/NTTController.cs
@@ -11,8 +11,8 @@ namespace Netch.Controllers
public NTTController()
{
- MainFile = "NTT";
- InitCheck();
+ Name = "NTT";
+ MainFile = "NTT.exe";
}
///
@@ -21,10 +21,9 @@ namespace Netch.Controllers
///
public (bool, string, string, string) Start()
{
- if (!Ready) return (false, null, null, null);
try
{
- Instance = GetProcess("bin\\NTT.exe");
+ Instance = GetProcess();
Instance.StartInfo.Arguments = $" {Global.Settings.STUN_Server} {Global.Settings.STUN_Server_Port}";
@@ -52,7 +51,7 @@ namespace Netch.Controllers
}
}
- private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
+ private new void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
_lastResult = e.Data;
diff --git a/Netch/Controllers/PrivoxyController.cs b/Netch/Controllers/PrivoxyController.cs
index e11b2af5..dde33995 100644
--- a/Netch/Controllers/PrivoxyController.cs
+++ b/Netch/Controllers/PrivoxyController.cs
@@ -9,15 +9,13 @@ namespace Netch.Controllers
{
public PrivoxyController()
{
- MainFile = "Privoxy";
- ExtFiles = new[] {"default.conf"};
- InitCheck();
+ Name = "Privoxy";
+ MainFile = "Privoxy.exe";
+ RedirectStd = false;
}
public bool Start(Server server, Mode mode)
{
- if (!Ready) return false;
-
var isSocks5 = server.Type == "Socks5";
var socks5Port = isSocks5 ? server.Port : Global.Settings.Socks5LocalPort;
var text = File.ReadAllText("bin\\default.conf")
@@ -28,7 +26,7 @@ namespace Netch.Controllers
text = text.Replace("/ 127.0.0.1", $"/ {server.Hostname}");
File.WriteAllText("data\\privoxy.conf", text);
- Instance = GetProcess("bin\\Privoxy.exe", false);
+ Instance = GetProcess();
Instance.StartInfo.Arguments = "..\\data\\privoxy.conf";
Instance.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Instance.StartInfo.UseShellExecute = true;
diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs
index ee826d82..30d7f5c4 100644
--- a/Netch/Controllers/UpdateChecker.cs
+++ b/Netch/Controllers/UpdateChecker.cs
@@ -53,7 +53,7 @@ namespace Netch.Controllers
}
catch (Exception e)
{
- Debug.WriteLine(e.Message);
+ Debug.WriteLine(e.ToString());
if (notifyNoFound) NewVersionFoundFailed?.Invoke(this, new EventArgs());
}
}
diff --git a/Netch/Forms/MainForm.MenuStrip.cs b/Netch/Forms/MainForm.MenuStrip.cs
index 10d82f72..212b64e8 100644
--- a/Netch/Forms/MainForm.MenuStrip.cs
+++ b/Netch/Forms/MainForm.MenuStrip.cs
@@ -279,7 +279,7 @@ namespace Netch.Forms
}
catch (Exception e)
{
- MessageBox.Show(i18N.Translate("Error", e.Message));
+ MessageBoxX.Show(e.ToString(),info:false);
Console.WriteLine(e);
throw;
}
diff --git a/Netch/Forms/MainForm.Profile.cs b/Netch/Forms/MainForm.Profile.cs
index 2f919f34..4c046a44 100644
--- a/Netch/Forms/MainForm.Profile.cs
+++ b/Netch/Forms/MainForm.Profile.cs
@@ -200,7 +200,7 @@ namespace Netch.Forms
{
Task.Run(() =>
{
- Logging.Info(ee.Message);
+ Logging.Info(ee.ToString());
ProfileButtons[index].Text = i18N.Translate("Error");
Thread.Sleep(1200);
ProfileButtons[index].Text = i18N.Translate("None");
diff --git a/Netch/Netch.cs b/Netch/Netch.cs
index f9f28aad..a67fae89 100644
--- a/Netch/Netch.cs
+++ b/Netch/Netch.cs
@@ -2,6 +2,7 @@
using System.IO;
using System.Threading;
using System.Windows.Forms;
+using Netch.Controllers;
using Netch.Forms;
using Netch.Utils;
@@ -57,6 +58,8 @@ namespace Netch
// 记录当前系统语言
Logging.Info($"当前语言:{Global.Settings.Language}");
+ Logging.Info($"版本:{UpdateChecker.Owner}/{UpdateChecker.Repo} {UpdateChecker.Version}");
+ Logging.Info($"主程序创建日期:{File.GetCreationTime(Global.NetchDir + "\\Netch.exe"):yyyy-M-d HH:mm}");
// 检查是否已经运行
if (!mutex.WaitOne(0, false))