Skip to content

Commit

Permalink
因为ProxyProtocol协议存在,haproxy/nginx等代理会返回原始客户端IP端口
Browse files Browse the repository at this point in the history
这里修改Remote以后,NetSession层将会使用新的Remote地址
  • Loading branch information
nnhy committed Nov 10, 2024
1 parent e4edcd6 commit 09bef9e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion NewLife.Core/Net/NetHandlerContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NewLife.Data;
using System.Net;
using System.Net.Sockets;
using NewLife.Data;
using NewLife.Messaging;
using NewLife.Model;
using NewLife.Serialization;
Expand All @@ -15,6 +17,12 @@ public class NetHandlerContext : HandlerContext
/// <summary>数据帧</summary>
public IData? Data { get; set; }

/// <summary>远程地址。因为ProxyProtocol协议存在,haproxy/nginx等代理会返回原始客户端IP端口</summary>
public IPEndPoint? Remote { get; set; }

/// <summary>Socket事件参数</summary>
public SocketAsyncEventArgs? EventArgs { get; set; }

/// <summary>读取管道过滤后最终处理消息</summary>
/// <param name="message"></param>
public override void FireRead(Object message)
Expand Down Expand Up @@ -67,6 +75,10 @@ public override void FireRead(Object message)
var data = Data ?? new ReceivedEventArgs();
data.Message = message;

// 因为ProxyProtocol协议存在,haproxy/nginx等代理会返回原始客户端IP端口
// 这里修改Remote以后,NetSession层将会使用新的Remote地址
if (Remote != null) data.Remote = Remote;

// 解析协议指令后,事件变量里面的数据是之前的原始报文,有可能多帧指令粘包在一起,需要拆分填充当前指令的数据报文,避免上层重复使用原始大报文
if (message is DefaultMessage dm)
{
Expand Down
1 change: 1 addition & 0 deletions NewLife.Core/Net/SessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ private void ProcessReceive(IPacket pk, SocketAsyncEventArgs se, IPEndPoint remo
{
var ctx = CreateContext(ss);
ctx.Data = e;
ctx.EventArgs = se;

// 进入管道处理,如果有一个或多个结果通过Finish来处理
pp.Read(ctx, pk);
Expand Down

0 comments on commit 09bef9e

Please sign in to comment.