From e68929a32a2796200645e1fa6c5572801c5a6629 Mon Sep 17 00:00:00 2001 From: AmazingDM Date: Wed, 26 Aug 2020 14:23:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=E4=BF=AE=E5=A4=8D=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=B5=81=E9=87=8F=E7=BB=9F=E8=AE=A1=20=F0=9F=90=9B?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E4=BF=9D=E5=AD=98=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E9=98=B2=E6=AD=A2=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E7=A8=8B=E5=BA=8F=E6=97=B6=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=A8=A1=E5=BC=8Findex=E4=B8=A2=E5=A4=B1=EF=BC=88debu?= =?UTF-8?q?g=E7=94=A8=EF=BC=89=20=F0=9F=90=9B=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=A9=BA=E5=BC=82=E5=B8=B8=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../EncryptedProxy/SSController.cs | 8 +- Netch/Controllers/Interface/Instance.cs | 10 +- Netch/Forms/MainForm.Control.cs | 1 + Netch/Forms/MainForm.Status.cs | 1 + Netch/Utils/Bandwidth.cs | 97 ++++++++++++------- 6 files changed, 81 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 54ee098d..7f7f9051 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.vs -/packages \ No newline at end of file +/packages +.idea/ diff --git a/Netch/Controllers/EncryptedProxy/SSController.cs b/Netch/Controllers/EncryptedProxy/SSController.cs index d0e1d325..59e059de 100644 --- a/Netch/Controllers/EncryptedProxy/SSController.cs +++ b/Netch/Controllers/EncryptedProxy/SSController.cs @@ -6,6 +6,8 @@ namespace Netch.Controllers { public class SSController : EncryptedProxy { + private bool dllFlag = false; + public SSController() { Name = "Shadowsocks"; @@ -19,6 +21,7 @@ namespace Netch.Controllers //从DLL启动Shaowsocks if (Global.Settings.BootShadowsocksFromDLL && (mode.Type == 0 || mode.Type == 1 || mode.Type == 2)) { + dllFlag = true; State = State.Starting; var client = Encoding.UTF8.GetBytes($"{LocalAddress}:{Socks5LocalPort}"); var remote = Encoding.UTF8.GetBytes($"{server.Hostname}:{server.Port}"); @@ -48,7 +51,8 @@ namespace Netch.Controllers #region Argument var argument = new StringBuilder(); - argument.Append($"-s {server.Hostname} -p {server.Port} -b {LocalAddress} -l {Socks5LocalPort} -m {server.EncryptMethod} -k \"{server.Password}\" -u"); + argument.Append( + $"-s {server.Hostname} -p {server.Port} -b {LocalAddress} -l {Socks5LocalPort} -m {server.EncryptMethod} -k \"{server.Password}\" -u"); if (!string.IsNullOrWhiteSpace(server.Plugin) && !string.IsNullOrWhiteSpace(server.PluginOption)) argument.Append($" --plugin {server.Plugin} --plugin-opts \"{server.PluginOption}\""); if (mode.BypassChina) argument.Append(" --acl default.acl"); @@ -63,7 +67,7 @@ namespace Netch.Controllers /// public override void Stop() { - if (Global.Settings.BootShadowsocksFromDLL) NativeMethods.Shadowsocks.Stop(); + if (dllFlag) NativeMethods.Shadowsocks.Stop(); else StopInstance(); } diff --git a/Netch/Controllers/Interface/Instance.cs b/Netch/Controllers/Interface/Instance.cs index 6398c130..c8f70ca7 100644 --- a/Netch/Controllers/Interface/Instance.cs +++ b/Netch/Controllers/Interface/Instance.cs @@ -169,7 +169,8 @@ namespace Netch.Controllers { SaveBufferTimer.Enabled = false; } - SaveBufferTimerEvent(null,null); + + SaveBufferTimerEvent(null, null); State = State.Stopped; } @@ -207,8 +208,11 @@ namespace Netch.Controllers { try { - File.AppendAllText(_logPath, _logBuffer.ToString()); - _logBuffer.Clear(); + if (_logPath != null && _logBuffer != null) + { + File.AppendAllText(_logPath, _logBuffer.ToString()); + _logBuffer.Clear(); + } } catch (Exception exception) { diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index f96a843f..f64b0bba 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -18,6 +18,7 @@ namespace Netch.Forms private async void ControlFun() { + Configuration.Save(); if (State == State.Waiting || State == State.Stopped) { // 服务器、模式 需选择 diff --git a/Netch/Forms/MainForm.Status.cs b/Netch/Forms/MainForm.Status.cs index 49e5b1da..d51807f9 100644 --- a/Netch/Forms/MainForm.Status.cs +++ b/Netch/Forms/MainForm.Status.cs @@ -88,6 +88,7 @@ namespace Netch.Forms LastUploadBandwidth = 0; LastDownloadBandwidth = 0; + Bandwidth.Stop(); ProfileGroupBox.Enabled = true; StartDisableItems(true); diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index 503d5d11..72c68234 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -14,6 +14,7 @@ namespace Netch.Utils public static class Bandwidth { public static int received; + public static TraceEventSession tSession; /// /// 计算流量 @@ -39,6 +40,35 @@ namespace Netch.Utils return string.Format("{0} {1}", Math.Round(result, 2), units[i]); } + public static string ToByteSize(long size) + { + var mStrSize = @"0"; + const double step = 1024.00; + var factSize = size; + if (factSize < step) + { + mStrSize = $@"{factSize:0.##} B"; + } + else if (factSize >= step && factSize < 1048576) + { + mStrSize = $@"{factSize / step:0.##} KB"; + } + else if (factSize >= 1048576 && factSize < 1073741824) + { + mStrSize = $@"{factSize / step / step:0.##} MB"; + } + else if (factSize >= 1073741824 && factSize < 1099511627776) + { + mStrSize = $@"{factSize / step / step / step:0.##} GB"; + } + else if (factSize >= 1099511627776) + { + mStrSize = $@"{factSize / step / step / step / step:0.##} TB"; + } + + return mStrSize; + } + /// /// 根据程序名统计流量 /// @@ -54,7 +84,8 @@ namespace Netch.Utils { instances.Add(((HTTPController) mainController.pModeController).pPrivoxyController.Instance); } - else if (server.Type.Equals("SS") && Global.Settings.BootShadowsocksFromDLL) + else if (server.Type.Equals("SS") && Global.Settings.BootShadowsocksFromDLL && + (mode.Type == 0 || mode.Type == 1 || mode.Type == 2)) { instances.Add(Process.GetCurrentProcess()); } @@ -69,53 +100,53 @@ namespace Netch.Utils var processList = instances.Select(instance => instance.Id).ToList(); - Logging.Info("流量统计进程:" + string.Join(",", instances.Select(instance => $"({instance.Id})"+instance.ProcessName).ToArray())); + Logging.Info("流量统计进程:" + string.Join(",", + instances.Select(instance => $"({instance.Id})" + instance.ProcessName).ToArray())); Task.Run(() => { - using (var session = new TraceEventSession("KernelAndClrEventsSession")) + tSession = new TraceEventSession("KernelAndClrEventsSession"); + tSession.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP); + + //这玩意儿上传和下载得到的data是一样的:) + //所以暂时没办法区分上传下载流量 + tSession.Source.Kernel.TcpIpRecv += data => { - session.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP); - - //这玩意儿上传和下载得到的data是一样的:) - //所以暂时没办法区分上传下载流量 - session.Source.Kernel.TcpIpRecv += data => + if (processList.Contains(data.ProcessID)) { - if (processList.Contains(data.ProcessID)) - { - lock (counterLock) - received += data.size; - //Logging.Info($"TcpIpRecv: {Compute(data.size)}"); - } - }; - session.Source.Kernel.UdpIpRecv += data => - { - if (processList.Contains(data.ProcessID)) - { - lock (counterLock) - received += data.size; - //Logging.Info($"UdpIpRecv: {Compute(data.size)}"); - } - }; + lock (counterLock) + received += data.size; - session.Source.Process(); - } + // Debug.WriteLine($"TcpIpRecv: {ToByteSize(data.size)}"); + } + }; + tSession.Source.Kernel.UdpIpRecv += data => + { + if (processList.Contains(data.ProcessID)) + { + lock (counterLock) + received += data.size; + + // Debug.WriteLine($"UdpIpRecv: {ToByteSize(data.size)}"); + } + }; + tSession.Source.Process(); }); - if ((Convert.ToInt32(Global.MainForm.LastDownloadBandwidth) - Convert.ToInt32(received)) == 0) - { - Global.MainForm.OnBandwidthUpdated(0); - received = 0; - } - while (Global.MainForm.State != State.Stopped) { Task.Delay(1000).Wait(); lock (counterLock) { - Global.MainForm.OnBandwidthUpdated(Convert.ToInt64(received)); + Global.MainForm.OnBandwidthUpdated(received); } } } + + public static void Stop() + { + if (tSession != null) tSession.Dispose(); + received = 0; + } } } \ No newline at end of file