Skip to content

Commit

Permalink
expose source endpoint through app session
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 10, 2024
1 parent d09bbb3 commit 5149616
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/SuperSocket.Connection/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.ProtoBase;
using SuperSocket.ProtoBase.ProxyProtocol;

namespace SuperSocket.Connection
{
Expand All @@ -30,6 +31,8 @@ public abstract class ConnectionBase : IConnection

public CancellationToken ConnectionToken { get; protected set; }

public ProxyInfo ProxyInfo { get; protected set; }

protected virtual void OnClosed()
{
IsClosed = true;
Expand Down
3 changes: 3 additions & 0 deletions src/SuperSocket.Connection/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.ProtoBase;
using SuperSocket.ProtoBase.ProxyProtocol;

namespace SuperSocket.Connection
{
Expand Down Expand Up @@ -35,5 +36,7 @@ public interface IConnection
CloseReason? CloseReason { get; }

CancellationToken ConnectionToken { get; }

ProxyInfo ProxyInfo { get; }
}
}
7 changes: 7 additions & 0 deletions src/SuperSocket.Connection/PipeConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using SuperSocket.ProtoBase;
using SuperSocket.ProtoBase.ProxyProtocol;

namespace SuperSocket.Connection
{
Expand Down Expand Up @@ -322,6 +323,12 @@ private bool ReaderBuffer<TPackageInfo>(ref ReadOnlySequence<byte> buffer, IPipe

if (nextFilter != null)
{
// ProxyProtocolPipelineFilter always is the first filter and its next filter is the actual first filter.
if (bytesConsumedTotal == 0 && pipelineFilter is IProxyProtocolPipelineFilter proxyProtocolPipelineFilter)
{
ProxyInfo = proxyProtocolPipelineFilter.ProxyInfo;
}

nextFilter.Context = pipelineFilter.Context; // pass through the context
_pipelineFilter = pipelineFilter = nextFilter;
filterSwitched = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Buffers;

namespace SuperSocket.ProtoBase.ProxyProtocol
{
public interface IProxyProtocolPipelineFilter : IPipelineFilter
{
ProxyInfo ProxyInfo { get; }
}
}
10 changes: 10 additions & 0 deletions src/SuperSocket.ProtoBase/ProxyProtocol/ProxyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public class ProxyInfo

internal int AddressLength { get; set; }

public EndPoint SourceEndPoint { get; private set; }

public EndPoint DestinationEndPoint { get; private set; }

internal void Prepare()
{
SourceEndPoint = new IPEndPoint(SourceIPAddress, SourcePort);
DestinationEndPoint = new IPEndPoint(DestinationIPAddress, DestinationPort);
}

public ProxyInfo()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SuperSocket.ProtoBase.ProxyProtocol
{
public class ProxyProtocolPipelineFilter<TPackageInfo> : PackagePartsPipelineFilter<TPackageInfo>
public class ProxyProtocolPipelineFilter<TPackageInfo> : PackagePartsPipelineFilter<TPackageInfo>, IProxyProtocolPipelineFilter
{
private readonly IPipelineFilter<TPackageInfo> _applicationPipelineFilter;

Expand All @@ -29,6 +29,7 @@ protected override IPackagePartReader<TPackageInfo> GetFirstPartReader()
public override void Reset()
{
// This method will be called when the proxy package handling finishes
ProxyInfo.Prepare();
NextFilter = _applicationPipelineFilter;
base.Reset();
Context = _originalFilterContext;
Expand Down
10 changes: 9 additions & 1 deletion src/SuperSocket.Server/AppSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ IConnection IAppSession.Connection

public EndPoint RemoteEndPoint
{
get { return _connection?.RemoteEndPoint; }
get
{
var connection = _connection;

if (connection == null)
return null;

return connection.ProxyInfo?.SourceEndPoint ?? connection.RemoteEndPoint;
}
}

public EndPoint LocalEndPoint
Expand Down

0 comments on commit 5149616

Please sign in to comment.