【fix】 so nice serve proxy client is easy

This commit is contained in:
wujiawei 2025-04-07 21:50:05 +08:00
parent b1b329aae1
commit 3444e90d3d
10 changed files with 142 additions and 5 deletions

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.TcpMessageTypeEnums;
public abstract class AbstractHttpServerProxyClientTypeAdvanced <MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_SERVER_PROXY_CLIENT_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -346,6 +346,24 @@ public class TcpMessageType {
public static final byte HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ = HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ + 1;
/**
* http 服务端代理客户端
*
* @see TcpMessageTypeEnums#HTTP_SERVER_PROXY_CLIENT_
* @see AbstractHttpServerProxyClientTypeAdvanced
*/
public static final byte HTTP_SERVER_PROXY_CLIENT_ = HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ + 1;
/**
* 下发 客户端接收连接成功通知
*

View File

@ -221,6 +221,11 @@ public enum TcpMessageTypeEnums {
* @see AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced
*/
HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_, "http 下发传输通道关闭"),
/**
* 服务端代理客户端
* @see AbstractHttpServerProxyClientTypeAdvanced
*/
HTTP_SERVER_PROXY_CLIENT_(TcpMessageType.HTTP_SERVER_PROXY_CLIENT_, "http 服务端代理客户端"),

View File

@ -99,6 +99,11 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_CLIENT_);
}else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){
proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_);
}else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
String clientId = clientProxyRoute.getClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(TcpMessageType.HTTP_SERVER_PROXY_CLIENT_);
}
}
channelTypeAdapter.handler(ctx.channel(), proxyMsg);

View File

@ -8,7 +8,8 @@ import lombok.Getter;
public enum RouteType {
LOCAL("本地路由"),
CLIENT_PROXY_CLIENT("客户端远程客户端路由"),
CLIENT_PROXY_SEVER("客户端远程服务端路由");
CLIENT_PROXY_SEVER("客户端远程服务端路由"),
SERVER_PROXY_CLIENT("服务端远程客户端路由");
private final String desc;

View File

@ -224,5 +224,13 @@ public class ServerAutoConfiguration {
public ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced serverHandleHttpReportClientProxyClientTransferResponseTypeAdvanced() {
return new ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced();
}
@Bean
public ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced1 serverHandleHttpReportClientProxyServerProxyTransferTypeAdvanced1(){
return new ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced1();
}
@Bean
public ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced serverHandleHttpReportClientProxyClientTransferCloseTypeAdvanced(){
return new ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced();
}
}
}

View File

@ -28,7 +28,7 @@ public class ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced
/**
* 处理当前数据
*
* @param transferChannel 当前通道
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override

View File

@ -0,0 +1,65 @@
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.AbstractHttpServerProxyClientTypeAdvanced;
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;
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced1
extends AbstractHttpServerProxyClientTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, 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(channel, clientId);
ChannelAttributeKeyUtils.buildVisitorId(channel, msgVisitorId);
NettyTransferChannelContext.pushVisitor(channel, msgVisitorId);
NettyByteBuf nettyByteBuf = new NettyByteBuf();
nettyByteBuf.setData(data);
ChannelAttributeKeyUtils.buildNettyByteBufData(channel, 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 target client:【】 channel",clientId);
}
}
}

View File

@ -56,12 +56,20 @@ public class LazyNettyClientRouteRepositoryImpl implements LazyNettyClientRout
String routeIp = lazyNettyClientRoute.getRouteIp();
String routePort = lazyNettyClientRoute.getRoutePort();
String clientId = lazyNettyClientRoute.getClientId();
ClientProxyRoute serverProxyClientRoute = new ClientProxyRoute();
serverProxyClientRoute.setClientId(clientId);
serverProxyClientRoute.setAllowIp(routeIp);
serverProxyClientRoute.setAllowPort(routePort);
serverProxyClientRoute.setRouteType(RouteType.SERVER_PROXY_CLIENT);
RouteContext.setRoute(serverProxyClientRoute);
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();

View File

@ -60,13 +60,19 @@ public class LazyNettyServerRouteRepositoryImpl implements LazyNettyServerRout
serverProxyRoute.setServerIp("default");
serverProxyRoute.setAllowIp(routeIp);
serverProxyRoute.setAllowPort(routePort);
serverProxyRoute.setRouteType(RouteType.CLIENT_PROXY_SEVER);
serverProxyRoute.setRouteType(RouteType.LOCAL);
RouteContext.setRoute(serverProxyRoute);
ServerProxyRoute clientProxyServerRoute = new ServerProxyRoute();
clientProxyServerRoute.setServerIp("default");
clientProxyServerRoute.setAllowIp(routeIp);
clientProxyServerRoute.setAllowPort(routePort);
clientProxyServerRoute.setRouteType(RouteType.CLIENT_PROXY_SEVER);
// 发送所有客户端本地路由刷新
ChannelContext.getChannels().forEach((clientId, channels) -> {
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_);
nettyMsg.setData((JSON.toJSONString(serverProxyRoute)
nettyMsg.setData((JSON.toJSONString(clientProxyServerRoute)
.getBytes(StandardCharsets.UTF_8)));
// 发送所有客户端ID
for (Channel channel : channels) {