【fix】修复流量计费bug

This commit is contained in:
wujiawei
2024-09-22 18:13:51 +08:00
parent 4f58a675d0
commit c04ba09763
21 changed files with 603 additions and 65 deletions

View File

@ -0,0 +1,33 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.advanced;
import io.netty.channel.Channel;
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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportClientPermeateServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.stereotype.Component;
/**
* 上报 客户端渗透服务端通信通道关闭
*/
@Slf4j
@Component
public class ServerHandleReportClientPermeateServerTransferCloseTypeAdvanced extends AbstractHandleReportClientPermeateServerTransferCloseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 关闭传输通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
nextChannel.close();
channel.close();
}
}

View File

@ -0,0 +1,63 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.advanced;
import io.netty.buffer.ByteBuf;
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;
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;
/**
* 上报 客户端渗透服务端通信
* REPORT_CLIENT_PERMEATE_SERVER_TRANSFER
*/
@Slf4j
@Component
public class ServerHandleReportClientPermeateServerTransferTypeAdvanced extends AbstractHandleReportClientPermeateServerTransferTypeAdvanced<NettyProxyMsg> {
private final ChannelFlowAdapter channelFlowAdapter;
public ServerHandleReportClientPermeateServerTransferTypeAdvanced(ChannelFlowAdapter channelFlowAdapter) {
this.channelFlowAdapter = channelFlowAdapter;
}
/**
* 处理当前数据
*
* @param channel 当前通道
* @param msg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg msg) {
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()));
// 将数据转发访客通道
Channel visitor = NettyRealIdContext.getReal(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel != null) {
ByteBuf buf = nextChannel.config().getAllocator().buffer(msg.getData().length);
buf.writeBytes(msg.getData());
ChannelFuture channelFuture = nextChannel.writeAndFlush(buf);
boolean success = channelFuture.isSuccess();
log.debug("visitor writerAndFlush status: {}", success);
// 记录出口数据
ServerChannelFlow serverChannelFlow = ServerChannelFlow
.builder()
.channelFlowEnum(ChannelFlowEnum.OUT_FLOW)
.port(visitorPort)
.clientId(clientId)
.flow(msg.getData().length)
.build();
channelFlowAdapter.asyncHandler(channel, serverChannelFlow);
}
log.debug("访客ID:【{}】接收到客户端:[{}] 发送真实数据成功", new String(visitorId), clientId);
}
}

View File

@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets;
/**
* 服务端处理客户端数据传输
* REPORT_CLIENT_STAGING_CLOSED
* REPORT_CLIENT_TRANSFER
*/
@Slf4j
@Component

View File

@ -5,8 +5,7 @@ import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.ClientPermeateServerRealHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.PermeateClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.NettyClientPermeateServerRealHandler;
/**
* 客户端渗透服务端
@ -24,7 +23,7 @@ public class ClientPermeateServerRealFilter extends DebugChannelInitializer<Sock
// 解码、编码
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
pipeline.addLast(new TransferEncoder());
pipeline.addLast(new ClientPermeateServerRealHandler());
pipeline.addLast(new NettyClientPermeateServerRealHandler());
}
}

View File

@ -1,8 +1,6 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.handler;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
@ -17,7 +15,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
* 客户端渗透服务端
*/
@Slf4j
public class ClientPermeateServerRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
public class NettyClientPermeateServerRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
@ -42,7 +40,7 @@ public class ClientPermeateServerRealHandler extends SimpleChannelInboundHandler
// 消息下发到客户端
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_TRANSFER);
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER);
nettyMsg.setData(bytes);
nextChannel.writeAndFlush(nettyMsg);
@ -66,13 +64,6 @@ public class ClientPermeateServerRealHandler extends SimpleChannelInboundHandler
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 获取访客的传输通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
log.debug("transfer AUTO_READ:{} ",ctx.channel().isWritable());
nextChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
}
}
@Override

View File

@ -27,7 +27,7 @@ public class LazyVisitorPortFlowDO {
* 客户端ID
*/
@Schema(description = "客户端ID", name = "clientId", example = "")
@LazyTableFieldUnique(name = "client_id", comment = "客户端ID", columnType = "varchar(50)")
@LazyTableFieldUnique(name = "client_id", comment = "客户端ID", columnType = "varchar(50)",notNull = true)
private String clientId;
/**