mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 18:35:05 +08:00
【fix】 添加客户端代理客户端
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端代理服务端配置信息
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public class NettyHttpClientProxyClient {
|
||||
|
||||
/**
|
||||
* 目标地址
|
||||
*/
|
||||
private String targetIp;
|
||||
|
||||
/**
|
||||
* 目标端口
|
||||
*/
|
||||
private Integer targetPort;
|
||||
|
||||
/**
|
||||
* 目标客户端ID
|
||||
*/
|
||||
private String targetClientId;
|
||||
|
||||
/**
|
||||
* 服务端地址信息
|
||||
*/
|
||||
private NettyClientProperties nettyClientProperties;
|
||||
|
||||
/**
|
||||
* 通道处理器
|
||||
*/
|
||||
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
|
||||
|
||||
|
||||
/**
|
||||
* 发送数据
|
||||
*/
|
||||
private byte[] data;
|
||||
/**
|
||||
* 是否是ssl
|
||||
*/
|
||||
private boolean isSsl;
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* http 远程代理
|
||||
* 客户端:客户端远程到服务端、客户端远程到客户端
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleDistributeHttpClientProxyClientConnectionTransferSuccessfulAdvanced extends
|
||||
AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
// 客户端主动连接服务端创建一个传输通道
|
||||
|
||||
byte[] msgClientId = nettyProxyMsg.getClientId();
|
||||
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
|
||||
|
||||
String clientId = new String(msgClientId);
|
||||
String clientTargetIp = new String(msgClientTargetIp);
|
||||
Integer clientTargetPort = Integer.parseInt(new String(msgClientTargetPort));
|
||||
String visitorId = new String(msgVisitorId);
|
||||
|
||||
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
|
||||
NettyHttpClientProxyClientRealSocket.buildRealServer(
|
||||
clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId,
|
||||
nettyClientProperties,
|
||||
handleChannelTypeAdvancedList
|
||||
);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientConnectionTransferSuccessfulTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* http 远程代理
|
||||
* 客户端:客户端远程到服务端、客户端远程到客户端
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleDistributeHttpClientProxyClientTransferRequestAdvanced extends
|
||||
AbstractHandleHttpDistributeClientProxyClientTransferRequestTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
// 数据发送
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if(ObjectUtils.isNotEmpty(nextChannel)){
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
nextChannel.writeAndFlush(buf);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
|
||||
|
||||
/**
|
||||
* http 远程代理
|
||||
* 客户端:客户端远程到服务端、客户端远程到客户端
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleDistributeHttpClientProxyServerClientRouteTypeAdvanced extends
|
||||
AbstractHandleHttpDistributeClientProxyServerClientRouteTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
byte[] data = nettyProxyMsg.getData();
|
||||
ClientProxyRoute clientProxyRoute = JSON.parseObject(data, ClientProxyRoute.class);
|
||||
RouteContext.setRoute(clientProxyRoute);
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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<HandleChannelTypeAdvanced> 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<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline().addLast(
|
||||
new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024 * 10),
|
||||
new TransferEncoder(),
|
||||
new NettyHttpProxyBackendHandler()
|
||||
);
|
||||
}
|
||||
});
|
||||
ChannelFuture f = b.connect(targetIpString, Integer.parseInt(targetPortString));
|
||||
Channel proxyChannel = f.channel();
|
||||
f.addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
ChannelAttributeKeyUtils.buildNextChannel(proxyChannel, channel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(channel, proxyChannel);
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
proxyChannel.writeAndFlush(buf);
|
||||
} else {
|
||||
channel.close();
|
||||
}
|
||||
});
|
||||
nettyHttpClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
|
||||
nettyHttpClientProxyClient.setNettyClientProperties(nettyClientProperties);
|
||||
nettyHttpClientProxyClient.setData(data);
|
||||
|
||||
NettyHttpClientProxyClientProxySocket.buildTransferServer(nettyHttpClientProxyClient, channel);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpClientProxyClientRealSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* http 远程代理
|
||||
* 客户端:客户端远程到服务端、客户端远程到客户端
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced extends
|
||||
AbstractHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
// 关闭传输通道
|
||||
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
realChannel.close();// 真实通道关闭
|
||||
channel.close(); // 数据传输通道关闭
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
/**
|
||||
* http 远程代理
|
||||
* 客户端:客户端远程到服务端、客户端远程到客户端
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced extends
|
||||
AbstractHandleHttpDistributeClientProxyClientTransferResponseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
// 数据发送
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if(ObjectUtils.isNotEmpty(nextChannel)){
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
nextChannel.writeAndFlush(buf);
|
||||
}
|
||||
// 关闭连接
|
||||
|
||||
}
|
||||
}
|
@ -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<SocketChannel> {
|
||||
public class NettyHttpClientProxyClientProxyFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
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));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
public class NettyHttpClientProxyClientRealFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
/**
|
||||
* This method will be called once the {@link Channel} was registered. After the method returns this instance
|
||||
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
|
||||
*
|
||||
* @param ch the {@link Channel} which was registered.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// 解码、编码
|
||||
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
|
||||
pipeline.addLast(new TransferEncoder());
|
||||
pipeline.addLast(new NettyHttpClientProxyClientRealHandler());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.handler.NettyTcpClientPermeateClientTransferRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientTransferRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
/**
|
||||
* netty 客户端连接真实服服务端访客拦截器
|
||||
*/
|
||||
public class NettyHttpClientProxyClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpClientProxyClientTransferRealFilter(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will be called once the {@link Channel} was registered. After the method returns this instance
|
||||
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
|
||||
*
|
||||
* @param ch the {@link Channel} which was registered.
|
||||
* @throws Exception is thrown if an error occurs. In that case it will be handled by
|
||||
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
|
||||
* the {@link Channel}.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// // 解码、编码
|
||||
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettMsgEncoder());
|
||||
|
||||
pipeline.addLast(new IdleStateHandler(0, 4, 0));
|
||||
|
||||
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
pipeline.addLast(new NettyHttpClientProxyClientTransferRealHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyServerProxyHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
/**
|
||||
* netty 客户端代理服务端数据传输通道
|
||||
*/
|
||||
public class NettyHttpClientProxyServerProxyFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpClientProxyServerProxyFilter(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will be called once the {@link Channel} was registered. After the method returns this instance
|
||||
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
|
||||
*
|
||||
* @param ch the {@link Channel} which was registered.
|
||||
* @throws Exception is thrown if an error occurs. In that case it will be handled by
|
||||
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
|
||||
* the {@link Channel}.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// // 解码、编码
|
||||
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettMsgEncoder());
|
||||
|
||||
pipeline.addLast(new IdleStateHandler(0, 4, 0));
|
||||
|
||||
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
pipeline.addLast(new NettyHttpClientProxyServerProxyHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 客户端访客通信通道 处理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpClientProxyClientProxyHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpClientProxyClientProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
channelTypeAdapter.handler(channel, nettyProxyMsg);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
// 关闭访客
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.debug("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.debug("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 来自客户端 真实服务器返回的数据请求
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpClientProxyClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
|
||||
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
|
||||
|
||||
byte[] bytes = nettyByteBuf.getData();
|
||||
log.debug("bytes.length:{}",bytes.length);
|
||||
log.debug("接收客户端真实服务数据:{}", new String(bytes));
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(ctx.channel());
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
// 访客通信通道 上报服务端代理完成
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
NettyProxyMsg returnMessage = new NettyProxyMsg();
|
||||
returnMessage.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_RESPONSE_);
|
||||
returnMessage.setVisitorId(visitorId);
|
||||
returnMessage.setClientId(clientId);
|
||||
returnMessage.setVisitorPort(visitorPort);
|
||||
returnMessage.setData(bytes);
|
||||
|
||||
nextChannel.writeAndFlush(returnMessage);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
// 客户端真实通信通道
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
// 获取访客的传输通道
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.debug("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.debug("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 客户端访客通信通道 处理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpClientProxyClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpClientProxyClientTransferRealHandler(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
channelTypeAdapter.handler(channel, nettyProxyMsg);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
log.warn("close client permeate client transfer real clientId:{} visitorId:{}", clientId, visitorId);
|
||||
// 关闭访客
|
||||
if (nextChannel != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
// 处理客户端本地真实通道问题
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.debug("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.debug("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -14,10 +14,10 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
|
||||
* 客户端访客通信通道 处理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpClientProxyServerTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
public class NettyHttpClientProxyServerProxyHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpClientProxyServerTransferHandler(ChannelTypeAdapter channelTypeAdapter) {
|
||||
public NettyHttpClientProxyServerProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket;
|
||||
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.filter.NettyTcpClientPermeateClientTransferRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpClientProxyClientTransferRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端连接真实服务
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpClientProxyClientRealSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
|
||||
public static void buildRealServer(String clientId,
|
||||
String clientTargetIp,
|
||||
Integer clientTargetPort,
|
||||
String visitorId,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
|
||||
try {
|
||||
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
|
||||
// 设置读缓冲区为2M
|
||||
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
// .option(ChannelOption.TCP_NODELAY, false)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
|
||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
|
||||
.handler(new NettyHttpClientProxyClientRealFilter())
|
||||
|
||||
;
|
||||
|
||||
|
||||
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
|
||||
Channel realChannel = future.channel();
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, false);
|
||||
|
||||
log.info("访客通过 客户端:【{}】,visitorId:{},绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, visitorId, clientTargetIp, clientTargetPort);
|
||||
// 客户端真实通道
|
||||
NettyRealIdContext.pushReal(realChannel, visitorId);
|
||||
// 绑定访客ID到当前真实通道属性
|
||||
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
|
||||
|
||||
// 连接服务端 然后绑定通道
|
||||
// 新建一个通道处理
|
||||
newVisitorConnect2Server(
|
||||
clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId,
|
||||
realChannel,
|
||||
nettyClientProperties,
|
||||
handleChannelTypeAdvancedList
|
||||
);
|
||||
|
||||
|
||||
} else {
|
||||
log.error("客户:【{}】,无法连接当前网络内的目标IP:【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建访客连接服务端
|
||||
*
|
||||
* @param nettyClientProperties 服务端配置信息
|
||||
* @param handleChannelTypeAdvancedList 处理器适配器
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
protected static void newVisitorConnect2Server(String clientId,
|
||||
String clientTargetIp,
|
||||
Integer clientTargetPort,
|
||||
String visitorId,
|
||||
Channel realChannel,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) throws InterruptedException {
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
// 设置读缓冲区为2M
|
||||
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
// .option(ChannelOption.TCP_NODELAY, false)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
|
||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
|
||||
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
|
||||
|
||||
.handler(new NettyHttpClientProxyClientTransferRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
|
||||
;
|
||||
|
||||
String inetHost = nettyClientProperties.getInetHost();
|
||||
int inetPort = nettyClientProperties.getInetPort();
|
||||
// local client id
|
||||
|
||||
// String clientId = nettyClientProperties.getClientId();
|
||||
|
||||
|
||||
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
|
||||
log.info("client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with visitorId:{} & clientId:{}", inetHost, inetPort, visitorId, clientId);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
// 通知服务端访客连接成功
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
nettyProxyMsg.setVisitorId(visitorId);
|
||||
nettyProxyMsg.setClientId(clientId);
|
||||
nettyProxyMsg.setClientTargetIp(clientTargetIp);
|
||||
nettyProxyMsg.setClientTargetPort(clientTargetPort);
|
||||
nettyProxyMsg.setType(TcpMessageType.HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_);
|
||||
transferChannel.writeAndFlush(nettyProxyMsg);
|
||||
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
|
||||
|
||||
// 绑定客户端真实通信通道
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
|
||||
|
||||
} else {
|
||||
log.info("无法连接到服务端....");
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
newVisitorConnect2Server(clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
Reference in New Issue
Block a user