From 0164e71f37cff66a3d2d88fab7ffd9b59f78cfbf Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sat, 24 May 2025 18:53:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3http=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=8B=86=E5=8C=85=E6=83=85=E5=86=B5=E4=B8=8B=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpVisitorSecurityChannelHandler.java | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java index e817dfe3..d32c25ff 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java @@ -1,10 +1,13 @@ package org.dromara.neutrinoproxy.server.proxy.security; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.checkerframework.checker.i18nformatter.qual.I18nFormat; import org.dromara.neutrinoproxy.core.Constants; import org.dromara.neutrinoproxy.core.util.HttpUtil; import org.dromara.neutrinoproxy.core.util.IpUtil; @@ -28,18 +31,35 @@ public class HttpVisitorSecurityChannelHandler extends ChannelInboundHandlerAdap */ private Boolean isHttps; + // 拼接收到的 ByteBuf 内容 + private ByteBuf cumulationBuf = Unpooled.buffer(); +// private boolean initialized = false; + public HttpVisitorSecurityChannelHandler(Boolean isHttps) { this.isHttps = isHttps; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { +// if (initialized) { +// // 已经初始化,直接透传 +// ctx.fireChannelRead(msg); +// return; +// } + + // 累加数据包 ByteBuf buf = (ByteBuf) msg; + cumulationBuf.writeBytes(buf); + + String dataStr = cumulationBuf.toString(CharsetUtil.UTF_8); + int headerEndIndex = dataStr.indexOf("\r\n\r\n"); + if (-1 == headerEndIndex) { + // 请求头还没读完,继续等 + return; + } // 获取Host请求头 - byte[] bytes = new byte[buf.readableBytes()]; - buf.readBytes(bytes); - String httpContent = new String(bytes); - String host = HttpUtil.getHostIgnorePort(httpContent); //test1.asgc.fun + String headerPart = dataStr.substring(0, headerEndIndex + 4); + String host = HttpUtil.getHostIgnorePort(headerPart); //test1.asgc.fun log.debug("HttpProxy host: {}", host); if (StringUtils.isBlank(host)) { @@ -68,7 +88,7 @@ public class HttpVisitorSecurityChannelHandler extends ChannelInboundHandlerAdap } // 判断IP是否在该端口绑定的安全组允许的规则内 - String ip = IpUtil.getRealRemoteIp(httpContent); + String ip = IpUtil.getRealRemoteIp(headerPart); if (ip == null) { ip = IpUtil.getRemoteIp(ctx); } @@ -82,9 +102,11 @@ public class HttpVisitorSecurityChannelHandler extends ChannelInboundHandlerAdap ctx.channel().attr(Constants.SERVER_PORT).set(serverPort); } - // 继续传播 - buf.resetReaderIndex(); - ctx.fireChannelRead(buf); - } + // 从此之后所有的 in 都是“直接透传”。 + // 否则,若请求体过长,数据包被拆分为多个,后续的数据包都无法解析出host,导致转发数据不完整。 + ctx.pipeline().remove(this); + // 继续传播 + ctx.fireChannelRead(cumulationBuf); + } }