mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-04 19:45:00 +03:00
csharp_style_namespace_declarations = file_scoped
This commit is contained in:
@@ -1,127 +1,126 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
|
||||
namespace ServiceLib.Services.Statistics
|
||||
namespace ServiceLib.Services.Statistics;
|
||||
|
||||
public class StatisticsSingboxService
|
||||
{
|
||||
public class StatisticsSingboxService
|
||||
private readonly Config _config;
|
||||
private bool _exitFlag;
|
||||
private ClientWebSocket? webSocket;
|
||||
private Action<ServerSpeedItem>? _updateFunc;
|
||||
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
|
||||
private static readonly string _tag = "StatisticsSingboxService";
|
||||
|
||||
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
|
||||
{
|
||||
private readonly Config _config;
|
||||
private bool _exitFlag;
|
||||
private ClientWebSocket? webSocket;
|
||||
private Action<ServerSpeedItem>? _updateFunc;
|
||||
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
|
||||
private static readonly string _tag = "StatisticsSingboxService";
|
||||
_config = config;
|
||||
_updateFunc = updateFunc;
|
||||
_exitFlag = false;
|
||||
|
||||
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
|
||||
_ = Task.Run(Run);
|
||||
}
|
||||
|
||||
private async Task Init()
|
||||
{
|
||||
await Task.Delay(5000);
|
||||
|
||||
try
|
||||
{
|
||||
_config = config;
|
||||
_updateFunc = updateFunc;
|
||||
_exitFlag = false;
|
||||
|
||||
_ = Task.Run(Run);
|
||||
}
|
||||
|
||||
private async Task Init()
|
||||
{
|
||||
await Task.Delay(5000);
|
||||
|
||||
try
|
||||
if (webSocket == null)
|
||||
{
|
||||
if (webSocket == null)
|
||||
{
|
||||
webSocket = new ClientWebSocket();
|
||||
await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None);
|
||||
}
|
||||
webSocket = new ClientWebSocket();
|
||||
await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void Close()
|
||||
public void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
_exitFlag = true;
|
||||
if (webSocket != null)
|
||||
{
|
||||
webSocket.Abort();
|
||||
webSocket = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Run()
|
||||
{
|
||||
await Init();
|
||||
|
||||
while (!_exitFlag)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
try
|
||||
{
|
||||
_exitFlag = true;
|
||||
if (!_config.IsRunningCore(ECoreType.sing_box))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (webSocket != null)
|
||||
{
|
||||
webSocket.Abort();
|
||||
webSocket = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
if (webSocket.State is WebSocketState.Aborted or WebSocketState.Closed)
|
||||
{
|
||||
webSocket.Abort();
|
||||
webSocket = null;
|
||||
await Init();
|
||||
continue;
|
||||
}
|
||||
|
||||
private async Task Run()
|
||||
{
|
||||
await Init();
|
||||
|
||||
while (!_exitFlag)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
try
|
||||
{
|
||||
if (!_config.IsRunningCore(ECoreType.sing_box))
|
||||
if (webSocket.State != WebSocketState.Open)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (webSocket != null)
|
||||
|
||||
var buffer = new byte[1024];
|
||||
var res = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
while (!res.CloseStatus.HasValue)
|
||||
{
|
||||
if (webSocket.State is WebSocketState.Aborted or WebSocketState.Closed)
|
||||
var result = Encoding.UTF8.GetString(buffer, 0, res.Count);
|
||||
if (result.IsNotEmpty())
|
||||
{
|
||||
webSocket.Abort();
|
||||
webSocket = null;
|
||||
await Init();
|
||||
continue;
|
||||
}
|
||||
ParseOutput(result, out var up, out var down);
|
||||
|
||||
if (webSocket.State != WebSocketState.Open)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var buffer = new byte[1024];
|
||||
var res = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
while (!res.CloseStatus.HasValue)
|
||||
{
|
||||
var result = Encoding.UTF8.GetString(buffer, 0, res.Count);
|
||||
if (result.IsNotEmpty())
|
||||
_updateFunc?.Invoke(new ServerSpeedItem()
|
||||
{
|
||||
ParseOutput(result, out var up, out var down);
|
||||
|
||||
_updateFunc?.Invoke(new ServerSpeedItem()
|
||||
{
|
||||
ProxyUp = (long)(up / 1000),
|
||||
ProxyDown = (long)(down / 1000)
|
||||
});
|
||||
}
|
||||
res = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
ProxyUp = (long)(up / 1000),
|
||||
ProxyDown = (long)(down / 1000)
|
||||
});
|
||||
}
|
||||
res = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseOutput(string source, out ulong up, out ulong down)
|
||||
{
|
||||
up = 0;
|
||||
down = 0;
|
||||
try
|
||||
{
|
||||
var trafficItem = JsonUtils.Deserialize<TrafficItem>(source);
|
||||
if (trafficItem != null)
|
||||
{
|
||||
up = trafficItem.Up;
|
||||
down = trafficItem.Down;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseOutput(string source, out ulong up, out ulong down)
|
||||
{
|
||||
up = 0;
|
||||
down = 0;
|
||||
try
|
||||
{
|
||||
var trafficItem = JsonUtils.Deserialize<TrafficItem>(source);
|
||||
if (trafficItem != null)
|
||||
{
|
||||
up = trafficItem.Up;
|
||||
down = trafficItem.Down;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user