代码清理

This commit is contained in:
Gui.H 2022-05-08 21:38:59 +08:00
parent ea8072ac6e
commit 593f12d925
65 changed files with 1682 additions and 2229 deletions

View File

@ -6,7 +6,6 @@
using FastTunnel.Api.Models;
using FastTunnel.Core.Config;
using FastTunnel.Server.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

View File

@ -4,7 +4,7 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Server.Models;
using FastTunnel.Api.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

View File

@ -4,8 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Api.Models;
using FastTunnel.Core.Client;
using FastTunnel.Server.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;

View File

@ -4,7 +4,7 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Server.Models;
using FastTunnel.Api.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

View File

@ -4,7 +4,7 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
namespace FastTunnel.Server.Models
namespace FastTunnel.Api.Models
{
public class ApiResponse
{

View File

@ -4,22 +4,22 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Config;
using FastTunnel.Core.Models;
using System;
using System.Text;
using System.Threading.Tasks;
using FastTunnel.Core.Extensions;
using System.Threading;
using Microsoft.Extensions.Logging;
using FastTunnel.Core.Handlers.Client;
using Microsoft.Extensions.Options;
using System.Net.WebSockets;
using FastTunnel.Core.Utilitys;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Config;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Handlers.Client;
using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
using FastTunnel.Core.Utilitys;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace FastTunnel.Core.Client;
namespace FastTunnel.Core.Client
{
public class FastTunnelClient : IFastTunnelClient
{
private ClientWebSocket socket;
@ -110,7 +110,7 @@ namespace FastTunnel.Core.Client
private async Task ReceiveServerAsync(CancellationToken cancellationToken)
{
byte[] buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH];
var buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH];
while (!cancellationToken.IsCancellationRequested)
{
var res = await socket.ReceiveAsync(buffer, cancellationToken);
@ -153,4 +153,3 @@ namespace FastTunnel.Core.Client
_logger.LogInformation("===== FastTunnel Client Stoping =====");
}
}
}

View File

@ -4,26 +4,24 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Config;
using FastTunnel.Core.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.Options;
using System.IO;
using Yarp.ReverseProxy.Configuration;
using System.Collections.Generic;
namespace FastTunnel.Core.Client
{
namespace FastTunnel.Core.Client;
public class FastTunnelServer
{
public int ConnectedClientCount;
public readonly IOptionsMonitor<DefaultServerConfig> ServerOption;
public IProxyConfigProvider proxyConfig;
readonly ILogger<FastTunnelServer> logger;
private readonly ILogger<FastTunnelServer> logger;
public ConcurrentDictionary<string, TaskCompletionSource<Stream>> ResponseTasks { get; } = new();
@ -37,11 +35,10 @@ namespace FastTunnel.Core.Client
/// </summary>
public IList<TunnelClient> Clients = new List<TunnelClient>();
public FastTunnelServer(ILogger<FastTunnelServer> logger, IProxyConfigProvider proxyConfig, IOptionsMonitor<DefaultServerConfig> serverSettings)
public FastTunnelServer(ILogger<FastTunnelServer> logger, IOptionsMonitor<DefaultServerConfig> serverSettings)
{
this.logger = logger;
this.ServerOption = serverSettings;
this.proxyConfig = proxyConfig;
}
/// <summary>
@ -73,4 +70,3 @@ namespace FastTunnel.Core.Client
return WebList.TryGetValue(host, out web);
}
}
}

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
@ -6,12 +6,11 @@
using System.Threading;
namespace FastTunnel.Core.Client
{
namespace FastTunnel.Core.Client;
public interface IFastTunnelClient
{
void StartAsync(CancellationToken cancellationToken);
void Stop(CancellationToken cancellationToken);
}
}

View File

@ -1,14 +1,14 @@
// 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.Models;
using System.Collections.Generic;
using FastTunnel.Core.Models;
namespace FastTunnel.Core.Config;
namespace FastTunnel.Core.Config
{
public class DefaultClientConfig : IClientConfig
{
public SuiDaoServer Server { get; set; }
@ -19,4 +19,3 @@ namespace FastTunnel.Core.Config
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
@ -7,8 +7,8 @@
using System;
using System.Collections.Generic;
namespace FastTunnel.Core.Config
{
namespace FastTunnel.Core.Config;
public class DefaultServerConfig : IServerConfig
{
public string WebDomain { get; set; }
@ -51,4 +51,3 @@ namespace FastTunnel.Core.Config
public string Password { get; set; }
}
}
}

View File

@ -1,14 +1,14 @@
// 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.Models;
using System.Collections.Generic;
using FastTunnel.Core.Models;
namespace FastTunnel.Core.Config;
namespace FastTunnel.Core.Config
{
public interface IClientConfig
{
public SuiDaoServer Server { get; set; }
@ -26,4 +26,3 @@ namespace FastTunnel.Core.Config
public int ServerPort { get; set; }
}
}

View File

@ -1,11 +1,11 @@
// 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
namespace FastTunnel.Core.Config
{
namespace FastTunnel.Core.Config;
public interface IServerConfig
{
string WebDomain { get; set; }
@ -14,4 +14,3 @@ namespace FastTunnel.Core.Config
bool EnableForward { get; set; }
}
}

View File

@ -5,11 +5,9 @@
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Exceptions
{
namespace FastTunnel.Core.Exceptions;
public class APIErrorException : Exception
{
public APIErrorException(string message)
@ -17,4 +15,3 @@ namespace FastTunnel.Core.Exceptions
{
}
}
}

View File

@ -6,8 +6,8 @@
using System;
namespace FastTunnel.Core.Exceptions
{
namespace FastTunnel.Core.Exceptions;
public class ClienOffLineException : Exception
{
public ClienOffLineException(string message)
@ -15,4 +15,3 @@ namespace FastTunnel.Core.Exceptions
{
}
}
}

View File

@ -5,17 +5,12 @@
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FastTunnel.Core.Exceptions
{
namespace FastTunnel.Core.Exceptions;
public class SocketClosedException : Exception
{
public SocketClosedException(string msg) : base(msg)
{
}
}
}

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
@ -6,8 +6,8 @@
using System.Text;
namespace FastTunnel.Core.Extensions
{
namespace FastTunnel.Core.Extensions;
public static class ByteArrayExtensions
{
public static string GetString(this byte[] buffer, int offset, int count)
@ -15,4 +15,3 @@ namespace FastTunnel.Core.Extensions
return Encoding.UTF8.GetString(buffer, offset, count);
}
}
}

View File

@ -4,11 +4,6 @@
// 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.Client;
using FastTunnel.Core.Forwarder.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Core;

View File

@ -4,11 +4,11 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using Microsoft.Extensions.Logging;
using System;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Extensions;
namespace FastTunnel.Core.Extensions
{
public static class LoggerExtentions
{
public static void LogError(this ILogger logger, Exception ex)
@ -16,4 +16,3 @@ namespace FastTunnel.Core.Extensions
logger.LogError(ex, string.Empty);
}
}
}

View File

@ -6,8 +6,8 @@
using System.Text.Json;
namespace FastTunnel.Core.Extensions
{
namespace FastTunnel.Core.Extensions;
public static class ObjectExtensions
{
public static string ToJson(this object message)
@ -21,4 +21,3 @@ namespace FastTunnel.Core.Extensions
return JsonSerializer.Serialize(message, message.GetType(), jsonOptions);
}
}
}

View File

@ -7,25 +7,15 @@
using FastTunnel.Core.Client;
using FastTunnel.Core.Config;
using FastTunnel.Core.Forwarder.MiddleWare;
using FastTunnel.Core.Forwarder;
using FastTunnel.Core.Handlers.Client;
using FastTunnel.Core.Handlers.Server;
using FastTunnel.Core.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Yarp.ReverseProxy.Forwarder;
using Microsoft.AspNetCore.Builder;
using FastTunnel.Core.Filters;
using Microsoft.AspNetCore.Mvc.Filters;
using FastTunnel.Core.Models;
using FastTunnel.Core.Handlers.Server;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.AspNetCore.Http;
namespace FastTunnel.Core.Extensions
{
namespace FastTunnel.Core.Extensions;
public static class ServicesExtensions
{
/// <summary>
@ -41,7 +31,6 @@ namespace FastTunnel.Core.Extensions
public static void AddFastTunnelClient(this IServiceCollection services)
{
services.AddTransient<IFastTunnelClient, FastTunnelClient>()
.AddSingleton<IExceptionFilter, FastTunnelExceptionFilter>()
.AddSingleton<LogHandler>()
.AddSingleton<SwapHandler>();
@ -54,11 +43,7 @@ namespace FastTunnel.Core.Extensions
/// <param name="services"></param>
public static void AddFastTunnelServer(this IServiceCollection services, IConfigurationSection configurationSection)
{
services.AddReverseProxy().LoadFromMemory();
services.AddSingleton<IForwarderHttpClientFactory, FastTunnelForwarderHttpClientFactory>();
services.Configure<DefaultServerConfig>(configurationSection)
.AddSingleton<IExceptionFilter, FastTunnelExceptionFilter>()
.AddTransient<ILoginHandler, LoginHandler>()
.AddSingleton<FastTunnelClientHandler>()
.AddSingleton<FastTunnelSwapHandler>()
@ -77,24 +62,4 @@ namespace FastTunnel.Core.Extensions
var clientHandler = app.ApplicationServices.GetRequiredService<FastTunnelClientHandler>();
app.Use(clientHandler.Handle);
}
//public static void MapFastTunnelServer(this IEndpointRouteBuilder endpoints)
//{
// endpoints.MapReverseProxy();
// endpoints.MapFallback(context =>
// {
// var options = context.RequestServices.GetRequiredService<IOptionsMonitor<DefaultServerConfig>>();
// var host = context.Request.Host.Host;
// if (!host.EndsWith(options.CurrentValue.WebDomain) || host.Equals(options.CurrentValue.WebDomain))
// {
// context.Response.StatusCode = 404;
// return Task.CompletedTask;
// }
// context.Response.StatusCode = 200;
// context.Response.WriteAsync(TunnelResource.Page_NotFound, CancellationToken.None);
// return Task.CompletedTask;
// });
//}
}
}

View File

@ -4,15 +4,13 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
namespace FastTunnel.Core.Extensions;
namespace FastTunnel.Core.Extensions
{
public static class SocketExtensions
{
public static void SendCmd<T>(this Socket socket, Message<T> message)
@ -21,4 +19,3 @@ namespace FastTunnel.Core.Extensions
socket.Send(Encoding.UTF8.GetBytes(message.ToJson() + "\n"));
}
}
}

View File

@ -1,18 +1,15 @@
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Extensions
{
namespace FastTunnel.Core.Extensions;
public static class TaskCompletionSourceExtensions
{
public static void SetTimeOut<T>(this TaskCompletionSource<T> tcs, int timeoutMs, Action? action)
@ -28,4 +25,3 @@ namespace FastTunnel.Core.Extensions
}, useSynchronizationContext: false);
}
}
}

View File

@ -4,16 +4,12 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace FastTunnel.Core.Extensions
{
namespace FastTunnel.Core.Extensions;
internal static class ValueTaskExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -48,4 +44,3 @@ namespace FastTunnel.Core.Extensions
}
}
}
}

View File

@ -4,19 +4,16 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Exceptions;
using FastTunnel.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Exceptions;
using FastTunnel.Core.Models;
namespace FastTunnel.Core.Extensions;
namespace FastTunnel.Core.Extensions
{
public static class WebSocketExtensions
{
public static async Task SendCmdAsync(this WebSocket socket, MessageType type, string content, CancellationToken cancellationToken)
@ -33,4 +30,3 @@ namespace FastTunnel.Core.Extensions
await socket.SendAsync(buffer, WebSocketMessageType.Binary, false, cancellationToken);
}
}
}

View File

@ -18,11 +18,14 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0-preview.3.22175.4" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0-preview.3.22175.4" />
<PackageReference Include="Yarp.ReverseProxy" Version="1.1.0-rc.1.22211.2" />
</ItemGroup>
<ItemGroup>

View File

@ -4,14 +4,8 @@
// 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;
namespace FastTunnel.Core;
namespace FastTunnel.Core
{
public class FastTunnelConst
{
public const string FASTTUNNEL_VERSION = "FT_VERSION";
@ -20,4 +14,3 @@ namespace FastTunnel.Core
public const int MAX_CMD_LENGTH = 100;
}
}

View File

@ -1,13 +1,17 @@
// 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 Microsoft.AspNetCore.Mvc.Filters;
using FastTunnel.Core.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using FastTunnel.Core.Extensions;
namespace FastTunnel.Core.Filters
{
namespace FastTunnel.Core.Filters;
public class FastTunnelExceptionFilter : IExceptionFilter
{
private readonly IWebHostEnvironment _hostingEnvironment;
@ -31,4 +35,3 @@ namespace FastTunnel.Core.Filters
logger.LogError(context.Exception, "[全局异常]");
}
}
}

View File

@ -1,108 +0,0 @@
// Copyright (c) 2019-2022 Gui.H. https://github.com/FastTunnel/FastTunnel
// The FastTunnel licenses this file to you under the Apache License Version 2.0.
// For more details,You may obtain License file at: https://github.com/FastTunnel/FastTunnel/blob/v2/LICENSE
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Models;
using FastTunnel.Core.Sockets;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Yarp.ReverseProxy.Forwarder;
namespace FastTunnel.Core.Forwarder
{
public class FastTunnelForwarderHttpClientFactory : ForwarderHttpClientFactory
{
readonly ILogger<FastTunnelForwarderHttpClientFactory> logger;
readonly FastTunnelServer fastTunnelServer;
public FastTunnelForwarderHttpClientFactory(ILogger<FastTunnelForwarderHttpClientFactory> logger, FastTunnelServer fastTunnelServer)
{
this.fastTunnelServer = fastTunnelServer;
this.logger = logger;
}
protected override void ConfigureHandler(ForwarderHttpClientContext context, SocketsHttpHandler handler)
{
base.ConfigureHandler(context, handler);
handler.ConnectCallback = ConnectCallback;
}
private async ValueTask<Stream> ConnectCallback(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
var host = context.InitialRequestMessage.RequestUri.Host;
try
{
var res = await proxyAsync(host, context, cancellationToken);
return res;
}
catch (Exception ex)
{
logger.LogError(ex, "ConnectCallback Error");
throw;
}
}
public async ValueTask<Stream> proxyAsync(string host, SocketsHttpConnectionContext context, CancellationToken cancellation)
{
WebInfo web;
if (!fastTunnelServer.WebList.TryGetValue(host, out web))
{
// 客户端已离线
return await OfflinePage(host, context);
}
var msgId = Guid.NewGuid().ToString().Replace("-", "");
TaskCompletionSource<Stream> tcs = new(cancellation);
logger.LogDebug($"[Http]Swap开始 {msgId}|{host}=>{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}");
tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Proxy TimeOut]:{msgId}"); });
fastTunnelServer.ResponseTasks.TryAdd(msgId, tcs);
try
{
// 发送指令给客户端,等待建立隧道
await web.Socket.SendCmdAsync(MessageType.SwapMsg, $"{msgId}|{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}", cancellation);
var res = await tcs.Task;
logger.LogDebug($"[Http]Swap OK {msgId}");
return res;
}
catch (WebSocketException)
{
// 通讯异常,返回客户端离线
return await OfflinePage(host, context);
}
catch (Exception)
{
throw;
}
finally
{
fastTunnelServer.ResponseTasks.TryRemove(msgId, out _);
}
}
private async ValueTask<Stream> OfflinePage(string host, SocketsHttpConnectionContext context)
{
var bytes = Encoding.UTF8.GetBytes(
$"HTTP/1.1 200 OK\r\nContent-Type:text/html; charset=utf-8\r\n\r\n{TunnelResource.Page_Offline}\r\n");
return await Task.FromResult(new ResponseStream(bytes));
}
}
}

View File

@ -1,34 +0,0 @@
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Configuration;
namespace FastTunnel.Core.Forwarder
{
public class FastTunnelProxyConfig : IProxyConfig
{
public FastTunnelProxyConfig()
: this(Array.Empty<RouteConfig>(), Array.Empty<ClusterConfig>())
{
}
public FastTunnelProxyConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
this.Routes = routes;
this.Clusters = clusters;
this.ChangeToken = new CancellationChangeToken(cancellationToken.Token);
}
public IReadOnlyList<RouteConfig> Routes { get; }
public IReadOnlyList<ClusterConfig> Clusters { get; }
public IChangeToken ChangeToken { get; }
private readonly CancellationTokenSource cancellationToken = new();
}
}

View File

@ -1,107 +0,0 @@
// Copyright (c) 2019-2022 Gui.H. https://github.com/FastTunnel/FastTunnel
// The FastTunnel licenses this file to you under the Apache License Version 2.0.
// For more details,You may obtain License file at: https://github.com/FastTunnel/FastTunnel/blob/v2/LICENSE
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Models;
using FastTunnel.Core.Sockets;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Yarp.ReverseProxy.Forwarder;
namespace FastTunnel.Core.Forwarder
{
public class ForwarderClientFactory : ForwarderHttpClientFactory
{
readonly ILogger<ForwarderClientFactory> logger;
readonly FastTunnelServer fastTunnelServer;
public ForwarderClientFactory(ILogger<ForwarderClientFactory> logger, FastTunnelServer fastTunnelServer)
{
this.fastTunnelServer = fastTunnelServer;
this.logger = logger;
}
protected override void ConfigureHandler(ForwarderHttpClientContext context, SocketsHttpHandler handler)
{
base.ConfigureHandler(context, handler);
handler.ConnectCallback = ConnectCallback;
}
private async ValueTask<Stream> ConnectCallback(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
var host = context.InitialRequestMessage.RequestUri.Host;
try
{
var res = await proxyAsync(host, context, cancellationToken);
return res;
}
catch (Exception)
{
throw;
}
}
public async ValueTask<Stream> proxyAsync(string host, SocketsHttpConnectionContext context, CancellationToken cancellation)
{
WebInfo web;
if (!fastTunnelServer.WebList.TryGetValue(host, out web))
{
// 客户端已离线
return await OfflinePage(host, context);
}
var msgId = Guid.NewGuid().ToString().Replace("-", "");
TaskCompletionSource<Stream> tcs = new();
logger.LogDebug($"[Http]Swap开始 {msgId}|{host}=>{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}");
tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Proxy TimeOut]:{msgId}"); });
fastTunnelServer.ResponseTasks.TryAdd(msgId, tcs);
try
{
// 发送指令给客户端,等待建立隧道
await web.Socket.SendCmdAsync(MessageType.SwapMsg, $"{msgId}|{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}", cancellation);
var res = await tcs.Task;
logger.LogDebug($"[Http]Swap OK {msgId}");
return res;
}
catch (WebSocketException)
{
// 通讯异常,返回客户端离线
return await OfflinePage(host, context);
}
catch (Exception)
{
throw;
}
finally
{
fastTunnelServer.ResponseTasks.TryRemove(msgId, out _);
}
}
private async ValueTask<Stream> OfflinePage(string host, SocketsHttpConnectionContext context)
{
var bytes = Encoding.UTF8.GetBytes(
$"HTTP/1.1 200 OK\r\nContent-Type:text/html; charset=utf-8\r\n\r\n{TunnelResource.Page_Offline}\r\n");
return await Task.FromResult(new ResponseStream(bytes));
}
}
}

View File

@ -1,127 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Yarp.ReverseProxy.Configuration;
using System.Linq;
namespace FastTunnel.Core.Forwarder
{
/// <summary>
/// Extends the IReverseProxyBuilder to support the InMemoryConfigProvider
/// </summary>
public static class InMemoryConfigProviderExtensions
{
public static IReverseProxyBuilder LoadFromMemory(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IProxyConfigProvider>(
new InMemoryConfigProvider(Array.Empty<RouteConfig>(), Array.Empty<ClusterConfig>()));
return builder;
}
}
/// <summary>
/// Provides an implementation of IProxyConfigProvider to support config being generated by code.
/// </summary>
public class InMemoryConfigProvider : IProxyConfigProvider
{
// Marked as volatile so that updates are atomic
private volatile InMemoryConfig _config;
private object locker = new object();
public InMemoryConfigProvider(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
_config = new InMemoryConfig(routes, clusters);
}
/// <summary>
/// Implementation of the IProxyConfigProvider.GetConfig method to supply the current snapshot of configuration
/// </summary>
/// <returns>An immutable snapshot of the current configuration state</returns>
public IProxyConfig GetConfig() => _config;
/// <summary>
/// Swaps the config state with a new snapshot of the configuration, then signals the change
/// </summary>
public void Update(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
var oldConfig = _config;
_config = new InMemoryConfig(routes, clusters);
oldConfig.SignalChange();
}
public void AddWeb(string hostName)
{
lock (locker)
{
var oldConfig = _config;
if (oldConfig.Routes.Any(x => x.ClusterId == hostName))
{
return;
}
var newRoutes = oldConfig.Routes.ToList();
newRoutes.Add(new RouteConfig
{
ClusterId = hostName,
RouteId = hostName,
Match = new RouteMatch { Hosts = new string[] { hostName } }
});
var newClusters = oldConfig.Clusters.ToList();
newClusters.Add(new ClusterConfig
{
ClusterId = hostName,
Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
{
{ "default", new DestinationConfig() {Address = $"http://{hostName}",} }
}
});
_config = new InMemoryConfig(newRoutes, newClusters);
oldConfig.SignalChange();
}
}
/// <summary>
/// Implementation of IProxyConfig which is a snapshot of the current config state. The data for this class should be immutable.
/// </summary>
private class InMemoryConfig : IProxyConfig
{
// Used to implement the change token for the state
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
public InMemoryConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
Routes = routes;
Clusters = clusters;
ChangeToken = new CancellationChangeToken(_cts.Token);
}
/// <summary>
/// A snapshot of the list of routes for the proxy
/// </summary>
public IReadOnlyList<RouteConfig> Routes { get; }
/// <summary>
/// A snapshot of the list of Clusters which are collections of interchangable destination endpoints
/// </summary>
public IReadOnlyList<ClusterConfig> Clusters { get; }
/// <summary>
/// Fired to indicate the the proxy state has changed, and that this snapshot is now stale
/// </summary>
public IChangeToken ChangeToken { get; }
internal void SignalChange()
{
_cts.Cancel();
}
}
}
}

View File

@ -6,22 +6,18 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Forwarder.Kestrel;
internal class ClientConnectionMiddleware
{
readonly ConnectionDelegate next;
readonly ILogger<ClientConnectionMiddleware> logger;
FastTunnelServer fastTunnelServer;
private readonly ConnectionDelegate next;
private readonly ILogger<ClientConnectionMiddleware> logger;
private readonly FastTunnelServer fastTunnelServer;
public ClientConnectionMiddleware(ConnectionDelegate next, ILogger<ClientConnectionMiddleware> logger, FastTunnelServer fastTunnelServer)
{
@ -43,7 +39,7 @@ internal class ClientConnectionMiddleware
/// </summary>
/// <param name="context"></param>
/// <returns>is for FastTunnel</returns>
async Task<bool> ReadPipeAsync(ConnectionContext context)
private async Task<bool> ReadPipeAsync(ConnectionContext context)
{
var reader = context.Transport.Input;

View File

@ -8,7 +8,6 @@ using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
@ -20,9 +19,9 @@ using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Forwarder.Kestrel;
internal class FastTunnelConnectionContext : ConnectionContext
{
private ConnectionContext _inner;
FastTunnelServer fastTunnelServer;
ILogger _logger;
private readonly ConnectionContext _inner;
private readonly FastTunnelServer fastTunnelServer;
private readonly ILogger _logger;
public FastTunnelConnectionContext(ConnectionContext context, FastTunnelServer fastTunnelServer, ILogger logger)
{
@ -48,7 +47,7 @@ internal class FastTunnelConnectionContext : ConnectionContext
return _inner.DisposeAsync();
}
ReadOnlySequence<byte> readableBuffer;
private readonly ReadOnlySequence<byte> readableBuffer;
/// <summary>
/// 解析FastTunnel协议
@ -106,9 +105,8 @@ internal class FastTunnelConnectionContext : ConnectionContext
public string Method;
public string Host = null;
public string MessageId;
bool complete = false;
bool isFirstLine = true;
private bool complete = false;
private bool isFirstLine = true;
/// <summary>
///
@ -154,7 +152,7 @@ internal class FastTunnelConnectionContext : ConnectionContext
if (Method != "PROXY")
{
// 匹配Host
if (fastTunnelServer.TryGetWebProxyByHost(Host, out WebInfo web))
if (fastTunnelServer.TryGetWebProxyByHost(Host, out var web))
{
MatchWeb = web;
}

View File

@ -4,11 +4,6 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using Microsoft.AspNetCore.Connections;
@ -18,9 +13,9 @@ namespace FastTunnel.Core.Forwarder.Kestrel;
internal class HandleHttpConnectionMiddleware
{
readonly ConnectionDelegate next;
readonly ILogger<HandleHttpConnectionMiddleware> logger;
FastTunnelServer fastTunnelServer;
private readonly ConnectionDelegate next;
private readonly ILogger<HandleHttpConnectionMiddleware> logger;
private readonly FastTunnelServer fastTunnelServer;
public HandleHttpConnectionMiddleware(ConnectionDelegate next, ILogger<HandleHttpConnectionMiddleware> logger, FastTunnelServer fastTunnelServer)
{

View File

@ -6,10 +6,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
@ -25,9 +22,9 @@ namespace FastTunnel.Core.Forwarder.Kestrel;
internal class SwapConnectionMiddleware
{
readonly ConnectionDelegate next;
readonly ILogger<SwapConnectionMiddleware> logger;
FastTunnelServer fastTunnelServer;
private readonly ConnectionDelegate next;
private readonly ILogger<SwapConnectionMiddleware> logger;
private readonly FastTunnelServer fastTunnelServer;
public SwapConnectionMiddleware(ConnectionDelegate next, ILogger<SwapConnectionMiddleware> logger, FastTunnelServer fastTunnelServer)
{

View File

@ -6,12 +6,9 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Extensions;
@ -76,7 +73,7 @@ internal class DuplexPipeStream : Stream
public override int Read(byte[] buffer, int offset, int count)
{
ValueTask<int> vt = ReadAsyncInternal(new Memory<byte>(buffer, offset, count), default);
var vt = ReadAsyncInternal(new Memory<byte>(buffer, offset, count), default);
return vt.IsCompleted ?
vt.Result :
vt.AsTask().GetAwaiter().GetResult();

View File

@ -1,26 +1,28 @@
// Copyright (c) 2019-2022 Gui.H. https://github.com/FastTunnel/FastTunnel
// The FastTunnel licenses this file to you under the Apache License Version 2.0.
// For more details,You may obtain License file at: https://github.com/FastTunnel/FastTunnel/blob/v2/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 System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Handlers.Server;
using FastTunnel.Core.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Forwarder.MiddleWare
{
namespace FastTunnel.Core.Forwarder.MiddleWare;
public class FastTunnelClientHandler
{
readonly ILogger<FastTunnelClientHandler> logger;
readonly FastTunnelServer fastTunnelServer;
readonly Version serverVersion;
readonly ILoginHandler loginHandler;
private readonly ILogger<FastTunnelClientHandler> logger;
private readonly FastTunnelServer fastTunnelServer;
private readonly Version serverVersion;
private readonly ILoginHandler loginHandler;
public FastTunnelClientHandler(
ILogger<FastTunnelClientHandler> logger, FastTunnelServer fastTunnelServer, ILoginHandler loginHandler)
@ -110,4 +112,3 @@ namespace FastTunnel.Core.Forwarder.MiddleWare
return false;
}
}
}

View File

@ -1,20 +1,23 @@
// 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.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FastTunnel.Core.Forwarder.MiddleWare
{
namespace FastTunnel.Core.Forwarder.MiddleWare;
public class FastTunnelSwapHandler
{
ILogger<FastTunnelClientHandler> logger;
FastTunnelServer fastTunnelServer;
private readonly ILogger<FastTunnelClientHandler> logger;
private readonly FastTunnelServer fastTunnelServer;
public FastTunnelSwapHandler(ILogger<FastTunnelClientHandler> logger, FastTunnelServer fastTunnelServer)
{
@ -68,4 +71,3 @@ namespace FastTunnel.Core.Forwarder.MiddleWare
}
}
}
}

View File

@ -5,17 +5,15 @@
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Forwarder.MiddleWare
{
namespace FastTunnel.Core.Forwarder.MiddleWare;
internal sealed class LoggingStream : Stream
{
private readonly Stream _inner;
@ -84,28 +82,28 @@ namespace FastTunnel.Core.Forwarder.MiddleWare
public override int Read(byte[] buffer, int offset, int count)
{
int read = _inner.Read(buffer, offset, count);
var read = _inner.Read(buffer, offset, count);
Log("[Read]", new ReadOnlySpan<byte>(buffer, offset, read));
return read;
}
public override int Read(Span<byte> destination)
{
int read = _inner.Read(destination);
var read = _inner.Read(destination);
Log("[Read]", destination.Slice(0, read));
return read;
}
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
int read = await _inner.ReadAsync(buffer.AsMemory(offset, count), cancellationToken);
var read = await _inner.ReadAsync(buffer.AsMemory(offset, count), cancellationToken);
Log("[ReadAsync]", new ReadOnlySpan<byte>(buffer, offset, read));
return read;
}
public override async ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default)
{
int read = await _inner.ReadAsync(destination, cancellationToken);
var read = await _inner.ReadAsync(destination, cancellationToken);
Log("[ReadAsync]", destination.Span.Slice(0, read));
return read;
}
@ -160,7 +158,7 @@ namespace FastTunnel.Core.Forwarder.MiddleWare
var charBuilder = new StringBuilder();
// Write the hex
for (int i = 0; i < buffer.Length; i++)
for (var i = 0; i < buffer.Length; i++)
{
builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture));
builder.Append(' ');
@ -233,4 +231,3 @@ namespace FastTunnel.Core.Forwarder.MiddleWare
TaskToApm.End(asyncResult);
}
}
}

View File

@ -5,10 +5,7 @@
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

View File

@ -9,8 +9,8 @@ using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace FastTunnel.Core.Forwarder
{
namespace FastTunnel.Core.Forwarder;
public class ResponseStream : Stream
{
public override bool CanRead => true;
@ -23,7 +23,7 @@ namespace FastTunnel.Core.Forwarder
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
readonly MemoryStream m_Stream;
private readonly MemoryStream m_Stream;
public ResponseStream(byte[] bytes)
{
@ -35,7 +35,7 @@ namespace FastTunnel.Core.Forwarder
throw new NotImplementedException();
}
bool complete;
private bool complete;
public override int Read(byte[] buffer, int offset, int count)
{
@ -80,4 +80,3 @@ namespace FastTunnel.Core.Forwarder
return ValueTask.CompletedTask;
}
}
}

View File

@ -1,18 +1,17 @@
// Copyright (c) 2019-2022 Gui.H. https://github.com/FastTunnel/FastTunnel
// The FastTunnel licenses this file to you under the Apache License Version 2.0.
// For more details,You may obtain License file at: https://github.com/FastTunnel/FastTunnel/blob/v2/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 Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace FastTunnel.Core.Forwarder;
namespace FastTunnel.Core.Forwarder
{
public class TranStream : Stream
{
private readonly Stream readStream;
@ -108,4 +107,3 @@ namespace FastTunnel.Core.Forwarder
base.Close();
}
}
}

View File

@ -1,17 +1,18 @@
using Microsoft;
using Microsoft.AspNetCore.Connections.Features;
// 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.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections.Features;
namespace FastTunnel.Core.Forwarder
{
sealed class WebSocketStream : Stream
namespace FastTunnel.Core.Forwarder;
internal sealed class WebSocketStream : Stream
{
private readonly Stream readStream;
private readonly Stream wirteStream;
@ -109,4 +110,3 @@ namespace FastTunnel.Core.Forwarder
return ValueTask.CompletedTask;
}
}
}

View File

@ -4,19 +4,13 @@
// 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 System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
namespace FastTunnel.Core.Handlers.Client;
namespace FastTunnel.Core.Handlers.Client
{
public interface IClientHandler
{
Task HandlerMsgAsync(FastTunnelClient cleint, string msg, CancellationToken cancellationToken);
}
}

View File

@ -4,20 +4,16 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Config;
using FastTunnel.Core.Models;
using Microsoft.Extensions.Logging;
using System;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Client;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Handlers.Client;
namespace FastTunnel.Core.Handlers.Client
{
public class LogHandler : IClientHandler
{
ILogger<LogHandler> _logger;
private readonly ILogger<LogHandler> _logger;
public LogHandler(ILogger<LogHandler> logger)
{
@ -30,4 +26,3 @@ namespace FastTunnel.Core.Handlers.Client
await Task.CompletedTask;
}
}
}

View File

@ -1,23 +1,25 @@
// Copyright (c) 2019-2022 Gui.H. https://github.com/FastTunnel/FastTunnel
// The FastTunnel licenses this file to you under the Apache License Version 2.0.
// For more details,You may obtain License file at: https://github.com/FastTunnel/FastTunnel/blob/v2/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 System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Sockets;
using Microsoft.Extensions.Logging;
using System.Net.Security;
namespace FastTunnel.Core.Handlers.Client
{
namespace FastTunnel.Core.Handlers.Client;
public class SwapHandler : IClientHandler
{
readonly ILogger<SwapHandler> _logger;
private readonly ILogger<SwapHandler> _logger;
public SwapHandler(ILogger<SwapHandler> logger)
{
@ -32,8 +34,8 @@ namespace FastTunnel.Core.Handlers.Client
try
{
using (Stream serverStream = await createRemote(requestId, cleint, cancellationToken))
using (Stream localStream = await createLocal(requestId, address, cancellationToken))
using (var serverStream = await createRemote(requestId, cleint, cancellationToken))
using (var localStream = await createLocal(requestId, address, cancellationToken))
{
var taskX = serverStream.CopyToAsync(localStream, cancellationToken);
var taskY = localStream.CopyToAsync(serverStream, cancellationToken);
@ -42,7 +44,7 @@ namespace FastTunnel.Core.Handlers.Client
{
await Task.WhenAll(taskX, taskY);
}
catch (Exception ex)
catch (Exception)
{
}
@ -83,4 +85,3 @@ namespace FastTunnel.Core.Handlers.Client
return serverStream;
}
}
}

View File

@ -4,29 +4,25 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.IO;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Exceptions;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Models;
using FastTunnel.Core.Sockets;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Handlers.Server
{
namespace FastTunnel.Core.Handlers.Server;
public class ForwardDispatcher
{
private FastTunnelServer _server;
private ForwardConfig _config;
ILogger logger;
private readonly FastTunnelServer _server;
private readonly ForwardConfig _config;
private readonly ILogger logger;
public ForwardDispatcher(ILogger logger, FastTunnelServer server, ForwardConfig config)
{
@ -109,4 +105,3 @@ namespace FastTunnel.Core.Handlers.Server
}
}
}
}

View File

@ -4,21 +4,15 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Client;
using FastTunnel.Core.Models;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
namespace FastTunnel.Core.Handlers.Server;
namespace FastTunnel.Core.Handlers.Server
{
public interface IClientMessageHandler
{
bool NeedRecive { get; }
Task<bool> HandlerMsg(FastTunnelServer server, WebSocket client, string msg);
}
}

View File

@ -4,14 +4,13 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Models;
using System.Threading.Tasks;
namespace FastTunnel.Core.Handlers.Server
{
namespace FastTunnel.Core.Handlers.Server;
public interface ILoginHandler
{
Task<bool> HandlerMsg(FastTunnelServer fastTunnelServer, TunnelClient tunnelClient, string lineCmd);
}
}

View File

@ -4,37 +4,33 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Forwarder;
using FastTunnel.Core.Listener;
using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Configuration;
using FastTunnel.Core.Client;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Listener;
using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Handlers.Server;
namespace FastTunnel.Core.Handlers.Server
{
public class LoginHandler : ILoginHandler
{
readonly ILogger logger;
readonly IProxyConfigProvider proxyConfig;
private readonly ILogger logger;
public const bool NeedRecive = true;
public LoginHandler(ILogger<LoginHandler> logger, IProxyConfigProvider proxyConfig)
public LoginHandler(ILogger<LoginHandler> logger)
{
this.proxyConfig = proxyConfig;
this.logger = logger;
}
protected async Task HandleLoginAsync(FastTunnelServer server, TunnelClient client, LogInMassage requet)
{
bool hasTunnel = false;
var hasTunnel = false;
await client.webSocket.SendCmdAsync(MessageType.Log, $"穿透协议 | 映射关系(公网=>内网)", CancellationToken.None);
Thread.Sleep(300);
@ -49,7 +45,6 @@ namespace FastTunnel.Core.Handlers.Server
logger.LogDebug($"new domain '{hostName}'");
server.WebList.AddOrUpdate(hostName, info, (key, oldInfo) => { return info; });
(proxyConfig as InMemoryConfigProvider).AddWeb(hostName);
await client.webSocket.SendCmdAsync(MessageType.Log, $" HTTP | http://{hostName}:{client.ConnectionPort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None);
client.AddWeb(info);
@ -61,7 +56,6 @@ namespace FastTunnel.Core.Handlers.Server
// TODO:validateDomain
hostName = www.Trim().ToLower();
server.WebList.AddOrUpdate(www, info, (key, oldInfo) => { return info; });
(proxyConfig as InMemoryConfigProvider).AddWeb(www);
await client.webSocket.SendCmdAsync(MessageType.Log, $" HTTP | http://{www}:{client.ConnectionPort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None);
client.AddWeb(info);
@ -80,12 +74,11 @@ namespace FastTunnel.Core.Handlers.Server
{
try
{
ForwardInfo<ForwardHandlerArg> old;
if (server.ForwardList.TryGetValue(item.RemotePort, out old))
if (server.ForwardList.TryGetValue(item.RemotePort, out var old))
{
logger.LogDebug($"Remove Listener {old.Listener.ListenIp}:{old.Listener.ListenPort}");
old.Listener.Stop();
server.ForwardList.TryRemove(item.RemotePort, out ForwardInfo<ForwardHandlerArg> _);
server.ForwardList.TryRemove(item.RemotePort, out var _);
}
// TODO: 客户端离线时销毁
@ -127,4 +120,3 @@ namespace FastTunnel.Core.Handlers.Server
return NeedRecive;
}
}
}

View File

@ -4,30 +4,29 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Handlers.Server;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Threading;
using FastTunnel.Core.Handlers.Server;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Listener;
namespace FastTunnel.Core.Listener
{
public class PortProxyListener
{
readonly ILogger _logerr;
private readonly ILogger _logerr;
public string ListenIp { get; set; }
public int ListenPort { get; set; }
int m_numConnectedSockets;
bool shutdown;
ForwardDispatcher _requestDispatcher;
readonly Socket listenSocket;
readonly WebSocket client;
private int m_numConnectedSockets;
private bool shutdown;
private ForwardDispatcher _requestDispatcher;
private readonly Socket listenSocket;
private readonly WebSocket client;
public PortProxyListener(string ip, int port, ILogger logerr, WebSocket client)
{
@ -36,8 +35,8 @@ namespace FastTunnel.Core.Listener
this.ListenIp = ip;
this.ListenPort = port;
IPAddress ipa = IPAddress.Parse(ListenIp);
IPEndPoint localEndPoint = new IPEndPoint(ipa, ListenPort);
var ipa = IPAddress.Parse(ListenIp);
var localEndPoint = new IPEndPoint(ipa, ListenPort);
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(localEndPoint);
@ -67,7 +66,7 @@ namespace FastTunnel.Core.Listener
acceptEventArg.AcceptSocket = null;
}
bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
var willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
if (!willRaiseEvent)
{
ProcessAcceptAsync(acceptEventArg);
@ -124,4 +123,3 @@ namespace FastTunnel.Core.Listener
}
}
}
}

View File

@ -4,8 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
namespace FastTunnel.Core.Models
{
namespace FastTunnel.Core.Models;
public class ForwardConfig
{
/// <summary>
@ -35,4 +35,3 @@ namespace FastTunnel.Core.Models
UDP = 1,
}
}

View File

@ -4,17 +4,13 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
namespace FastTunnel.Core.Models
{
namespace FastTunnel.Core.Models;
public class ForwardHandlerArg
{
public ForwardConfig SSHConfig { get; set; }
public Socket LocalClient { get; set; }
}
}

View File

@ -4,15 +4,11 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Listener;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using FastTunnel.Core.Listener;
namespace FastTunnel.Core.Models;
namespace FastTunnel.Core.Models
{
public class ForwardInfo<T>
{
public WebSocket Socket { get; set; }
@ -21,4 +17,3 @@ namespace FastTunnel.Core.Models
public PortProxyListener Listener { get; set; }
}
}

View File

@ -6,8 +6,8 @@
using System.Collections.Generic;
namespace FastTunnel.Core.Models.Massage
{
namespace FastTunnel.Core.Models.Massage;
public class LogInMassage : TunnelMassage
{
/// <summary>
@ -20,4 +20,3 @@ namespace FastTunnel.Core.Models.Massage
/// </summary>
public IEnumerable<ForwardConfig> Forwards { get; set; }
}
}

View File

@ -4,9 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
namespace FastTunnel.Core.Models.Massage
{
namespace FastTunnel.Core.Models.Massage;
public class TunnelMassage
{
}
}

View File

@ -4,12 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Models;
namespace FastTunnel.Core.Models
{
public struct Message<T>
{
public MessageType MessageType { get; set; }
@ -24,4 +20,3 @@ namespace FastTunnel.Core.Models
Forward = 3,
Log = 4,
}
}

View File

@ -4,20 +4,18 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Client;
using FastTunnel.Core.Handlers.Server;
using FastTunnel.Core.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Models
{
namespace FastTunnel.Core.Models;
public class TunnelClient
{
public WebSocket webSocket { get; private set; }
@ -27,13 +25,13 @@ namespace FastTunnel.Core.Models
/// </summary>
public int ConnectionPort { get; set; }
readonly FastTunnelServer fastTunnelServer;
readonly ILoginHandler loginHandler;
private readonly FastTunnelServer fastTunnelServer;
private readonly ILoginHandler loginHandler;
public IPAddress RemoteIpAddress { get; private set; }
readonly IList<WebInfo> webInfos = new List<WebInfo>();
readonly IList<ForwardInfo<ForwardHandlerArg>> forwardInfos = new List<ForwardInfo<ForwardHandlerArg>>();
private readonly IList<WebInfo> webInfos = new List<WebInfo>();
private readonly IList<ForwardInfo<ForwardHandlerArg>> forwardInfos = new List<ForwardInfo<ForwardHandlerArg>>();
public TunnelClient(WebSocket webSocket, FastTunnelServer fastTunnelServer, ILoginHandler loginHandler, IPAddress remoteIpAddress)
{
@ -104,4 +102,3 @@ namespace FastTunnel.Core.Models
webSocket.CloseAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None);
}
}
}

View File

@ -4,12 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Models;
namespace FastTunnel.Core.Models
{
public class WebConfig
{
/// <summary>
@ -32,4 +28,3 @@ namespace FastTunnel.Core.Models
/// </summary>
public string[] WWW { get; set; }
}
}

View File

@ -5,13 +5,10 @@
// Copyright (c) 2019 Gui.H
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
namespace FastTunnel.Core.Models
{
namespace FastTunnel.Core.Models;
public class WebInfo
{
public WebSocket Socket { get; set; }
@ -24,4 +21,3 @@ namespace FastTunnel.Core.Models
throw new NotImplementedException();
}
}
}

View File

@ -4,17 +4,17 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Extensions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FastTunnel.Core.Extensions;
namespace FastTunnel.Core.Protocol;
namespace FastTunnel.Core.Protocol
{
public class TunnelProtocol
{
string massgeTemp;
string m_sectionFlag = "\n";
private string massgeTemp;
private readonly string m_sectionFlag = "\n";
public IEnumerable<string> HandleBuffer(byte[] buffer, int offset, int count)
{
@ -41,4 +41,3 @@ namespace FastTunnel.Core.Protocol
}
}
}
}

View File

@ -4,24 +4,19 @@
// 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;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.ExceptionServices;
using System.IO;
using FastTunnel.Core.Client;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace FastTunnel.Core.Services;
namespace FastTunnel.Core.Services
{
public class ServiceFastTunnelClient : IHostedService
{
readonly ILogger<ServiceFastTunnelClient> _logger;
readonly IFastTunnelClient _fastTunnelClient;
private readonly ILogger<ServiceFastTunnelClient> _logger;
private readonly IFastTunnelClient _fastTunnelClient;
public ServiceFastTunnelClient(ILogger<ServiceFastTunnelClient> logger, IFastTunnelClient fastTunnelClient)
{
@ -56,4 +51,3 @@ namespace FastTunnel.Core.Services
}
}
}
}

View File

@ -4,25 +4,19 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace FastTunnel.Core.Sockets
{
namespace FastTunnel.Core.Sockets;
public class DnsSocketFactory
{
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);
var dnsEndPoint = new DnsEndPoint(host, port);
await Socket.ConnectAsync(dnsEndPoint);
return Socket;
}
}
}

View File

@ -6,8 +6,8 @@
using System;
namespace FastTunnel.Core.Utilitys
{
namespace FastTunnel.Core.Utilitys;
public static class AssemblyUtility
{
public static Version GetVersion()
@ -15,4 +15,3 @@ namespace FastTunnel.Core.Utilitys
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
}
}
}

View File

@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastTunnel.Hosting", "FastTunnel.Hosting\FastTunnel.Hosting.csproj", "{D7F07110-E85C-4F0E-B479-AEC9760CD2A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -40,6 +42,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
{D7F07110-E85C-4F0E-B479-AEC9760CD2A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7F07110-E85C-4F0E-B479-AEC9760CD2A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7F07110-E85C-4F0E-B479-AEC9760CD2A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7F07110-E85C-4F0E-B479-AEC9760CD2A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -47,6 +53,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}
{D7F07110-E85C-4F0E-B479-AEC9760CD2A7} = {0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3D9C6B44-6706-4EE8-9043-802BBE474A2E}