【fix】

This commit is contained in:
wujiawei
2025-04-30 15:19:56 +08:00
parent 98587b442c
commit f2a22c177e
144 changed files with 958 additions and 669 deletions

View File

@ -2,10 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
@ -26,11 +23,12 @@ public class HttpClientProxyClientProxyTypeAdvanced extends AbstractHttpClientPr
/**
* 处理当前数据
*
* @param channel 当前通道
* @param channelHandlerContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
protected void doHandler(ChannelHandlerContext channelHandlerContext, NettyProxyMsg nettyProxyMsg) {
Channel channel = channelHandlerContext.channel();
String targetPortString = nettyProxyMsg.getTargetPortString();
String targetIpString = nettyProxyMsg.getTargetIpString();
Bootstrap b = new Bootstrap();
@ -56,7 +54,7 @@ public class HttpClientProxyClientProxyTypeAdvanced extends AbstractHttpClientPr
buf.writeBytes(nettyProxyMsg.getData());
proxyChannel.writeAndFlush(buf);
} else {
channel.close();
channelHandlerContext.close();
}
});
}

View File

@ -2,10 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
@ -26,14 +23,15 @@ public class HttpProtocolHandleChannelLocalProxyTypeAdvanced extends AbstractHtt
/**
* 处理当前数据
*
* @param channel 当前通道
* @param channelHandlerContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
protected void doHandler(ChannelHandlerContext channelHandlerContext, NettyProxyMsg nettyProxyMsg) {
String targetPortString = nettyProxyMsg.getTargetPortString();
String targetIpString = nettyProxyMsg.getTargetIpString();
Bootstrap b = new Bootstrap();
Channel channel = channelHandlerContext.channel();
b.group(channel.eventLoop())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {

View File

@ -0,0 +1,70 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.socksx.v5.DefaultSocks5CommandResponse;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5CommandStatus;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
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.NettyClient2DestInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyDest2ClientInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
@Slf4j
@Component
public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
extends AbstractHandleSocketLocalProxyTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param ctx 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) {
EventLoopGroup group = EventLoopGroupFactory.createClientWorkGroup();
String host = nettyProxyMsg.getTargetIpString();
Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
Bootstrap b = new Bootstrap();
Socks5AddressType socks5AddressType=Socks5AddressType.IPv4;
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyDest2ClientInboundHandler(ctx));
}
});
log.info("准备连接目标服务器ip={},port={}", host, port);
ChannelFuture f = b.connect(new InetSocketAddress(host, port));
f.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
log.info("目标服务器连接成功");
//添加客户端转发请求到服务端的Handler
ctx.pipeline().addLast(new NettyClient2DestInboundHandler(future));
DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
ctx.writeAndFlush(commandResponse);
ctx.pipeline().remove(NettySocks5CommandRequestHandler.class);
ctx.pipeline().remove(Socks5CommandRequestDecoder.class);
} else {
log.error("连接目标服务器失败,address={},port={}", host, port);
DefaultSocks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, socks5AddressType);
ctx.writeAndFlush(commandResponse);
future.channel().close();
}
});
}
}

View File

@ -1,89 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.context;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.context.SocketApplicationListener;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyUdpProxyHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Slf4j
@Component
public class NettyUdpProxySocketApplicationListener implements SocketApplicationListener {
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private ChannelFuture channelFuture;
private final ProtocolProxyProperties protocolProxyProperties;
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
public NettyUdpProxySocketApplicationListener(ProtocolProxyProperties protocolProxyProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
this.protocolProxyProperties = protocolProxyProperties;
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
}
/**
* 运行
*
* @throws InterruptedException
*/
@Override
public void doRunning() throws Exception {
try {
ChannelTypeAdapter channelTypeAdapter = new ChannelTypeAdapter(handleChannelTypeAdvancedList);
NettyUdpProxyHandler nettyUdpProxyHandler = new NettyUdpProxyHandler(channelTypeAdapter);// 通道业务处理
ProtocolProxyProperties.UdpProtocolProxy udpProtocolProxy = protocolProxyProperties.getUdpProtocolProxy();
Integer udpProtocolProxyPort = udpProtocolProxy.getPort();
Bootstrap b = new Bootstrap();
b.group(bossGroup)
.channel(NioDatagramChannel.class)
// 给服务端channel设置属性
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
// .childOption(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(nettyUdpProxyHandler);
channelFuture = b.bind(udpProtocolProxyPort).sync();
channelFuture.addListener((ChannelFutureListener) channelFuture -> {
// 服务器已启动
log.info("UDP 协议代理 服务器启动成功【{}】",udpProtocolProxyPort);
});
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
log.error("启动UDP 协议代理 失败", e);
} finally {
destroy();
// 服务器已关闭
log.warn("UDP 协议代理 服务关闭");
}
}
@PreDestroy
@Override
public void destroy() throws Exception {
if (channelFuture != null) {
channelFuture.channel().close().syncUninterruptibly();
}
if (!bossGroup.isShutdown()) {
bossGroup.shutdownGracefully();
}
}
}

View File

@ -32,7 +32,6 @@ public class NettyHttpProxyFilter extends DebugChannelInitializer<SocketChannel>
// 类型处理器适配器
ChannelTypeAdapter channelTypeAdapter = new ChannelTypeAdapter(handleChannelTypeAdvancedList);
// pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpRequestDecoder());
pipeline.addLast(new HttpObjectAggregator(1048576));
pipeline.addLast("doHandler", new NettyHttpProxyHandler(channelTypeAdapter));// 服务端业务逻辑

View File

@ -1,13 +1,17 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.filter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5ServerEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.factory.EventLoopGroupFactory;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyTcpProxyHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5InitialRequestHandler;
import org.springframework.stereotype.Component;
import java.util.List;
@ -31,7 +35,24 @@ public class NettyTcpProxyFilter extends DebugChannelInitializer<SocketChannel>
ChannelPipeline pipeline = ch.pipeline();
// 类型处理器适配器
ChannelTypeAdapter channelTypeAdapter = new ChannelTypeAdapter(handleChannelTypeAdvancedList);
pipeline.addLast("doHandler", new NettyTcpProxyHandler(channelTypeAdapter));// 服务端业务逻辑
//socks5响应最后一个encode
pipeline.addLast(Socks5ServerEncoder.DEFAULT);
// 初始化连接
pipeline.addLast(new Socks5InitialRequestDecoder());
pipeline.addLast(new NettySocks5InitialRequestHandler());
// 认证
// ch.pipeline().addLast(new Socks5PasswordAuthRequestDecoder());
// ch.pipeline().addLast(new Socks5PasswordAuthRequestInboundHandler());
EventLoopGroup clientWorkGroup = EventLoopGroupFactory.createClientWorkGroup();
// 连接请求
pipeline.addLast(new Socks5CommandRequestDecoder());
pipeline.addLast(new NettySocks5CommandRequestHandler(channelTypeAdapter));

View File

@ -0,0 +1,53 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NettyClient2DestInboundHandler extends ChannelInboundHandlerAdapter {
private final ChannelFuture dstChannelFuture;
public NettyClient2DestInboundHandler(ChannelFuture dstChannelFuture) {
this.dstChannelFuture = dstChannelFuture;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("转发客户端的请求到代理服务器");
if (dstChannelFuture.channel().isActive()) {
dstChannelFuture.channel().writeAndFlush(msg);
} else {
log.info("释放内存");
ReferenceCountUtil.release(msg);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("客户端与代理服务器的连接已经断开,即将断开代理服务器和目标服务器的连接");
if (dstChannelFuture.channel().isActive()) {
if (ctx.channel().isActive()) {
ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettyClient2DestInboundHandler exception", cause);
ctx.close();
}
}

View File

@ -0,0 +1,49 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NettyDest2ClientInboundHandler extends ChannelInboundHandlerAdapter {
private final ChannelHandlerContext clientChannelHandlerContext;
public NettyDest2ClientInboundHandler(ChannelHandlerContext clientChannelHandlerContext) {
this.clientChannelHandlerContext = clientChannelHandlerContext;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.trace("开始写回客户端数据");
if (clientChannelHandlerContext.channel().isActive()) {
clientChannelHandlerContext.writeAndFlush(msg);
} else {
log.info("释放内存");
ReferenceCountUtil.release(msg);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.trace("代理服务器和目标服务器的连接已经断开,即将断开客户端和代理服务器的连接");
if (clientChannelHandlerContext.channel().isActive()) {
clientChannelHandlerContext.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("NettyDest2ClientInboundHandler exception", cause);
ctx.close();
}
}

View File

@ -106,7 +106,7 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
proxyMsg.setType(ProxyMessageType.HTTP_SERVER_PROXY_CLIENT_);
}
}
channelTypeAdapter.handler(ctx.channel(), proxyMsg);
channelTypeAdapter.handler(ctx, proxyMsg);
// 判断是否被路由
} else {

View File

@ -0,0 +1,93 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.channel.*;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.socksx.v5.*;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.wu.framework.core.utils.ObjectUtils;
import java.util.UUID;
@Slf4j
public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandler<Socks5CommandRequest> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettySocks5CommandRequestHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
if (channel instanceof NioSocketChannel) {
System.out.println("这是一个TCP通道");
} else if (channel instanceof NioDatagramChannel) {
System.out.println("这是一个UDP通道");
} else {
System.out.println("未知类型的通道");
}
String visitorId = UUID.randomUUID().toString();
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
super.channelActive(ctx);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Socks5CommandRequest request) throws Exception {
if (request.type() == Socks5CommandType.CONNECT) {
String host = request.dstAddr();
int port = request.dstPort();
Socks5AddressType socks5AddressType = request.dstAddrType();
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
NettyProxyMsg proxyMsg = new NettyProxyMsg();
proxyMsg.setVisitorId(visitorId);
proxyMsg.setClientTargetIp(host);
proxyMsg.setClientTargetPort(port);
ProxyRoute route = RouteContext.getRoute(host, String.valueOf(port));
if(ObjectUtils.isEmpty(route)){
proxyMsg.setType(ProxyMessageType.SOCKET_LOCAL_PROXY);
}else {
if(RouteType.LOCAL.equals(route.getRouteType())){
proxyMsg.setType(ProxyMessageType.SOCKET_LOCAL_PROXY);
}else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
String clientId = clientProxyRoute.getClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.SOCKET_CLIENT_PROXY_CLIENT_);
}else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){
proxyMsg.setType(ProxyMessageType.SOCKET_CLIENT_PROXY_SERVER_);
}else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
String clientId = clientProxyRoute.getClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.SOCKET_SERVER_PROXY_CLIENT_);
}
}
channelTypeAdapter.handler(ctx, proxyMsg);
} else {
log.info("receive commandRequest type={}", request.type());
ReferenceCountUtil.retain(request);
ctx.fireChannelRead(request);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

View File

@ -0,0 +1,34 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.socksx.v5.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NettySocks5InitialRequestHandler extends SimpleChannelInboundHandler<Socks5InitialRequest> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Socks5InitialRequest msg) throws Exception {
boolean failure = msg.decoderResult().isFailure();
if (failure) {
log.error("初始化socks5失败请检查是否是socks5协议");
// ReferenceCountUtil.retain(msg);
ctx.fireChannelRead(msg);
return;
}
log.info("初始化socket连接");
// 不验证账号密码
Socks5InitialResponse socks5InitialResponse = new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH);
ctx.writeAndFlush(socks5InitialResponse);
ctx.pipeline().remove(this);
ctx.pipeline().remove(Socks5InitialRequestDecoder.class);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

View File

@ -1,31 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import java.net.InetSocketAddress;
public class NettyUdpProxyBackendHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private final Channel inboundChannel;
private final InetSocketAddress clientAddress;
public NettyUdpProxyBackendHandler(Channel inboundChannel, InetSocketAddress clientAddress) {
this.inboundChannel = inboundChannel;
this.clientAddress = clientAddress;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
inboundChannel.writeAndFlush(new DatagramPacket(packet.content().retain(), clientAddress));
ctx.channel().close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

View File

@ -1,46 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import java.net.InetSocketAddress;
/**
* description 服务端数据处理器
*
* @author 吴佳伟
* @date 2023/09/13 10:27
*/
@Slf4j
public class NettyUdpProxyHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private final ChannelTypeAdapter channelTypeAdapter;
private InetSocketAddress targetAddress = new InetSocketAddress("example.com", 80);
public NettyUdpProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
Bootstrap b = new Bootstrap();
b.group(ctx.channel().eventLoop())
.channel(NioDatagramChannel.class)
.handler(new NettyUdpProxyBackendHandler(ctx.channel(), packet.sender()));
Channel channel = b.bind(0).sync().channel();
channel.writeAndFlush(new DatagramPacket(packet.content().retain(), targetAddress));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

View File

@ -4,7 +4,6 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.init;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.protocol.context.NettyHttpProxySocketApplicationListener;
import org.framework.lazy.cloud.network.heartbeat.protocol.context.NettyTcpProxySocketApplicationListener;
import org.framework.lazy.cloud.network.heartbeat.protocol.context.NettyUdpProxySocketApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -18,8 +17,7 @@ import org.springframework.context.annotation.Import;
@Slf4j
@Configuration
@Import({NettyHttpProxySocketApplicationListener.class,
NettyTcpProxySocketApplicationListener.class,
NettyUdpProxySocketApplicationListener.class})
NettyTcpProxySocketApplicationListener.class})
public class InitProtocolProxySocket {

View File

@ -1,6 +1,5 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.socket5;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;