diff --git a/FastTunnel.Client/FastTunnel.Client.csproj b/FastTunnel.Client/FastTunnel.Client.csproj index 0b2ddf2..a9d0752 100644 --- a/FastTunnel.Client/FastTunnel.Client.csproj +++ b/FastTunnel.Client/FastTunnel.Client.csproj @@ -13,7 +13,7 @@ - + diff --git a/FastTunnel.Client/Program.cs b/FastTunnel.Client/Program.cs index 4e8dff1..cf20d95 100644 --- a/FastTunnel.Client/Program.cs +++ b/FastTunnel.Client/Program.cs @@ -7,10 +7,9 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; -using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Serilog; -using FastTunnel.Core.Extensions; +using FastTunnel.Core.Client.Extensions; namespace FastTunnel.Client; diff --git a/FastTunnel.Core/Config/DefaultClientConfig.cs b/FastTunnel.Core.Client/DefaultClientConfig.cs similarity index 85% rename from FastTunnel.Core/Config/DefaultClientConfig.cs rename to FastTunnel.Core.Client/DefaultClientConfig.cs index c4b8022..5f7afa3 100644 --- a/FastTunnel.Core/Config/DefaultClientConfig.cs +++ b/FastTunnel.Core.Client/DefaultClientConfig.cs @@ -1,9 +1,10 @@ -// Licensed under the Apache License, Version 2.0 (the "License"). +// Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // You may obtain a copy of the License at // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using FastTunnel.Core.Client; using FastTunnel.Core.Models; using System.Collections.Generic; @@ -19,4 +20,4 @@ namespace FastTunnel.Core.Config public IEnumerable Forwards { get; set; } } -} \ No newline at end of file +} diff --git a/FastTunnel.Core.Client/Extensions/ServicesExtensions.cs b/FastTunnel.Core.Client/Extensions/ServicesExtensions.cs new file mode 100644 index 0000000..a9bf949 --- /dev/null +++ b/FastTunnel.Core.Client/Extensions/ServicesExtensions.cs @@ -0,0 +1,36 @@ +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE +// Copyright (c) 2019 Gui.H + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FastTunnel.Core.Config; +using FastTunnel.Core.Handlers.Client; +using FastTunnel.Core.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +namespace FastTunnel.Core.Client.Extensions; + +public static class ServicesExtensions +{ + /// + /// 客户端依赖及HostedService + /// + /// + public static void AddFastTunnelClient(this IServiceCollection services, IConfigurationSection configurationSection) + { + services.Configure(configurationSection); + + services.AddTransient() + .AddSingleton() + .AddSingleton(); + + services.AddHostedService(); + } + +} diff --git a/FastTunnel.Core/Extensions/SocketExtensions.cs b/FastTunnel.Core.Client/Extensions/SocketExtensions.cs similarity index 100% rename from FastTunnel.Core/Extensions/SocketExtensions.cs rename to FastTunnel.Core.Client/Extensions/SocketExtensions.cs diff --git a/FastTunnel.Core.Client/FastTunnel.Core.Client.csproj b/FastTunnel.Core.Client/FastTunnel.Core.Client.csproj new file mode 100644 index 0000000..c790d86 --- /dev/null +++ b/FastTunnel.Core.Client/FastTunnel.Core.Client.csproj @@ -0,0 +1,38 @@ + + + + 2.1.1 + net6.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FastTunnel.Core/Client/FastTunnelClient.cs b/FastTunnel.Core.Client/FastTunnelClient.cs similarity index 99% rename from FastTunnel.Core/Client/FastTunnelClient.cs rename to FastTunnel.Core.Client/FastTunnelClient.cs index b1ce625..d1f8820 100644 --- a/FastTunnel.Core/Client/FastTunnelClient.cs +++ b/FastTunnel.Core.Client/FastTunnelClient.cs @@ -17,7 +17,6 @@ using FastTunnel.Core.Handlers.Client; using FastTunnel.Core.Models; using FastTunnel.Core.Models.Massage; using FastTunnel.Core.Utilitys; -using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; diff --git a/FastTunnel.Core/Handlers/Client/IClientHandler.cs b/FastTunnel.Core.Client/Handlers/IClientHandler.cs similarity index 53% rename from FastTunnel.Core/Handlers/Client/IClientHandler.cs rename to FastTunnel.Core.Client/Handlers/IClientHandler.cs index 51916f9..f38df91 100644 --- a/FastTunnel.Core/Handlers/Client/IClientHandler.cs +++ b/FastTunnel.Core.Client/Handlers/IClientHandler.cs @@ -13,17 +13,16 @@ using System.Text; using System.Threading.Tasks; using System.Threading; -namespace FastTunnel.Core.Handlers.Client +namespace FastTunnel.Core.Handlers.Client; + +public interface IClientHandler { - public interface IClientHandler - { - /// - /// 处理消息 - /// - /// - /// - /// - /// - Task HandlerMsgAsync(FastTunnelClient cleint, string msg, CancellationToken cancellationToken); - } + /// + /// 处理消息 + /// + /// + /// + /// + /// + Task HandlerMsgAsync(FastTunnelClient cleint, string msg, CancellationToken cancellationToken); } diff --git a/FastTunnel.Core/Handlers/Client/LogHandler.cs b/FastTunnel.Core.Client/Handlers/LogHandler.cs similarity index 100% rename from FastTunnel.Core/Handlers/Client/LogHandler.cs rename to FastTunnel.Core.Client/Handlers/LogHandler.cs diff --git a/FastTunnel.Core/Handlers/Client/SwapHandler.cs b/FastTunnel.Core.Client/Handlers/SwapHandler.cs similarity index 98% rename from FastTunnel.Core/Handlers/Client/SwapHandler.cs rename to FastTunnel.Core.Client/Handlers/SwapHandler.cs index 2319528..5c77af0 100644 --- a/FastTunnel.Core/Handlers/Client/SwapHandler.cs +++ b/FastTunnel.Core.Client/Handlers/SwapHandler.cs @@ -9,9 +9,9 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; -using FastTunnel.Core.Sockets; using Microsoft.Extensions.Logging; using System.Net.Security; +using FastTunnel.Core.Client.Sockets; namespace FastTunnel.Core.Handlers.Client { diff --git a/FastTunnel.Core.Client/IClientConfig.cs b/FastTunnel.Core.Client/IClientConfig.cs new file mode 100644 index 0000000..8874f5b --- /dev/null +++ b/FastTunnel.Core.Client/IClientConfig.cs @@ -0,0 +1,24 @@ +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE +// Copyright (c) 2019 Gui.H + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FastTunnel.Core.Config; +using FastTunnel.Core.Models; + +namespace FastTunnel.Core.Client; + +public interface IClientConfig +{ + public SuiDaoServer Server { get; set; } + + public IEnumerable Webs { get; set; } + + public IEnumerable Forwards { get; set; } +} diff --git a/FastTunnel.Core/Client/IFastTunnelClient.cs b/FastTunnel.Core.Client/IFastTunnelClient.cs similarity index 100% rename from FastTunnel.Core/Client/IFastTunnelClient.cs rename to FastTunnel.Core.Client/IFastTunnelClient.cs diff --git a/FastTunnel.Core/Config/IClientConfig.cs b/FastTunnel.Core.Client/Models/SuiDaoServer.cs similarity index 62% rename from FastTunnel.Core/Config/IClientConfig.cs rename to FastTunnel.Core.Client/Models/SuiDaoServer.cs index 79010ac..a1344be 100644 --- a/FastTunnel.Core/Config/IClientConfig.cs +++ b/FastTunnel.Core.Client/Models/SuiDaoServer.cs @@ -1,4 +1,4 @@ -// Licensed under the Apache License, Version 2.0 (the "License"). +// Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // You may obtain a copy of the License at // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE @@ -9,15 +9,6 @@ using System.Collections.Generic; namespace FastTunnel.Core.Config { - public interface IClientConfig - { - public SuiDaoServer Server { get; set; } - - public IEnumerable Webs { get; set; } - - public IEnumerable Forwards { get; set; } - } - public class SuiDaoServer { public string Protocol { get; set; } = "ws"; diff --git a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs b/FastTunnel.Core.Client/Services/ServiceFastTunnelClient.cs similarity index 96% rename from FastTunnel.Core/Services/ServiceFastTunnelClient.cs rename to FastTunnel.Core.Client/Services/ServiceFastTunnelClient.cs index 52ad9e9..496602d 100644 --- a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs +++ b/FastTunnel.Core.Client/Services/ServiceFastTunnelClient.cs @@ -4,9 +4,7 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H -using FastTunnel.Core.Config; using FastTunnel.Core.Client; -using FastTunnel.Core.Models; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; diff --git a/FastTunnel.Core/Sockets/DnsSocketFactory.cs b/FastTunnel.Core.Client/Sockets/DnsSocketFactory.cs similarity index 54% rename from FastTunnel.Core/Sockets/DnsSocketFactory.cs rename to FastTunnel.Core.Client/Sockets/DnsSocketFactory.cs index c64cce0..7d74743 100644 --- a/FastTunnel.Core/Sockets/DnsSocketFactory.cs +++ b/FastTunnel.Core.Client/Sockets/DnsSocketFactory.cs @@ -13,16 +13,15 @@ using System.Net.Sockets; using System.Text; using System.Threading.Tasks; -namespace FastTunnel.Core.Sockets +namespace FastTunnel.Core.Client.Sockets; + +public class DnsSocketFactory { - public class DnsSocketFactory + public static async Task ConnectAsync(string host, int port) { - public static async Task ConnectAsync(string host, int port) - { - var Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - DnsEndPoint dnsEndPoint = new DnsEndPoint(host, port); - await Socket.ConnectAsync(dnsEndPoint); - return Socket; - } + var Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + var dnsEndPoint = new DnsEndPoint(host, port); + await Socket.ConnectAsync(dnsEndPoint); + return Socket; } } diff --git a/FastTunnel.Core/Utilitys/AssemblyUtility.cs b/FastTunnel.Core.Client/Utilitys/AssemblyUtility.cs similarity index 100% rename from FastTunnel.Core/Utilitys/AssemblyUtility.cs rename to FastTunnel.Core.Client/Utilitys/AssemblyUtility.cs diff --git a/FastTunnel.Core/Extensions/ServicesExtensions.cs b/FastTunnel.Core/Extensions/ServicesExtensions.cs index 2c13031..65f80c2 100644 --- a/FastTunnel.Core/Extensions/ServicesExtensions.cs +++ b/FastTunnel.Core/Extensions/ServicesExtensions.cs @@ -11,9 +11,7 @@ using FastTunnel.Core.Config; using FastTunnel.Core.Filters; using FastTunnel.Core.Forwarder; using FastTunnel.Core.Forwarder.MiddleWare; -using FastTunnel.Core.Handlers.Client; using FastTunnel.Core.Handlers.Server; -using FastTunnel.Core.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters; @@ -27,25 +25,6 @@ namespace FastTunnel.Core.Extensions; public static class ServicesExtensions { - /// - /// 客户端依赖及HostedService - /// - /// - public static void AddFastTunnelClient(this IServiceCollection services, IConfigurationSection configurationSection) - { - services.Configure(configurationSection); - services.AddFastTunnelClient(); - } - - public static void AddFastTunnelClient(this IServiceCollection services) - { - services.AddTransient() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - - services.AddHostedService(); - } /// /// 添加服务端后台进程 diff --git a/FastTunnel.Core/Forwarder/FastTunnelForwarderHttpClientFactory.cs b/FastTunnel.Core/Forwarder/FastTunnelForwarderHttpClientFactory.cs index b4ca44a..dbef3f1 100644 --- a/FastTunnel.Core/Forwarder/FastTunnelForwarderHttpClientFactory.cs +++ b/FastTunnel.Core/Forwarder/FastTunnelForwarderHttpClientFactory.cs @@ -7,7 +7,6 @@ using FastTunnel.Core.Client; using FastTunnel.Core.Extensions; using FastTunnel.Core.Models; -using FastTunnel.Core.Sockets; using Microsoft.AspNetCore.Connections.Features; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; diff --git a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs index 01d9263..e32ea52 100644 --- a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs +++ b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs @@ -9,7 +9,6 @@ using FastTunnel.Core.Exceptions; using FastTunnel.Core.Extensions; using FastTunnel.Core.Listener; using FastTunnel.Core.Models; -using FastTunnel.Core.Sockets; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.Extensions.Logging; using System; diff --git a/FastTunnel.Core/Listener/PortProxyListenerV2.cs b/FastTunnel.Core/Listener/PortProxyListenerV2.cs new file mode 100644 index 0000000..b19076d --- /dev/null +++ b/FastTunnel.Core/Listener/PortProxyListenerV2.cs @@ -0,0 +1,106 @@ +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE +// Copyright (c) 2019 Gui.H + +using BeetleX; +using BeetleX.EventArgs; +using FastTunnel.Core.Handlers.Server; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.Extensions.Logging; +using System; +using System.Net; +using System.Net.Sockets; +using System.Net.WebSockets; +using System.Threading; +using IServer = BeetleX.IServer; + +namespace FastTunnel.Core.Listener +{ + public class PortProxyListenerV2 + { + ILogger _logerr; + + public string ListenIp { get; private set; } + + public int ListenPort { get; private set; } + + public IServer Server { get; set; } + + int m_numConnectedSockets; + + private IServer server; + + bool shutdown; + ForwardDispatcher _requestDispatcher; + WebSocket client; + + // string ip, int port, ILogger logerr, WebSocket client + public PortProxyListenerV2() + { + //IPAddress ipa = IPAddress.Parse(ListenIp); + //IPEndPoint localEndPoint = new IPEndPoint(ipa, ListenPort); + } + + public void Start(ForwardDispatcher requestDispatcher, string host, int port, ILogger logger, WebSocket webSocket) + { + this.client = webSocket; + this._logerr = logger; + this.ListenIp = host; + this.ListenPort = port; + + shutdown = false; + _requestDispatcher = requestDispatcher; + + server = SocketFactory.CreateTcpServer(); + var handler = server.Handler as TcpServerHandler; + handler.Sethanler(this); + server.Options.DefaultListen.Port = port; + server.Options.DefaultListen.Host = host; + server.Open(); + } + + //protected override void OnReceiveMessage(IServer server, ISession session, object message) + //{ + // base.OnReceiveMessage(server, session, message); + //} + + private void ProcessAcceptAsync(SocketAsyncEventArgs e) + { + // 将此客户端交由Dispatcher进行管理 + } + + internal async void Process(SessionReceiveEventArgs e) + { + //var pipeStream = e.Session.Stream.ToPipeStream(); + //if (pipeStream.TryReadLine(out string name)) + //{ + // Console.WriteLine(name); + // e.Stream.ToPipeStream().WriteLine("hello " + name); + // e.Stream.Flush(); + //} + + await _requestDispatcher.DispatchAsync(e.Stream, client); + } + + public void Stop() + { + if (shutdown) + return; + + try + { + server.Dispose(); + } + catch (Exception) + { + } + finally + { + + } + } + + } +} diff --git a/FastTunnel.Core/Listener/TcpServerHandler.cs b/FastTunnel.Core/Listener/TcpServerHandler.cs new file mode 100644 index 0000000..6233c33 --- /dev/null +++ b/FastTunnel.Core/Listener/TcpServerHandler.cs @@ -0,0 +1,67 @@ +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE +// Copyright (c) 2019 Gui.H + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BeetleX; +using BeetleX.EventArgs; + +namespace FastTunnel.Core.Listener; + +internal class TcpServerHandler : ServerHandlerBase +{ + PortProxyListenerV2 proxyListenerV2; + + public override void Connected(IServer server, ConnectedEventArgs e) + { + Console.WriteLine("[Connected]"); + } + + public override void Connecting(IServer server, ConnectingEventArgs e) + { + Console.WriteLine("[Connecting]"); + } + + public override void Disconnect(IServer server, SessionEventArgs e) + { + Console.WriteLine("[Disconnect]"); + } + + public override void Error(IServer server, ServerErrorEventArgs e) + { + Console.WriteLine("[Error]"); + } + + public override void Opened(IServer server) + { + Console.WriteLine("[Opened]"); + } + + public override void SessionDetection(IServer server, SessionDetectionEventArgs e) + { + Console.WriteLine("[SessionDetection]"); + } + + public override void SessionPacketDecodeCompleted(IServer server, PacketDecodeCompletedEventArgs e) + { + Console.WriteLine("[SessionPacketDecodeCompleted]"); + } + + public override void SessionReceive(IServer server, SessionReceiveEventArgs e) + { + Console.WriteLine("[SessionReceive]"); + + proxyListenerV2.Process(e); + } + + internal void Sethanler(PortProxyListenerV2 portProxyListenerV2) + { + this.proxyListenerV2 = portProxyListenerV2; + } +} diff --git a/FastTunnel.Core/Models/ForwardConfig.cs b/FastTunnel.Core/Models/ForwardConfig.cs index e4af5bd..e93404d 100644 --- a/FastTunnel.Core/Models/ForwardConfig.cs +++ b/FastTunnel.Core/Models/ForwardConfig.cs @@ -22,7 +22,7 @@ namespace FastTunnel.Core.Models /// 服务端监听的端口号 1~65535 /// public int RemotePort { get; set; } - + /// /// 协议,内网服务监听的协议 /// diff --git a/FastTunnel.Core/Models/Massage/LogInMassage.cs b/FastTunnel.Core/Models/LogInMassage.cs similarity index 100% rename from FastTunnel.Core/Models/Massage/LogInMassage.cs rename to FastTunnel.Core/Models/LogInMassage.cs diff --git a/FastTunnel.Core/Models/Massage/TunnelMassage.cs b/FastTunnel.Core/Models/TunnelMassage.cs similarity index 100% rename from FastTunnel.Core/Models/Massage/TunnelMassage.cs rename to FastTunnel.Core/Models/TunnelMassage.cs diff --git a/FastTunnel.sln b/FastTunnel.sln index 6a3916b..75e9a92 100644 --- a/FastTunnel.sln +++ b/FastTunnel.sln @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .github\workflows\dotnetcore.yml = .github\workflows\dotnetcore.yml EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastTunnel.Core.Client", "FastTunnel.Core.Client\FastTunnel.Core.Client.csproj", "{67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,6 +43,10 @@ Global {7D560A9A-E480-40F4-AAF7-398447438255}.Debug|Any CPU.Build.0 = Debug|Any CPU {7D560A9A-E480-40F4-AAF7-398447438255}.Release|Any CPU.ActiveCfg = Release|Any CPU {7D560A9A-E480-40F4-AAF7-398447438255}.Release|Any CPU.Build.0 = Release|Any CPU + {67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -48,6 +54,7 @@ Global GlobalSection(NestedProjects) = preSolution {C8ADFEB1-59DB-4CE3-8D04-5B547107BCCB} = {0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C} {7D560A9A-E480-40F4-AAF7-398447438255} = {0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C} + {67DB3ED6-B1DD-49AA-8C5D-34FABC3C643B} = {0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3D9C6B44-6706-4EE8-9043-802BBE474A2E}