mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
客户端在线计数
Swap增加超时取消等待
This commit is contained in:
parent
2a3a7323eb
commit
56bd3ecf50
|
@ -21,6 +21,8 @@ namespace FastTunnel.Core.Client
|
|||
|
||||
public ConcurrentDictionary<string, WebInfo> WebList { get; private set; } = new();
|
||||
|
||||
public int ConnectedClientCount = 0;
|
||||
|
||||
public ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>> ForwardList { get; private set; }
|
||||
= new ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>>();
|
||||
|
||||
|
|
22
FastTunnel.Core/Extensions/TaskCompletionSourceExtensions.cs
Normal file
22
FastTunnel.Core/Extensions/TaskCompletionSourceExtensions.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Extensions
|
||||
{
|
||||
public static class TaskCompletionSourceExtensions
|
||||
{
|
||||
public static void SetTimeOut<T>(this TaskCompletionSource<T> tcs, int timeoutMs, Action action)
|
||||
{
|
||||
var ct = new CancellationTokenSource(timeoutMs);
|
||||
ct.Token.Register(() =>
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
action.Invoke();
|
||||
}, useSynchronizationContext: false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,11 +63,14 @@ namespace FastTunnel.Core.Forwarder
|
|||
// 发送指令给客户端,等待建立隧道
|
||||
await web.Socket.SendCmdAsync(MessageType.SwapMsg, $"{RequestId}|{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}", cancellation);
|
||||
|
||||
// TODO:超时处理
|
||||
TaskCompletionSource<Stream> task = new(cancellation);
|
||||
_fastTunnelServer.ResponseTasks.TryAdd(RequestId, task);
|
||||
TaskCompletionSource<Stream> tcs = new(cancellation);
|
||||
tcs.SetTimeOut(10000, () =>
|
||||
{
|
||||
_logger.LogError($"客户端在指定时间内为建立Swap连接 {RequestId}|{host}=>{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}");
|
||||
});
|
||||
|
||||
var res = await task.Task;
|
||||
_fastTunnelServer.ResponseTasks.TryAdd(RequestId, tcs);
|
||||
var res = await tcs.Task;
|
||||
return res;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -64,12 +64,14 @@ namespace FastTunnel.Core.MiddleWares
|
|||
|
||||
try
|
||||
{
|
||||
logger.LogInformation($"客户端连接 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress}");
|
||||
Interlocked.Increment(ref fastTunnelServer.ConnectedClientCount);
|
||||
logger.LogInformation($"客户端连接 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress} 当前在线数:{fastTunnelServer.ConnectedClientCount}");
|
||||
await tunnelClient.ReviceAsync(CancellationToken.None);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
logger.LogInformation($"客户端关闭 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress}");
|
||||
Interlocked.Decrement(ref fastTunnelServer.ConnectedClientCount);
|
||||
logger.LogInformation($"客户端关闭 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress} 当前在线数:{fastTunnelServer.ConnectedClientCount}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,6 +38,11 @@ namespace FastTunnel.Core.Dispatchers
|
|||
|
||||
try
|
||||
{
|
||||
if (client.State == WebSocketState.Aborted)
|
||||
{
|
||||
logger.LogError("客户端已离线");
|
||||
return;
|
||||
}
|
||||
|
||||
await client.SendCmdAsync(MessageType.Forward, $"{msgid}|{_config.LocalIp}:{_config.LocalPort}", CancellationToken.None);
|
||||
}
|
||||
|
@ -57,7 +62,7 @@ namespace FastTunnel.Core.Dispatchers
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex);
|
||||
logger.LogError("Forward Swap Error:" + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
3
FastTunnel.Core/TunnelResource.Designer.cs
generated
3
FastTunnel.Core/TunnelResource.Designer.cs
generated
|
@ -110,7 +110,6 @@ namespace FastTunnel.Core {
|
|||
/// <summary>
|
||||
/// 查找类似 <!DOCTYPE html>
|
||||
///<html lang="en">
|
||||
///
|
||||
///<head>
|
||||
/// <meta charset="utf-8" />
|
||||
/// <meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
@ -128,7 +127,7 @@ namespace FastTunnel.Core {
|
|||
///
|
||||
/// .btn-primary {
|
||||
/// color: #fff;
|
||||
/// background-color [字符串的其余部分被截断]"; 的本地化字符串。
|
||||
/// background-color: [字符串的其余部分被截断]"; 的本地化字符串。
|
||||
/// </summary>
|
||||
internal static string Page_NoSite {
|
||||
get {
|
||||
|
|
|
@ -242,7 +242,6 @@
|
|||
<data name="Page_NoSite" xml:space="preserve">
|
||||
<value><!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
|
Loading…
Reference in New Issue
Block a user