[fix] 统一使用next 通道进行数据处理

This commit is contained in:
wujiawei
2024-09-18 23:43:56 +08:00
parent 37d9065c5a
commit 7028e001ea
12 changed files with 253 additions and 28 deletions

View File

@ -2,13 +2,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.config;
import org.framework.lazy.cloud.network.heartbeat.client.netty.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Role;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.util.List;
@ -55,6 +55,7 @@ public class HeartbeatClientConfiguration {
return new ClientHandleDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced();
}
@Bean
public ClientHandleDistributeSingleClientRealCloseVisitorTypeAdvanced handleDistributeSingleClientRealCloseVisitorTypeAdvanced() {
return new ClientHandleDistributeSingleClientRealCloseVisitorTypeAdvanced();
@ -90,10 +91,20 @@ public class HeartbeatClientConfiguration {
return new HandleDistributeStagingOpenedTypeAdvanced();
}
/**
* 处理 客户端渗透服务端init信息
*
* @return ClientHandleDistributeClientPermeateServerInitTypeAdvanced
*/
@Bean
public ClientHandleDistributeClientPermeateServerInitTypeAdvanced clientHandleDistributeClientPermeateServerInitTypeAdvanced(NettyClientProperties nettyClientProperties) {
return new ClientHandleDistributeClientPermeateServerInitTypeAdvanced(nettyClientProperties);
}
@Bean
public ClientHandleDistributeSingleClientRealConnectTypeAdvanced clientHandleDistributeSingleClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
return new ClientHandleDistributeSingleClientRealConnectTypeAdvanced(nettyClientProperties, handleChannelTypeAdvancedList);
}
}

View File

@ -0,0 +1,68 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateServerVisitorSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeClientPermeateServerInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.inner.lazy.orm.spring.SpringConnectionHandler;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 客户端渗透服务端init信息
*
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT
*/
@Slf4j
public class ClientHandleDistributeClientPermeateServerInitTypeAdvanced extends AbstractHandleDistributeClientPermeateServerInitTypeAdvanced<NettyProxyMsg> {
private final NettyClientProperties nettyClientProperties;
public ClientHandleDistributeClientPermeateServerInitTypeAdvanced(NettyClientProperties nettyClientProperties) {
this.nettyClientProperties = nettyClientProperties;
}
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 初始化 客户端渗透服务端socket
byte[] clientIdBytes = nettyProxyMsg.getClientId();
byte[] visitorPort = nettyProxyMsg.getVisitorPort();
byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
// ChannelFlowAdapter channelFlowAdapter = SpringContextHolder.getBean(ChannelFlowAdapter.class);
NettyClientPermeateServerVisitorSocket nettyClientPermeateServerVisitorSocket = NettyClientPermeateServerVisitorSocket.NettyVisitorSocketBuilder.builder()
.builderClientId(new String(clientIdBytes))
.builderClientTargetIp(new String(clientTargetIp))
.builderClientTargetPort(Integer.parseInt(new String(clientTargetPort)))
.builderVisitorPort(Integer.parseInt(new String(visitorPort)))
.builderNettyClientProperties(nettyClientProperties)
// .builderChannelFlowAdapter(channelFlowAdapter)
.builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)
.build();
try {
nettyClientPermeateServerVisitorSocket.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -10,6 +10,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
@ -36,7 +37,8 @@ public class ClientHandleDistributeClientTransferServerPermeateChannelConnection
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
String visitorId = new String(msgVisitorId);
Channel visitor = NettyRealIdContext.getReal(visitorId);
visitor.config().setOption(ChannelOption.AUTO_READ, true);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
}

View File

@ -23,14 +23,10 @@ import java.util.UUID;
@Slf4j
public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor;
// private final ChannelFlowAdapter channelFlowAdapter;// 流量适配器
// private final NettyChannelPool nettyChannelPool = new DefaultNettyChannelPool(10);
public NettyClientPermeateServerVisitorHandler(InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor) {
this.internalNetworkPermeateServerVisitor = internalNetworkPermeateServerVisitor;
// this.channelFlowAdapter = channelFlowAdapter;
}
/**
@ -59,7 +55,7 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
// Channel transferChannel = nettyChannelPool.availableChannel(visitorId);
// 创建访客连接服务端通道
NettyClientTransferServerSocket.buildTransferServer(internalNetworkPermeateServerVisitor);
log.info("服务端访客端口连接成功了");
log.info("客户端渗透服务端访客端口连接成功了");
super.channelActive(ctx);
}
@ -74,12 +70,14 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
// 获取客户端通道,而后进行数据下发
log.debug("【服务端】访客端口成功接收数据:{}", new String(bytes));
log.debug("客户端渗透服务端】访客端口成功接收数据:{}", new String(bytes));
// 使用访客的通信通道
Integer visitorPort = internalNetworkPermeateServerVisitor.getVisitorPort();
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setType(MessageType.REPORT_CLIENT_TRANSFER);
nettyProxyMsg.setVisitorId(visitorId);
nettyProxyMsg.setVisitorPort(visitorPort);
nettyProxyMsg.setData(bytes);
nextChannel.writeAndFlush(nettyProxyMsg);
// 处理访客流量
@ -91,7 +89,7 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
// .flow(bytes.length)
// .build();
// channelFlowAdapter.asyncHandler(visitorChannel, serverChannelFlow);
log.debug("服务端访客端口成功发送数据了");
log.debug("【客户端渗透服务端访客端口成功发送数据了");
}
@Override
@ -119,7 +117,7 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
// 关闭 访客通信通道、访客真实通道
NettyRealIdContext.clear(visitorId);
NettyCommunicationIdContext.clear(visitorId);
log.warn("服务端访客端口断开连接");
log.warn("【客户端渗透服务端访客端口断开连接");
super.channelInactive(ctx);
}
@ -167,13 +165,14 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
// 使用通信通道 下发关闭访客
Channel visitorChannel = NettyCommunicationIdContext.getVisitor(visitorId);
if (visitorChannel != null) {
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 下发关闭访客
NettyProxyMsg closeRealClient = new NettyProxyMsg();
closeRealClient.setType(MessageType.DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT_AUTO_READ);
closeRealClient.setClientId(clientId);
closeRealClient.setVisitorId(visitorId);
visitorChannel.writeAndFlush(closeRealClient);
nextChannel.writeAndFlush(closeRealClient);
}
ctx.close();

View File

@ -12,8 +12,10 @@ import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClien
import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.io.IOException;
import java.util.List;
/**
* 内网穿透 客户端渗透服务端通道
@ -143,6 +145,11 @@ public class NettyClientPermeateServerVisitorSocket {
*/
private NettyClientProperties nettyClientProperties;
/**
* 处理器
*/
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
public static NettyVisitorSocketBuilder builder() {
return new NettyVisitorSocketBuilder();
}
@ -200,6 +207,15 @@ public class NettyClientPermeateServerVisitorSocket {
public NettyVisitorSocketBuilder builderChannelFlowAdapter(ChannelFlowAdapter channelFlowAdapter) {
this.channelFlowAdapter = channelFlowAdapter;
return this;
} /**
* 绑定流量适配器
*
* @param handleChannelTypeAdvancedList 流量适配器
* @return 当前对象
*/
public NettyVisitorSocketBuilder builderHandleChannelTypeAdvancedList(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
return this;
}
/**
* 服务端地址信息
@ -238,6 +254,9 @@ public class NettyClientPermeateServerVisitorSocket {
internalNetworkPermeateServerVisitor.setTargetIp(clientTargetIp);
internalNetworkPermeateServerVisitor.setTargetPort(clientTargetPort);
internalNetworkPermeateServerVisitor.setVisitorPort(visitorPort);
internalNetworkPermeateServerVisitor.setNettyClientProperties(nettyClientProperties);
internalNetworkPermeateServerVisitor.setChannelFlowAdapter(channelFlowAdapter);
internalNetworkPermeateServerVisitor.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
NettyClientPermeateServerVisitorFilter visitorFilter = new NettyClientPermeateServerVisitorFilter(internalNetworkPermeateServerVisitor);