拆分Client部分代码

This commit is contained in:
Gui.H 2022-11-11 22:31:27 +08:00
parent 29ce9731ec
commit 839861fed9
25 changed files with 305 additions and 64 deletions

View File

@ -13,7 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FastTunnel.Core\FastTunnel.Core.csproj" />
<ProjectReference Include="..\FastTunnel.Core.Client\FastTunnel.Core.Client.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -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;

View File

@ -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<ForwardConfig> Forwards { get; set; }
}
}
}

View File

@ -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
{
/// <summary>
/// 客户端依赖及HostedService
/// </summary>
/// <param name="services"></param>
public static void AddFastTunnelClient(this IServiceCollection services, IConfigurationSection configurationSection)
{
services.Configure<DefaultClientConfig>(configurationSection);
services.AddTransient<IFastTunnelClient, FastTunnelClient>()
.AddSingleton<LogHandler>()
.AddSingleton<SwapHandler>();
services.AddHostedService<ServiceFastTunnelClient>();
}
}

View File

@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.1.1</Version>
<TargetFrameworks>net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\FastTunnel.Core\Exceptions\SocketClosedException.cs" Link="Exceptions\SocketClosedException.cs" />
<Compile Include="..\FastTunnel.Core\Extensions\LoggerExtentions.cs" Link="Extensions\LoggerExtentions.cs" />
<Compile Include="..\FastTunnel.Core\Extensions\ObjectExtensions.cs" Link="Extensions\ObjectExtensions.cs" />
<Compile Include="..\FastTunnel.Core\Extensions\WebSocketExtensions.cs" Link="Extensions\WebSocketExtensions.cs" />
<Compile Include="..\FastTunnel.Core\FastTunnelConst.cs" Link="FastTunnelConst.cs" />
<Compile Include="..\FastTunnel.Core\Models\ForwardConfig.cs" Link="Models\ForwardConfig.cs" />
<Compile Include="..\FastTunnel.Core\Models\LogInMassage.cs" Link="Models\LogInMassage.cs" />
<Compile Include="..\FastTunnel.Core\Models\Message.cs" Link="Models\Message.cs" />
<Compile Include="..\FastTunnel.Core\Models\TunnelMassage.cs" Link="Models\TunnelMassage.cs" />
<Compile Include="..\FastTunnel.Core\Models\WebConfig.cs" Link="Models\WebConfig.cs" />
<Compile Include="..\FastTunnel.Core\Utilitys\WebSocketUtility.cs" Link="Utilitys\WebSocketUtility.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Exceptions\" />
</ItemGroup>
</Project>

View File

@ -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;

View File

@ -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
{
/// <summary>
/// 处理消息
/// </summary>
/// <param name="cleint"></param>
/// <param name="msg"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task HandlerMsgAsync(FastTunnelClient cleint, string msg, CancellationToken cancellationToken);
}
/// <summary>
/// 处理消息
/// </summary>
/// <param name="cleint"></param>
/// <param name="msg"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task HandlerMsgAsync(FastTunnelClient cleint, string msg, CancellationToken cancellationToken);
}

View File

@ -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
{

View File

@ -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<WebConfig> Webs { get; set; }
public IEnumerable<ForwardConfig> Forwards { get; set; }
}

View File

@ -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<WebConfig> Webs { get; set; }
public IEnumerable<ForwardConfig> Forwards { get; set; }
}
public class SuiDaoServer
{
public string Protocol { get; set; } = "ws";

View File

@ -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;

View File

@ -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<Socket> ConnectAsync(string host, int port)
{
public static async Task<Socket> 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;
}
}

View File

@ -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
{
/// <summary>
/// 客户端依赖及HostedService
/// </summary>
/// <param name="services"></param>
public static void AddFastTunnelClient(this IServiceCollection services, IConfigurationSection configurationSection)
{
services.Configure<DefaultClientConfig>(configurationSection);
services.AddFastTunnelClient();
}
public static void AddFastTunnelClient(this IServiceCollection services)
{
services.AddTransient<IFastTunnelClient, FastTunnelClient>()
.AddSingleton<IExceptionFilter, FastTunnelExceptionFilter>()
.AddSingleton<LogHandler>()
.AddSingleton<SwapHandler>();
services.AddHostedService<ServiceFastTunnelClient>();
}
/// <summary>
/// 添加服务端后台进程

View File

@ -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;

View File

@ -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;

View File

@ -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<TcpServerHandler>();
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
{
}
}
}
}

View File

@ -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;
}
}

View File

@ -22,7 +22,7 @@ namespace FastTunnel.Core.Models
/// 服务端监听的端口号 1~65535
/// </summary>
public int RemotePort { get; set; }
/// <summary>
/// 协议,内网服务监听的协议
/// </summary>

View File

@ -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}