diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/config/ClientAutoConfiguration.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/config/ClientAutoConfiguration.java index 1bf20e6..0af6cbe 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/config/ClientAutoConfiguration.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/config/ClientAutoConfiguration.java @@ -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.tcp.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.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.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced.*; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -354,5 +351,28 @@ public class ClientAutoConfiguration { public ClientHandleDistributeHttpClientProxyServerServerRouteTypeAdvanced 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(); + } } } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/NettyHttpClientProxyClient.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/NettyHttpClientProxyClient.java new file mode 100644 index 0000000..eb03045 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/NettyHttpClientProxyClient.java @@ -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 handleChannelTypeAdvancedList; + + + /** + * 发送数据 + */ + private byte[] data; + /** + * 是否是ssl + */ + private boolean isSsl; + +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced.java new file mode 100644 index 0000000..7a3ea82 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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 handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values()); + NettyHttpClientProxyClientRealSocket.buildRealServer( + clientId, + clientTargetIp, + clientTargetPort, + visitorId, + nettyClientProperties, + handleChannelTypeAdvancedList + ); + + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced.java new file mode 100644 index 0000000..1467c53 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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); + } + + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced.java new file mode 100644 index 0000000..eaf2509 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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); + + + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyClientTypeAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyClientTypeAdvanced.java index a42c090..df455d7 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyClientTypeAdvanced.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyClientTypeAdvanced.java @@ -1,22 +1,16 @@ 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.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; +import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties; +import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyClient; +import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientProxySocket; 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.decoder.TransferDecoder; -import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder; -import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; -import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettyHttpProxyBackendHandler; -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; +import org.wu.framework.spring.utils.SpringContextHolder; + +import java.util.ArrayList; +import java.util.List; /** * http 远程代理 @@ -35,42 +29,23 @@ public class ClientHandleHttpClientProxyClientTypeAdvanced extends */ @Override protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) { - String targetPortString = nettyProxyMsg.getTargetPortString(); String targetIpString = nettyProxyMsg.getTargetIpString(); + byte[] data = nettyProxyMsg.getData(); + byte[] clientId = nettyProxyMsg.getClientId(); + List handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values()); + NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class); // 判断代理到客户端还是服务端 - ProxyRoute route = RouteContext.getRoute(targetIpString, targetPortString); - if(RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){ - // 服务端代理 + NettyHttpClientProxyClient nettyHttpClientProxyClient = new NettyHttpClientProxyClient(); + 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())){ - // 客户端代理 - } - Bootstrap b = new Bootstrap(); - b.group(channel.eventLoop()) - .channel(NioSocketChannel.class) - .handler(new ChannelInitializer() { - @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(); - } - }); + nettyHttpClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList); + nettyHttpClientProxyClient.setNettyClientProperties(nettyClientProperties); + nettyHttpClientProxyClient.setData(data); + + NettyHttpClientProxyClientProxySocket.buildTransferServer(nettyHttpClientProxyClient, channel); } } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyServerTypeAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyServerTypeAdvanced.java index e9f71ea..2ced95b 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyServerTypeAdvanced.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpClientProxyServerTypeAdvanced.java @@ -3,7 +3,7 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advan import io.netty.channel.Channel; 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.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.advanced.HandleChannelTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyServerTypeAdvanced; @@ -45,7 +45,7 @@ public class ClientHandleHttpClientProxyServerTypeAdvanced extends nettyHttpClientProxyServer.setNettyClientProperties(nettyClientProperties); nettyHttpClientProxyServer.setData(data); - NettyHttpClientProxyServerTransferSocket.buildTransferServer(nettyHttpClientProxyServer, channel); + NettyHttpClientProxyServerProxySocket.buildTransferServer(nettyHttpClientProxyServer, channel); } } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java new file mode 100644 index 0000000..ada1ad0 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @param channel 当前通道 + * @param nettyProxyMsg 通道数据 + */ + @Override + protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) { + + // 关闭传输通道 + Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel); + realChannel.close();// 真实通道关闭 + channel.close(); // 数据传输通道关闭 + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java new file mode 100644 index 0000000..dc20568 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/advanced/ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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); + } + // 关闭连接 + + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerTransferFilter.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientProxyFilter.java similarity index 80% rename from wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerTransferFilter.java rename to wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientProxyFilter.java index 88ecfbf..c7346cd 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerTransferFilter.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientProxyFilter.java @@ -5,8 +5,7 @@ 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.NettyTcpClientPermeateClientTransferHandler; -import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyServerTransferHandler; +import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientProxyHandler; 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; @@ -15,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInit /** * netty 客户端代理服务端数据传输通道 */ -public class NettyHttpClientProxyServerTransferFilter extends DebugChannelInitializer { +public class NettyHttpClientProxyClientProxyFilter extends DebugChannelInitializer { private final ChannelTypeAdapter channelTypeAdapter; - public NettyHttpClientProxyServerTransferFilter(ChannelTypeAdapter channelTypeAdapter) { + public NettyHttpClientProxyClientProxyFilter(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 NettyProxyMsgEncoder()); - pipeline.addLast(new NettyHttpClientProxyServerTransferHandler(channelTypeAdapter)); + pipeline.addLast(new NettyHttpClientProxyClientProxyHandler(channelTypeAdapter)); } } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientRealFilter.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientRealFilter.java new file mode 100644 index 0000000..ab7b3a1 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientRealFilter.java @@ -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 { + /** + * 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()); + + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientTransferRealFilter.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientTransferRealFilter.java new file mode 100644 index 0000000..1eef05d --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyClientTransferRealFilter.java @@ -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 { + 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)); + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerProxyFilter.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerProxyFilter.java new file mode 100644 index 0000000..a6f5122 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/filter/NettyHttpClientProxyServerProxyFilter.java @@ -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 { + 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)); + } +} diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientProxyHandler.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientProxyHandler.java new file mode 100644 index 0000000..ce59057 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientProxyHandler.java @@ -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 { + 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); + } +} \ No newline at end of file diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientRealHandler.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientRealHandler.java new file mode 100644 index 0000000..02a512b --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientRealHandler.java @@ -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 { + + + @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); + } +} \ No newline at end of file diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientTransferRealHandler.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientTransferRealHandler.java new file mode 100644 index 0000000..b261ffe --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyClientTransferRealHandler.java @@ -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 { + 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); + } +} \ No newline at end of file diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerTransferHandler.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerProxyHandler.java similarity index 92% rename from wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerTransferHandler.java rename to wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerProxyHandler.java index 2996510..52da863 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerTransferHandler.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/handler/NettyHttpClientProxyServerProxyHandler.java @@ -14,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK * 客户端访客通信通道 处理器 */ @Slf4j -public class NettyHttpClientProxyServerTransferHandler extends SimpleChannelInboundHandler { +public class NettyHttpClientProxyServerProxyHandler extends SimpleChannelInboundHandler { private final ChannelTypeAdapter channelTypeAdapter; - public NettyHttpClientProxyServerTransferHandler(ChannelTypeAdapter channelTypeAdapter) { + public NettyHttpClientProxyServerProxyHandler(ChannelTypeAdapter channelTypeAdapter) { this.channelTypeAdapter = channelTypeAdapter; } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientProxySocket.java similarity index 70% rename from wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java rename to wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientProxySocket.java index bcb3272..76ec551 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientProxySocket.java @@ -7,8 +7,8 @@ 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.NettyClientPermeateClientVisitor; -import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientTransferFilter; +import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.NettyHttpClientProxyClient; +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.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; @@ -20,14 +20,13 @@ import java.util.concurrent.TimeUnit; * 客户端代理客户端传输通道 */ @Slf4j -public class NettyHttpClientProxyClientTransferSocket { +public class NettyHttpClientProxyClientProxySocket { 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.group(eventLoopGroup) @@ -43,24 +42,25 @@ public class NettyHttpClientProxyClientTransferSocket { // .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 NettyTcpClientPermeateClientTransferFilter(new ChannelTypeAdapter(nettyClientPermeateClientVisitor.getHandleChannelTypeAdvancedList()))) + .handler(new NettyHttpClientProxyClientProxyFilter(new ChannelTypeAdapter(nettyHttpClientProxyClient.getHandleChannelTypeAdvancedList()))) ; - NettyClientProperties nettyClientProperties = nettyClientPermeateClientVisitor.getNettyClientProperties(); + NettyClientProperties nettyClientProperties = nettyHttpClientProxyClient.getNettyClientProperties(); String inetHost = nettyClientProperties.getInetHost(); int inetPort = nettyClientProperties.getInetPort(); // local client id String clientId = nettyClientProperties.getClientId(); - String targetIp = nettyClientPermeateClientVisitor.getTargetIp(); - Integer targetPort = nettyClientPermeateClientVisitor.getTargetPort(); - + String targetIp = nettyHttpClientProxyClient.getTargetIp(); + Integer targetPort = nettyHttpClientProxyClient.getTargetPort(); + String targetClientId = nettyHttpClientProxyClient.getTargetClientId(); String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel); - Integer visitorPort = nettyClientPermeateClientVisitor.getVisitorPort(); - String toClientId = nettyClientPermeateClientVisitor.getToClientId(); + byte[] data = nettyHttpClientProxyClient.getData(); - // 客户端新建访客通道 连接服务端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); // 使用的客户端ID:{} @@ -69,12 +69,12 @@ public class NettyHttpClientProxyClientTransferSocket { if (futureListener.isSuccess()) { 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 - nettyProxyMsg.setClientId(toClientId); - nettyProxyMsg.setVisitorPort(visitorPort); + nettyProxyMsg.setClientId(targetClientId); nettyProxyMsg.setClientTargetIp(targetIp); nettyProxyMsg.setClientTargetPort(targetPort); + nettyProxyMsg.setData(data); nettyProxyMsg.setVisitorId(visitorId); transferChannel.writeAndFlush(nettyProxyMsg); @@ -90,7 +90,7 @@ public class NettyHttpClientProxyClientTransferSocket { log.info("无法连接到服务端...."); eventLoopGroup.schedule(() -> { try { - buildTransferServer(nettyClientPermeateClientVisitor, visitorChannel); + buildTransferServer(nettyHttpClientProxyClient, visitorChannel); } catch (Exception e) { e.printStackTrace(); } diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientRealSocket.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientRealSocket.java new file mode 100644 index 0000000..87f9c9a --- /dev/null +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyClientRealSocket.java @@ -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 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 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); + } + }); + } + +} \ No newline at end of file diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerTransferSocket.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerProxySocket.java similarity index 94% rename from wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerTransferSocket.java rename to wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerProxySocket.java index b864ee5..1d6f5a9 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerTransferSocket.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/netty/proxy/http/socket/NettyHttpClientProxyServerProxySocket.java @@ -8,7 +8,7 @@ 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.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.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; @@ -20,7 +20,7 @@ import java.util.concurrent.TimeUnit; * 客户端代理客户端传输通道 */ @Slf4j -public class NettyHttpClientProxyServerTransferSocket { +public class NettyHttpClientProxyServerProxySocket { 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.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(); String inetHost = nettyClientProperties.getInetHost(); diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced.java new file mode 100644 index 0000000..04885b4 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced.java @@ -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 + */ +public abstract class AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java new file mode 100644 index 0000000..fbb494a --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced.java @@ -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 extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_.getTypeByte() == nettyProxyMsg.getType(); + } + +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced.java new file mode 100644 index 0000000..cd06a23 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced.java @@ -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 extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java new file mode 100644 index 0000000..24102c3 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced.java @@ -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 extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced.java new file mode 100644 index 0000000..08258a8 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/client/AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced.java @@ -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 + */ +public abstract class AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced.java new file mode 100644 index 0000000..2aca3a8 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced.java @@ -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 + */ +public abstract class AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_REPORT_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java new file mode 100644 index 0000000..cdb88a2 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java @@ -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 + */ +public abstract class AbstractHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced extends AbstractHandleChannelTypeAdvanced 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(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java new file mode 100644 index 0000000..fac14a0 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java @@ -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 extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java new file mode 100644 index 0000000..8dda742 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/advanced/proxy/http/server/AbstractHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java @@ -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 extends AbstractHandleChannelTypeAdvanced implements HandleChannelTypeAdvanced { + + + /** + * 是否支持当前类型 + * + * @param nettyProxyMsg 通道数据 + * @return 布尔类型 是、否 + */ + @Override + protected boolean doSupport(NettyProxyMsg nettyProxyMsg) { + return TcpMessageTypeEnums.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_.getTypeByte() == nettyProxyMsg.getType(); + } +} diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/constant/TcpMessageType.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/constant/TcpMessageType.java index fa25f6d..c13edc0 100644 --- a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/constant/TcpMessageType.java +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/constant/TcpMessageType.java @@ -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.AbstractHttpClientProxyServerTypeAdvanced; 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.AbstractHandleHttpDistributeClientProxyServerTransferTypeAdvanced; -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.advanced.proxy.http.client.*; +import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*; 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; + /** + * 下发客户端路由 + * + * @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; /** diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/enums/TcpMessageTypeEnums.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/enums/TcpMessageTypeEnums.java index 2c207e2..19f1c07 100644 --- a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/enums/TcpMessageTypeEnums.java +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/enums/TcpMessageTypeEnums.java @@ -2,16 +2,14 @@ package org.framework.lazy.cloud.network.heartbeat.common.enums; import lombok.AllArgsConstructor; 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.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.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.client.AbstractHandleHttpDistributeClientProxyServerTransferTypeAdvanced; -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.advanced.permeate.tcp.AbstractTcpHandleChannelHeartbeatTypeAdvanced; +import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced; +import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.*; +import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.*; 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 下发服务端路由 * @see AbstractHandleHttpDistributeClientProxyServerServerRouteTypeAdvanced */ 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 下发传输通道关闭"), diff --git a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/utils/ChannelAttributeKeyUtils.java b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/utils/ChannelAttributeKeyUtils.java index da743ad..9a09bd5 100644 --- a/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/utils/ChannelAttributeKeyUtils.java +++ b/wu-lazy-cloud-heartbeat-common/src/main/java/org/framework/lazy/cloud/network/heartbeat/common/utils/ChannelAttributeKeyUtils.java @@ -2,6 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.common.utils; import io.netty.channel.Channel; 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 NEXT_CHANNEL = AttributeKey.newInstance("nextChannel"); private static final AttributeKey TRANSFER_NEXT_CHANNEL = AttributeKey.newInstance("transferNextChannel"); + private static final AttributeKey NETTY_BYTE_BUF_DATA = AttributeKey.newInstance("nettyByteBufData"); /** @@ -177,6 +179,25 @@ public class ChannelAttributeKeyUtils { public static Channel getTransferNextChannel(Channel channel) { 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(); + } /** diff --git a/wu-lazy-cloud-heartbeat-protocol-proxy/src/main/java/org/framework/lazy/cloud/network/heartbeat/protocol/handler/NettyHttpProxyHandler.java b/wu-lazy-cloud-heartbeat-protocol-proxy/src/main/java/org/framework/lazy/cloud/network/heartbeat/protocol/handler/NettyHttpProxyHandler.java index 79d46a3..7153922 100644 --- a/wu-lazy-cloud-heartbeat-protocol-proxy/src/main/java/org/framework/lazy/cloud/network/heartbeat/protocol/handler/NettyHttpProxyHandler.java +++ b/wu-lazy-cloud-heartbeat-protocol-proxy/src/main/java/org/framework/lazy/cloud/network/heartbeat/protocol/handler/NettyHttpProxyHandler.java @@ -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.constant.TcpMessageType; 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.RouteContext; 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())){ proxyMsg.setType(TcpMessageType.HTTP_LOCAL_PROXY); }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_); }else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){ proxyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_); diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/config/ServerAutoConfiguration.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/config/ServerAutoConfiguration.java index d37a4d1..c51e1aa 100644 --- a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/config/ServerAutoConfiguration.java +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/config/ServerAutoConfiguration.java @@ -4,8 +4,7 @@ package org.framework.lazy.cloud.network.heartbeat.server.config; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter; import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.tcp.advanced.*; import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.advanced.*; -import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced; -import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced; +import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced.*; import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*; import org.springframework.beans.factory.config.BeanDefinition; @@ -51,6 +50,7 @@ public class ServerAutoConfiguration { LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication, + LazyNettyClientRouteApplication lazyNettyClientRouteApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, ServerNodeProperties serverNodeProperties ) { @@ -61,6 +61,7 @@ public class ServerAutoConfiguration { lazyClientPermeateServerMappingApplication, lazyClientPermeateClientMappingApplication, lazyNettyServerRouteApplication, + lazyNettyClientRouteApplication, lazyNettyClientTokenBucketApplication, serverNodeProperties ); @@ -208,5 +209,20 @@ public class ServerAutoConfiguration { public ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced serverHandleHttpReportClientProxyServerTransferCloseTypeAdvanced() { return new ServerHandleHttpReportClientProxyServerTransferCloseTypeAdvanced(); } + + @Bean + public ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced serverHandleHttpReportClientProxyClientConnectTransferTypeAdvanced() { + return new ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced(); + } + + @Bean + public ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced serverHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced() { + return new ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced(); + } + + @Bean + public ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced serverHandleHttpReportClientProxyClientTransferResponseTypeAdvanced() { + return new ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced(); + } } } diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/permeate/tcp/advanced/ServerHandleTcpClientConnectSuccessTypeAdvanced.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/permeate/tcp/advanced/ServerHandleTcpClientConnectSuccessTypeAdvanced.java index b53fb1f..95de1c6 100644 --- a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/permeate/tcp/advanced/ServerHandleTcpClientConnectSuccessTypeAdvanced.java +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/permeate/tcp/advanced/ServerHandleTcpClientConnectSuccessTypeAdvanced.java @@ -9,16 +9,19 @@ import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.server.AbstractHandleTcpClientConnectSuccessTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; +import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute; import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType; import org.framework.lazy.cloud.network.heartbeat.protocol.route.ServerProxyRoute; import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.*; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.client.mapping.LazyClientPermeateClientMappingQueryListCommand; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyClientPermeateServerMappingQueryListCommand; +import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.route.LazyNettyClientRouteQueryListCommand; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.state.LazyNettyClientLoginCommand; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.server.route.LazyNettyServerRouteQueryListCommand; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyClientPermeateClientMappingDTO; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyClientPermeateServerMappingDTO; +import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyClientRouteDTO; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyServerRouteDTO; import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.blacklist.LazyNettyClientBlacklist; import org.springframework.beans.factory.config.BeanDefinition; @@ -46,17 +49,19 @@ public class ServerHandleTcpClientConnectSuccessTypeAdvanced extends AbstractHan private final LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication; private final LazyNettyServerRouteApplication lazyNettyServerRouteApplication; + private final LazyNettyClientRouteApplication lazyNettyClientRouteApplication; private final LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication; private final ServerNodeProperties serverNodeProperties; - public ServerHandleTcpClientConnectSuccessTypeAdvanced(LazyClientStatsChangeApplication lazyClientStatsChangeApplication, LazyNettyClientBlacklistApplication lazyNettyClientBlacklistApplication, LazyServerPermeateClientMappingApplication lazyServerPermeateClientMappingApplication, LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, ServerNodeProperties serverNodeProperties) { + public ServerHandleTcpClientConnectSuccessTypeAdvanced(LazyClientStatsChangeApplication lazyClientStatsChangeApplication, LazyNettyClientBlacklistApplication lazyNettyClientBlacklistApplication, LazyServerPermeateClientMappingApplication lazyServerPermeateClientMappingApplication, LazyClientPermeateServerMappingApplication lazyClientPermeateServerMappingApplication, LazyClientPermeateClientMappingApplication lazyClientPermeateClientMappingApplication, LazyNettyServerRouteApplication lazyNettyServerRouteApplication, LazyNettyClientRouteApplication lazyNettyClientRouteApplication, LazyNettyClientTokenBucketApplication lazyNettyClientTokenBucketApplication, ServerNodeProperties serverNodeProperties) { this.lazyClientStatsChangeApplication = lazyClientStatsChangeApplication; this.lazyNettyClientBlacklistApplication = lazyNettyClientBlacklistApplication; this.lazyServerPermeateClientMappingApplication = lazyServerPermeateClientMappingApplication; this.lazyClientPermeateServerMappingApplication = lazyClientPermeateServerMappingApplication; this.lazyClientPermeateClientMappingApplication = lazyClientPermeateClientMappingApplication; this.lazyNettyServerRouteApplication = lazyNettyServerRouteApplication; + this.lazyNettyClientRouteApplication = lazyNettyClientRouteApplication; this.lazyNettyClientTokenBucketApplication = lazyNettyClientTokenBucketApplication; this.serverNodeProperties = serverNodeProperties; } @@ -203,6 +208,29 @@ public class ServerHandleTcpClientConnectSuccessTypeAdvanced extends AbstractHan } }); + + // 下发客户端路由 + LazyNettyClientRouteQueryListCommand lazyNettyClientRouteQueryListCommand = new LazyNettyClientRouteQueryListCommand(); + lazyNettyClientRouteQueryListCommand.setIsDeleted(false); + lazyNettyClientRouteApplication.findList(lazyNettyClientRouteQueryListCommand) + .accept(lazyNettyClientRouteDTOList -> { + for (LazyNettyClientRouteDTO lazyNettyClientRouteDTO : lazyNettyClientRouteDTOList) { + String routeIp = lazyNettyClientRouteDTO.getRouteIp(); + String routePort = lazyNettyClientRouteDTO.getRoutePort(); + String routeClientId = lazyNettyClientRouteDTO.getClientId(); + ClientProxyRoute clientProxyRoute = new ClientProxyRoute(); + clientProxyRoute.setClientId(routeClientId); + clientProxyRoute.setAllowIp(routeIp); + clientProxyRoute.setAllowPort(routePort); + clientProxyRoute.setRouteType(RouteType.CLIENT_PROXY_CLIENT); + NettyProxyMsg nettyMsg = new NettyProxyMsg(); + nettyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_); + nettyMsg.setData((JSON.toJSONString(clientProxyRoute) + .getBytes(StandardCharsets.UTF_8))); + newChannel.writeAndFlush(nettyMsg); + } + }); + } } diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/NettyHttpClientProxyClientTransfer.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/NettyHttpClientProxyClientTransfer.java new file mode 100644 index 0000000..6083399 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/NettyHttpClientProxyClientTransfer.java @@ -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 handleChannelTypeAdvancedList; + + + /** + * 发送数据 + */ + private byte[] data; + /** + * 是否是ssl + */ + private boolean isSsl; + +} diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced.java new file mode 100644 index 0000000..b355db1 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientConnectTransferTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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); + } + + } + +} diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java new file mode 100644 index 0000000..dbfb323 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferChannelInitSuccessfulTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @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); + + + } + +} diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java new file mode 100644 index 0000000..9737e32 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @param transferChannel 当前通道 + * @param nettyProxyMsg 通道数据 + */ + @Override + public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) { + + // 关闭通道 + + // 关闭 next、next transfer 通道 + + Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel); + Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel); + + // 下发关闭客户端真实通道 + NettyProxyMsg closeTransferNettyProxyMsg = new NettyProxyMsg(); + closeTransferNettyProxyMsg.setType(TcpMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_); + transferNextChannel.writeAndFlush(closeTransferNettyProxyMsg); + + channel.close(); + nextChannel.close(); + transferNextChannel.close(); + } + +} diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java new file mode 100644 index 0000000..408c38b --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/advanced/ServerHandleHttpReportClientProxyClientTransferResponseTypeAdvanced.java @@ -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 { + + + /** + * 处理当前数据 + * + * @param transferChannel 当前通道 + * @param nettyProxyMsg 通道数据 + */ + @Override + public void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) { + // 将返回数据下发客户端 + Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(transferChannel); + + if(ObjectUtils.isNotEmpty(nettyProxyMsg)) { + + transferNextChannel.writeAndFlush(nettyProxyMsg); + } + + } + +} diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java new file mode 100644 index 0000000..f3cbcf8 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/netty/proxy/http/socket/NettyHttpClientProxyClientTransferSocket.java @@ -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(); + } + } + + +} \ No newline at end of file diff --git a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/standalone/infrastructure/persistence/LazyNettyClientRouteRepositoryImpl.java b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/standalone/infrastructure/persistence/LazyNettyClientRouteRepositoryImpl.java index 2fbc215..6863d0b 100644 --- a/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/standalone/infrastructure/persistence/LazyNettyClientRouteRepositoryImpl.java +++ b/wu-lazy-cloud-heartbeat-server/src/main/java/org/framework/lazy/cloud/network/heartbeat/server/standalone/infrastructure/persistence/LazyNettyClientRouteRepositoryImpl.java @@ -1,10 +1,19 @@ package org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.persistence; +import com.alibaba.fastjson.JSON; +import io.netty.channel.Channel; +import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext; +import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; +import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType; +import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute; +import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext; +import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType; import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.entity.LazyNettyClientRouteDO; import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.converter.LazyNettyClientRouteConverter; -import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.mapper.LazyNettyClientRouteMapper; import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.route.LazyNettyClientRouteRepository; import org.springframework.stereotype.Repository; + +import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyUpdateSetValueWrappers; @@ -43,6 +52,27 @@ public class LazyNettyClientRouteRepositoryImpl implements LazyNettyClientRout public Result story(LazyNettyClientRoute lazyNettyClientRoute) { LazyNettyClientRouteDO lazyNettyClientRouteDO = LazyNettyClientRouteConverter.INSTANCE.fromLazyNettyClientRoute(lazyNettyClientRoute); lazyLambdaStream.upsert(lazyNettyClientRouteDO); + // 更新服务端路由 + String routeIp = lazyNettyClientRoute.getRouteIp(); + String routePort = lazyNettyClientRoute.getRoutePort(); + String clientId = lazyNettyClientRoute.getClientId(); + ClientProxyRoute clientProxyRoute = new ClientProxyRoute(); + clientProxyRoute.setClientId(clientId); + clientProxyRoute.setAllowIp(routeIp); + clientProxyRoute.setAllowPort(routePort); + clientProxyRoute.setRouteType(RouteType.CLIENT_PROXY_CLIENT); + RouteContext.setRoute(clientProxyRoute); + // 发送所有客户端本地路由刷新 + ChannelContext.getChannels().forEach((channelClientId, channels) -> { + NettyProxyMsg nettyMsg = new NettyProxyMsg(); + nettyMsg.setType(TcpMessageType.HTTP_CLIENT_PROXY_SERVER_CLIENT_ROUTE_DISTRIBUTE_); + nettyMsg.setData((JSON.toJSONString(clientProxyRoute) + .getBytes(StandardCharsets.UTF_8))); + // 发送所有客户端ID + for (Channel channel : channels) { + channel.writeAndFlush(nettyMsg); + } + }); return ResultFactory.successOf(); }