[fix] 修复位置问题

This commit is contained in:
wujiawei
2024-09-24 19:38:53 +08:00
parent bc1c2b008b
commit 576498afc7
35 changed files with 221 additions and 172 deletions

View File

@ -26,12 +26,12 @@ public class HeartbeatServerConfiguration {
/**
* 处理 服务端处理客户端数据传输
*
* @return ServerHandleReportHandleChannelTransferTypeAdvanced
* @return ServerHandleReportServicePermeateClientTransferTypeAdvanced
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Bean
public ServerHandleReportHandleChannelTransferTypeAdvanced serverReportChannelTransferTypeAdvanced(ChannelFlowAdapter channelFlowAdapter) {
return new ServerHandleReportHandleChannelTransferTypeAdvanced(channelFlowAdapter);
public ServerHandleReportServicePermeateClientTransferTypeAdvanced serverReportChannelTransferTypeAdvanced(ChannelFlowAdapter channelFlowAdapter) {
return new ServerHandleReportServicePermeateClientTransferTypeAdvanced(channelFlowAdapter);
}
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ -69,14 +69,14 @@ public class HeartbeatServerConfiguration {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Bean
public ServerHandleReportSingleClientCloseVisitorTypeAdvanced serverReportSingleClientCloseVisitorTypeAdvanced() {
return new ServerHandleReportSingleClientCloseVisitorTypeAdvanced();
public ServerHandleReportServicePermeateClientCloseVisitorTypeAdvanced serverReportSingleClientCloseVisitorTypeAdvanced() {
return new ServerHandleReportServicePermeateClientCloseVisitorTypeAdvanced();
}
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Bean
public ServerHandleReportSingleClientRealConnectTypeAdvanced serverReportSingleClientRealConnectTypeAdvanced() {
return new ServerHandleReportSingleClientRealConnectTypeAdvanced();
public ServerHandleReportServicePermeateClientRealConnectTypeAdvanced serverReportSingleClientRealConnectTypeAdvanced() {
return new ServerHandleReportServicePermeateClientRealConnectTypeAdvanced();
}
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)

View File

@ -5,7 +5,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
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.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportClientPermeateServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
@ -37,10 +36,10 @@ public class ServerHandleReportClientPermeateServerTransferTypeAdvanced extends
String clientId = new String(msg.getClientId());
Integer visitorPort = Integer.valueOf(new String(msg.getVisitorPort()));
byte[] visitorId = msg.getVisitorId();
// log.info("访客ID:【{}】 访客端口:[{}] 接收到客户端:[{}]", new String(visitorId), visitorPort, clientId);
// log.debug("访客ID:【{}】接收到客户端:[{}] 返回数据大小:[{}] 内网穿透返回的数据:[{}]", new String(visitorId), clientId, msg.getData().length, new String(msg.getData()));
log.info("【客户端渗透服务端】访客ID:【{}】 访客端口:[{}] 接收到客户端:[{}] 接收服务端数据大小:[{}]", new String(visitorId), visitorPort, clientId, msg.getData().length);
log.debug("【客户端渗透服务端】访客ID:【{}】接收到客户端:[{}] 接收服务端数据大小:[{}] 接收服务端数据:[{}]", new String(visitorId), clientId, msg.getData().length, new String(msg.getData()));
// 将数据转发访客通道
Channel visitor = NettyRealIdContext.getReal(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel != null) {
ByteBuf buf = nextChannel.config().getAllocator().buffer(msg.getData().length);
@ -58,6 +57,6 @@ public class ServerHandleReportClientPermeateServerTransferTypeAdvanced extends
.build();
channelFlowAdapter.asyncHandler(channel, serverChannelFlow);
}
log.debug("访客ID:【{}】接收到客户端:[{}] 发送真实数据成功", new String(visitorId), clientId);
log.debug("客户端渗透服务端】访客ID:【{}】接收到客户端:[{}] 传输真实数据成功", new String(visitorId), clientId);
}
}

View File

@ -14,11 +14,11 @@ public class ServerHandleReportClientTransferServerPermeateChannelConnectionSucc
/**
* 处理当前数据
*
* @param channel 当前通道
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
protected void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
// 创建目标地址连接
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
@ -28,7 +28,7 @@ public class ServerHandleReportClientTransferServerPermeateChannelConnectionSucc
Integer.parseInt(new String(msgVisitorPort)),
new String(msgClientTargetIp),
Integer.parseInt(new String(msgClientTargetPort)),
channel
transferChannel
);

View File

@ -6,14 +6,14 @@ import org.springframework.stereotype.Component;
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.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportSingleClientCloseVisitorTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportServicePermeateClientCloseVisitorTypeAdvanced;
/**
* 服务端处理客户端 关闭一个访客
*/
@Slf4j
@Component
public class ServerHandleReportSingleClientCloseVisitorTypeAdvanced extends AbstractHandleReportSingleClientCloseVisitorTypeAdvanced<NettyProxyMsg> {
public class ServerHandleReportServicePermeateClientCloseVisitorTypeAdvanced extends AbstractHandleReportServicePermeateClientCloseVisitorTypeAdvanced<NettyProxyMsg> {
/**

View File

@ -7,7 +7,7 @@ import org.springframework.stereotype.Component;
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.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportSingleClientRealConnectTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportServicePermeateClientRealConnectTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
@ -15,16 +15,16 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
*/
@Slf4j
@Component
public class ServerHandleReportSingleClientRealConnectTypeAdvanced extends AbstractHandleReportSingleClientRealConnectTypeAdvanced<NettyProxyMsg> {
public class ServerHandleReportServicePermeateClientRealConnectTypeAdvanced extends AbstractHandleReportServicePermeateClientRealConnectTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
protected void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
// 客户端绑定端口成功
byte[] clientId = nettyProxyMsg.getClientId();
byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
@ -33,16 +33,16 @@ public class ServerHandleReportSingleClientRealConnectTypeAdvanced extends Abstr
byte[] visitorId = nettyProxyMsg.getVisitorId();
log.info("客户端:{},绑定真实服务ip:{},port:{},成功", new String(clientId), new String(clientTargetIp), new String(clientTargetPort));
// 绑定服务端访客通信通道
NettyCommunicationIdContext.pushVisitor(channel, new String(visitorId));
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
ChannelAttributeKeyUtils.buildClientId(channel, clientId);
ChannelAttributeKeyUtils.buildVisitorPort(channel, Integer.valueOf(new String(visitorPort)));
NettyCommunicationIdContext.pushVisitor(transferChannel, new String(visitorId));
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
ChannelAttributeKeyUtils.buildVisitorPort(transferChannel, Integer.valueOf(new String(visitorPort)));
// 访客通道开启自动读取
Channel visitorRealChannel = NettyRealIdContext.getReal(new String(visitorId));
Channel visitorRealChannel = NettyRealIdContext.getReal(visitorId);
visitorRealChannel.config().setOption(ChannelOption.AUTO_READ, true);
ChannelAttributeKeyUtils.buildNextChannel(channel, visitorRealChannel);
ChannelAttributeKeyUtils.buildNextChannel(visitorRealChannel, channel);
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, visitorRealChannel);
ChannelAttributeKeyUtils.buildNextChannel(visitorRealChannel, transferChannel);
// 或许此处还应该通知服务端 这个访客绑定的客户端真实通道打开

View File

@ -8,14 +8,12 @@ import lombok.extern.slf4j.Slf4j;
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.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportHandleChannelTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportServicePermeateClientTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.ServerChannelFlow;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
/**
* 服务端处理客户端数据传输
@ -23,11 +21,11 @@ import java.nio.charset.StandardCharsets;
*/
@Slf4j
@Component
public class ServerHandleReportHandleChannelTransferTypeAdvanced extends AbstractHandleReportHandleChannelTransferTypeAdvanced<NettyProxyMsg> {
public class ServerHandleReportServicePermeateClientTransferTypeAdvanced extends AbstractHandleReportServicePermeateClientTransferTypeAdvanced<NettyProxyMsg> {
private final ChannelFlowAdapter channelFlowAdapter;
public ServerHandleReportHandleChannelTransferTypeAdvanced(ChannelFlowAdapter channelFlowAdapter) {
public ServerHandleReportServicePermeateClientTransferTypeAdvanced(ChannelFlowAdapter channelFlowAdapter) {
this.channelFlowAdapter = channelFlowAdapter;
}

View File

@ -45,14 +45,6 @@ public class NettyClientPermeateServerRealHandler extends SimpleChannelInboundHa
nextChannel.writeAndFlush(nettyMsg);
// 记录出口数据
// ServerChannelFlow serverChannelFlow = ServerChannelFlow
// .builder()
// .channelFlowEnum(ChannelFlowEnum.OUT_FLOW)
// .port(visitorPort)
// .clientId(clientId)
// .flow(msg.getData().length)
// .build();
// channelFlowAdapter.asyncHandler(channel, serverChannelFlow);
}
@ -62,8 +54,11 @@ public class NettyClientPermeateServerRealHandler extends SimpleChannelInboundHa
// 客户端真实通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
nextChannel.close();
log.info("【客户端渗透服务端】服务端真实连接断开");
// 下发关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
@ -71,12 +66,13 @@ public class NettyClientPermeateServerRealHandler extends SimpleChannelInboundHa
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
log.info("channelWritabilityChanged");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.info("exceptionCaught:{}", cause.getMessage());
super.exceptionCaught(ctx, cause);
}
}

View File

@ -96,7 +96,6 @@ public class NettyServerPermeateClientVisitorHandler extends SimpleChannelInboun
log.debug("【服务端】访客端口成功接收数据:{}", new String(bytes));
// 使用访客的通信通道
Channel visitorCommunicationChannel = NettyCommunicationIdContext.getVisitor(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(visitorChannel);
// 绑定数据流量
ChannelAttributeKeyUtils.buildInFlow(nextChannel, bytes.length);
@ -159,7 +158,6 @@ public class NettyServerPermeateClientVisitorHandler extends SimpleChannelInboun
return;
}
Channel visitorCommunicationChannel = NettyCommunicationIdContext.getVisitor(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
@ -193,7 +191,6 @@ public class NettyServerPermeateClientVisitorHandler extends SimpleChannelInboun
String clientId = ChannelAttributeKeyUtils.getClientId(channel);
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
// 使用通信通道 下发关闭访客
Channel visitorChannel = NettyCommunicationIdContext.getVisitor(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 下发关闭访客

View File

@ -6,12 +6,12 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPermeateRealServer;
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.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.ClientPermeateServerRealFilter;
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.PermeateClientRealFilter;
import java.util.concurrent.TimeUnit;
/**
* 客户端渗透服务端 连接真实通道
@ -25,7 +25,7 @@ public class NettyClientPermeateServerConnectRealSocket {
* 连接真实服务
*/
public static void buildNewRealServer(
String visitorId,int visitorPort,String targetIp,int targetPort, Channel visitorChannel) {
String visitorId, int visitorPort, String targetIp, int targetPort, Channel transferChannel) {
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
@ -54,8 +54,8 @@ public class NettyClientPermeateServerConnectRealSocket {
log.info("服务端内网渗透通过,绑定本地服务,IP:{},端口:{} channelID:{} 新建通道成功", targetIp, targetPort,realChannel.id().asLongText());
ChannelAttributeKeyUtils.buildVisitorPort(realChannel, visitorPort);
// 缓存当前端口对应的通道、通道池
ChannelAttributeKeyUtils.buildNextChannel(realChannel, visitorChannel);
ChannelAttributeKeyUtils.buildNextChannel(visitorChannel, realChannel);
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
@ -66,15 +66,14 @@ public class NettyClientPermeateServerConnectRealSocket {
myMsg.setClientTargetIp(targetIp);
myMsg.setClientTargetPort(targetPort);
myMsg.setVisitorId(visitorId);
visitorChannel.writeAndFlush(myMsg);
transferChannel.writeAndFlush(myMsg);
} else {
log.error("服务端内网渗透 无法连接当前网络内的目标IP【{}】,目标端口:【{}】", targetIp, targetPort);
// future.channel().eventLoop().schedule(() -> {
// buildNewRealServer(internalNetworkPermeateRealServer);
// }, 2, TimeUnit.SECONDS);
channelFuture.channel().eventLoop().schedule(() -> {
buildNewRealServer(visitorId, visitorPort, targetIp, targetPort, transferChannel);
}, 2, TimeUnit.SECONDS);
}
});

View File

@ -10,6 +10,8 @@ import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPermeate
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.PermeateClientRealFilter;
import java.util.concurrent.TimeUnit;
/**
* 服务端渗透服务端 连接真实通道
*/
@ -74,9 +76,9 @@ public class NettyServerPermeateServerConnectRealSocket {
} else {
log.error("服务端内网渗透 无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientTargetIp, clientTargetPort);
// future.channel().eventLoop().schedule(() -> {
// buildNewRealServer(internalNetworkPermeateRealServer);
// }, 2, TimeUnit.SECONDS);
channelFuture.channel().eventLoop().schedule(() -> {
buildNewRealServer(internalNetworkPermeateRealServer, visitorChannel, visitorId);
}, 2, TimeUnit.SECONDS);
}
});