From 7dc576304de0fe4061c576e40e6dff81c961b913 Mon Sep 17 00:00:00 2001 From: "Gui.H" Date: Tue, 5 Jul 2022 16:59:15 +0800 Subject: [PATCH] 3.0.0 beta2 --- .editorconfig | 29 ++- Dockerfile | 4 +- .../Controllers/AccountController.cs | 10 +- .../Controllers/SystemController.cs | 7 +- FastTunnel.Api/FastTunnelApiHostingStartup.cs | 5 - FastTunnel.Client/FastTunnel.Client.csproj | 10 + FastTunnel.Client/Program.cs | 14 +- .../appsettings.Development.json | 7 + FastTunnel.Client/appsettings.json | 29 ++- FastTunnel.Core/Client/FastTunnelClient.cs | 6 +- .../Forwarder/FastTunelProtocol.cs | 4 - .../Kestrel/Features/FastTunnelFeature.cs | 4 - .../Kestrel/Features/IFastTunnelFeature.cs | 5 - .../Kestrel/MiddleWare/ForwarderMiddleware.cs | 10 +- .../MiddleWare/InitializerMiddleware.cs | 1 - .../Forwarder/Streams/DuplexPipeStream.cs | 1 - .../Forwarder/Streams/SocketDuplexPipe.cs | 3 - .../Handlers/Client/SwapHandler.cs | 4 +- .../Handlers/Server/ForwardDispatcher.cs | 14 +- .../Handlers/Server/LoginHandler.cs | 2 +- FastTunnel.Core/Listener/PortProxyListener.cs | 1 - FastTunnel.Core/Models/WebInfo.cs | 1 - FastTunnel.Core/Refs/DuplexPipeStream.cs | 173 ------------------ .../Refs/DuplexPipeStreamAdapter.cs | 57 ------ FastTunnel.Core/Server/FastTunnelServer.cs | 1 - FastTunnel.Core/Utilitys/SwapUtility.cs | 6 +- .../FastTunnelHostingStartup.cs | 4 +- FastTunnel.Server/FastTunnel.Server.csproj | 3 + FastTunnel.Server/Program.cs | 8 +- FastTunnel.Server/Startup.cs | 4 - .../config/appsettings.Development.json | 13 +- FastTunnel.Server/config/appsettings.json | 28 ++- FastTunnel.Server/{cmd => }/install.bat | 2 +- FastTunnel.Server/{cmd => }/uninstall.bat | 2 +- 34 files changed, 133 insertions(+), 339 deletions(-) create mode 100644 FastTunnel.Client/appsettings.Development.json delete mode 100644 FastTunnel.Core/Refs/DuplexPipeStream.cs delete mode 100644 FastTunnel.Core/Refs/DuplexPipeStreamAdapter.cs rename FastTunnel.Server/{cmd => }/install.bat (90%) rename FastTunnel.Server/{cmd => }/uninstall.bat (83%) diff --git a/.editorconfig b/.editorconfig index 61cc4da..5bf1bd9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -50,7 +50,7 @@ csharp_new_line_before_members_in_object_initializers = true csharp_new_line_before_members_in_anonymous_types = true # Namespace settings -csharp_style_namespace_declarations = file_scoped +csharp_style_namespace_declarations = file_scoped:silent [*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}] indent_size = 2 @@ -213,6 +213,16 @@ file_header_template = Licensed under the Apache License, Version 2.0 (the "Lice # IDE0161: Convert to file-scoped namespace dotnet_diagnostic.IDE0161.severity = warning +end_of_line = crlf +dotnet_style_qualification_for_field = false:suggestion +dotnet_style_qualification_for_property = false:suggestion +dotnet_style_qualification_for_method = false:silent +dotnet_style_qualification_for_event = false:silent +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent +dotnet_code_quality_unused_parameters = all:suggestion +dotnet_style_readonly_field = true:suggestion +tab_width = 4 +indent_size = 4 [**/{test,samples,perf}/**.{cs,vb}] # CA1018: Mark attributes with AttributeUsageAttribute @@ -309,6 +319,23 @@ dotnet_naming_style.pascal_case.required_prefix = dotnet_naming_style.pascal_case.required_suffix = dotnet_naming_style.pascal_case.word_separator = dotnet_naming_style.pascal_case.capitalization = pascal_case +csharp_using_directive_placement = outside_namespace:silent +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = false:silent +csharp_style_conditional_delegate_call = true:suggestion +csharp_prefer_simple_using_statement = true:suggestion +csharp_prefer_braces = true:silent +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_top_level_statements = true:silent +csharp_prefer_static_local_function = true:suggestion +csharp_space_around_binary_operators = before_and_after +csharp_indent_labels = one_less_than_current [*.vb] #### ʽ #### diff --git a/Dockerfile b/Dockerfile index 08b6316..5e13001 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /src COPY ["FastTunnel.Server/FastTunnel.Server.csproj", "FastTunnel.Server/"] COPY ["FastTunnel.Api/FastTunnel.Api.csproj", "FastTunnel.Api/"] diff --git a/FastTunnel.Api/Controllers/AccountController.cs b/FastTunnel.Api/Controllers/AccountController.cs index e245537..5301000 100644 --- a/FastTunnel.Api/Controllers/AccountController.cs +++ b/FastTunnel.Api/Controllers/AccountController.cs @@ -4,17 +4,17 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; using FastTunnel.Api.Models; using FastTunnel.Core.Config; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; -using System; -using System.Collections.Generic; -using System.IdentityModel.Tokens.Jwt; -using System.Linq; -using System.Security.Claims; namespace FastTunnel.Api.Controllers { diff --git a/FastTunnel.Api/Controllers/SystemController.cs b/FastTunnel.Api/Controllers/SystemController.cs index 553e125..405d3f7 100644 --- a/FastTunnel.Api/Controllers/SystemController.cs +++ b/FastTunnel.Api/Controllers/SystemController.cs @@ -4,14 +4,11 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using System; +using System.Linq; using FastTunnel.Api.Models; using FastTunnel.Core.Server; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace FastTunnel.Api.Controllers { diff --git a/FastTunnel.Api/FastTunnelApiHostingStartup.cs b/FastTunnel.Api/FastTunnelApiHostingStartup.cs index 412e6a2..87e36be 100644 --- a/FastTunnel.Api/FastTunnelApiHostingStartup.cs +++ b/FastTunnel.Api/FastTunnelApiHostingStartup.cs @@ -5,22 +5,17 @@ // Copyright (c) 2019 Gui.H using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Text; -using System.Threading.Tasks; using FastTunnel.Api; using FastTunnel.Api.Filters; using FastTunnel.Core.Config; using FastTunnel.Core.Extensions; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; [assembly: HostingStartup(typeof(FastTunnelApiHostingStartup))] diff --git a/FastTunnel.Client/FastTunnel.Client.csproj b/FastTunnel.Client/FastTunnel.Client.csproj index 0b54fbd..d0f8aad 100644 --- a/FastTunnel.Client/FastTunnel.Client.csproj +++ b/FastTunnel.Client/FastTunnel.Client.csproj @@ -4,6 +4,16 @@ net7.0 Exe + + + + + + PreserveNewest + true + PreserveNewest + + diff --git a/FastTunnel.Client/Program.cs b/FastTunnel.Client/Program.cs index 0a2de75..7c989fc 100644 --- a/FastTunnel.Client/Program.cs +++ b/FastTunnel.Client/Program.cs @@ -4,13 +4,11 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H -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 Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Hosting; +using Serilog; namespace FastTunnel.Client; @@ -49,12 +47,12 @@ class Program Host.CreateDefaultBuilder(args) .UseWindowsService() .UseSerilog((context, services, configuration) => configuration - .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day) - .WriteTo.Console()) + .ReadFrom.Configuration(context.Configuration) + .WriteTo.Console()) .ConfigureServices((hostContext, services) => { // -------------------FastTunnel START------------------ - services.AddFastTunnelClient(hostContext.Configuration.GetSection("ClientSettings")); + services.AddFastTunnelClient(hostContext.Configuration.GetSection("FastTunnel")); // -------------------FastTunnel EDN-------------------- }); } diff --git a/FastTunnel.Client/appsettings.Development.json b/FastTunnel.Client/appsettings.Development.json new file mode 100644 index 0000000..a576829 --- /dev/null +++ b/FastTunnel.Client/appsettings.Development.json @@ -0,0 +1,7 @@ +{ + "Serilog": { + "MinimumLevel": { + "Default": "Debug" + } + } +} diff --git a/FastTunnel.Client/appsettings.json b/FastTunnel.Client/appsettings.json index 1975247..a32caf7 100644 --- a/FastTunnel.Client/appsettings.json +++ b/FastTunnel.Client/appsettings.json @@ -1,15 +1,24 @@ { - "Logging": { - "LogLevel": { - // Trace Debug Information Warning Error - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "WriteTo": [ + //{ + // "Name": "File", + // "Args": { + // "path": "Logs/log-.log", + // "rollingInterval": 3 + // } + //} + ] }, - // 是否启用文件日志输出 - "EnableFileLog": false, - "ClientSettings": { + "FastTunnel": { "Server": { // [必选] 服务端ip/域名(来自服务端配置文件的urls参数) "ServerAddr": "test.cc", diff --git a/FastTunnel.Core/Client/FastTunnelClient.cs b/FastTunnel.Core/Client/FastTunnelClient.cs index bdb01ae..e5145aa 100644 --- a/FastTunnel.Core/Client/FastTunnelClient.cs +++ b/FastTunnel.Core/Client/FastTunnelClient.cs @@ -20,6 +20,9 @@ using Microsoft.Extensions.Options; namespace FastTunnel.Core.Client; +/// +/// 客户端 +/// public class FastTunnelClient : IFastTunnelClient { private ClientWebSocket socket; @@ -48,8 +51,7 @@ public class FastTunnelClient : IFastTunnelClient /// /// 启动客户端 /// - /// - /// 自定义登录信息,可进行扩展业务 + /// public async void StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("===== FastTunnel Client Start ====="); diff --git a/FastTunnel.Core/Forwarder/FastTunelProtocol.cs b/FastTunnel.Core/Forwarder/FastTunelProtocol.cs index d666598..b050dde 100644 --- a/FastTunnel.Core/Forwarder/FastTunelProtocol.cs +++ b/FastTunnel.Core/Forwarder/FastTunelProtocol.cs @@ -6,10 +6,7 @@ using System; using System.Buffers; -using System.Collections.Generic; using System.IO.Pipelines; -using System.Linq; -using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; using FastTunnel.Core.Forwarder.Kestrel.Features; @@ -17,7 +14,6 @@ using FastTunnel.Core.Models; using FastTunnel.Core.Protocol; using FastTunnel.Core.Server; using Microsoft.AspNetCore.Connections; -using Microsoft.Extensions.FileSystemGlobbing; namespace FastTunnel.Core.Forwarder; diff --git a/FastTunnel.Core/Forwarder/Kestrel/Features/FastTunnelFeature.cs b/FastTunnel.Core/Forwarder/Kestrel/Features/FastTunnelFeature.cs index d38cdfe..f242a6c 100644 --- a/FastTunnel.Core/Forwarder/Kestrel/Features/FastTunnelFeature.cs +++ b/FastTunnel.Core/Forwarder/Kestrel/Features/FastTunnelFeature.cs @@ -4,11 +4,7 @@ // 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.Models; namespace FastTunnel.Core.Forwarder.Kestrel.Features; diff --git a/FastTunnel.Core/Forwarder/Kestrel/Features/IFastTunnelFeature.cs b/FastTunnel.Core/Forwarder/Kestrel/Features/IFastTunnelFeature.cs index 213888b..2d2bdb4 100644 --- a/FastTunnel.Core/Forwarder/Kestrel/Features/IFastTunnelFeature.cs +++ b/FastTunnel.Core/Forwarder/Kestrel/Features/IFastTunnelFeature.cs @@ -4,13 +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; using FastTunnel.Core.Models; -using FastTunnel.Core.Protocol; namespace FastTunnel.Core.Forwarder.Kestrel.Features; diff --git a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/ForwarderMiddleware.cs b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/ForwarderMiddleware.cs index a13e548..b8d8d81 100644 --- a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/ForwarderMiddleware.cs +++ b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/ForwarderMiddleware.cs @@ -5,24 +5,18 @@ // Copyright (c) 2019 Gui.H using System; -using System.Buffers; using System.IO; -using System.IO.Pipelines; using System.Net.WebSockets; -using System.Text; using System.Threading; using System.Threading.Tasks; using FastTunnel.Core.Exceptions; using FastTunnel.Core.Extensions; -using FastTunnel.Core.Forwarder.Kestrel; using FastTunnel.Core.Forwarder.Kestrel.Features; using FastTunnel.Core.Forwarder.Streams; using FastTunnel.Core.Models.Massage; using FastTunnel.Core.Protocol; using FastTunnel.Core.Server; -using FastTunnel.Core.Utilitys; using Microsoft.AspNetCore.Connections; -using Microsoft.AspNetCore.Connections.Features; using Microsoft.Extensions.Logging; namespace FastTunnel.Core.Forwarder.Kestrel.MiddleWare; @@ -132,7 +126,7 @@ internal class ForwarderMiddleware finally { Interlocked.Decrement(ref UserCount); - logger.LogDebug($"=========USER END {requestId}==========="); + logger.LogDebug($"=========USER END {requestId} {UserCount}==========="); fastTunnelServer.ResponseTasks.TryRemove(requestId, out _); await context.Transport.Input.CompleteAsync(); @@ -183,7 +177,7 @@ internal class ForwarderMiddleware finally { Interlocked.Decrement(ref ClientCount); - logger.LogDebug($"=========CLINET END {requestId}==========="); + logger.LogDebug($"=========CLINET END {requestId} {ClientCount}==========="); await context.Transport.Input.CompleteAsync(); await context.Transport.Output.CompleteAsync(); } diff --git a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/InitializerMiddleware.cs b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/InitializerMiddleware.cs index 42abbd0..8531958 100644 --- a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/InitializerMiddleware.cs +++ b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/InitializerMiddleware.cs @@ -5,7 +5,6 @@ // Copyright (c) 2019 Gui.H using System.Threading.Tasks; -using FastTunnel.Core.Forwarder.Kestrel; using FastTunnel.Core.Server; using Microsoft.AspNetCore.Connections; using Microsoft.Extensions.Logging; diff --git a/FastTunnel.Core/Forwarder/Streams/DuplexPipeStream.cs b/FastTunnel.Core/Forwarder/Streams/DuplexPipeStream.cs index 825d535..79a5a13 100644 --- a/FastTunnel.Core/Forwarder/Streams/DuplexPipeStream.cs +++ b/FastTunnel.Core/Forwarder/Streams/DuplexPipeStream.cs @@ -9,7 +9,6 @@ using System.Buffers; using System.IO; using System.IO.Pipelines; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using System.Threading.Tasks; using FastTunnel.Core.Extensions; diff --git a/FastTunnel.Core/Forwarder/Streams/SocketDuplexPipe.cs b/FastTunnel.Core/Forwarder/Streams/SocketDuplexPipe.cs index 9e06c54..1d18015 100644 --- a/FastTunnel.Core/Forwarder/Streams/SocketDuplexPipe.cs +++ b/FastTunnel.Core/Forwarder/Streams/SocketDuplexPipe.cs @@ -5,11 +5,8 @@ // Copyright (c) 2019 Gui.H using System; -using System.Collections.Generic; using System.IO.Pipelines; -using System.Linq; using System.Net.Sockets; -using System.Text; using System.Threading.Tasks; namespace FastTunnel.Core.Forwarder.Streams; diff --git a/FastTunnel.Core/Handlers/Client/SwapHandler.cs b/FastTunnel.Core/Handlers/Client/SwapHandler.cs index 283b5f1..cc514c0 100644 --- a/FastTunnel.Core/Handlers/Client/SwapHandler.cs +++ b/FastTunnel.Core/Handlers/Client/SwapHandler.cs @@ -46,8 +46,6 @@ public class SwapHandler : IClientHandler var taskY = localStream.CopyToAsync(serverStream, cancellationToken); await Task.WhenAny(taskX, taskY).WaitAsync(cancellationToken); - - _logger.LogDebug($"[HandlerMsgAsync] success {requestId}"); } catch (Exception ex) { @@ -56,7 +54,7 @@ public class SwapHandler : IClientHandler finally { Interlocked.Decrement(ref SwapCount); - _logger.LogDebug($"========Swap End:{requestId}=========="); + _logger.LogDebug($"========Swap End:{requestId} {SwapCount}=========="); } } diff --git a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs index 5fa1392..7325b6e 100644 --- a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs +++ b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs @@ -6,14 +6,12 @@ using System; using System.IO; -using System.IO.Pipelines; using System.Net.Sockets; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; using FastTunnel.Core.Exceptions; using FastTunnel.Core.Extensions; -using FastTunnel.Core.Forwarder.Streams; using FastTunnel.Core.Models; using FastTunnel.Core.Models.Massage; using FastTunnel.Core.Server; @@ -34,6 +32,8 @@ public class ForwardDispatcher _config = config; } + int SwapCount; + /// /// /// @@ -46,6 +46,8 @@ public class ForwardDispatcher (Stream Stream, CancellationTokenSource TokenSource) res = default; + Interlocked.Increment(ref SwapCount); + try { logger.LogDebug($"[Forward]Swap开始 {msgId}|{_config.RemotePort}=>{_config.LocalIp}:{_config.LocalPort}"); @@ -53,7 +55,10 @@ public class ForwardDispatcher var tcs = new TaskCompletionSource<(Stream Stream, CancellationTokenSource TokenSource)>(); tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Dispatch TimeOut]:{msgId}"); }); - _server.ResponseTasks.TryAdd(msgId, tcs); + if (!_server.ResponseTasks.TryAdd(msgId, tcs)) + { + return; + } try { @@ -88,8 +93,9 @@ public class ForwardDispatcher } finally { + Interlocked.Decrement(ref SwapCount); res.TokenSource?.Cancel(); - logger.LogDebug($"[Forward]Swap OK {msgId}"); + logger.LogDebug($"[Forward]Swap OK {msgId} {SwapCount}"); _server.ResponseTasks.TryRemove(msgId, out _); } } diff --git a/FastTunnel.Core/Handlers/Server/LoginHandler.cs b/FastTunnel.Core/Handlers/Server/LoginHandler.cs index 7ba81a2..bbe87a7 100644 --- a/FastTunnel.Core/Handlers/Server/LoginHandler.cs +++ b/FastTunnel.Core/Handlers/Server/LoginHandler.cs @@ -43,7 +43,7 @@ public class LoginHandler : ILoginHandler var hostName = $"{item.SubDomain}.{server.ServerOption.CurrentValue.WebDomain}".Trim().ToLower(); var info = new WebInfo { Socket = client.webSocket, WebConfig = item }; - logger.LogDebug($"new domain '{hostName}'"); + logger.LogDebug($"New Http '{hostName}'"); server.WebList.AddOrUpdate(hostName, info, (key, oldInfo) => { return info; }); await client.webSocket.SendCmdAsync(MessageType.Log, $" HTTP | http://{hostName}:{client.ConnectionPort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); diff --git a/FastTunnel.Core/Listener/PortProxyListener.cs b/FastTunnel.Core/Listener/PortProxyListener.cs index eabb7cd..288b787 100644 --- a/FastTunnel.Core/Listener/PortProxyListener.cs +++ b/FastTunnel.Core/Listener/PortProxyListener.cs @@ -54,7 +54,6 @@ public class PortProxyListener private void StartAccept(SocketAsyncEventArgs acceptEventArg) { - _logerr.LogDebug($"【{ListenIp}:{ListenPort}】: StartAccept"); if (acceptEventArg == null) { acceptEventArg = new SocketAsyncEventArgs(); diff --git a/FastTunnel.Core/Models/WebInfo.cs b/FastTunnel.Core/Models/WebInfo.cs index 1a46b31..69d9c68 100644 --- a/FastTunnel.Core/Models/WebInfo.cs +++ b/FastTunnel.Core/Models/WebInfo.cs @@ -4,7 +4,6 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H -using System; using System.Net.WebSockets; namespace FastTunnel.Core.Models; diff --git a/FastTunnel.Core/Refs/DuplexPipeStream.cs b/FastTunnel.Core/Refs/DuplexPipeStream.cs deleted file mode 100644 index a62748c..0000000 --- a/FastTunnel.Core/Refs/DuplexPipeStream.cs +++ /dev/null @@ -1,173 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Buffers; -using System.IO; -using System.IO.Pipelines; -using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; -using FastTunnel.Core.Extensions; -using Microsoft.AspNetCore.Internal; - -#nullable enable - -namespace FastTunnel.Core.Refs; - -internal class DuplexPipeStream : Stream -{ - private readonly PipeReader _input; - private readonly PipeWriter _output; - private readonly bool _throwOnCancelled; - private volatile bool _cancelCalled; - - public DuplexPipeStream(PipeReader input, PipeWriter output, bool throwOnCancelled = false) - { - _input = input; - _output = output; - _throwOnCancelled = throwOnCancelled; - } - - public void CancelPendingRead() - { - _cancelCalled = true; - _input.CancelPendingRead(); - } - - public override bool CanRead => true; - - public override bool CanSeek => false; - - public override bool CanWrite => true; - - public override long Length - { - get - { - throw new NotSupportedException(); - } - } - - public override long Position - { - get - { - throw new NotSupportedException(); - } - set - { - throw new NotSupportedException(); - } - } - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } - - public override void SetLength(long value) - { - throw new NotSupportedException(); - } - - public override int Read(byte[] buffer, int offset, int count) - { - var vt = ReadAsyncInternal(new Memory(buffer, offset, count), default); - return vt.IsCompleted ? - vt.Result : - vt.AsTask().GetAwaiter().GetResult(); - } - - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default) - { - return ReadAsyncInternal(new Memory(buffer, offset, count), cancellationToken).AsTask(); - } - - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default) - { - return ReadAsyncInternal(destination, cancellationToken); - } - - public override void Write(byte[] buffer, int offset, int count) - { - WriteAsync(buffer, offset, count).GetAwaiter().GetResult(); - } - - public override Task WriteAsync(byte[]? buffer, int offset, int count, CancellationToken cancellationToken) - { - return _output.WriteAsync(buffer.AsMemory(offset, count), cancellationToken).GetAsTask(); - } - - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default) - { - return _output.WriteAsync(source, cancellationToken).GetAsValueTask(); - } - - public override void Flush() - { - FlushAsync(CancellationToken.None).GetAwaiter().GetResult(); - } - - public override Task FlushAsync(CancellationToken cancellationToken) - { - return _output.FlushAsync(cancellationToken).GetAsTask(); - } - - [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))] - private async ValueTask ReadAsyncInternal(Memory destination, CancellationToken cancellationToken) - { - while (true) - { - var result = await _input.ReadAsync(cancellationToken); - var readableBuffer = result.Buffer; - try - { - if (_throwOnCancelled && result.IsCanceled && _cancelCalled) - { - // Reset the bool - _cancelCalled = false; - throw new OperationCanceledException(); - } - - if (!readableBuffer.IsEmpty) - { - // buffer.Count is int - var count = (int)Math.Min(readableBuffer.Length, destination.Length); - readableBuffer = readableBuffer.Slice(0, count); - readableBuffer.CopyTo(destination.Span); - return count; - } - - if (result.IsCompleted) - { - return 0; - } - } - finally - { - _input.AdvanceTo(readableBuffer.End, readableBuffer.End); - } - } - } - - public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - { - return TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); - } - - public override int EndRead(IAsyncResult asyncResult) - { - return TaskToApm.End(asyncResult); - } - - public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - { - return TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); - } - - public override void EndWrite(IAsyncResult asyncResult) - { - TaskToApm.End(asyncResult); - } -} diff --git a/FastTunnel.Core/Refs/DuplexPipeStreamAdapter.cs b/FastTunnel.Core/Refs/DuplexPipeStreamAdapter.cs deleted file mode 100644 index 4ddf2c1..0000000 --- a/FastTunnel.Core/Refs/DuplexPipeStreamAdapter.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.IO; -using System.IO.Pipelines; -using System.Threading.Tasks; - -namespace FastTunnel.Core.Refs; - -/// -/// A helper for wrapping a Stream decorator from an . -/// -/// -internal class DuplexPipeStreamAdapter : DuplexPipeStream, IDuplexPipe where TStream : Stream -{ - private bool _disposed; - private readonly object _disposeLock = new object(); - - public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, Func createStream) : - this(duplexPipe, new StreamPipeReaderOptions(leaveOpen: true), new StreamPipeWriterOptions(leaveOpen: true), createStream) - { - } - - public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, StreamPipeReaderOptions readerOptions, StreamPipeWriterOptions writerOptions, Func createStream) : - base(duplexPipe.Input, duplexPipe.Output) - { - var stream = createStream(this); - Stream = stream; - Input = PipeReader.Create(stream, readerOptions); - Output = PipeWriter.Create(stream, writerOptions); - } - - public TStream Stream { get; } - - public PipeReader Input { get; } - - public PipeWriter Output { get; } - - public override async ValueTask DisposeAsync() - { - lock (_disposeLock) - { - if (_disposed) - { - return; - } - _disposed = true; - } - - await Input.CompleteAsync(); - await Output.CompleteAsync(); - } - - protected override void Dispose(bool disposing) - { - throw new NotSupportedException(); - } -} - diff --git a/FastTunnel.Core/Server/FastTunnelServer.cs b/FastTunnel.Core/Server/FastTunnelServer.cs index 5a43743..84ac39f 100644 --- a/FastTunnel.Core/Server/FastTunnelServer.cs +++ b/FastTunnel.Core/Server/FastTunnelServer.cs @@ -8,7 +8,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; -using System.IO.Pipelines; using System.Threading; using System.Threading.Tasks; using FastTunnel.Core.Config; diff --git a/FastTunnel.Core/Utilitys/SwapUtility.cs b/FastTunnel.Core/Utilitys/SwapUtility.cs index c491869..27d4ce8 100644 --- a/FastTunnel.Core/Utilitys/SwapUtility.cs +++ b/FastTunnel.Core/Utilitys/SwapUtility.cs @@ -4,12 +4,8 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H -using System; using System.Buffers; -using System.Collections.Generic; using System.IO.Pipelines; -using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -40,7 +36,7 @@ internal class SwapUtility result = await pipe.Input.ReadAsync(cancellationToken); readableBuffer = result.Buffer; - + } } } diff --git a/FastTunnel.Hosting/FastTunnelHostingStartup.cs b/FastTunnel.Hosting/FastTunnelHostingStartup.cs index 2c64e8c..6b5ce37 100644 --- a/FastTunnel.Hosting/FastTunnelHostingStartup.cs +++ b/FastTunnel.Hosting/FastTunnelHostingStartup.cs @@ -4,9 +4,9 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using FastTunnel.Core.Extensions; using FastTunnel.Hosting; using Microsoft.AspNetCore.Hosting; -using FastTunnel.Core.Extensions; using Microsoft.Extensions.Configuration; [assembly: HostingStartup(typeof(FastTunnelHostingStartup))] @@ -24,7 +24,7 @@ public class FastTunnelHostingStartup : IHostingStartup builder.UseKestrel((context, options) => { - var basePort = context.Configuration.GetValue("FastTunnel:BasePort") ?? 1270; + var basePort = context.Configuration.GetValue("FastTunnel:BinPort") ?? 1270; options.ListenAnyIP(basePort, listenOptions => { listenOptions.UseConnectionFastTunnel(); diff --git a/FastTunnel.Server/FastTunnel.Server.csproj b/FastTunnel.Server/FastTunnel.Server.csproj index fe8158b..7aa11d7 100644 --- a/FastTunnel.Server/FastTunnel.Server.csproj +++ b/FastTunnel.Server/FastTunnel.Server.csproj @@ -35,6 +35,9 @@ + + Always + Always diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index d3d15c5..d7e3a65 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -4,13 +4,11 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using System; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using FastTunnel.Core.Extensions; using Serilog; -using System; namespace FastTunnel.Server; @@ -49,7 +47,9 @@ public class Program Host.CreateDefaultBuilder(args) .UseWindowsService() .UseSerilog((context, services, configuration) => configuration - .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day) + .ReadFrom.Configuration(context.Configuration) + .ReadFrom.Services(services) + .Enrich.FromLogContext() .WriteTo.Console()) .ConfigureWebHost(webHostBuilder => { diff --git a/FastTunnel.Server/Startup.cs b/FastTunnel.Server/Startup.cs index 287fdc8..d1211fb 100644 --- a/FastTunnel.Server/Startup.cs +++ b/FastTunnel.Server/Startup.cs @@ -5,16 +5,12 @@ // Copyright (c) 2019 Gui.H using FastTunnel.Core.Extensions; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.IdentityModel.Tokens; -using System; -using FastTunnel.Core.Config; using System.Text; #if DEBUG diff --git a/FastTunnel.Server/config/appsettings.Development.json b/FastTunnel.Server/config/appsettings.Development.json index 834acb9..a576829 100644 --- a/FastTunnel.Server/config/appsettings.Development.json +++ b/FastTunnel.Server/config/appsettings.Development.json @@ -1,12 +1,7 @@ { - "Logging": { - "LogLevel": { - // Trace Debug Information Warning Error - "Default": "Debug", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Serilog": { + "MinimumLevel": { + "Default": "Debug" } - }, - "AllowedHosts": "*", - "EnableFileLog": false + } } diff --git a/FastTunnel.Server/config/appsettings.json b/FastTunnel.Server/config/appsettings.json index 3306654..d9a4717 100644 --- a/FastTunnel.Server/config/appsettings.json +++ b/FastTunnel.Server/config/appsettings.json @@ -1,18 +1,30 @@ { - "Logging": { - "LogLevel": { - // Trace Debug Information Warning Error + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "WriteTo": [ + //{ + // "Name": "File", + // "Args": { + // "path": "Logs/log-.log", + // "rollingInterval": 3 + // } + //} + ] }, "AllowedHosts": "*", - // Http&客户端通讯端口 - "urls": "http://*:1270", // 是否启用文件日志输出 "EnableFileLog": false, "FastTunnel": { + // 服务监听端口 + "BinPort": 1270, + // 可选,绑定的根域名, // 客户端需配置SubDomain,实现 ${SubDomain}.${WebDomain}访问内网的站点,注意:需要通过域名访问网站时必选。 "WebDomain": "test.cc", diff --git a/FastTunnel.Server/cmd/install.bat b/FastTunnel.Server/install.bat similarity index 90% rename from FastTunnel.Server/cmd/install.bat rename to FastTunnel.Server/install.bat index a2dba30..9099578 100644 --- a/FastTunnel.Server/cmd/install.bat +++ b/FastTunnel.Server/install.bat @@ -4,7 +4,7 @@ color 0e @echo ================================== @echo 提醒:请右键本文件,用管理员方式打开。 @echo ================================== -@echo Start Install ./../FastTunnel.Server +@echo Start Install FastTunnel.Server sc create FastTunnel.Server binPath=%~dp0\FastTunnel.Server.exe start= auto sc description FastTunnel.Server "FastTunnel-开源内网穿透服务,仓库地址:https://github.com/SpringHgui/FastTunnel star项目以支持作者" diff --git a/FastTunnel.Server/cmd/uninstall.bat b/FastTunnel.Server/uninstall.bat similarity index 83% rename from FastTunnel.Server/cmd/uninstall.bat rename to FastTunnel.Server/uninstall.bat index 809820a..27a65bc 100644 --- a/FastTunnel.Server/cmd/uninstall.bat +++ b/FastTunnel.Server/uninstall.bat @@ -4,7 +4,7 @@ color 0e @echo ================================== @echo 提醒:请右键本文件,用管理员方式打开。 @echo ================================== -@echo Start Remove ./../FastTunnel.Server +@echo Start Remove FastTunnel.Server Net stop FastTunnel.Server sc delete FastTunnel.Server