流量统计

This commit is contained in:
snltty
2024-09-28 14:56:28 +08:00
parent 7417278254
commit 9403b84f7c
17 changed files with 81 additions and 39 deletions

View File

@@ -37,7 +37,7 @@ jobs:
release_name: v1.4.5.${{ steps.date.outputs.today }}
draft: false
prerelease: false
body: "1. 订阅消费,按需同步数据,减少流量消费\r\n2. 更详细的流量统计信息"
body: "1. 订阅消费,按需同步数据,减少流量消费\r\n2. 更详细的流量统计信息\r\n3. 修复路由表失效问题"
- name: upload-win-x86-oss
id: upload-win-x86-oss
uses: tvrcgo/oss-action@v0.1.1

View File

@@ -6,37 +6,77 @@ namespace linker.libs
{
private long ticks = Environment.TickCount64;
/// <summary>
/// 当前值
/// </summary>
public long Value => ticks;
public void Update()
{
ticks = Environment.TickCount64;
}
public bool Less(long ms)
/// <summary>
/// 差小于等于
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
public bool DiffLessEqual(long ms)
{
return Environment.TickCount64 - ticks <= ms;
}
public bool Greater(long ms)
/// <summary>
/// 差大于
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
public bool DiffGreater(long ms)
{
return Environment.TickCount64 - ticks > ms;
}
/// <summary>
/// 等于
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
public bool Equal(long ms)
{
return ticks == ms;
}
public bool NotEqual(long ms)
{
return ticks != ms;
}
/// <summary>
/// 差值
/// </summary>
/// <returns></returns>
public long Diff()
{
return Environment.TickCount64 - ticks;
}
public bool Timeout(long ms)
/// <summary>
/// 超时
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
public bool Expired(long ms)
{
return ticks == 0 || Environment.TickCount64 - ticks > ms;
}
/// <summary>
/// 不等于
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
public bool HasValue()
{
return ticks != 0;
}
/// <summary>
/// 更新值
/// </summary>
public void Update()
{
ticks = Environment.TickCount64;
}
/// <summary>
/// 清除值
/// </summary>
public void Clear()
{
ticks = 0;

View File

@@ -32,7 +32,7 @@ namespace linker.tunnel.connection
public byte BufferSize { get; init; } = 3;
public bool Connected => Stream != null && Stream.CanWrite && LastTicks.NotEqual(0);
public bool Connected => Stream != null && Stream.CanWrite && LastTicks.HasValue();
public int Delay { get; private set; }
public long SendBytes { get; private set; }
public long ReceiveBytes { get; private set; }
@@ -179,7 +179,7 @@ namespace linker.tunnel.connection
{
while (cancellationTokenSource.IsCancellationRequested == false)
{
if (LastTicks.Greater(3000))
if (LastTicks.DiffGreater(3000))
{
pingTicks.Update();
await SendPingPong(pingBytes).ConfigureAwait(false);

View File

@@ -27,7 +27,7 @@ namespace linker.tunnel.connection
public IPEndPoint IPEndPoint { get; init; }
public bool SSL { get; init; }
public byte BufferSize { get; init; } = 3;
public bool Connected => Socket != null && LastTicks.Timeout(15000) == false;
public bool Connected => Socket != null && LastTicks.Expired(15000) == false;
public int Delay { get; private set; }
public long SendBytes { get; private set; }
public long ReceiveBytes { get; private set; }
@@ -192,7 +192,7 @@ namespace linker.tunnel.connection
break;
}
if (LastTicks.Greater(3000))
if (LastTicks.DiffGreater(3000))
{
pingTicks.Update();
await SendPingPong(pingBytes).ConfigureAwait(false);

View File

@@ -27,7 +27,7 @@ namespace linker.tunnel.connection
public bool SSL { get; init; }
public byte BufferSize { get; init; } = 3;
public bool Connected => UdpClient != null && LastTicks.Timeout(15000) == false;
public bool Connected => UdpClient != null && LastTicks.Expired(15000) == false;
public int Delay { get; private set; }
public long SendBytes { get; private set; }
public long ReceiveBytes { get; private set; }
@@ -178,7 +178,7 @@ namespace linker.tunnel.connection
break;
}
if (LastTicks.Greater(3000))
if (LastTicks.DiffGreater(3000))
{
pingTicks.Update();
await SendPingPong(pingBytes).ConfigureAwait(false);

View File

@@ -125,7 +125,7 @@ namespace linker.tunnel.transport
}
}
}
catch (Exception ex)
catch (Exception)
{
socket.SafeClose();
break;
@@ -351,7 +351,7 @@ namespace linker.tunnel.transport
{
TimerHelper.SetInterval(() =>
{
var keys = connectionsDic.Where(c => (c.Value.Connection == null && c.Value.LastTicks.Greater(5000)) || (c.Value.Connection != null && c.Value.Connection.Connected == false)).Select(c => c.Key).ToList();
var keys = connectionsDic.Where(c => (c.Value.Connection == null && c.Value.LastTicks.DiffGreater(5000)) || (c.Value.Connection != null && c.Value.Connection.Connected == false)).Select(c => c.Key).ToList();
foreach (var item in keys)
{
connectionsDic.TryRemove(item, out _);

View File

@@ -20,7 +20,8 @@
<Authors>snltty</Authors>
<Company>snltty</Company>
<Description>1. 订阅消费,按需同步数据,减少流量消费
2. 更详细的流量统计信息</Description>
2. 更详细的流量统计信息
3. 修复路由表失效问题</Description>
<Copyright>snltty</Copyright>
<PackageProjectUrl>https://github.com/snltty/linker</PackageProjectUrl>
<RepositoryUrl>https://github.com/snltty/linker</RepositoryUrl>

View File

@@ -1,5 +1,4 @@
using linker.libs;
using MemoryPack;
using MemoryPack;
using System.Text.Json.Serialization;
namespace linker.plugins.flow

View File

@@ -385,7 +385,7 @@ namespace linker.plugins.forward.proxy
public byte[] Buffer { get; set; }
public LastTicksManager LastTicks { get; set; } = new LastTicksManager();
public bool Timeout => LastTicks.Timeout(60 * 60 * 1000);
public bool Timeout => LastTicks.Expired(60 * 1000);
public void Clear()
{
TargetSocket?.SafeClose();

View File

@@ -246,7 +246,7 @@ namespace linker.plugins.messenger
}
public override bool Connected => SourceSocket != null && lastTicks.Timeout(15000) == false;
public override bool Connected => SourceSocket != null && lastTicks.Expired(15000) == false;
private IConnectionReceiveCallback callback;
@@ -257,7 +257,7 @@ namespace linker.plugins.messenger
private ReceiveDataBuffer bufferCache = new ReceiveDataBuffer();
private LastTicksManager lastTicks = new LastTicksManager();
private LastTicksManager pingTicks =new LastTicksManager();
private LastTicksManager pingTicks = new LastTicksManager();
private static byte[] pingBytes = Encoding.UTF8.GetBytes($"{Helper.GlobalString}.tcp.ping");
private static byte[] pongBytes = Encoding.UTF8.GetBytes($"{Helper.GlobalString}.tcp.pong");
private bool pong = true;
@@ -384,9 +384,9 @@ namespace linker.plugins.messenger
{
while (cancellationTokenSource.IsCancellationRequested == false)
{
if (lastTicks.Greater(3000))
if (lastTicks.DiffGreater(3000))
{
pingTicks .Update();
pingTicks.Update();
await SendPingPong(pingBytes).ConfigureAwait(false);
}

View File

@@ -19,7 +19,7 @@ namespace linker.plugins.relay
{
TimerHelper.SetInterval(() =>
{
if (lastTicksManager.Less(5000))
if (lastTicksManager.DiffLessEqual(5000))
{
foreach (var item in flows.Values)
{

View File

@@ -19,7 +19,7 @@ namespace linker.plugins.sforward.proxy
{
TimerHelper.SetInterval(() =>
{
if (lastTicksManager.Less(5000))
if (lastTicksManager.DiffLessEqual(5000))
{
foreach (var item in flows.Values)
{

View File

@@ -292,7 +292,7 @@ namespace linker.plugins.sforward.proxy
{
public IPEndPoint IPEndPoint { get; set; }
public LastTicksManager LastTicks { get; set; } = new LastTicksManager();
public bool Timeout => LastTicks.Greater(5 * 60 * 1000);
public bool Timeout => LastTicks.DiffGreater(5 * 60 * 1000);
}
public sealed class UdpConnectedCache
@@ -300,7 +300,7 @@ namespace linker.plugins.sforward.proxy
public Socket SourceSocket { get; set; }
public Socket TargetSocket { get; set; }
public LastTicksManager LastTicks { get; set; } = new LastTicksManager();
public bool Timeout => LastTicks.Greater(5 * 60 * 1000);
public bool Timeout => LastTicks.DiffGreater(5 * 60 * 1000);
public void Clear()
{

View File

@@ -10,6 +10,7 @@ using System.Buffers.Binary;
using linker.plugins.client;
using linker.plugins.tunnel;
using System.Buffers;
using linker.libs.extends;
namespace linker.plugins.tuntap
{

View File

@@ -446,7 +446,7 @@ namespace linker.plugins.tuntap
return infos
//自己的ip不要
.Where(c => c.IP.Equals(runningConfig.Data.Tuntap.IP) == false && c.LastTicks.Greater(0))
.Where(c => c.IP.Equals(runningConfig.Data.Tuntap.IP) == false && c.LastTicks.Value > 0)
.OrderByDescending(c => c.LastTicks.Value)
.Select(c =>
{
@@ -539,7 +539,7 @@ namespace linker.plugins.tuntap
{
TimerHelper.SetInterval(async () =>
{
if (lastTicksManager.Less(5000))
if (lastTicksManager.DiffLessEqual(5000))
{
await Ping();
}
@@ -547,7 +547,7 @@ namespace linker.plugins.tuntap
}, 3000);
TimerHelper.SetInterval(async () =>
{
if (lastTicksManager.Greater(15000))
if (lastTicksManager.DiffGreater(15000))
{
await Ping();
}

View File

@@ -87,7 +87,7 @@ namespace linker.plugins.updater
}
//距离上次订阅超过一分钟,需要立即更新一次
bool needUpdate = lastTicksManager.Greater(60 * 1000);
bool needUpdate = lastTicksManager.DiffGreater(60 * 1000);
lastTicksManager.Update();
@@ -104,7 +104,7 @@ namespace linker.plugins.updater
if (updateInfo.Updated)
{
updateInfo.MachineId = fileConfig.Data.Client.Id;
string[] machines = subscribes.Where(c => c.Value.Less(15000)).Select(c => c.Key).ToArray();
string[] machines = subscribes.Where(c => c.Value.DiffLessEqual(15000)).Select(c => c.Key).ToArray();
if (machines.Length > 0)
{
await messengerSender.SendOnly(new MessageRequestWrap

View File

@@ -1,4 +1,5 @@
v1.4.5
2024-09-28 13:31:54
2024-09-28 14:56:27
1. 订阅消费,按需同步数据,减少流量消费
2. 更详细的流量统计信息
2. 更详细的流量统计信息
3. 修复路由表失效问题