mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 18:35:05 +08:00
[fix]
This commit is contained in:
@ -4,17 +4,16 @@ package org.framework.lazy.cloud.network.heartbeat.server.netty.advanced;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelId;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyClientStatsChangeApplication;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.socket.NettyServerPermeateClientVisitorSocket;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
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.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportDisconnectTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.socket.NettyServerPermeateClientVisitorSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyClientStatsChangeApplication;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@ -75,12 +74,8 @@ public class ServerHandleReportDisconnectTypeAdvanced extends AbstractHandleRepo
|
||||
if (!ObjectUtils.isEmpty(visitorSockets)) {
|
||||
for (NettyServerPermeateClientVisitorSocket visitorSocket : visitorSockets) {
|
||||
int visitorPort = visitorSocket.getVisitorPort();
|
||||
try {
|
||||
visitorSocket.close();
|
||||
log.warn("client :[{}] visitorPort:[{}] close", new String(clientId), visitorPort);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
visitorSocket.close();
|
||||
log.warn("client :[{}] visitorPort:[{}] close", new String(clientId), visitorPort);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ public class NettyServerPermeateClientVisitorHandler extends SimpleChannelInboun
|
||||
return;
|
||||
}
|
||||
// 通信通道自动读写打开 ,然后关闭通信通道
|
||||
// Channel visitorChannel = NettyCommunicationIdContext.getVisitor(visitorId);
|
||||
// Channel visitorChannel = NettyCommunicationIdContext.getVisitorChannel(visitorId);
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null && nextChannel.isActive()) {
|
||||
|
||||
|
@ -10,10 +10,9 @@ import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrat
|
||||
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.socket.PermeateVisitorSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.NettyServerPermeateClientVisitorFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 内网穿透服务端访客通道
|
||||
*
|
||||
@ -21,7 +20,7 @@ import java.io.IOException;
|
||||
* @see NettyClientVisitorContext
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyServerPermeateClientVisitorSocket {
|
||||
public class NettyServerPermeateClientVisitorSocket implements PermeateVisitorSocket {
|
||||
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
private final NettyServerPermeateClientVisitorFilter nettyServerPermeateClientVisitorFilter;
|
||||
@ -41,9 +40,10 @@ public class NettyServerPermeateClientVisitorSocket {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void startVisitorServer() throws Exception {
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
Channel visitor = NettyVisitorPortContext.getVisitor(visitorPort);
|
||||
Channel visitor = NettyVisitorPortContext.getVisitorChannel(visitorPort);
|
||||
if (visitor == null) {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap
|
||||
@ -65,38 +65,43 @@ public class NettyServerPermeateClientVisitorSocket {
|
||||
|
||||
|
||||
.childHandler(nettyServerPermeateClientVisitorFilter);
|
||||
ChannelFuture sync = bootstrap.bind(visitorPort).sync();
|
||||
sync.addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 这里时异步处理
|
||||
log.info("客户端:[{}]访客端口:[{}] 开启", clientId, visitorPort);
|
||||
NettyVisitorPortContext.pushVisitor(visitorPort, future.channel());
|
||||
try {
|
||||
bootstrap.bind(visitorPort).sync().addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 这里时异步处理
|
||||
log.info("客户端:[{}]访客端口:[{}] 开启", clientId, visitorPort);
|
||||
NettyVisitorPortContext.pushVisitorChannel(visitorPort, future.channel());
|
||||
|
||||
} else {
|
||||
log.error("客户端:[{}]访客端口:[{}]绑定失败", clientId, visitorPort);
|
||||
}
|
||||
});
|
||||
NettyClientVisitorContext.pushVisitorSocket(clientId, this);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
} else {
|
||||
log.error("客户端:[{}]访客端口:[{}]绑定失败", clientId, visitorPort);
|
||||
}
|
||||
});
|
||||
NettyClientVisitorContext.pushVisitorSocket(clientId, this);
|
||||
} else {
|
||||
log.warn("客户端:[{}]访客端口:[{}] 重复启动", clientId, visitorPort);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void close() throws IOException, InterruptedException {
|
||||
@Override
|
||||
public void close() {
|
||||
if (!bossGroup.isShutdown()) {
|
||||
bossGroup.shutdownGracefully();
|
||||
}
|
||||
if (!workerGroup.isShutdown()) {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
Channel visitor = NettyVisitorPortContext.getVisitor(visitorPort);
|
||||
Channel visitor = NettyVisitorPortContext.getVisitorChannel(visitorPort);
|
||||
if (visitor != null) {
|
||||
|
||||
// close channel
|
||||
visitor.close();
|
||||
// remove visitor
|
||||
NettyVisitorPortContext.removeVisitor(visitorPort);
|
||||
NettyVisitorPortContext.removeVisitorChannel(visitorPort);
|
||||
// remove client this
|
||||
NettyClientVisitorContext.removeVisitorSocket(clientId,this);
|
||||
log.warn("关闭客户端 :【{}】 访客户端口:【{}】", clientId, visitorPort);
|
||||
|
@ -92,7 +92,7 @@ public class NettyServerPermeateServerConnectVisitorSocket {
|
||||
if (!workerGroup.isShutdown()) {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
Channel visitor = NettyVisitorPortContext.getVisitor(visitorPort);
|
||||
Channel visitor = NettyVisitorPortContext.getVisitorChannel(visitorPort);
|
||||
if (visitor != null) {
|
||||
// close channel
|
||||
visitor.close();
|
||||
|
@ -57,20 +57,7 @@ public class LazyInternalNetworkClientPermeateServerMappingApplicationImpl imple
|
||||
String serverId = serverNodeProperties.getNodeId();
|
||||
lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
|
||||
// 发送客户端初始化渗透
|
||||
String clientId = lazyInternalNetworkClientPermeateServerMapping.getClientId();
|
||||
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
|
||||
if(clientChannel!=null && clientChannel.getChannel()!=null&&clientChannel.getChannel().isActive()){
|
||||
Channel channel = clientChannel.getChannel();
|
||||
String permeateTargetIp = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetIp();
|
||||
Integer permeateTargetPort = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetPort();
|
||||
Integer visitorPort = lazyInternalNetworkClientPermeateServerMapping.getVisitorPort();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT);
|
||||
nettyMsg.setClientTargetIp(permeateTargetIp);
|
||||
nettyMsg.setClientTargetPort(permeateTargetPort);
|
||||
nettyMsg.setVisitorPort(visitorPort);
|
||||
channel.writeAndFlush(nettyMsg);
|
||||
}
|
||||
createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
|
||||
return lazyInternalNetworkClientPermeateServerMappingRepository.story(lazyInternalNetworkClientPermeateServerMapping);
|
||||
}
|
||||
/**
|
||||
@ -89,6 +76,7 @@ public class LazyInternalNetworkClientPermeateServerMappingApplicationImpl imple
|
||||
for (LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping : lazyInternalNetworkClientPermeateServerMappingList) {
|
||||
String serverId = serverNodeProperties.getNodeId();
|
||||
lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
|
||||
createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
|
||||
}
|
||||
return lazyInternalNetworkClientPermeateServerMappingRepository.batchStory(lazyInternalNetworkClientPermeateServerMappingList);
|
||||
}
|
||||
@ -107,6 +95,13 @@ public class LazyInternalNetworkClientPermeateServerMappingApplicationImpl imple
|
||||
LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingUpdateCommand);
|
||||
String serverId = serverNodeProperties.getNodeId();
|
||||
lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
|
||||
// 关闭
|
||||
LazyInternalNetworkClientPermeateServerMapping mapping = new LazyInternalNetworkClientPermeateServerMapping();
|
||||
mapping.setId(lazyInternalNetworkClientPermeateServerMapping.getId());
|
||||
lazyInternalNetworkClientPermeateServerMappingRepository.findOne(mapping).accept(this::closeClientPermeateServerSocketMessage);
|
||||
|
||||
|
||||
createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
|
||||
return lazyInternalNetworkClientPermeateServerMappingRepository.story(lazyInternalNetworkClientPermeateServerMapping);
|
||||
}
|
||||
|
||||
@ -174,4 +169,47 @@ public class LazyInternalNetworkClientPermeateServerMappingApplicationImpl imple
|
||||
return lazyInternalNetworkClientPermeateServerMappingRepository.remove(lazyInternalNetworkClientPermeateServerMapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭 客户端渗透服务端socket 消息
|
||||
* @param lazyInternalNetworkClientPermeateServerMapping 客户端渗透服务端映射
|
||||
*/
|
||||
public void closeClientPermeateServerSocketMessage(LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping){
|
||||
// 发送客户端初始化渗透
|
||||
String clientId = lazyInternalNetworkClientPermeateServerMapping.getClientId();
|
||||
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
|
||||
if(clientChannel!=null && clientChannel.getChannel()!=null&&clientChannel.getChannel().isActive()){
|
||||
Channel channel = clientChannel.getChannel();
|
||||
String permeateTargetIp = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetIp();
|
||||
Integer permeateTargetPort = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetPort();
|
||||
Integer visitorPort = lazyInternalNetworkClientPermeateServerMapping.getVisitorPort();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE);
|
||||
nettyMsg.setClientTargetIp(permeateTargetIp);
|
||||
nettyMsg.setClientTargetPort(permeateTargetPort);
|
||||
nettyMsg.setVisitorPort(visitorPort);
|
||||
channel.writeAndFlush(nettyMsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 客户端渗透服务端socket 消息
|
||||
* @param lazyInternalNetworkClientPermeateServerMapping 客户端渗透服务端映射
|
||||
*/
|
||||
public void createClientPermeateServerSocketMessage(LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping){
|
||||
// 发送客户端初始化渗透
|
||||
String clientId = lazyInternalNetworkClientPermeateServerMapping.getClientId();
|
||||
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
|
||||
if(clientChannel!=null && clientChannel.getChannel()!=null&&clientChannel.getChannel().isActive()){
|
||||
Channel channel = clientChannel.getChannel();
|
||||
String permeateTargetIp = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetIp();
|
||||
Integer permeateTargetPort = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetPort();
|
||||
Integer visitorPort = lazyInternalNetworkClientPermeateServerMapping.getVisitorPort();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT);
|
||||
nettyMsg.setClientTargetIp(permeateTargetIp);
|
||||
nettyMsg.setClientTargetPort(permeateTargetPort);
|
||||
nettyMsg.setVisitorPort(visitorPort);
|
||||
channel.writeAndFlush(nettyMsg);
|
||||
}
|
||||
}
|
||||
}
|
@ -164,11 +164,8 @@ public class LazyInternalNetworkPenetrationMappingApplicationImpl implements Laz
|
||||
if (!ObjectUtils.isEmpty(nettyServerPermeateClientVisitorSocketList)) {
|
||||
// 关闭端口
|
||||
for (NettyServerPermeateClientVisitorSocket nettyServerPermeateClientVisitorSocket : nettyServerPermeateClientVisitorSocketList) {
|
||||
try {
|
||||
nettyServerPermeateClientVisitorSocket.close();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -291,7 +288,7 @@ public class LazyInternalNetworkPenetrationMappingApplicationImpl implements Laz
|
||||
.builderChannelFlowAdapter(channelFlowAdapter)
|
||||
.build();
|
||||
try {
|
||||
nettyServerPermeateClientVisitorSocket.startVisitorServer();
|
||||
nettyServerPermeateClientVisitorSocket.start();
|
||||
} catch (Exception e) {
|
||||
log.error("客户端:{},网络端口:{},开放失败", clientId, visitorPort);
|
||||
throw new RuntimeException(e);
|
||||
|
@ -1,28 +1,27 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.server.standalone.application.impl;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import jakarta.annotation.Resource;
|
||||
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.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.socket.NettyServerPermeateClientVisitorSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyNettyClientStateApplication;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.assembler.NettyClientStateDTOAssembler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.netty.client.state.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyClientStateDTO;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.state.LazyNettyClientState;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.state.LazyNettyClientStateRepository;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyClientStateDTO;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
|
||||
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl;
|
||||
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
|
||||
import org.wu.framework.web.response.Result;
|
||||
import org.wu.framework.web.response.ResultFactory;
|
||||
import io.netty.channel.Channel;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl;
|
||||
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.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@ -173,11 +172,7 @@ public class LazyNettyClientStateApplicationImpl implements LazyNettyClientState
|
||||
List<NettyServerPermeateClientVisitorSocket> nettyServerPermeateClientVisitorSocketList = NettyClientVisitorContext.getVisitorSockets(clientId);
|
||||
if (!ObjectUtils.isEmpty(nettyServerPermeateClientVisitorSocketList)) {
|
||||
for (NettyServerPermeateClientVisitorSocket nettyServerPermeateClientVisitorSocket : nettyServerPermeateClientVisitorSocketList) {
|
||||
try {
|
||||
nettyServerPermeateClientVisitorSocket.close();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
nettyServerPermeateClientVisitorSocket.close();
|
||||
}
|
||||
}
|
||||
return lazyNettyClientStateRepository.remove(lazyNettyClientState);
|
||||
|
Reference in New Issue
Block a user