From 4a924e688c0e4f516b505c846f513cfea4ba476a Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Fri, 9 Aug 2024 21:37:16 -0700 Subject: [PATCH] add server option EnableProxyProtocol --- src/SuperSocket.ProtoBase/Extensions.cs | 6 ------ src/SuperSocket.ProtoBase/PackagePartsPipelineFilter.cs | 5 ++--- .../ProxyProtocol/ProxyProtocolPipelineFilter.cs | 1 - .../ProxyProtocol/ProxyProtocolPipelineFilterFactory.cs | 1 - src/SuperSocket.Server.Abstractions/ServerOptions.cs | 2 ++ src/SuperSocket.Server/SuperSocketService.cs | 8 +++++++- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/SuperSocket.ProtoBase/Extensions.cs b/src/SuperSocket.ProtoBase/Extensions.cs index 7abd5b738..4684f70ad 100644 --- a/src/SuperSocket.ProtoBase/Extensions.cs +++ b/src/SuperSocket.ProtoBase/Extensions.cs @@ -148,11 +148,5 @@ public static int Write(this IBufferWriter writer, ReadOnlySpan text return totalBytes; } - - public static IPipelineFilterFactory UseProxyProtocol(this IPipelineFilterFactory pipelineFilterFactory) - where TPackageInfo : class - { - return new ProxyProtocolPipelineFilterFactory(pipelineFilterFactory); - } } } \ No newline at end of file diff --git a/src/SuperSocket.ProtoBase/PackagePartsPipelineFilter.cs b/src/SuperSocket.ProtoBase/PackagePartsPipelineFilter.cs index 7d6fdb619..2b32d2f99 100644 --- a/src/SuperSocket.ProtoBase/PackagePartsPipelineFilter.cs +++ b/src/SuperSocket.ProtoBase/PackagePartsPipelineFilter.cs @@ -3,7 +3,6 @@ namespace SuperSocket.ProtoBase { public abstract class PackagePartsPipelineFilter : IPipelineFilter - where TPackageInfo : class { private IPackagePartReader _currentPartReader; @@ -40,7 +39,7 @@ public virtual TPackageInfo Filter(SequenceReader reader) } if (needMoreData || reader.Remaining <= 0) - return null; + return default; } } @@ -51,7 +50,7 @@ protected virtual void OnPartReaderSwitched(IPackagePartReader cur public virtual void Reset() { - CurrentPackage = null; + CurrentPackage = default; _currentPartReader = null; } diff --git a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilter.cs b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilter.cs index b885867c9..d8eb339d5 100644 --- a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilter.cs +++ b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilter.cs @@ -4,7 +4,6 @@ namespace SuperSocket.ProtoBase.ProxyProtocol { public class ProxyProtocolPipelineFilter : PackagePartsPipelineFilter - where TPackageInfo : class { private readonly IPipelineFilter _applicationPipelineFilter; diff --git a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilterFactory.cs b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilterFactory.cs index 590629c2b..d02cbbec0 100644 --- a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilterFactory.cs +++ b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolPipelineFilterFactory.cs @@ -4,7 +4,6 @@ namespace SuperSocket.ProtoBase.ProxyProtocol { public class ProxyProtocolPipelineFilterFactory : IPipelineFilterFactory - where TPackageInfo : class { private readonly IPipelineFilterFactory _pipelineFilterFactory; diff --git a/src/SuperSocket.Server.Abstractions/ServerOptions.cs b/src/SuperSocket.Server.Abstractions/ServerOptions.cs index 5f6325595..7bd644171 100644 --- a/src/SuperSocket.Server.Abstractions/ServerOptions.cs +++ b/src/SuperSocket.Server.Abstractions/ServerOptions.cs @@ -20,5 +20,7 @@ public class ServerOptions : ConnectionOptions /// In seconds. /// public int PackageHandlingTimeOut { get; set; } = 30; + + public bool EnableProxyProtocol { get; set; } } } \ No newline at end of file diff --git a/src/SuperSocket.Server/SuperSocketService.cs b/src/SuperSocket.Server/SuperSocketService.cs index ba255d6fa..86916f8ab 100644 --- a/src/SuperSocket.Server/SuperSocketService.cs +++ b/src/SuperSocket.Server/SuperSocketService.cs @@ -15,6 +15,7 @@ using SuperSocket.Server.Abstractions.Session; using SuperSocket.Server.Abstractions.Middleware; using SuperSocket.Server.Connection; +using SuperSocket.ProtoBase.ProxyProtocol; namespace SuperSocket.Server { @@ -113,7 +114,12 @@ public SuperSocketService(IServiceProvider serviceProvider, IOptions GetPipelineFilterFactory() { - return _serviceProvider.GetRequiredService>(); + var filterFactory = _serviceProvider.GetRequiredService>(); + + if (Options.EnableProxyProtocol) + filterFactory = new ProxyProtocolPipelineFilterFactory(filterFactory); + + return filterFactory; } private bool AddConnectionListener(ListenOptions listenOptions, ServerOptions serverOptions)