【fix】init route ip

This commit is contained in:
wujiawei 2025-05-13 11:15:21 +08:00
parent a3c88ffa9f
commit 32a641ce99
8 changed files with 119 additions and 14 deletions

View File

@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf; import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/** /**
@ -49,13 +48,13 @@ public class NettySocksServerProxyClientRealHandler extends SimpleChannelInbound
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel()); String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 客户端真实通信通道 // 客户端真实通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel()); Channel transferChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) { if (transferChannel != null&&transferChannel.isActive()) {
// 上报关闭这个客户端的访客通道 // 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg(); NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_); closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId); closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg); transferChannel.writeAndFlush(closeVisitorMsg);
} }
super.channelInactive(ctx); super.channelInactive(ctx);

View File

@ -35,16 +35,11 @@ public class NettySocksServerProxyClientTransferHandler extends SimpleChannelInb
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel()); String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel()); String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel()); Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId); log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
// 关闭访客 // 关闭真实通道
if (nextChannel != null) { if (realChannel != null) {
realChannel.close();
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(ProxyMessageType.SOCKS_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
} }
super.channelInactive(ctx); super.channelInactive(ctx);

View File

@ -129,6 +129,7 @@ public class NettySocksServerProxyClientRealSocket {
future.addListener((ChannelFutureListener) futureListener -> { future.addListener((ChannelFutureListener) futureListener -> {
Channel transferChannel = futureListener.channel(); Channel transferChannel = futureListener.channel();
if (futureListener.isSuccess()) { if (futureListener.isSuccess()) {
log.info("服务端代理客户端socks客户端连接服务端传输通道成功");
realChannel.config().setOption(ChannelOption.AUTO_READ, true); realChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 通知服务端访客连接成功 // 通知服务端访客连接成功
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg(); NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();

View File

@ -26,7 +26,7 @@ public class NettyProxy2RealInboundHandler extends ChannelInboundHandlerAdapter
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("转发客户端的请求到代理服务器"); log.info("本地转发客户端的请求到代理服务器");
if (dstChannelFuture.channel().isActive()) { if (dstChannelFuture.channel().isActive()) {
dstChannelFuture.channel().writeAndFlush(msg); dstChannelFuture.channel().writeAndFlush(msg);
} else { } else {

View File

@ -25,6 +25,7 @@ public class RouteContext {
String virtualPort = route.getVirtualPort(); String virtualPort = route.getVirtualPort();
String key = virtualIp + ":" + virtualPort + "_" + routeType; String key = virtualIp + ":" + virtualPort + "_" + routeType;
if (m.containsKey(key)) { if (m.containsKey(key)) {
m.put(key, route);
return; return;
} }
// TODO 验证ip端口类型 // TODO 验证ip端口类型

View File

@ -14,6 +14,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.se
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler; import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.filter.NettySocksServerProxyClientVisitorFilter;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettyServerProxyClientVisitorInboundHandler; import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettyServerProxyClientVisitorInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler; import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
@ -59,6 +60,9 @@ public class ServerHandleSocksReportServerProxyClientConnectionSuccessTypeAdvanc
DefaultSocks5CommandResponse commandResponse = DefaultSocks5CommandResponse commandResponse =
new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType); new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
nextChannel.writeAndFlush(commandResponse); nextChannel.writeAndFlush(commandResponse);
nextChannel.pipeline().addLast(new NettySocksServerProxyClientVisitorFilter());
nextChannel.pipeline().remove(NettySocks5CommandRequestHandler.class); nextChannel.pipeline().remove(NettySocks5CommandRequestHandler.class);
nextChannel.pipeline().remove(Socks5CommandRequestDecoder.class); nextChannel.pipeline().remove(Socks5CommandRequestDecoder.class);
} }

View File

@ -0,0 +1,41 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
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.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksServerProxyClientVisitorHandler;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class NettySocksServerProxyClientVisitorFilter extends DebugChannelInitializer<SocketChannel> {
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
* @throws Exception is thrown if an error occurs. In that case it will be handled by
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
* the {@link Channel}.
*/
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ChannelDuplexHandler());
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
pipeline.addLast(new NettySocksServerProxyClientVisitorHandler(new ChannelTypeAdapter(handleChannelTypeAdvancedList)));
}
}

View File

@ -0,0 +1,64 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
@Slf4j
public class NettySocksServerProxyClientVisitorHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettySocksServerProxyClientVisitorHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
channelTypeAdapter.handler(ctx, nettyProxyMsg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
log.warn("close server proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
// 关闭真实通道
if (realChannel != null) {
realChannel.close();
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 处理客户端本地真实通道问题
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}