mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
增加版本号的检查
This commit is contained in:
parent
1b672122a3
commit
0d840499dd
|
@ -16,6 +16,7 @@ using System.Net.WebSockets;
|
|||
using System.Text.Json;
|
||||
using FastTunnel.Core.Protocol;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using FastTunnel.Core.Utilitys;
|
||||
|
||||
namespace FastTunnel.Core.Client
|
||||
{
|
||||
|
@ -79,8 +80,7 @@ namespace FastTunnel.Core.Client
|
|||
// 连接到的目标IP
|
||||
socket = new ClientWebSocket();
|
||||
socket.Options.RemoteCertificateValidationCallback = delegate { return true; };
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_FLAG, "2.0.0");
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_TYPE, FastTunnelConst.TYPE_CLIENT);
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_VERSION, AssemblyUtility.GetVersion().ToString());
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_TOKEN, ClientConfig.Token);
|
||||
|
||||
_logger.LogInformation($"正在连接服务端 {Server.ServerAddr}:{Server.ServerPort}");
|
||||
|
@ -110,7 +110,7 @@ namespace FastTunnel.Core.Client
|
|||
|
||||
private async Task ReceiveServerAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
byte[] buffer = new byte[FastTunnelConst.CMD_MAX_LENGTH];
|
||||
byte[] buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH];
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var res = await socket.ReceiveAsync(buffer, cancellationToken);
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace FastTunnel.Core.Extensions
|
|||
public static async Task SendCmdAsync(this WebSocket socket, MessageType type, string content, CancellationToken cancellationToken)
|
||||
{
|
||||
var buffer = Encoding.UTF8.GetBytes($"{(char)type}{content}\n");
|
||||
if (type != MessageType.LogIn && buffer.Length > FastTunnelConst.CMD_MAX_LENGTH)
|
||||
if (type != MessageType.LogIn && buffer.Length > FastTunnelConst.MAX_CMD_LENGTH)
|
||||
throw new ArgumentOutOfRangeException(nameof(content));
|
||||
|
||||
await socket.SendAsync(buffer, WebSocketMessageType.Binary, false, cancellationToken);
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
<Compile Remove="Listener.cs" />
|
||||
<Compile Remove="Listener\ClientListener.cs" />
|
||||
<Compile Remove="Listener\ClientListenerV2.cs" />
|
||||
<Compile Remove="Listener\ForwardListener.cs" />
|
||||
<Compile Remove="Listener\HttpListener.cs" />
|
||||
<Compile Remove="Server.cs" />
|
||||
<Compile Remove="Sockets\AsyncSocketSwap.cs" />
|
||||
|
|
|
@ -8,17 +8,10 @@ namespace FastTunnel.Core
|
|||
{
|
||||
public class FastTunnelConst
|
||||
{
|
||||
public const string FASTTUNNEL_FLAG = "FASTTUNNEL_VERSION";
|
||||
public const string FASTTUNNEL_TYPE = "FASTTUNNEL_TYPE";
|
||||
public const string FASTTUNNEL_MSGID = "FASTTUNNEL_MSGID";
|
||||
public const string FASTTUNNEL_TOKEN = "FASTTUNNEL_TOKEN";
|
||||
|
||||
public const string TYPE_CLIENT = "CLIENT";
|
||||
|
||||
public const string TYPE_SWAP = "SWAP";
|
||||
|
||||
public const int CMD_MAX_LENGTH = 100;
|
||||
|
||||
public const string FASTTUNNEL_VERSION = "FT_VERSION";
|
||||
public const string FASTTUNNEL_MSGID = "FT_MSGID";
|
||||
public const string FASTTUNNEL_TOKEN = "FT_TOKEN";
|
||||
|
||||
public const int MAX_CMD_LENGTH = 100;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,35 +24,41 @@ namespace FastTunnel.Core.MiddleWares
|
|||
{
|
||||
ILogger<FastTunnelClientHandler> logger;
|
||||
FastTunnelServer fastTunnelServer;
|
||||
Version serverVersion;
|
||||
|
||||
public FastTunnelClientHandler(ILogger<FastTunnelClientHandler> logger, FastTunnelServer fastTunnelServer)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.fastTunnelServer = fastTunnelServer;
|
||||
|
||||
serverVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
||||
}
|
||||
|
||||
public async Task Handle(HttpContext context, Func<Task> next)
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest
|
||||
|| !context.Request.Headers.TryGetValue(FastTunnelConst.FASTTUNNEL_FLAG, out var version)
|
||||
|| !context.Request.Headers.TryGetValue(FastTunnelConst.FASTTUNNEL_TYPE, out var type))
|
||||
if (!context.WebSockets.IsWebSocketRequest || !context.Request.Headers.TryGetValue(FastTunnelConst.FASTTUNNEL_VERSION, out var version))
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
};
|
||||
|
||||
await handleClient(context, next);
|
||||
await handleClient(context, next, version);
|
||||
}
|
||||
|
||||
private async Task handleClient(HttpContext context, Func<Task> next)
|
||||
private async Task handleClient(HttpContext context, Func<Task> next, string clientVersion)
|
||||
{
|
||||
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
var tunnelClient = context.RequestServices.GetRequiredService<TunnelClient>().SetSocket(webSocket);
|
||||
|
||||
if (Version.Parse(clientVersion).Major != serverVersion.Major)
|
||||
{
|
||||
await Close(webSocket, $"客户端版本{clientVersion}与服务端版本{serverVersion}不兼容,请升级。");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!checkToken(context))
|
||||
{
|
||||
await webSocket.SendCmdAsync(MessageType.Log, "Token验证失败", CancellationToken.None);
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);
|
||||
await Close(webSocket, "Token验证失败");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -67,6 +73,13 @@ namespace FastTunnel.Core.MiddleWares
|
|||
}
|
||||
}
|
||||
|
||||
private static async Task Close(WebSocket webSocket, string reason)
|
||||
{
|
||||
await webSocket.SendCmdAsync(MessageType.Log, reason, CancellationToken.None);
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);
|
||||
return;
|
||||
}
|
||||
|
||||
private bool checkToken(HttpContext context)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fastTunnelServer.ServerOption.CurrentValue.Token))
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace FastTunnel.Core.Models
|
|||
|
||||
public async Task ReviceAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var buffer = new byte[FastTunnelConst.CMD_MAX_LENGTH];
|
||||
var buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH];
|
||||
var tunnelProtocol = new TunnelProtocol();
|
||||
|
||||
while (true)
|
||||
|
|
16
FastTunnel.Core/Utilitys/AssemblyUtility.cs
Normal file
16
FastTunnel.Core/Utilitys/AssemblyUtility.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Utilitys
|
||||
{
|
||||
public static class AssemblyUtility
|
||||
{
|
||||
public static Version GetVersion()
|
||||
{
|
||||
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user