【fix】 添加客户端代理客户端

This commit is contained in:
wujiawei
2025-04-06 18:52:31 +08:00
parent ba8ca7bcd9
commit fe8a2c865a
42 changed files with 1677 additions and 96 deletions

View File

@ -4,8 +4,7 @@ package org.framework.lazy.cloud.network.heartbeat.server.config;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.tcp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*;
import org.springframework.beans.factory.config.BeanDefinition;
@ -51,6 +50,7 @@ public class ServerAutoConfiguration {
LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication,
LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication,
LazyNettyServerRouteApplication lazyNettyServerRouteApplication,
LazyNettyClientRouteApplication lazyNettyClientRouteApplication,
LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication,
ServerNodeProperties serverNodeProperties
) {
@ -61,6 +61,7 @@ public class ServerAutoConfiguration {
lazyClientPermeateServerMappingApplication,
lazyClientPermeateClientMappingApplication,
lazyNettyServerRouteApplication,
lazyNettyClientRouteApplication,
lazyNettyClientTokenBucketApplication,
serverNodeProperties
);
@ -208,5 +209,20 @@ public class ServerAutoConfiguration {
public ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced serverHandleHttpReportClientProxyServerTransferCloseTypeAdvanced() {
return new ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced();
}
@Bean
public ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced serverHandleHttpReportClientProxyClientConnectTransferTypeAdvanced() {
return new ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced();
}
@Bean
public ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced serverHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced() {
return new ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced();
}
@Bean
public ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced serverHandleHttpReportClientProxyClientTransferResponseTypeAdvanced() {
return new ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced();
}
}
}

View File

@ -9,16 +9,19 @@ import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.server.AbstractHandleTcpClientConnectSuccessTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ServerProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.client.mapping.LazyClientPermeateClientMappingQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyClientPermeateServerMappingQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.route.LazyNettyClientRouteQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.state.LazyNettyClientLoginCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.server.route.LazyNettyServerRouteQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyClientPermeateClientMappingDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyClientPermeateServerMappingDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyClientRouteDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyServerRouteDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.blacklist.LazyNettyClientBlacklist;
import org.springframework.beans.factory.config.BeanDefinition;
@ -46,17 +49,19 @@ public class ServerHandleTcpClientConnectSuccessTypeAdvanced extends AbstractHan
private final LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication;
private final LazyNettyServerRouteApplication lazyNettyServerRouteApplication;
private final LazyNettyClientRouteApplication lazyNettyClientRouteApplication;
private final LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication;
private final ServerNodeProperties serverNodeProperties;
public ServerHandleTcpClientConnectSuccessTypeAdvanced(LazyClientStatsChangeApplication lazyClientStatsChangeApplication, LazyNettyClientBlacklistApplication lazyNettyClientBlacklistApplication, LazyServerPermeateClientMappingApplication lazyServerPermeateClientMappingApplication, LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, ServerNodeProperties serverNodeProperties) {
public ServerHandleTcpClientConnectSuccessTypeAdvanced(LazyClientStatsChangeApplication lazyClientStatsChangeApplication, LazyNettyClientBlacklistApplication lazyNettyClientBlacklistApplication, LazyServerPermeateClientMappingApplication lazyServerPermeateClientMappingApplication, LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication, LazyNettyClientRouteApplication lazyNettyClientRouteApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, ServerNodeProperties serverNodeProperties) {
this.lazyClientStatsChangeApplication = lazyClientStatsChangeApplication;
this.lazyNettyClientBlacklistApplication = lazyNettyClientBlacklistApplication;
this.lazyServerPermeateClientMappingApplication = lazyServerPermeateClientMappingApplication;
this.lazyClientPermeateServerMappingApplication = lazyClientPermeateServerMappingApplication;
this.lazyClientPermeateClientMappingApplication = lazyClientPermeateClientMappingApplication;
this.lazyNettyServerRouteApplication = lazyNettyServerRouteApplication;
this.lazyNettyClientRouteApplication = lazyNettyClientRouteApplication;
this.lazyNettyClientTokenBucketApplication = lazyNettyClientTokenBucketApplication;
this.serverNodeProperties = serverNodeProperties;
}
@ -203,6 +208,29 @@ public class ServerHandleTcpClientConnectSuccessTypeAdvanced extends AbstractHan
}
});
// 下发客户端路由
LazyNettyClientRouteQueryListCommand lazyNettyClientRouteQueryListCommand = new LazyNettyClientRouteQueryListCommand();
lazyNettyClientRouteQueryListCommand.setIsDeleted(false);
lazyNettyClientRouteApplication.findList(lazyNettyClientRouteQueryListCommand)
.accept(lazyNettyClientRouteDTOList -> {
for (LazyNettyClientRouteDTO lazyNettyClientRouteDTO : lazyNettyClientRouteDTOList) {
String routeIp = lazyNettyClientRouteDTO.getRouteIp();
String routePort = lazyNettyClientRouteDTO.getRoutePort();
String routeClientId = lazyNettyClientRouteDTO.getClientId();
ClientProxyRoute clientProxyRoute = new ClientProxyRoute();
clientProxyRoute.setClientId(routeClientId);
clientProxyRoute.setAllowIp(routeIp);
clientProxyRoute.setAllowPort(routePort);
clientProxyRoute.setRouteType(RouteType.CLIENT_PROXY_CLIENT);
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_);
nettyMsg.setData((JSON.toJSONString(clientProxyRoute)
.getBytes(StandardCharsets.UTF_8)));
newChannel.writeAndFlush(nettyMsg);
}
});
}
}

View File

@ -0,0 +1,52 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.util.List;
/**
* 客户端代理到服务端转发请求数据
*/
@NoArgsConstructor
@Data
public class NettyHttpClientProxyClientTransfer {
/**
* 目标地址
*/
private String targetIp;
/**
* 目标端口
*/
private Integer targetPort;
/**
* 目标客户端ID
*/
private String targetClientId;
/**
* 服务端地址信息
*/
private NettyClientProperties nettyClientProperties;
/**
* 通道处理器
*/
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
/**
* 发送数据
*/
private byte[] data;
/**
* 是否是ssl
*/
private boolean isSsl;
}

View File

@ -0,0 +1,69 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.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.NettyByteBuf;
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.proxy.http.server.AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
/**
* 服务端处理客户端代理到服务端的请求
* HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced
extends AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
// 创建链接、发送数据
String targetPortString = nettyProxyMsg.getTargetPortString();
String targetIpString = nettyProxyMsg.getTargetIpString();
byte[] data = nettyProxyMsg.getData();
byte[] visitorId = nettyProxyMsg.getVisitorId();
byte[] clientId = nettyProxyMsg.getClientId();
String msgVisitorId = new String(visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId);
NettyTransferChannelContext.pushVisitor(transferChannel, msgVisitorId);
NettyByteBuf nettyByteBuf = new NettyByteBuf();
nettyByteBuf.setData(data);
ChannelAttributeKeyUtils.buildNettyByteBufData(transferChannel, nettyByteBuf);
// 获取客户端心跳通道
Channel loadBalance = ChannelContext.getLoadBalance(clientId);
NettyProxyMsg clientConnectTagetNettyProxyMsg = new NettyProxyMsg();
clientConnectTagetNettyProxyMsg.setVisitorId(msgVisitorId);
clientConnectTagetNettyProxyMsg.setClientTargetIp(targetIpString);
clientConnectTagetNettyProxyMsg.setClientTargetPort(Integer.parseInt(targetPortString));
clientConnectTagetNettyProxyMsg.setClientId(clientId);
clientConnectTagetNettyProxyMsg.setType(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_);
if (loadBalance != null) {
// 让客户端主动创建一个数据传输通道
loadBalance.writeAndFlush(clientConnectTagetNettyProxyMsg);
}else {
log.error("can not find the client:【】 channel",clientId);
}
}
}

View File

@ -0,0 +1,68 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.proxy.http.server.AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
/**
* 目标客户端传输通道连接初始化成功
* HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced
extends AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
byte[] clientId = nettyProxyMsg.getClientId();
// next translation
Channel nextTransferChannel = NettyTransferChannelContext.getVisitor(msgVisitorId);
ChannelAttributeKeyUtils.buildTransferNextChannel(nextTransferChannel, transferChannel);
ChannelAttributeKeyUtils.buildTransferNextChannel(transferChannel, nextTransferChannel);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId);
NettyByteBuf nettyByteBufData = ChannelAttributeKeyUtils.getNettyByteBufData(nextTransferChannel);
NettyProxyMsg clientConnectTagetNettyProxyMsg = new NettyProxyMsg();
clientConnectTagetNettyProxyMsg.setVisitorId(msgVisitorId);
clientConnectTagetNettyProxyMsg.setClientTargetIp(msgClientTargetIp);
clientConnectTagetNettyProxyMsg.setClientTargetPort(msgClientTargetPort);
clientConnectTagetNettyProxyMsg.setClientId(clientId);
clientConnectTagetNettyProxyMsg.setData(nettyByteBufData.getData());
clientConnectTagetNettyProxyMsg.setType(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_);
transferChannel.writeAndFlush(clientConnectTagetNettyProxyMsg);
}
}

View File

@ -0,0 +1,54 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.proxy.http.server.AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
/**
* 目标客户端传输通道连接初始化成功
* HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced
extends AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 关闭通道
// 关闭 next、next transfer 通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel);
// 下发关闭客户端真实通道
NettyProxyMsg closeTransferNettyProxyMsg = new NettyProxyMsg();
closeTransferNettyProxyMsg.setType(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
transferNextChannel.writeAndFlush(closeTransferNettyProxyMsg);
channel.close();
nextChannel.close();
transferNextChannel.close();
}
}

View File

@ -0,0 +1,48 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.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.NettyByteBuf;
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.proxy.http.server.AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;
/**
* 服务端处理客户端代理到服务端的请求
* HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced
extends AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param transferChannel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
// 将返回数据下发客户端
Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(transferChannel);
if(ObjectUtils.isNotEmpty(nettyProxyMsg)) {
transferNextChannel.writeAndFlush(nettyProxyMsg);
}
}
}

View File

@ -0,0 +1,92 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
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.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.NettyHttpClientProxyClientTransfer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.NettyHttpClientProxyServerTransfer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.filter.NettyHttpClientProxyServerRealFilter;
import java.util.concurrent.TimeUnit;
/**
* 服务端渗透服务端 传输通道
*/
@Slf4j
public class NettyHttpClientProxyClientTransferSocket {
private static final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
/**
* @param nettyHttpClientProxyClientTransfer 访客信息
* @param proxyTransferChannel 代理传输通道
* @param visitorId 请求ID
*/
public static void buildRealServer(NettyHttpClientProxyClientTransfer nettyHttpClientProxyClientTransfer,
Channel proxyTransferChannel, String visitorId) {
try {
String clientTargetIp = nettyHttpClientProxyClientTransfer.getTargetIp();
Integer clientTargetPort = nettyHttpClientProxyClientTransfer.getTargetPort();
byte[] data = nettyHttpClientProxyClientTransfer.getData();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
// .option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyHttpClientProxyServerRealFilter(nettyHttpClientProxyClientTransfer))
;
bootstrap
.connect(clientTargetIp, clientTargetPort)
.sync()
.addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
Channel realChannel = channelFuture.channel();
// realChannel.config().setOption(ChannelOption.AUTO_READ, false);
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
log.info("客户端代理服务端,绑定本地服务,IP:{},端口:{} channelID:{} 新建通道成功", clientTargetIp, clientTargetPort, realChannel.id().asLongText());
// 缓存当前端口对应的通道、通道池
ChannelAttributeKeyUtils.buildNextChannel(realChannel, proxyTransferChannel);
ChannelAttributeKeyUtils.buildNextChannel(proxyTransferChannel, realChannel);
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
proxyTransferChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 发送数据
ByteBuf buf = realChannel.config().getAllocator().buffer(data.length);
buf.writeBytes(data);
realChannel.writeAndFlush(buf);
} else {
log.error("客户端代理服务端 无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientTargetIp, clientTargetPort);
eventLoopGroup.schedule(() -> {
buildRealServer(nettyHttpClientProxyClientTransfer, proxyTransferChannel, visitorId);
}, 2, TimeUnit.SECONDS);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,10 +1,19 @@
package org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.persistence;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.entity.LazyNettyClientRouteDO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.converter.LazyNettyClientRouteConverter;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.mapper.LazyNettyClientRouteMapper;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.route.LazyNettyClientRouteRepository;
import org.springframework.stereotype.Repository;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyUpdateSetValueWrappers;
@ -43,6 +52,27 @@ public class LazyNettyClientRouteRepositoryImpl implements LazyNettyClientRout
public Result<LazyNettyClientRoute> story(LazyNettyClientRoute lazyNettyClientRoute) {
LazyNettyClientRouteDO lazyNettyClientRouteDO = LazyNettyClientRouteConverter.INSTANCE.fromLazyNettyClientRoute(lazyNettyClientRoute);
lazyLambdaStream.upsert(lazyNettyClientRouteDO);
// 更新服务端路由
String routeIp = lazyNettyClientRoute.getRouteIp();
String routePort = lazyNettyClientRoute.getRoutePort();
String clientId = lazyNettyClientRoute.getClientId();
ClientProxyRoute clientProxyRoute = new ClientProxyRoute();
clientProxyRoute.setClientId(clientId);
clientProxyRoute.setAllowIp(routeIp);
clientProxyRoute.setAllowPort(routePort);
clientProxyRoute.setRouteType(RouteType.CLIENT_PROXY_CLIENT);
RouteContext.setRoute(clientProxyRoute);
// 发送所有客户端本地路由刷新
ChannelContext.getChannels().forEach((channelClientId, channels) -> {
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_);
nettyMsg.setData((JSON.toJSONString(clientProxyRoute)
.getBytes(StandardCharsets.UTF_8)));
// 发送所有客户端ID
for (Channel channel : channels) {
channel.writeAndFlush(nettyMsg);
}
});
return ResultFactory.successOf();
}