mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 10:25:07 +08:00
【fix】1.3.1-JDK17-SNAPSHOT
This commit is contained in:
@ -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.proxy.socket.AbstractHandleSocketLocalProxyTypeAdvanced;
|
||||
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.NettySocketReal2ProxyInboundHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyProxy2RealInboundHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocketBackendHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -48,7 +48,7 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
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);
|
||||
@ -57,7 +57,7 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
|
||||
if (future.isSuccess()) {
|
||||
log.info("目标服务器连接成功");
|
||||
//添加客户端转发请求到服务端的Handler
|
||||
channel.pipeline().addLast(new NettyProxy2realInboundHandler(future));
|
||||
channel.pipeline().addLast(new NettyProxy2RealInboundHandler(future));
|
||||
DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
|
||||
channel.writeAndFlush(commandResponse);
|
||||
channel.pipeline().remove(NettySocks5CommandRequestHandler.class);
|
||||
|
@ -11,11 +11,11 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class NettyProxy2realInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private final ChannelFuture dstChannelFuture;
|
||||
|
||||
public NettyProxy2realInboundHandler(ChannelFuture dstChannelFuture) {
|
||||
public NettyProxy2RealInboundHandler(ChannelFuture dstChannelFuture) {
|
||||
this.dstChannelFuture = dstChannelFuture;
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ public class NettyProxy2realInboundHandler extends ChannelInboundHandlerAdapter
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
log.error("NettyProxy2realInboundHandler exception", cause);
|
||||
log.error("NettyProxy2RealInboundHandler exception", cause);
|
||||
ctx.close();
|
||||
}
|
||||
}
|
@ -9,11 +9,11 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class NettySocketReal2ProxyInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
public class NettySocketBackendHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private final ChannelHandlerContext clientChannelHandlerContext;
|
||||
|
||||
public NettySocketReal2ProxyInboundHandler(ChannelHandlerContext clientChannelHandlerContext) {
|
||||
public NettySocketBackendHandler(ChannelHandlerContext clientChannelHandlerContext) {
|
||||
this.clientChannelHandlerContext = clientChannelHandlerContext;
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ public class NettySocketReal2ProxyInboundHandler extends ChannelInboundHandlerAd
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
log.error("NettySocketReal2ProxyInboundHandler exception", cause);
|
||||
log.error("NettySocketBackendHandler exception", cause);
|
||||
ctx.close();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user