【fix】1.3.1-JDK17-SNAPSHOT

This commit is contained in:
wujiawei 2025-05-03 10:40:46 +08:00
parent 6e79901be3
commit 7b26e9cb60
5 changed files with 10 additions and 132 deletions

View File

@ -14,8 +14,8 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyP
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socket.AbstractHandleSocketLocalProxyTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socket.AbstractHandleSocketLocalProxyTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory; import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyProxy2realInboundHandler; import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyProxy2RealInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocketReal2ProxyInboundHandler; import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocketBackendHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler; import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -48,7 +48,7 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
.handler(new ChannelInitializer<SocketChannel>() { .handler(new ChannelInitializer<SocketChannel>() {
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettySocketReal2ProxyInboundHandler(channelHandlerContext)); ch.pipeline().addLast(new NettySocketBackendHandler(channelHandlerContext));
} }
}); });
log.info("准备连接目标服务器ip={},port={}", host, port); log.info("准备连接目标服务器ip={},port={}", host, port);
@ -57,7 +57,7 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
if (future.isSuccess()) { if (future.isSuccess()) {
log.info("目标服务器连接成功"); log.info("目标服务器连接成功");
//添加客户端转发请求到服务端的Handler //添加客户端转发请求到服务端的Handler
channel.pipeline().addLast(new NettyProxy2realInboundHandler(future)); channel.pipeline().addLast(new NettyProxy2RealInboundHandler(future));
DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType); DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
channel.writeAndFlush(commandResponse); channel.writeAndFlush(commandResponse);
channel.pipeline().remove(NettySocks5CommandRequestHandler.class); channel.pipeline().remove(NettySocks5CommandRequestHandler.class);

View File

@ -11,11 +11,11 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class NettyProxy2realInboundHandler extends ChannelInboundHandlerAdapter { public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter {
private final ChannelFuture dstChannelFuture; private final ChannelFuture dstChannelFuture;
public NettyProxy2realInboundHandler(ChannelFuture dstChannelFuture) { public NettyProxy2RealInboundHandler(ChannelFuture dstChannelFuture) {
this.dstChannelFuture = dstChannelFuture; this.dstChannelFuture = dstChannelFuture;
} }
@ -47,7 +47,7 @@ public class NettyProxy2realInboundHandler extends ChannelInboundHandlerAdapter
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettyProxy2realInboundHandler exception", cause); log.error("NettyProxy2RealInboundHandler exception", cause);
ctx.close(); ctx.close();
} }
} }

View File

@ -9,11 +9,11 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class NettySocketReal2ProxyInboundHandler extends ChannelInboundHandlerAdapter { public class NettySocketBackendHandler extends ChannelInboundHandlerAdapter {
private final ChannelHandlerContext clientChannelHandlerContext; private final ChannelHandlerContext clientChannelHandlerContext;
public NettySocketReal2ProxyInboundHandler(ChannelHandlerContext clientChannelHandlerContext) { public NettySocketBackendHandler(ChannelHandlerContext clientChannelHandlerContext) {
this.clientChannelHandlerContext = clientChannelHandlerContext; this.clientChannelHandlerContext = clientChannelHandlerContext;
} }
@ -43,7 +43,7 @@ public class NettySocketReal2ProxyInboundHandler extends ChannelInboundHandlerAd
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettySocketReal2ProxyInboundHandler exception", cause); log.error("NettySocketBackendHandler exception", cause);
ctx.close(); ctx.close();
} }
} }

View File

@ -1,38 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class NettyTcpProxyBackendHandler extends ChannelInboundHandlerAdapter {
private final Channel inboundChannel;
public NettyTcpProxyBackendHandler(Channel inboundChannel) {
this.inboundChannel = inboundChannel;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
inboundChannel.writeAndFlush(msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
closeOnFlush(inboundChannel);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}

View File

@ -1,84 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import java.net.URI;
/**
* description 服务端数据处理器
*
* @author 吴佳伟
* @date 2023/09/13 10:27
*/
@Slf4j
public class NettyTcpProxyHandler extends ChannelInboundHandlerAdapter {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyTcpProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
private Channel outboundChannel;
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
if (outboundChannel == null) {
// 假设目标服务器地址和端口
String targetHost = "example.com";
int targetPort = 80;
Bootstrap b = new Bootstrap();
b.group(ctx.channel().eventLoop())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyTcpProxyBackendHandler(ctx.channel()));
}
});
ChannelFuture f = b.connect(targetHost, targetPort);
outboundChannel = f.channel();
f.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
outboundChannel.writeAndFlush(msg);
} else {
ctx.channel().close();
}
});
} else {
outboundChannel.writeAndFlush(msg);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (outboundChannel != null) {
closeOnFlush(outboundChannel);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}