【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

@ -5,10 +5,7 @@ import org.framework.lazy.cloud.network.heartbeat.client.context.NettyClientSock
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.event.ClientChangeEvent; import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.advanced.*; import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.udp.advanced.*; import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.udp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.ClientHandleDistributeHttpClientProxyServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.ClientHandleHttpClientProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.ClientHandleHttpClientProxyServerTypeAdvanced;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -354,5 +351,28 @@ public class ClientAutoConfiguration {
public ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced clientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced() { public ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced clientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced() {
return new ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced(); return new ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced();
} }
@Bean
public ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced clientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced() {
return new ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced();
}
@Bean
public ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced clientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced() {
return new ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced();
}
@Bean
public ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced clientHandleDistributeHttpClientProxyClientTransferRequestAdvanced() {
return new ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced();
}
@Bean
public ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced clientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced(){
return new ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced();
}
@Bean
public ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced clientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced(){
return new ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced();
}
} }
} }

View File

@ -0,0 +1,53 @@
package org.framework.lazy.cloud.network.heartbeat.client.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 NettyHttpClientProxyClient {
/**
* 目标地址
*/
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,57 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* http 远程代理
* 客户端客户端远程到服务端客户端远程到客户端
*/
@Slf4j
public class ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced extends
AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 客户端主动连接服务端创建一个传输通道
byte[] msgClientId = nettyProxyMsg.getClientId();
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
String clientId = new String(msgClientId);
String clientTargetIp = new String(msgClientTargetIp);
Integer clientTargetPort = Integer.parseInt(new String(msgClientTargetPort));
String visitorId = new String(msgVisitorId);
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
NettyHttpClientProxyClientRealSocket.buildRealServer(
clientId,
clientTargetIp,
clientTargetPort,
visitorId,
nettyClientProperties,
handleChannelTypeAdvancedList
);
}
}

View File

@ -0,0 +1,46 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* http 远程代理
* 客户端客户端远程到服务端客户端远程到客户端
*/
@Slf4j
public class ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced extends
AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 数据发送
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if(ObjectUtils.isNotEmpty(nextChannel)){
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
nextChannel.writeAndFlush(buf);
}
}
}

View File

@ -0,0 +1,35 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
/**
* http 远程代理
* 客户端客户端远程到服务端客户端远程到客户端
*/
@Slf4j
public class ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced extends
AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
byte[] data = nettyProxyMsg.getData();
ClientProxyRoute clientProxyRoute = JSON.parseObject(data, ClientProxyRoute.class);
RouteContext.setRoute(clientProxyRoute);
}
}

View File

@ -1,22 +1,16 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced; package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import io.netty.channel.ChannelFutureListener; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyClient;
import io.netty.channel.ChannelInitializer; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientProxySocket;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder; import org.wu.framework.spring.utils.SpringContextHolder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import java.util.ArrayList;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyHttpProxyBackendHandler; import java.util.List;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
/** /**
* http 远程代理 * http 远程代理
@ -35,42 +29,23 @@ public class ClientHandleHttpClientProxyClientTypeAdvanced extends
*/ */
@Override @Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) { protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
String targetPortString = nettyProxyMsg.getTargetPortString(); String targetPortString = nettyProxyMsg.getTargetPortString();
String targetIpString = nettyProxyMsg.getTargetIpString(); String targetIpString = nettyProxyMsg.getTargetIpString();
byte[] data = nettyProxyMsg.getData();
byte[] clientId = nettyProxyMsg.getClientId();
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
// 判断代理到客户端还是服务端 // 判断代理到客户端还是服务端
ProxyRoute route = RouteContext.getRoute(targetIpString, targetPortString); NettyHttpClientProxyClient nettyHttpClientProxyClient = new NettyHttpClientProxyClient();
if(RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){ nettyHttpClientProxyClient.setSsl(false);
// 服务端代理 nettyHttpClientProxyClient.setTargetIp(targetIpString);
nettyHttpClientProxyClient.setTargetPort(Integer.parseInt(targetPortString));
nettyHttpClientProxyClient.setTargetClientId(new String(clientId));
}else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){ nettyHttpClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
// 客户端代理 nettyHttpClientProxyClient.setNettyClientProperties(nettyClientProperties);
} nettyHttpClientProxyClient.setData(data);
Bootstrap b = new Bootstrap();
b.group(channel.eventLoop()) NettyHttpClientProxyClientProxySocket.buildTransferServer(nettyHttpClientProxyClient, channel);
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024 * 10),
new TransferEncoder(),
new NettyHttpProxyBackendHandler()
);
}
});
ChannelFuture f = b.connect(targetIpString, Integer.parseInt(targetPortString));
Channel proxyChannel = f.channel();
f.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
ChannelAttributeKeyUtils.buildNextChannel(proxyChannel, channel);
ChannelAttributeKeyUtils.buildNextChannel(channel, proxyChannel);
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
proxyChannel.writeAndFlush(buf);
} else {
channel.close();
}
});
} }
} }

View File

@ -3,7 +3,7 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advan
import io.netty.channel.Channel; import io.netty.channel.Channel;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties; import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyServer; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyServer;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyServerTransferSocket; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyServerProxySocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced;
@ -45,7 +45,7 @@ public class ClientHandleHttpClientProxyServerTypeAdvanced extends
nettyHttpClientProxyServer.setNettyClientProperties(nettyClientProperties); nettyHttpClientProxyServer.setNettyClientProperties(nettyClientProperties);
nettyHttpClientProxyServer.setData(data); nettyHttpClientProxyServer.setData(data);
NettyHttpClientProxyServerTransferSocket.buildTransferServer(nettyHttpClientProxyServer, channel); NettyHttpClientProxyServerProxySocket.buildTransferServer(nettyHttpClientProxyServer, channel);
} }
} }

View File

@ -0,0 +1,39 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* http 远程代理
* 客户端客户端远程到服务端客户端远程到客户端
*/
@Slf4j
public class ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced extends
AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 关闭传输通道
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
realChannel.close();// 真实通道关闭
channel.close(); // 数据传输通道关闭
}
}

View File

@ -0,0 +1,39 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* http 远程代理
* 客户端客户端远程到服务端客户端远程到客户端
*/
@Slf4j
public class ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced extends
AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 数据发送
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if(ObjectUtils.isNotEmpty(nextChannel)){
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
nextChannel.writeAndFlush(buf);
}
// 关闭连接
}
}

View File

@ -5,8 +5,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientTransferHandler; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientProxyHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyServerTransferHandler;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
@ -15,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInit
/** /**
* netty 客户端代理服务端数据传输通道 * netty 客户端代理服务端数据传输通道
*/ */
public class NettyHttpClientProxyServerTransferFilter extends DebugChannelInitializer<SocketChannel> { public class NettyHttpClientProxyClientProxyFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyServerTransferFilter(ChannelTypeAdapter channelTypeAdapter) { public NettyHttpClientProxyClientProxyFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter; this.channelTypeAdapter = channelTypeAdapter;
} }
@ -42,6 +41,6 @@ public class NettyHttpClientProxyServerTransferFilter extends DebugChannelInitia
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0)); pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
pipeline.addLast(new NettyProxyMsgEncoder()); pipeline.addLast(new NettyProxyMsgEncoder());
pipeline.addLast(new NettyHttpClientProxyServerTransferHandler(channelTypeAdapter)); pipeline.addLast(new NettyHttpClientProxyClientProxyHandler(channelTypeAdapter));
} }
} }

View File

@ -0,0 +1,28 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientRealHandler;
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;
public class NettyHttpClientProxyClientRealFilter extends DebugChannelInitializer<SocketChannel> {
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
*/
@Override
protected void initChannel0(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
// 解码编码
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
pipeline.addLast(new TransferEncoder());
pipeline.addLast(new NettyHttpClientProxyClientRealHandler());
}
}

View File

@ -0,0 +1,47 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientTransferRealHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientTransferRealHandler;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
/**
* netty 客户端连接真实服服务端访客拦截器
*/
public class NettyHttpClientProxyClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyClientTransferRealFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
* @throws Exception is thrown if an error occurs. In that case it will be handled by
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
* the {@link Channel}.
*/
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// // 解码编码
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
// pipeline.addLast(new NettMsgEncoder());
pipeline.addLast(new IdleStateHandler(0, 4, 0));
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
pipeline.addLast(new NettyProxyMsgEncoder());
pipeline.addLast(new NettyHttpClientProxyClientTransferRealHandler(channelTypeAdapter));
}
}

View File

@ -0,0 +1,46 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyServerProxyHandler;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
/**
* netty 客户端代理服务端数据传输通道
*/
public class NettyHttpClientProxyServerProxyFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyServerProxyFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
* @throws Exception is thrown if an error occurs. In that case it will be handled by
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
* the {@link Channel}.
*/
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// // 解码编码
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
// pipeline.addLast(new NettMsgEncoder());
pipeline.addLast(new IdleStateHandler(0, 4, 0));
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
pipeline.addLast(new NettyProxyMsgEncoder());
pipeline.addLast(new NettyHttpClientProxyServerProxyHandler(channelTypeAdapter));
}
}

View File

@ -0,0 +1,70 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettyHttpClientProxyClientProxyHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyClientProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
Channel channel = ctx.channel();
channelTypeAdapter.handler(channel, nettyProxyMsg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 关闭访客
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

View File

@ -0,0 +1,81 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
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.utils.ChannelAttributeKeyUtils;
/**
* 来自客户端 真实服务器返回的数据请求
*/
@Slf4j
public class NettyHttpClientProxyClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
@Override
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
byte[] bytes = nettyByteBuf.getData();
log.debug("bytes.length:{}",bytes.length);
log.debug("接收客户端真实服务数据:{}", new String(bytes));
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(ctx.channel());
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
// 访客通信通道 上报服务端代理完成
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
NettyProxyMsg returnMessage = new NettyProxyMsg();
returnMessage.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_);
returnMessage.setVisitorId(visitorId);
returnMessage.setClientId(clientId);
returnMessage.setVisitorPort(visitorPort);
returnMessage.setData(bytes);
nextChannel.writeAndFlush(returnMessage);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 客户端真实通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 获取访客的传输通道
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

View File

@ -0,0 +1,72 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
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.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettyHttpClientProxyClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyClientTransferRealHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
Channel channel = ctx.channel();
channelTypeAdapter.handler(channel, nettyProxyMsg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
log.warn("close client permeate client transfer real clientId:{} visitorId:{}", clientId, visitorId);
// 关闭访客
if (nextChannel != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 处理客户端本地真实通道问题
if (ctx.channel().isWritable()) {
log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据
} else {
log.debug("Channel is not writable");
// 暂停写入操作等待可写状态
}
log.info("channelWritabilityChanged!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

View File

@ -14,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
* 客户端访客通信通道 处理器 * 客户端访客通信通道 处理器
*/ */
@Slf4j @Slf4j
public class NettyHttpClientProxyServerTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> { public class NettyHttpClientProxyServerProxyHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpClientProxyServerTransferHandler(ChannelTypeAdapter channelTypeAdapter) { public NettyHttpClientProxyServerProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter; this.channelTypeAdapter = channelTypeAdapter;
} }

View File

@ -7,8 +7,8 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties; import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.NettyClientPermeateClientVisitor; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientTransferFilter; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyClientProxyFilter;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; 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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
@ -20,14 +20,13 @@ import java.util.concurrent.TimeUnit;
* 客户端代理客户端传输通道 * 客户端代理客户端传输通道
*/ */
@Slf4j @Slf4j
public class NettyHttpClientProxyClientTransferSocket { public class NettyHttpClientProxyClientProxySocket {
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
/** /**
* 连接服务端通信通道 * 连接服务端通信通道
*/ */
public static void buildTransferServer(NettyClientPermeateClientVisitor nettyClientPermeateClientVisitor, Channel visitorChannel) { public static void buildTransferServer(NettyHttpClientProxyClient nettyHttpClientProxyClient, Channel visitorChannel) {
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup) bootstrap.group(eventLoopGroup)
@ -43,24 +42,25 @@ public class NettyHttpClientProxyClientTransferSocket {
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .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)) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyTcpClientPermeateClientTransferFilter(new ChannelTypeAdapter(nettyClientPermeateClientVisitor.getHandleChannelTypeAdvancedList()))) .handler(new NettyHttpClientProxyClientProxyFilter(new ChannelTypeAdapter(nettyHttpClientProxyClient.getHandleChannelTypeAdvancedList())))
; ;
NettyClientProperties nettyClientProperties = nettyClientPermeateClientVisitor.getNettyClientProperties(); NettyClientProperties nettyClientProperties = nettyHttpClientProxyClient.getNettyClientProperties();
String inetHost = nettyClientProperties.getInetHost(); String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort(); int inetPort = nettyClientProperties.getInetPort();
// local client id // local client id
String clientId = nettyClientProperties.getClientId(); String clientId = nettyClientProperties.getClientId();
String targetIp = nettyClientPermeateClientVisitor.getTargetIp(); String targetIp = nettyHttpClientProxyClient.getTargetIp();
Integer targetPort = nettyClientPermeateClientVisitor.getTargetPort(); Integer targetPort = nettyHttpClientProxyClient.getTargetPort();
String targetClientId = nettyHttpClientProxyClient.getTargetClientId();
String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel); String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel);
Integer visitorPort = nettyClientPermeateClientVisitor.getVisitorPort(); byte[] data = nettyHttpClientProxyClient.getData();
String toClientId = nettyClientPermeateClientVisitor.getToClientId();
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
log.info("Client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with clientId:【{}】 toClientId:【{}】 & visitorId:【{}】", inetHost, inetPort, clientId, toClientId, visitorId); // 客户端链接服务端
log.info("Client creates a new proxy channel to connect to server IP: {}, connecting to server port: {} with clientId:【{}】 toClientId:【{}】 & visitorId:【{}】",
inetHost, inetPort, clientId, targetClientId, visitorId);
ChannelFuture future = bootstrap.connect(inetHost, inetPort); ChannelFuture future = bootstrap.connect(inetHost, inetPort);
// 使用的客户端ID:{} // 使用的客户端ID:{}
@ -69,12 +69,12 @@ public class NettyHttpClientProxyClientTransferSocket {
if (futureListener.isSuccess()) { if (futureListener.isSuccess()) {
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg(); NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setType(TcpMessageType.TCP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL); nettyProxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_);
// other clientId // other clientId
nettyProxyMsg.setClientId(toClientId); nettyProxyMsg.setClientId(targetClientId);
nettyProxyMsg.setVisitorPort(visitorPort);
nettyProxyMsg.setClientTargetIp(targetIp); nettyProxyMsg.setClientTargetIp(targetIp);
nettyProxyMsg.setClientTargetPort(targetPort); nettyProxyMsg.setClientTargetPort(targetPort);
nettyProxyMsg.setData(data);
nettyProxyMsg.setVisitorId(visitorId); nettyProxyMsg.setVisitorId(visitorId);
transferChannel.writeAndFlush(nettyProxyMsg); transferChannel.writeAndFlush(nettyProxyMsg);
@ -90,7 +90,7 @@ public class NettyHttpClientProxyClientTransferSocket {
log.info("无法连接到服务端...."); log.info("无法连接到服务端....");
eventLoopGroup.schedule(() -> { eventLoopGroup.schedule(() -> {
try { try {
buildTransferServer(nettyClientPermeateClientVisitor, visitorChannel); buildTransferServer(nettyHttpClientProxyClient, visitorChannel);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -0,0 +1,170 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket;
import io.netty.bootstrap.Bootstrap;
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.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientTransferRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyClientTransferRealFilter;
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.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 客户端连接真实服务
*/
@Slf4j
public class NettyHttpClientProxyClientRealSocket {
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
public static void buildRealServer(String clientId,
String clientTargetIp,
Integer clientTargetPort,
String visitorId,
NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
try {
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 NettyHttpClientProxyClientRealFilter())
;
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
Channel realChannel = future.channel();
realChannel.config().setOption(ChannelOption.AUTO_READ, false);
log.info("访客通过 客户端:【{}】,visitorId:{},绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, visitorId, clientTargetIp, clientTargetPort);
// 客户端真实通道
NettyRealIdContext.pushReal(realChannel, visitorId);
// 绑定访客ID到当前真实通道属性
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
// 连接服务端 然后绑定通道
// 新建一个通道处理
newVisitorConnect2Server(
clientId,
clientTargetIp,
clientTargetPort,
visitorId,
realChannel,
nettyClientProperties,
handleChannelTypeAdvancedList
);
} else {
log.error("客户:【{}】,无法连接当前网络内的目标IP【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建访客连接服务端
*
* @param nettyClientProperties 服务端配置信息
* @param handleChannelTypeAdvancedList 处理器适配器
* @throws InterruptedException 异常
*/
protected static void newVisitorConnect2Server(String clientId,
String clientTargetIp,
Integer clientTargetPort,
String visitorId,
Channel realChannel,
NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
// 设置读缓冲区为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, 256)//务端接受连接的队列长度 默认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 NettyHttpClientProxyClientTransferRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
;
String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort();
// local client id
// String clientId = nettyClientProperties.getClientId();
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
log.info("client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with visitorId:{} & clientId:{}", inetHost, inetPort, visitorId, clientId);
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
future.addListener((ChannelFutureListener) futureListener -> {
Channel transferChannel = futureListener.channel();
if (futureListener.isSuccess()) {
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 通知服务端访客连接成功
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setVisitorId(visitorId);
nettyProxyMsg.setClientId(clientId);
nettyProxyMsg.setClientTargetIp(clientTargetIp);
nettyProxyMsg.setClientTargetPort(clientTargetPort);
nettyProxyMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_);
transferChannel.writeAndFlush(nettyProxyMsg);
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
// 绑定客户端真实通信通道
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
} else {
log.info("无法连接到服务端....");
eventLoopGroup.schedule(() -> {
try {
newVisitorConnect2Server(clientId,
clientTargetIp,
clientTargetPort,
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
} catch (Exception e) {
e.printStackTrace();
}
}, 2, TimeUnit.SECONDS);
}
});
}
}

View File

@ -8,7 +8,7 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties; import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyServer; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyServer;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyServerTransferFilter; import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyServerProxyFilter;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
@ -20,7 +20,7 @@ import java.util.concurrent.TimeUnit;
* 客户端代理客户端传输通道 * 客户端代理客户端传输通道
*/ */
@Slf4j @Slf4j
public class NettyHttpClientProxyServerTransferSocket { public class NettyHttpClientProxyServerProxySocket {
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
/** /**
@ -45,7 +45,7 @@ public class NettyHttpClientProxyServerTransferSocket {
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .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)) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyHttpClientProxyServerTransferFilter(new ChannelTypeAdapter(nettyHttpClientProxyServer.getHandleChannelTypeAdvancedList()))) .handler(new NettyHttpClientProxyServerProxyFilter(new ChannelTypeAdapter(nettyHttpClientProxyServer.getHandleChannelTypeAdvancedList())))
; ;
NettyClientProperties nettyClientProperties = nettyHttpClientProxyServer.getNettyClientProperties(); NettyClientProperties nettyClientProperties = nettyHttpClientProxyServer.getNettyClientProperties();
String inetHost = nettyClientProperties.getInetHost(); String inetHost = nettyClientProperties.getInetHost();

View File

@ -0,0 +1,25 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client;
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;
/**
* 下发客户端路由
* @param <MSG>
*/
public abstract class AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,22 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client;
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 AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,21 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client;
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 AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced <MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,20 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client;
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 AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,25 @@
package org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client;
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;
/**
* 下发客户端路由
* @param <MSG>
*/
public abstract class AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,25 @@
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;
/**
* 服务端处理客户端上报的代理请求数据
* @param <MSG>
*/
public abstract class AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,25 @@
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;
/**
* 目标客户端传输通道连接初始化成功
* @param <MSG>
*/
public abstract class AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced<MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -0,0 +1,20 @@
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 AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced <MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_.getTypeByte() == nettyProxyMsg.getType();
}
}

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 AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced <MSG> extends AbstractHandleChannelTypeAdvanced<NettyProxyMsg> implements HandleChannelTypeAdvanced {
/**
* 是否支持当前类型
*
* @param nettyProxyMsg 通道数据
* @return 布尔类型
*/
@Override
protected boolean doSupport(NettyProxyMsg nettyProxyMsg) {
return TcpMessageTypeEnums.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_.getTypeByte() == nettyProxyMsg.getType();
}
}

View File

@ -6,10 +6,8 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.s
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerServerRouteTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerTransferTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.TcpMessageTypeEnums; import org.framework.lazy.cloud.network.heartbeat.common.enums.TcpMessageTypeEnums;
/** /**
@ -273,12 +271,79 @@ public class TcpMessageType {
*/ */
public static final byte HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_ = HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_ + 1; public static final byte HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_ = HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_ + 1;
/**
* 下发客户端路由
*
* @see TcpMessageTypeEnums#HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_
* @see AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced
*/
public static final byte HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_ = HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_ + 1;
/**
* http 客户端远程客户端代理传输连接
*
* @see TcpMessageTypeEnums#HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_
* @see AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced
*/
public static final byte HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_ = HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_ + 1;
/**
* http 客户端代理客户端客户端连接服务端成功
*
* @see TcpMessageTypeEnums#HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_
* @see AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced
*/
public static final byte HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_ = HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_ + 1;
/**
* http 目标客户端传输通道连接初始化成功
*
* @see TcpMessageTypeEnums#HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_
* @see AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced
*/
public static final byte HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_ = HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_ + 1;
/**
* http 下发数据到另一个客户端
*
* @see TcpMessageTypeEnums#HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_
* @see AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced
*/
public static final byte HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ = HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_ + 1;
/**
* http 另一个客户端将返回数据通过传输通道返回
*
* @see TcpMessageTypeEnums#HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_
* @see AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced
*/
public static final byte HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ = HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_ + 1;
/**
* 下发返回结果
*
* @see TcpMessageTypeEnums#HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_
* @see AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced
*/
public static final byte HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ = HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ + 1;
/**
* 上报传输通道关闭
*
* @see TcpMessageTypeEnums#HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_
* @see AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced
*/
public static final byte HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ = HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_ + 1;
/**
* 下发传输通道关闭
*
* @see TcpMessageTypeEnums#HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_
* @see AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced
*/
public static final byte HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ = HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_ + 1;
/** /**

View File

@ -2,16 +2,14 @@ package org.framework.lazy.cloud.network.heartbeat.common.enums;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.AbstractTcpHandleChannelHeartbeatTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.client.*; import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.server.*; import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.server.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerServerRouteTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerTransferTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferCloseTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.AbstractTcpHandleChannelHeartbeatTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
/** /**
@ -164,10 +162,65 @@ public enum TcpMessageTypeEnums {
*/ */
HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_, "http远程服务端代理传输数据下发"), HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_REPORT_, "http远程服务端代理传输数据下发"),
/** /**
* 下发服务端路由 * http 下发服务端路由
* @see AbstractHandleHttpDistributeClientProxyServerServerRouteTypeAdvanced * @see AbstractHandleHttpDistributeClientProxyServerServerRouteTypeAdvanced
*/ */
HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_, "http下发服务端路由"), HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_SERVER_ROUTE_DISTRIBUTE_, "http下发服务端路由"),
/**
* http 下发客户端路由
*
* @see AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced
*/
HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_, "http下发客户端路由"),
/**
* http 客户端远程客户端代理传输连接
*
* @see AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced
*/
HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_(TcpMessageType.HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_, "http 客户端远程客户端代理传输连接"),
/**
* http 客户端代理客户端客户端连接服务端成功
*
* @see AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced
*/
HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_, "http 客户端代理客户端,客户端连接服务端成功"),
/**
* http 目标客户端传输通道连接初始化成功
*
* @see AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced
*/
HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_, "http 目标客户端传输通道连接初始化成功"),
/**
* http 下发数据到另一个客户端
*
* @see AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced
*/
HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_, "http 下发数据到另一个客户端"),
/**
* http 另一个客户端将返回数据通过传输通道返回
*
* @see AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced
*/
HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_, "http 另一个客户端将返回数据通过传输通道返回"),
/**
* 下发返回结果
*
* @see AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced
*/
HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_, "http 下发返回结果"),
/**
* 上报传输通道关闭
*
* @see AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced
*/
HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_, "http 上报传输通道关闭"),
/**
* 下发传输通道关闭
* @see AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced
*/
HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_, "http 下发传输通道关闭"),

View File

@ -2,6 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.common.utils;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
/** /**
* 通道属性绑定工具 * 通道属性绑定工具
@ -19,6 +20,7 @@ public class ChannelAttributeKeyUtils {
private static final AttributeKey<Channel> NEXT_CHANNEL = AttributeKey.newInstance("nextChannel"); private static final AttributeKey<Channel> NEXT_CHANNEL = AttributeKey.newInstance("nextChannel");
private static final AttributeKey<Channel> TRANSFER_NEXT_CHANNEL = AttributeKey.newInstance("transferNextChannel"); private static final AttributeKey<Channel> TRANSFER_NEXT_CHANNEL = AttributeKey.newInstance("transferNextChannel");
private static final AttributeKey<NettyByteBuf> NETTY_BYTE_BUF_DATA = AttributeKey.newInstance("nettyByteBufData");
/** /**
@ -177,6 +179,25 @@ public class ChannelAttributeKeyUtils {
public static Channel getTransferNextChannel(Channel channel) { public static Channel getTransferNextChannel(Channel channel) {
return channel.attr(TRANSFER_NEXT_CHANNEL).get(); return channel.attr(TRANSFER_NEXT_CHANNEL).get();
} }
/**
* 为通道绑定 传输数据
*
* @param channel 通道
* @param nettyByteBuf 传输数据
*/
public static void buildNettyByteBufData(Channel channel, NettyByteBuf nettyByteBuf) {
channel.attr(NETTY_BYTE_BUF_DATA).set(nettyByteBuf);
}
/**
* 获取 通道中传输数据
*
* @param channel 通道
*/
public static NettyByteBuf getNettyByteBufData(Channel channel) {
return channel.attr(NETTY_BYTE_BUF_DATA).get();
}
/** /**

View File

@ -11,6 +11,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; 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.ProxyRoute; import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext; 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.protocol.route.RouteType;
@ -92,6 +93,9 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
if(RouteType.LOCAL.equals(route.getRouteType())){ if(RouteType.LOCAL.equals(route.getRouteType())){
proxyMsg.setType(TcpMessageType.HTTP_LOCAL_PROXY); proxyMsg.setType(TcpMessageType.HTTP_LOCAL_PROXY);
}else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){ }else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
String clientId = clientProxyRoute.getClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_CLIENT_); proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_CLIENT_);
}else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){ }else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){
proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_); proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_);

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.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.tcp.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.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.*;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties; 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.*;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
@ -51,6 +50,7 @@ public class ServerAutoConfiguration {
LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication,
LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication,
LazyNettyServerRouteApplication lazyNettyServerRouteApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication,
LazyNettyClientRouteApplication lazyNettyClientRouteApplication,
LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication,
ServerNodeProperties serverNodeProperties ServerNodeProperties serverNodeProperties
) { ) {
@ -61,6 +61,7 @@ public class ServerAutoConfiguration {
lazyClientPermeateServerMappingApplication, lazyClientPermeateServerMappingApplication,
lazyClientPermeateClientMappingApplication, lazyClientPermeateClientMappingApplication,
lazyNettyServerRouteApplication, lazyNettyServerRouteApplication,
lazyNettyClientRouteApplication,
lazyNettyClientTokenBucketApplication, lazyNettyClientTokenBucketApplication,
serverNodeProperties serverNodeProperties
); );
@ -208,5 +209,20 @@ public class ServerAutoConfiguration {
public ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced serverHandleHttpReportClientProxyServerTransferCloseTypeAdvanced() { public ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced serverHandleHttpReportClientProxyServerTransferCloseTypeAdvanced() {
return new 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.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.server.AbstractHandleTcpClientConnectSuccessTypeAdvanced; 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.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.RouteType;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ServerProxyRoute; 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.properties.ServerNodeProperties;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*; 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.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.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.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.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.LazyClientPermeateClientMappingDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyClientPermeateServerMappingDTO; 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.application.dto.LazyNettyServerRouteDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.blacklist.LazyNettyClientBlacklist; import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.blacklist.LazyNettyClientBlacklist;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
@ -46,17 +49,19 @@ public class ServerHandleTcpClientConnectSuccessTypeAdvanced extends AbstractHan
private final LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication; private final LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication;
private final LazyNettyServerRouteApplication lazyNettyServerRouteApplication; private final LazyNettyServerRouteApplication lazyNettyServerRouteApplication;
private final LazyNettyClientRouteApplication lazyNettyClientRouteApplication;
private final LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication; private final LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication;
private final ServerNodeProperties serverNodeProperties; 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.lazyClientStatsChangeApplication = lazyClientStatsChangeApplication;
this.lazyNettyClientBlacklistApplication = lazyNettyClientBlacklistApplication; this.lazyNettyClientBlacklistApplication = lazyNettyClientBlacklistApplication;
this.lazyServerPermeateClientMappingApplication = lazyServerPermeateClientMappingApplication; this.lazyServerPermeateClientMappingApplication = lazyServerPermeateClientMappingApplication;
this.lazyClientPermeateServerMappingApplication = lazyClientPermeateServerMappingApplication; this.lazyClientPermeateServerMappingApplication = lazyClientPermeateServerMappingApplication;
this.lazyClientPermeateClientMappingApplication = lazyClientPermeateClientMappingApplication; this.lazyClientPermeateClientMappingApplication = lazyClientPermeateClientMappingApplication;
this.lazyNettyServerRouteApplication = lazyNettyServerRouteApplication; this.lazyNettyServerRouteApplication = lazyNettyServerRouteApplication;
this.lazyNettyClientRouteApplication = lazyNettyClientRouteApplication;
this.lazyNettyClientTokenBucketApplication = lazyNettyClientTokenBucketApplication; this.lazyNettyClientTokenBucketApplication = lazyNettyClientTokenBucketApplication;
this.serverNodeProperties = serverNodeProperties; 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) {
// 关闭通道
// 关闭 nextnext 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; 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.entity.LazyNettyClientRouteDO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.converter.LazyNettyClientRouteConverter; 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.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.route.LazyNettyClientRouteRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyUpdateSetValueWrappers; 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) { public Result<LazyNettyClientRoute> story(LazyNettyClientRoute lazyNettyClientRoute) {
LazyNettyClientRouteDO lazyNettyClientRouteDO = LazyNettyClientRouteConverter.INSTANCE.fromLazyNettyClientRoute(lazyNettyClientRoute); LazyNettyClientRouteDO lazyNettyClientRouteDO = LazyNettyClientRouteConverter.INSTANCE.fromLazyNettyClientRoute(lazyNettyClientRoute);
lazyLambdaStream.upsert(lazyNettyClientRouteDO); 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(); return ResultFactory.successOf();
} }