【fix】修复客户端渗透客户端问题 通道关闭错误

This commit is contained in:
wujiawei 2024-09-22 21:33:17 +08:00
parent 3ec0ab3271
commit 06c45f4fef
6 changed files with 54 additions and 70 deletions

View File

@ -5,7 +5,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType; import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; 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.adapter.ChannelTypeAdapter;
@ -41,7 +40,6 @@ public class NettyClientPermeateServerTransferHandler extends SimpleChannelInbou
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel()); String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 关闭访客 // 关闭访客
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel()); Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
if (nextChannel != null) { if (nextChannel != null) {
// 上报关闭服务端客户端真实通道 // 上报关闭服务端客户端真实通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg(); NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();

View File

@ -42,20 +42,14 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
// 先不读取访客数据 // 先不读取访客数据
visitorChannel.config().setOption(ChannelOption.AUTO_READ, false); visitorChannel.config().setOption(ChannelOption.AUTO_READ, false);
// 生成访客ID // 生成访客ID
String visitorId = UUID.randomUUID().toString(); String visitorId = UUID.randomUUID().toString();
// 绑定访客真实通道
NettyRealIdContext.pushReal(visitorChannel, visitorId);
// 当前通道绑定访客ID
ChannelAttributeKeyUtils.buildVisitorId(visitorChannel, visitorId);
internalNetworkPermeateServerVisitor.setVisitorId(visitorId); internalNetworkPermeateServerVisitor.setVisitorId(visitorId);
// 判断是否有可用的通道 如果没有创建新的通道 // 判断是否有可用的通道 如果没有创建新的通道
// Channel transferChannel = nettyChannelPool.availableChannel(visitorId); // Channel transferChannel = nettyChannelPool.availableChannel(visitorId);
// 创建访客连接服务端通道 // 创建访客连接服务端通道
NettyClientPermeateServerVisitorTransferSocket.buildTransferServer(internalNetworkPermeateServerVisitor); NettyClientPermeateServerVisitorTransferSocket.buildTransferServer(internalNetworkPermeateServerVisitor,visitorChannel);
log.info("客户端渗透服务端访客端口连接成功了"); log.debug("客户端渗透服务端访客端口连接成功了");
super.channelActive(ctx); super.channelActive(ctx);
} }
@ -116,9 +110,6 @@ public class NettyClientPermeateServerVisitorHandler extends SimpleChannelInboun
myMsg.setVisitorId(visitorId); myMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(myMsg); nextChannel.writeAndFlush(myMsg);
} }
// 关闭 访客通信通道访客真实通道
NettyRealIdContext.clear(visitorId);
NettyCommunicationIdContext.clear(visitorId);
log.warn("【客户端渗透服务端】访客端口断开连接"); log.warn("【客户端渗透服务端】访客端口断开连接");
super.channelInactive(ctx); super.channelInactive(ctx);
} }

View File

@ -10,9 +10,7 @@ import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientPrope
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkPermeateServerVisitor; import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkPermeateServerVisitor;
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientPermeateServerTransferFilter; import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientPermeateServerTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType; import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; 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.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
@ -26,8 +24,8 @@ public class NettyClientPermeateServerVisitorTransferSocket {
/** /**
* 连接服务端通信通道 * 连接服务端通信通道
*/ */
public static void buildTransferServer(InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor) { public static void buildTransferServer(InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor, Channel visitorChannel) {
newTransferConnect2Server(internalNetworkPermeateServerVisitor); newTransferConnect2Server(internalNetworkPermeateServerVisitor,visitorChannel);
} }
@ -36,7 +34,7 @@ public class NettyClientPermeateServerVisitorTransferSocket {
* <p> * <p>
* internalNetworkPermeateServerVisitor * internalNetworkPermeateServerVisitor
*/ */
protected static void newTransferConnect2Server(InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor) { protected static void newTransferConnect2Server(InternalNetworkPermeateServerVisitor internalNetworkPermeateServerVisitor, Channel visitorChannel) {
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup) bootstrap.group(eventLoopGroup)
@ -87,17 +85,13 @@ public class NettyClientPermeateServerVisitorTransferSocket {
myMsg.setVisitorId(visitorId); myMsg.setVisitorId(visitorId);
channel.writeAndFlush(myMsg); channel.writeAndFlush(myMsg);
// 绑定客户端真实通信通道
NettyCommunicationIdContext.pushVisitor(channel, visitorId);
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId); ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
ChannelAttributeKeyUtils.buildClientId(channel, clientId); ChannelAttributeKeyUtils.buildClientId(channel, clientId);
// 传输通道打开后自动读取 // 传输通道打开后自动读取
Channel visitor = NettyRealIdContext.getReal(visitorId);
ChannelAttributeKeyUtils.buildNextChannel(visitor, channel);
ChannelAttributeKeyUtils.buildNextChannel(channel, visitor);
ChannelAttributeKeyUtils.buildNextChannel(visitorChannel, channel);
ChannelAttributeKeyUtils.buildNextChannel(channel, visitorChannel);
} else { } else {
log.info("客户端渗透服务端通信通道中断...."); log.info("客户端渗透服务端通信通道中断....");
} }

View File

@ -35,6 +35,7 @@ public class ServerHandleReportClientTransferClientPermeateChannelInitSuccessful
Channel nextTransferChannel = NettyTransferChannelContext.getVisitor(msgVisitorId); Channel nextTransferChannel = NettyTransferChannelContext.getVisitor(msgVisitorId);
ChannelAttributeKeyUtils.buildTransferNextChannel(nextTransferChannel,channel); ChannelAttributeKeyUtils.buildTransferNextChannel(nextTransferChannel,channel);
ChannelAttributeKeyUtils.buildTransferNextChannel(channel,nextTransferChannel); ChannelAttributeKeyUtils.buildTransferNextChannel(channel,nextTransferChannel);
NettyTransferChannelContext.clear(msgVisitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel); Channel nextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel);

View File

@ -27,29 +27,29 @@
<build> <build>
<plugins> <plugins>
<plugin> <!-- <plugin>-->
<groupId>org.graalvm.buildtools</groupId> <!-- <groupId>org.graalvm.buildtools</groupId>-->
<artifactId>native-maven-plugin</artifactId> <!-- <artifactId>native-maven-plugin</artifactId>-->
<version>0.9.23</version> <!-- <version>0.9.23</version>-->
<configuration> <!-- <configuration>-->
<!-- imageName用于设置生成的二进制文件名称 --> <!-- &lt;!&ndash; imageName用于设置生成的二进制文件名称 &ndash;&gt;-->
<imageName>${project.artifactId}</imageName> <!-- <imageName>${project.artifactId}</imageName>-->
<!-- mainClass用于指定main方法类路径 --> <!-- &lt;!&ndash; mainClass用于指定main方法类路径 &ndash;&gt;-->
<mainClass>org.framework.lazy.cloud.network.heartbeat.client.LazyCloudHeartbeatClientStart</mainClass> <!-- <mainClass>org.framework.lazy.cloud.network.heartbeat.client.LazyCloudHeartbeatClientStart</mainClass>-->
<buildArgs> <!-- <buildArgs>-->
--no-fallback <!-- &#45;&#45;no-fallback-->
</buildArgs> <!-- </buildArgs>-->
</configuration> <!-- </configuration>-->
<executions> <!-- <executions>-->
<execution> <!-- <execution>-->
<id>build-native</id> <!-- <id>build-native</id>-->
<goals> <!-- <goals>-->
<goal>compile-no-fork</goal> <!-- <goal>compile-no-fork</goal>-->
</goals> <!-- </goals>-->
<phase>package</phase> <!-- <phase>package</phase>-->
</execution> <!-- </execution>-->
</executions> <!-- </executions>-->
</plugin> <!-- </plugin>-->
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>

View File

@ -32,29 +32,29 @@
<build> <build>
<plugins> <plugins>
<plugin> <!-- <plugin>-->
<groupId>org.graalvm.buildtools</groupId> <!-- <groupId>org.graalvm.buildtools</groupId>-->
<artifactId>native-maven-plugin</artifactId> <!-- <artifactId>native-maven-plugin</artifactId>-->
<version>0.9.23</version> <!-- <version>0.9.23</version>-->
<configuration> <!-- <configuration>-->
<!-- imageName用于设置生成的二进制文件名称 --> <!-- &lt;!&ndash; imageName用于设置生成的二进制文件名称 &ndash;&gt;-->
<imageName>${project.artifactId}</imageName> <!-- <imageName>${project.artifactId}</imageName>-->
<!-- mainClass用于指定main方法类路径 --> <!-- &lt;!&ndash; mainClass用于指定main方法类路径 &ndash;&gt;-->
<mainClass>org.framework.lazy.cloud.network.heartbeat.server.LazyCloudHeartbeatServerStart</mainClass> <!-- <mainClass>org.framework.lazy.cloud.network.heartbeat.server.LazyCloudHeartbeatServerStart</mainClass>-->
<buildArgs> <!-- <buildArgs>-->
--no-fallback <!-- &#45;&#45;no-fallback-->
</buildArgs> <!-- </buildArgs>-->
</configuration> <!-- </configuration>-->
<executions> <!-- <executions>-->
<execution> <!-- <execution>-->
<id>build-native</id> <!-- <id>build-native</id>-->
<goals> <!-- <goals>-->
<goal>compile-no-fork</goal> <!-- <goal>compile-no-fork</goal>-->
</goals> <!-- </goals>-->
<phase>package</phase> <!-- <phase>package</phase>-->
</execution> <!-- </execution>-->
</executions> <!-- </executions>-->
</plugin> <!-- </plugin>-->
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>