🐛修复优化流量统计

🐛启动时保存配置文件,防止直接关闭程序时服务器模式index丢失(debug用)
🐛日志输出空异常修复
This commit is contained in:
AmazingDM
2020-08-26 14:23:03 +08:00
parent c274360cc2
commit e68929a32a
6 changed files with 81 additions and 39 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/.vs
/packages
/packages
.idea/

View File

@@ -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
/// </summary>
public override void Stop()
{
if (Global.Settings.BootShadowsocksFromDLL) NativeMethods.Shadowsocks.Stop();
if (dllFlag) NativeMethods.Shadowsocks.Stop();
else
StopInstance();
}

View File

@@ -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)
{

View File

@@ -18,6 +18,7 @@ namespace Netch.Forms
private async void ControlFun()
{
Configuration.Save();
if (State == State.Waiting || State == State.Stopped)
{
// 服务器、模式 需选择

View File

@@ -88,6 +88,7 @@ namespace Netch.Forms
LastUploadBandwidth = 0;
LastDownloadBandwidth = 0;
Bandwidth.Stop();
ProfileGroupBox.Enabled = true;
StartDisableItems(true);

View File

@@ -14,6 +14,7 @@ namespace Netch.Utils
public static class Bandwidth
{
public static int received;
public static TraceEventSession tSession;
/// <summary>
/// 计算流量
@@ -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;
}
/// <summary>
/// 根据程序名统计流量
/// </summary>
@@ -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;
}
}
}