mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
移除多余的class
This commit is contained in:
parent
bce13c8820
commit
1f44bf3e4b
|
@ -1,9 +1,10 @@
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FastTunnel.Core.Extensions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FastTunnel.Core.Client;
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using FastTunnel.Core;
|
||||
|
||||
namespace FastTunnel.Client
|
||||
{
|
||||
|
|
|
@ -25,10 +25,8 @@ namespace FastTunnel.Core.Client
|
|||
protected ILogger<FastTunnelClient> _logger;
|
||||
public DateTime lastHeart;
|
||||
|
||||
ForwardHandler _newCustomerHandler;
|
||||
SwapHandler _newCustomerHandler;
|
||||
LogHandler _logHandler;
|
||||
ClientHeartHandler _clientHeartHandler;
|
||||
Message<LogInMassage> loginMsg;
|
||||
|
||||
public DefaultClientConfig ClientConfig { get; private set; }
|
||||
private readonly CancellationTokenSource cancellationTokenSource = new();
|
||||
|
@ -37,15 +35,13 @@ namespace FastTunnel.Core.Client
|
|||
|
||||
public FastTunnelClient(
|
||||
ILogger<FastTunnelClient> logger,
|
||||
ForwardHandler newCustomerHandler,
|
||||
SwapHandler newCustomerHandler,
|
||||
LogHandler logHandler,
|
||||
IOptionsMonitor<DefaultClientConfig> configuration,
|
||||
ClientHeartHandler clientHeartHandler)
|
||||
IOptionsMonitor<DefaultClientConfig> configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_newCustomerHandler = newCustomerHandler;
|
||||
_logHandler = logHandler;
|
||||
_clientHeartHandler = clientHeartHandler;
|
||||
ClientConfig = configuration.CurrentValue;
|
||||
}
|
||||
|
||||
|
@ -74,8 +70,8 @@ namespace FastTunnel.Core.Client
|
|||
// 连接到的目标IP
|
||||
socket = new ClientWebSocket();
|
||||
socket.Options.RemoteCertificateValidationCallback = delegate { return true; };
|
||||
socket.Options.SetRequestHeader(HeaderConst.FASTTUNNEL_FLAG, "2.0.0");
|
||||
socket.Options.SetRequestHeader(HeaderConst.FASTTUNNEL_TYPE, HeaderConst.TYPE_CLIENT);
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_FLAG, "2.0.0");
|
||||
socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_TYPE, FastTunnelConst.TYPE_CLIENT);
|
||||
|
||||
await socket.ConnectAsync(
|
||||
new Uri($"ws://{ClientConfig.Server.ServerAddr}:{ClientConfig.Server.ServerPort}"), cancellationToken);
|
||||
|
|
|
@ -24,44 +24,13 @@ namespace FastTunnel.Core.Client
|
|||
public ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>> ForwardList { get; private set; }
|
||||
= new ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>>();
|
||||
|
||||
readonly ILogger _logger;
|
||||
public readonly IOptionsMonitor<DefaultServerConfig> serverOption;
|
||||
public IProxyConfigProvider proxyConfig;
|
||||
|
||||
public FastTunnelServer(ILogger<FastTunnelServer> logger, IProxyConfigProvider proxyConfig, IOptionsMonitor<DefaultServerConfig> serverSettings)
|
||||
public FastTunnelServer(IProxyConfigProvider proxyConfig, IOptionsMonitor<DefaultServerConfig> serverSettings)
|
||||
{
|
||||
_logger = logger;
|
||||
serverOption = serverSettings;
|
||||
this.proxyConfig = proxyConfig;
|
||||
|
||||
// clientListener = new ClientListenerV2(this, proxyConfig, "0.0.0.0", serverOption.CurrentValue.BindPort, _logger);
|
||||
// http_listener = new HttpListenerV2("0.0.0.0", serverOption.CurrentValue.WebProxyPort, _logger);
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Server Starting =====");
|
||||
|
||||
// listenClient();
|
||||
// listenHttp();
|
||||
}
|
||||
|
||||
//private void listenClient()
|
||||
//{
|
||||
// clientListener.Start();
|
||||
//}
|
||||
|
||||
//private void listenHttp()
|
||||
//{
|
||||
// http_listener.Start(new HttpDispatcherV2(this, _logger, serverOption));
|
||||
//}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Server Stoping =====");
|
||||
|
||||
//clientListener.Stop();
|
||||
//http_listener.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,6 @@ namespace FastTunnel.Core.Config
|
|||
{
|
||||
public class DefaultServerConfig : IServerConfig
|
||||
{
|
||||
// public int BindPort { get; set; }
|
||||
|
||||
public string WebDomain { get; set; }
|
||||
|
||||
public int WebProxyPort { get; set; } = 1270;
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Filters;
|
||||
using FastTunnel.Core.Forwarder.MiddleWare;
|
||||
using FastTunnel.Core.Forwarder;
|
||||
using FastTunnel.Core.Handlers;
|
||||
using FastTunnel.Core.Handlers.Client;
|
||||
using FastTunnel.Core.MiddleWares;
|
||||
using FastTunnel.Core.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Yarp.ReverseProxy.Forwarder;
|
||||
using Yarp.Sample;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace FastTunnel.Core.Extensions
|
||||
namespace FastTunnel.Core
|
||||
{
|
||||
public static class ServicesExtensions
|
||||
{
|
||||
|
@ -17,12 +23,16 @@ namespace FastTunnel.Core.Extensions
|
|||
/// <param name="services"></param>
|
||||
public static void AddFastTunnelServer(this IServiceCollection services, IConfigurationSection configurationSection)
|
||||
{
|
||||
services.AddReverseProxy().LoadFromMemory();
|
||||
|
||||
services.Configure<DefaultServerConfig>(configurationSection);
|
||||
|
||||
services.AddSingleton<IAuthenticationFilter, DefaultAuthenticationFilter>();
|
||||
services.AddSingleton<FastTunnelServer, FastTunnelServer>();
|
||||
|
||||
services.AddHostedService<ServiceFastTunnelServer>();
|
||||
services.AddSingleton<IForwarderHttpClientFactory, FastTunnelForwarderHttpClientFactory>();
|
||||
services.AddSingleton<FastTunnelClientHandler, FastTunnelClientHandler>();
|
||||
services.AddSingleton<FastTunnelSwapHandler, FastTunnelSwapHandler>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -36,10 +46,19 @@ namespace FastTunnel.Core.Extensions
|
|||
services.AddSingleton<IFastTunnelClient, FastTunnelClient>()
|
||||
.AddSingleton<ClientHeartHandler>()
|
||||
.AddSingleton<LogHandler>()
|
||||
.AddSingleton<ForwardHandler>()
|
||||
.AddSingleton<ForwardHandler>();
|
||||
.AddSingleton<SwapHandler>()
|
||||
.AddSingleton<SwapHandler>();
|
||||
|
||||
services.AddHostedService<ServiceFastTunnelClient>();
|
||||
}
|
||||
|
||||
public static void UseFastTunnel(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseWebSockets();
|
||||
var swapHandler = app.ApplicationServices.GetRequiredService<FastTunnelSwapHandler>();
|
||||
var clientHandler = app.ApplicationServices.GetRequiredService<FastTunnelClientHandler>();
|
||||
app.Use(clientHandler.Handle);
|
||||
app.Use(swapHandler.Handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace FastTunnel.Core
|
||||
{
|
||||
public class HeaderConst
|
||||
public class FastTunnelConst
|
||||
{
|
||||
public const string FASTTUNNEL_FLAG = "FASTTUNNEL_VERSION";
|
||||
public const string FASTTUNNEL_TYPE = "FASTTUNNEL_TYPE";
|
|
@ -1,15 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Forwarder
|
||||
{
|
||||
public interface IReadWriteStream
|
||||
{
|
||||
int Read(byte[] buffer);
|
||||
|
||||
void Write(byte[] buffer, int index, int num);
|
||||
}
|
||||
}
|
|
@ -29,8 +29,8 @@ namespace FastTunnel.Core.MiddleWares
|
|||
public async Task Handle(HttpContext context, Func<Task> next)
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest
|
||||
|| !context.Request.Headers.TryGetValue(HeaderConst.FASTTUNNEL_FLAG, out var version)
|
||||
|| !context.Request.Headers.TryGetValue(HeaderConst.FASTTUNNEL_TYPE, out var type))
|
||||
|| !context.Request.Headers.TryGetValue(FastTunnelConst.FASTTUNNEL_FLAG, out var version)
|
||||
|| !context.Request.Headers.TryGetValue(FastTunnelConst.FASTTUNNEL_TYPE, out var type))
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Forwarder
|
||||
{
|
||||
public class SocketReadWriteStream : IReadWriteStream
|
||||
{
|
||||
Socket socket;
|
||||
public SocketReadWriteStream(Socket socket)
|
||||
{
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public int Read(byte[] buffer)
|
||||
{
|
||||
return socket.Receive(buffer);
|
||||
}
|
||||
|
||||
public void Write(byte[] buffer, int index, int num)
|
||||
{
|
||||
socket.Send(buffer, index, num, SocketFlags.None);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Forwarder
|
||||
{
|
||||
public class WebSocktReadWriteStream : IReadWriteStream
|
||||
{
|
||||
WebSocket webSocket;
|
||||
public WebSocktReadWriteStream(WebSocket webSocket)
|
||||
{
|
||||
this.webSocket = webSocket;
|
||||
}
|
||||
|
||||
public int Read(byte[] buffer)
|
||||
{
|
||||
if (this.webSocket.CloseStatus.HasValue)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return webSocket.ReceiveAsync(buffer, CancellationToken.None).GetAwaiter().GetResult().Count;
|
||||
}
|
||||
|
||||
public void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
this.webSocket.SendAsync(new ArraySegment<byte>(buffer, offset, count), WebSocketMessageType.Binary, true, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,11 +19,11 @@ using System.Data.Common;
|
|||
|
||||
namespace FastTunnel.Core.Handlers.Client
|
||||
{
|
||||
public class ForwardHandler : IClientHandler
|
||||
public class SwapHandler : IClientHandler
|
||||
{
|
||||
ILogger<ForwardHandler> _logger;
|
||||
ILogger<SwapHandler> _logger;
|
||||
|
||||
public ForwardHandler(ILogger<ForwardHandler> logger)
|
||||
public SwapHandler(ILogger<SwapHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
|
@ -11,10 +11,8 @@ namespace FastTunnel.Core.Handlers
|
|||
{
|
||||
public interface IClientMessageHandler
|
||||
{
|
||||
Boolean NeedRecive { get; }
|
||||
bool NeedRecive { get; }
|
||||
|
||||
Task<bool> HandlerMsg<T>(FastTunnelServer server, WebSocket client, T msg) where T : TunnelMassage;
|
||||
|
||||
//void HandlerMsg(FastTunnelServer server, Socket client, Message<JObject> msg);
|
||||
}
|
||||
}
|
|
@ -24,7 +24,6 @@ namespace FastTunnel.Core.Handlers
|
|||
|
||||
public bool NeedRecive => true;
|
||||
|
||||
static object _locker = new object();
|
||||
IProxyConfigProvider proxyConfig;
|
||||
|
||||
public LoginHandler(ILogger logger, IProxyConfigProvider proxyConfig)
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
{
|
||||
public class LogMassage : TunnelMassage
|
||||
{
|
||||
public string Msg { get; set; }
|
||||
|
||||
public LogMsgType MsgType { get; set; }
|
||||
|
||||
public LogMassage(LogMsgType msgType, string msg)
|
||||
{
|
||||
this.Msg = msg;
|
||||
MsgType = msgType;
|
||||
}
|
||||
}
|
||||
|
||||
public enum LogMsgType
|
||||
{
|
||||
Info,
|
||||
|
||||
Error,
|
||||
|
||||
Debug
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
{
|
||||
public class NewCustomerMassage : TunnelMassage
|
||||
{
|
||||
public string MsgId { get; set; }
|
||||
|
||||
public WebConfig WebConfig { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
{
|
||||
public class NewForwardMessage : TunnelMassage
|
||||
{
|
||||
public string MsgId { get; set; }
|
||||
|
||||
public ForwardConfig SSHConfig { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
{
|
||||
public class SwapMassage : TunnelMassage
|
||||
{
|
||||
public string msgId { get; set; }
|
||||
|
||||
public SwapMassage(string msgId)
|
||||
{
|
||||
this.msgId = msgId;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Filters;
|
||||
using FastTunnel.Core.Global;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FastTunnel.Core.Models;
|
||||
|
||||
namespace FastTunnel.Core.Services
|
||||
{
|
||||
public class ServiceFastTunnelServer : IHostedService
|
||||
{
|
||||
readonly ILogger<ServiceFastTunnelServer> _logger;
|
||||
readonly IAuthenticationFilter _authenticationFilter;
|
||||
FastTunnelServer _server;
|
||||
|
||||
public ServiceFastTunnelServer(
|
||||
ILogger<ServiceFastTunnelServer> logger,
|
||||
FastTunnelServer fastTunnelServer,
|
||||
IAuthenticationFilter authenticationFilter)
|
||||
{
|
||||
_logger = logger;
|
||||
_authenticationFilter = authenticationFilter;
|
||||
_server = fastTunnelServer;
|
||||
//AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
|
||||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
||||
}
|
||||
|
||||
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogError("【UnhandledException】" + e.ExceptionObject);
|
||||
var type = e.ExceptionObject.GetType();
|
||||
_logger.LogError("ExceptionObject GetType " + type);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
|
||||
{
|
||||
if (e.Exception is DirectoryNotFoundException)
|
||||
{
|
||||
// nlog第一次找不到文件的错误,跳过
|
||||
}
|
||||
else if (e.Exception is PlatformNotSupportedException)
|
||||
{
|
||||
// log4net
|
||||
}
|
||||
else if (e.Exception is IOException && e.Exception.Source == "System.Net.Security")
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError(e.Exception, "【FirstChanceException】");
|
||||
}
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
FastTunnelGlobal.AddFilter(_authenticationFilter);
|
||||
|
||||
try
|
||||
{
|
||||
_server.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// NLog: catch any exception and log it.
|
||||
_logger.LogError(ex, "Server Error");
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_server.Stop();
|
||||
}, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public class ConnectSocket
|
||||
{
|
||||
string m_server;
|
||||
int m_port;
|
||||
|
||||
public ConnectSocket(string server, int port)
|
||||
{
|
||||
this.m_port = port;
|
||||
this.m_server = server;
|
||||
}
|
||||
|
||||
public Socket Connect()
|
||||
{
|
||||
Socket s = null;
|
||||
IPHostEntry hostEntry = null;
|
||||
|
||||
// Get host related information.
|
||||
hostEntry = Dns.GetHostEntry(m_server);
|
||||
|
||||
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
|
||||
// an exception that occurs when the host IP Address is not compatible with the address family
|
||||
// (typical in the IPv6 case).
|
||||
foreach (IPAddress address in hostEntry.AddressList)
|
||||
{
|
||||
IPEndPoint ipe = new IPEndPoint(address, m_port);
|
||||
Socket tempSocket =
|
||||
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
tempSocket.Connect(ipe);
|
||||
|
||||
if (tempSocket.Connected)
|
||||
{
|
||||
s = tempSocket;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,33 +25,10 @@ namespace FastTunnel.Core.Sockets
|
|||
Socket.NoDelay = true;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
DnsEndPoint dnsEndPoint = new DnsEndPoint(_host, _port);
|
||||
Socket.Connect(dnsEndPoint);
|
||||
}
|
||||
|
||||
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
DnsEndPoint dnsEndPoint = new DnsEndPoint(_host, _port);
|
||||
await Socket.ConnectAsync(dnsEndPoint);
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
Socket.Send(data);
|
||||
}
|
||||
|
||||
public void Send<T>(Message<T> msg)
|
||||
where T : TunnelMassage
|
||||
{
|
||||
Socket.SendCmd(msg);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Socket.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
using FastTunnel.Core.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public interface IFastTunnelClientSocket
|
||||
{
|
||||
Task<int> ReceiveAsync(byte[] buffer, CancellationToken cancellationToken);
|
||||
|
||||
Task SendAsync<T>(Message<T> loginMsg, CancellationToken cancellationToken)
|
||||
where T : TunnelMassage;
|
||||
|
||||
Task ConnectAsync(Uri url, CancellationToken cancellationToken);
|
||||
|
||||
Task CloseAsync();
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public interface ISocketSwap
|
||||
{
|
||||
/// <summary>
|
||||
/// 前置操作
|
||||
/// </summary>
|
||||
/// <param name="fun"></param>
|
||||
/// <returns></returns>
|
||||
ISocketSwap BeforeSwap(Action fun);
|
||||
|
||||
void StartSwap();
|
||||
}
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Sockets;
|
||||
using FastTunnel.Core.Forwarder;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public class ReadWriteStreamSwap
|
||||
{
|
||||
IReadWriteStream stream;
|
||||
IReadWriteStream stream1;
|
||||
ILogger logger;
|
||||
string msgId;
|
||||
|
||||
public ReadWriteStreamSwap(IReadWriteStream stream, IReadWriteStream stream1, ILogger logger, string msgId)
|
||||
{
|
||||
this.stream = stream;
|
||||
this.stream1 = stream1;
|
||||
|
||||
this.logger = logger;
|
||||
this.msgId = msgId;
|
||||
}
|
||||
|
||||
public async Task StartSwapAsync()
|
||||
{
|
||||
logger.LogDebug($"[StartSwapStart] {msgId}");
|
||||
var task = new Task(() =>
|
||||
{
|
||||
work(stream, stream1);
|
||||
});
|
||||
|
||||
var task1 = new Task(() =>
|
||||
{
|
||||
work(stream1, stream);
|
||||
});
|
||||
|
||||
await Task.WhenAll(task1, task);
|
||||
logger.LogDebug($"[StartSwapEnd] {msgId}");
|
||||
}
|
||||
|
||||
private void work(IReadWriteStream streamRevice, IReadWriteStream streamSend)
|
||||
{
|
||||
byte[] buffer = new byte[512];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int num;
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
num = streamRevice.Read(buffer);
|
||||
Console.WriteLine($"{Encoding.UTF8.GetString(buffer, 0, num)}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
close("Revice Fail");
|
||||
break;
|
||||
}
|
||||
|
||||
if (num == 0)
|
||||
{
|
||||
close("Normal Close");
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
streamSend.Write(buffer, 0, num);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
close("Send Fail");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogCritical(ex, "致命异常");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void close(string msg)
|
||||
{
|
||||
logger.LogError($"Sarp Error {msg}");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,158 +0,0 @@
|
|||
using FastTunnel.Core.Dispatchers;
|
||||
using FastTunnel.Core.Utility.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public class SocketSwap : ISocketSwap
|
||||
{
|
||||
private readonly Socket m_sockt1;
|
||||
private readonly Socket m_sockt2;
|
||||
private readonly string m_msgId = null;
|
||||
private readonly ILogger m_logger;
|
||||
|
||||
private bool swapeStarted = false;
|
||||
|
||||
private class Channel
|
||||
{
|
||||
public Socket Send { get; set; }
|
||||
|
||||
public Socket Receive { get; set; }
|
||||
}
|
||||
|
||||
public SocketSwap(Socket sockt1, Socket sockt2, ILogger logger, string msgId)
|
||||
{
|
||||
//sockt1.NoDelay = true;
|
||||
//sockt2.NoDelay = true;
|
||||
m_sockt1 = sockt1;
|
||||
m_sockt2 = sockt2;
|
||||
m_msgId = msgId;
|
||||
m_logger = logger;
|
||||
}
|
||||
|
||||
public void StartSwap()
|
||||
{
|
||||
m_logger?.LogDebug($"[StartSwapStart] {m_msgId}");
|
||||
swapeStarted = true;
|
||||
|
||||
ThreadPool.QueueUserWorkItem(swapCallback, new Channel
|
||||
{
|
||||
Send = m_sockt1,
|
||||
Receive = m_sockt2
|
||||
});
|
||||
|
||||
ThreadPool.QueueUserWorkItem(swapCallback, new Channel
|
||||
{
|
||||
Send = m_sockt2,
|
||||
Receive = m_sockt1
|
||||
});
|
||||
|
||||
m_logger?.LogDebug($"[StartSwapEnd] {m_msgId}");
|
||||
}
|
||||
|
||||
private void swapCallback(object state)
|
||||
{
|
||||
m_logger?.LogDebug($"swapCallback {m_msgId}");
|
||||
var chanel = state as Channel;
|
||||
byte[] result = new byte[512];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int num;
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
num = chanel.Receive.Receive(result, 0, result.Length, SocketFlags.None);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
closeSocket("Revice Fail");
|
||||
break;
|
||||
}
|
||||
|
||||
if (num == 0)
|
||||
{
|
||||
closeSocket("Normal Close");
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
chanel.Send.Send(result, 0, num, SocketFlags.None);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
closeSocket("Send Fail");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
m_logger.LogCritical(ex, "致命异常");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_msgId.Contains("_"))
|
||||
{
|
||||
var interval = long.Parse(DateTime.Now.GetChinaTicks()) - long.Parse(m_msgId.Split('_')[0]);
|
||||
m_logger?.LogDebug($"endSwap {m_msgId} 交互时常:{interval}ms");
|
||||
}
|
||||
}
|
||||
|
||||
private void closeSocket(string msg)
|
||||
{
|
||||
m_logger.LogDebug($"【closeSocket】:{msg}");
|
||||
|
||||
try
|
||||
{
|
||||
m_sockt1.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_sockt1.Close();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
m_sockt2.Shutdown(SocketShutdown.Both);
|
||||
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_sockt2.Close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ISocketSwap BeforeSwap(Action fun)
|
||||
{
|
||||
m_logger?.LogDebug($"BeforeSwap {m_msgId}");
|
||||
|
||||
if (swapeStarted)
|
||||
{
|
||||
throw new Exception("BeforeSwap must be invoked before StartSwap!");
|
||||
}
|
||||
|
||||
fun?.Invoke();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,98 +0,0 @@
|
|||
using FastTunnel.Core.Handlers.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Sockets
|
||||
{
|
||||
public class StreamSwap
|
||||
{
|
||||
private Stream stream1;
|
||||
private Stream stream2;
|
||||
private ILogger<ForwardHandler> logger;
|
||||
private string msgId;
|
||||
|
||||
public StreamSwap(Stream serverConnection, NetworkStream localConn, ILogger<ForwardHandler> logger, string msgId)
|
||||
{
|
||||
this.stream1 = serverConnection;
|
||||
this.stream2 = localConn;
|
||||
|
||||
this.logger = logger;
|
||||
this.msgId = msgId;
|
||||
}
|
||||
|
||||
public async Task StartSwapAsync()
|
||||
{
|
||||
logger.LogDebug($"[StartSwapStart] {msgId}");
|
||||
var task = new Task(() =>
|
||||
{
|
||||
work(stream1, stream2);
|
||||
});
|
||||
|
||||
var task1 = new Task(() =>
|
||||
{
|
||||
work(stream2, stream1);
|
||||
});
|
||||
|
||||
await Task.WhenAll(task1, task);
|
||||
|
||||
logger.LogDebug($"[StartSwapEnd] {msgId}");
|
||||
}
|
||||
|
||||
private void work(Stream streamRevice, Stream streamSend)
|
||||
{
|
||||
byte[] buffer = new byte[512];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int num;
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
num = streamRevice.Read(buffer);
|
||||
Console.WriteLine($"{Encoding.UTF8.GetString(buffer, 0, num)}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
close("Revice Fail");
|
||||
break;
|
||||
}
|
||||
|
||||
if (num == 0)
|
||||
{
|
||||
close("Normal Close");
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
streamSend.Write(buffer, 0, num);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
close("Send Fail");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogCritical(ex, "致命异常");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void close(string msg)
|
||||
{
|
||||
logger.LogError($"Sarp Error {msg}");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FastTunnel.Core;
|
||||
using FastTunnel.Core.Extensions;
|
||||
using FastTunnel.Core.Forwarder;
|
||||
using FastTunnel.Core.Forwarder.MiddleWare;
|
||||
|
@ -31,14 +32,6 @@ namespace FastTunnel.Server
|
|||
// -------------------FastTunnel START------------------
|
||||
services.AddFastTunnelServer(Configuration.GetSection("ServerSettings"));
|
||||
// -------------------FastTunnel END--------------------
|
||||
|
||||
// Add the reverse proxy to capability to the server
|
||||
services.AddReverseProxy().LoadFromMemory();
|
||||
|
||||
// ------------------------Custom Business------------------------------
|
||||
services.AddSingleton<IForwarderHttpClientFactory, FastTunnelForwarderHttpClientFactory>();
|
||||
services.AddSingleton<FastTunnelClientHandler, FastTunnelClientHandler>();
|
||||
services.AddSingleton<FastTunnelSwapHandler, FastTunnelSwapHandler>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
@ -47,13 +40,8 @@ namespace FastTunnel.Server
|
|||
if (env.IsDevelopment())
|
||||
{
|
||||
}
|
||||
|
||||
app.UseWebSockets();
|
||||
|
||||
var swapHandler = app.ApplicationServices.GetRequiredService<FastTunnelSwapHandler>();
|
||||
var clientHandler = app.ApplicationServices.GetRequiredService<FastTunnelClientHandler>();
|
||||
app.Use(clientHandler.Handle);
|
||||
app.Use(swapHandler.Handle);
|
||||
|
||||
app.UseFastTunnel();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user