mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-06 13:27:55 +08:00
[fix] 优化tcp、udp架构
This commit is contained in:
parent
2b3c7cb7c2
commit
0df0a42b2c
@ -7,15 +7,15 @@
|
||||
|
||||
#### 操作步骤:拥有公网ip的服务器开发6001(web)、7001端口(tcp),然后执行命令启动服务端
|
||||
```shell
|
||||
docker run -d -it -p 6001:6001 -p 7001:7001 -e spring.profiles.active=prod -e MAIN_DB_HOST=localhost:3306 -e MAIN_DB_PASSWORD=root -e MAIN_DB_PASSWORD=root --name wu-lazy-cloud-heartbeat-server-start registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it -p 6001:6001 -p 7001:7001 -e spring.profiles.active=prod -e MAIN_DB_HOST=localhost:3306 -e MAIN_DB_PASSWORD=root -e MAIN_DB_PASSWORD=root --name wu-lazy-cloud-heartbeat-server-start registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.3.0-JDK17-SNAPSHOT
|
||||
```
|
||||
|
||||
#### 操作步骤:杭州本地机房所在网络服务器启动客户端、你老家所在网络中启动客户端,命令如下
|
||||
```shell
|
||||
docker run -d -it --privileged --name hangzhou-client --restart=always -e spring.lazy.netty.client.inet-host=公网IP -e spring.lazy.netty.client.inet-port=7001 -e spring.lazy.netty.client.client-id="hangzhou-jifang" registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it --privileged --name hangzhou-client --restart=always -e spring.lazy.netty.client.inet-host=公网IP -e spring.lazy.netty.client.inet-port=7001 -e spring.lazy.netty.client.client-id="hangzhou-jifang" registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT
|
||||
```
|
||||
```shell
|
||||
docker run -d -it --privileged --name my-home-client --restart=always -e spring.lazy.netty.client.inet-host=公网IP -e spring.lazy.netty.client.inet-port=7001 -e spring.lazy.netty.client.client-id="my-home" registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it --privileged --name my-home-client --restart=always -e spring.lazy.netty.client.inet-host=公网IP -e spring.lazy.netty.client.inet-port=7001 -e spring.lazy.netty.client.client-id="my-home" registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT
|
||||
```
|
||||
#### 操作步骤:配置端口
|
||||

|
||||
|
18
README.md
18
README.md
@ -260,21 +260,21 @@ public class NettyClientSocket {
|
||||
|
||||
| 模块 | 版本 | 描述 |
|
||||
|------------------------------------------------------------------------------------------------------------|----------------------|------------------------------|
|
||||
| [wu-lazy-cloud-heartbeat-common](wu-lazy-cloud-heartbeat-common) | 1.2.9-JDK17-SNAPSHOT | 内网穿透公共模块(声明接口、枚举、常量、适配器、解析器) |
|
||||
| [wu-lazy-cloud-heartbeat-client](wu-lazy-cloud-heartbeat-client) | 1.2.9-JDK17-SNAPSHOT | 客户端(支持二次开发) |
|
||||
| [wu-lazy-cloud-heartbeat-server](wu-lazy-cloud-heartbeat-server) | 1.2.9-JDK17-SNAPSHOT | 服务端(支持二次开发) |
|
||||
| [wu-lazy-cloud-heartbeat-client-start](wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start) | 1.2.9-JDK17-SNAPSHOT | 客户端样例 |
|
||||
| [wu-lazy-cloud-heartbeat-server-start](wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-client-start) | 1.2.9-JDK17-SNAPSHOT | 服务端样例 |
|
||||
| [wu-lazy-cloud-heartbeat-common](wu-lazy-cloud-heartbeat-common) | 1.3.0-JDK17-SNAPSHOT | 内网穿透公共模块(声明接口、枚举、常量、适配器、解析器) |
|
||||
| [wu-lazy-cloud-heartbeat-client](wu-lazy-cloud-heartbeat-client) | 1.3.0-JDK17-SNAPSHOT | 客户端(支持二次开发) |
|
||||
| [wu-lazy-cloud-heartbeat-server](wu-lazy-cloud-heartbeat-server) | 1.3.0-JDK17-SNAPSHOT | 服务端(支持二次开发) |
|
||||
| [wu-lazy-cloud-heartbeat-client-start](wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start) | 1.3.0-JDK17-SNAPSHOT | 客户端样例 |
|
||||
| [wu-lazy-cloud-heartbeat-server-start](wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-client-start) | 1.3.0-JDK17-SNAPSHOT | 服务端样例 |
|
||||
|
||||
### 使用技术
|
||||
|
||||
| 框架 | 版本 | 描述 |
|
||||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------|--------------|
|
||||
| spring-boot | 3.0.7 | springboot框架 |
|
||||
| [wu-framework-web](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-framework-web) | 1.2.9-JDK17-SNAPSHOT | web容器 |
|
||||
| [Lazy -ORM](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-inner-intergration/wu-database-parent) | 1.2.9-JDK17-SNAPSHOT | ORM |
|
||||
| [wu-framework-web](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-framework-web) | 1.3.0-JDK17-SNAPSHOT | web容器 |
|
||||
| [Lazy -ORM](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-inner-intergration/wu-database-parent) | 1.3.0-JDK17-SNAPSHOT | ORM |
|
||||
| mysql-connector-j | 8.0.33 | mysql驱动 |
|
||||
| [wu-authorization-server-platform-starter](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-smart-platform/wu-authorization-server-platform-starter) | 1.2.9-JDK17-SNAPSHOT | 用户授权体系 |
|
||||
| [wu-authorization-server-platform-starter](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-smart-platform/wu-authorization-server-platform-starter) | 1.3.0-JDK17-SNAPSHOT | 用户授权体系 |
|
||||
|
||||
### 使用环境
|
||||
|
||||
@ -287,7 +287,7 @@ public class NettyClientSocket {
|
||||
|
||||
docker启动
|
||||
|
||||
docker run -d -it -p 18080:18080 --name wu-lazy-cloud-heartbeat-server registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it -p 18080:18080 --name wu-lazy-cloud-heartbeat-server registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server:1.3.0-JDK17-SNAPSHOT
|
||||
|
||||
http://127.0.0.1:18080/swagger-ui/index.html
|
||||
|
||||
|
6
pom.xml
6
pom.xml
@ -8,12 +8,12 @@
|
||||
<parent>
|
||||
<artifactId>wu-framework-parent</artifactId>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
<description>云上云下</description>
|
||||
|
||||
|
||||
@ -70,7 +70,7 @@
|
||||
<dependency>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-framework-dependencies</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -24,7 +24,7 @@
|
||||
[change] 新增服务端渗透服务端----本地同局域网内端口映射
|
||||
[change] 新增客户端渗透服务端----本地端口映射到另一个服务端中的局域网端口
|
||||
[change] 新增客户端渗透客户端----本地端口映射到另一个局域网端口
|
||||
#### 1.2.9-JDK17-SNAPSHOT
|
||||
#### 1.3.0-JDK17-SNAPSHOT
|
||||
[change] 添加appkey&appsecret 验证
|
||||
[change] 支持同一个客户端ID多次注册
|
||||
[change] 修复通道关闭导致调度线程池submit异常问题
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -5,5 +5,5 @@ import org.wu.framework.lazy.orm.core.stereotype.LazyScan;
|
||||
|
||||
@ComponentScan(basePackages = "org.framework.lazy.cloud.network.heartbeat.client")
|
||||
@LazyScan(scanBasePackages = "org.framework.lazy.cloud.network.heartbeat.client.infrastructure.entity")
|
||||
public class EnableHeartbeatClientAutoConfiguration {
|
||||
public class EnableClientAutoConfiguration {
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.config;
|
||||
|
||||
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.context.NettyTcpClientSocketApplicationListener;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.context.NettyUdpClientSocketApplicationListener;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.tcp.advanced.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
@ -8,10 +10,12 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Role;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Import({NettyTcpClientSocketApplicationListener.class, NettyUdpClientSocketApplicationListener.class})
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
@ConditionalOnProperty(prefix = NettyClientProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class ClientAutoConfiguration {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.config;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -1,51 +1,39 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.config;
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.context;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.infrastructure.entity.LazyNettyServerPropertiesDO;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.application.LazyNettyServerPropertiesApplication;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.PropertiesType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.infrastructure.entity.LazyNettyServerPropertiesDO;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.tcp.filter.NettyTcpServerFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Role;
|
||||
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
|
||||
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 初始化配置
|
||||
*/
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class InitConfig implements CommandLineRunner, DisposableBean {
|
||||
@Component
|
||||
public class NettyTcpClientSocketApplicationListener implements ApplicationListener<ApplicationStartedEvent>, DisposableBean {
|
||||
|
||||
private final NettyClientProperties nettyClientProperties;
|
||||
private final LazyLambdaStream lazyLambdaStream;
|
||||
|
||||
private final LazyNettyServerPropertiesApplication lazyNettyServerPropertiesApplication;
|
||||
|
||||
public InitConfig(NettyClientProperties nettyClientProperties, LazyLambdaStream lazyLambdaStream, LazyNettyServerPropertiesApplication lazyNettyServerPropertiesApplication) {
|
||||
public NettyTcpClientSocketApplicationListener(NettyClientProperties nettyClientProperties, LazyLambdaStream lazyLambdaStream, LazyNettyServerPropertiesApplication lazyNettyServerPropertiesApplication) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
this.lazyLambdaStream = lazyLambdaStream;
|
||||
this.lazyNettyServerPropertiesApplication = lazyNettyServerPropertiesApplication;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
try {
|
||||
// 存储配置到db
|
||||
initDb2Config();
|
||||
// 启动客户端连接
|
||||
lazyNettyServerPropertiesApplication.starterAllClientSocket();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 存储配置到db
|
||||
*/
|
||||
@ -81,11 +69,29 @@ public class InitConfig implements CommandLineRunner, DisposableBean {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 程序关闭后执行
|
||||
* Handle an application event.
|
||||
*
|
||||
* @param event the event to respond to
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
try {
|
||||
// 存储配置到db
|
||||
initDb2Config();
|
||||
// 启动客户端连接
|
||||
lazyNettyServerPropertiesApplication.starterAllClientSocket();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PreDestroy
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
lazyNettyServerPropertiesApplication.destroyClientSocket();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.context;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.epoll.EpollDatagramChannel;
|
||||
import io.netty.channel.epoll.EpollEventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
import io.netty.util.internal.SystemPropertyUtil;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.udp.filter.NettyUdpServerFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.properties.ServerNodeProperties;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class NettyUdpClientSocketApplicationListener implements ApplicationListener<ApplicationStartedEvent>,DisposableBean {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.AbstractUdpHandleChannelHeartbeatTypeAdvanced;
|
||||
|
||||
|
||||
/**
|
||||
* 服务端 处理客户端心跳
|
||||
* UDP_TYPE_HEARTBEAT
|
||||
*/
|
||||
public class ClientHandleUdpChannelHeartbeatTypeAdvanced extends AbstractUdpHandleChannelHeartbeatTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
NettyProxyMsg hb = new NettyProxyMsg();
|
||||
hb.setType(UdpMessageType.TCP_TYPE_HEARTBEAT);
|
||||
// channel.writeAndFlush(hb);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeServicePermeateClientTransferTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 服务端处理客户端数据传输
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_TRANSFER
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpChannelTransferTypeAdvancedHandleDistributeUdpDistribute extends AbstractHandleUdpDistributeServicePermeateClientTransferTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
private final NettyClientProperties nettyClientProperties;
|
||||
|
||||
public ClientHandleUdpChannelTransferTypeAdvancedHandleDistributeUdpDistribute(NettyClientProperties nettyClientProperties) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
log.debug("接收到服务端需要内网穿透的数据:{}" , nettyProxyMsg);
|
||||
String clientId = nettyClientProperties.getClientId();
|
||||
byte[] visitorPort = nettyProxyMsg.getVisitorPort();
|
||||
byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] visitorId = nettyProxyMsg.getVisitorId();
|
||||
// 真实服务通道
|
||||
// Channel realChannel = NettyRealIdContext.getReal(new String(visitorId));
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if (nextChannel == null) {
|
||||
log.error("无法获取访客:{} 真实服务", new String(visitorId));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 把数据转到真实服务
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
|
||||
nextChannel.writeAndFlush(buf);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
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.advanced.udp.client.AbstractHandleUdpClientChannelActiveAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 客户端通道 is active
|
||||
*/
|
||||
public class ClientHandleUdpClientChannelActiveAdvanced extends AbstractHandleUdpClientChannelActiveAdvanced<NettyProxyMsg> {
|
||||
private final NettyClientProperties nettyClientProperties;
|
||||
|
||||
public ClientHandleUdpClientChannelActiveAdvanced(NettyClientProperties nettyClientProperties) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 缓存当前通道
|
||||
byte[] clientIdByte = nettyProxyMsg.getClientId();
|
||||
String clientId = new String(clientIdByte);
|
||||
ChannelContext.push(channel, clientId);
|
||||
ChannelAttributeKeyUtils.buildClientId(channel, clientId);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
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.NettyVisitorPortContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeClientPermeateClientCloseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端渗透客户端init close 信息
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_CLOSE
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientPermeateClientCloseTypeAdvanced extends AbstractHandleUdpDistributeClientPermeateClientCloseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 初始化 客户端渗透服务端socket
|
||||
byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
|
||||
Integer visitorPort = Integer.parseInt(new String(msgVisitorPort));
|
||||
PermeateVisitorSocket visitorSocket = NettyVisitorPortContext.getVisitorSocket(visitorPort);
|
||||
// 关闭当前客户端渗透服务端访客通道
|
||||
try {
|
||||
visitorSocket.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
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.udp.client.AbstractHandleUdpDistributeClientPermeateClientTransferCloseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 下发客户端渗透客户端通信通道关闭
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientPermeateClientTransferCloseTypeAdvanced extends AbstractHandleUdpDistributeClientPermeateClientTransferCloseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 关闭客户端真实通道、访客通道
|
||||
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
realChannel.close();// 真实通道关闭
|
||||
channel.close(); // 数据传输通道关闭
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
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.NettyVisitorPortContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeClientPermeateServerCloseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端渗透服务端init close 信息
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientPermeateServerCloseTypeAdvanced extends AbstractHandleUdpDistributeClientPermeateServerCloseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 初始化 客户端渗透服务端socket
|
||||
byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
|
||||
Integer visitorPort = Integer.parseInt(new String(msgVisitorPort));
|
||||
PermeateVisitorSocket visitorSocket = NettyVisitorPortContext.getVisitorSocket(visitorPort);
|
||||
// 关闭当前客户端渗透服务端访客通道
|
||||
try {
|
||||
visitorSocket.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
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.udp.client.AbstractHandleUdpDistributeClientPermeateServerTransferCloseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 下发 客户端渗透服务端通信通道关闭
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientPermeateServerTransferCloseTypeAdvanced extends AbstractHandleUdpDistributeClientPermeateServerTransferCloseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 关闭本地通信通道
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
channel.close();
|
||||
nextChannel.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.client.AbstractHandleUdpDistributeClientPermeateServerTransferTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 服务端处理客户端数据传输
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_TRANSFER
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientPermeateServerTransferTypeAdvanced extends AbstractHandleUdpDistributeClientPermeateServerTransferTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
log.debug("客户端渗透服务端返回数据:{}" , new String(nettyProxyMsg.getData()));
|
||||
byte[] visitorPort = nettyProxyMsg.getVisitorPort();
|
||||
byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] visitorId = nettyProxyMsg.getVisitorId();
|
||||
// 真实服务通道
|
||||
// Channel realChannel = NettyRealIdContext.getReal(new String(visitorId));
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if (nextChannel == null) {
|
||||
log.error("无法获取访客:{} 真实服务", new String(visitorId));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 把数据转到真实服务
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
|
||||
nextChannel.writeAndFlush(buf);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.socket.NettyUdpClientPermeateClientRealSocket;
|
||||
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.udp.client.AbstractHandleUdpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端渗透客户端数据传输通道连接成功
|
||||
*
|
||||
*
|
||||
* @see MessageTypeEnums#UDP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleUdpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 创建connect 然后发送创建成功
|
||||
byte[] msgClientId = nettyProxyMsg.getClientId();
|
||||
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
|
||||
byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
|
||||
|
||||
String clientId=new String(msgClientId);
|
||||
String clientTargetIp=new String(msgClientTargetIp);
|
||||
Integer clientTargetPort=Integer.parseInt(new String(msgClientTargetPort));
|
||||
String visitorId=new String(msgVisitorId);
|
||||
Integer visitorPort=Integer.parseInt(new String(msgVisitorPort));
|
||||
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
|
||||
NettyUdpClientPermeateClientRealSocket.buildRealServer(
|
||||
clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorPort,
|
||||
visitorId,
|
||||
nettyClientProperties,
|
||||
handleChannelTypeAdvancedList
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 下发 客户端渗透客户端数据传输通道init 成功
|
||||
*
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced extends AbstractHandleUdpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 连接成功 开启自动读取写
|
||||
byte[] msgClientId = nettyProxyMsg.getClientId();
|
||||
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
|
||||
byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
|
||||
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.client.AbstractHandleUdpDistributeClientTransferClientRequestTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 下发客户端渗透客户端数据传输
|
||||
*
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientTransferClientRequestTypeAdvanced extends AbstractHandleUdpDistributeClientTransferClientRequestTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
// 把数据转到真实服务
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
log.debug("client permeate client send request to real socket byte:{} ",new String(nettyProxyMsg.getData()));
|
||||
nextChannel.writeAndFlush(buf);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler.NettyUdpClientPermeateServerVisitorHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientPermeateServerVisitorTransferSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 客户端渗透服务端数据传输通道连接成功
|
||||
* @see NettyUdpClientPermeateServerVisitorTransferSocket
|
||||
* @see NettyUdpClientPermeateServerVisitorHandler
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param transferChannel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel transferChannel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 连接成功 开启自动读取写
|
||||
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
|
||||
String visitorId = new String(msgVisitorId);
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(transferChannel);
|
||||
nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 客户端连接成功通知
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle extends AbstractHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle<NettyProxyMsg> {
|
||||
|
||||
private final ClientChangeEvent clientChangeEvent;
|
||||
|
||||
|
||||
public ClientHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
|
||||
this.clientChangeEvent = clientChangeEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
// 客户端ID:{},客户端:{}连接成功
|
||||
log.warn("Client ID: {}, Client Data : {} Connection successful", new String(msg.getClientId()), new String(msg.getData()));
|
||||
|
||||
|
||||
// 存储其他客户端状态
|
||||
List<String> clientIdList = JSONObject.parseArray(new String(msg.getData()), String.class);
|
||||
for (String clientId : clientIdList) {
|
||||
clientChangeEvent.clientOnLine(clientId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.AbstractHandleUdpDistributeDisconnectTypeAdvancedHandle;
|
||||
|
||||
|
||||
/**
|
||||
* 服务端处理客户端断开连接处理
|
||||
* TYPE_DISCONNECT
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeDisconnectTypeAdvancedHandle extends AbstractHandleUdpDistributeDisconnectTypeAdvancedHandle<NettyProxyMsg> {
|
||||
|
||||
|
||||
private final ClientChangeEvent clientChangeEvent;
|
||||
|
||||
public ClientHandleUdpDistributeDisconnectTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
|
||||
this.clientChangeEvent = clientChangeEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
// 服务下线
|
||||
byte[] data = msg.getData();
|
||||
byte[] clientId = msg.getClientId();
|
||||
String tenantId = new String(clientId);
|
||||
//客户端:{}下线
|
||||
log.warn("Client: {} Offline", tenantId);
|
||||
clientChangeEvent.clientOffLine(tenantId);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.socket.NettyUdpServerPermeateClientRealSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrationRealClient;
|
||||
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.udp.client.AbstractHandleUdpDistributeServicePermeateClientRealConnectTypeAdvanced;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 客户端创建真实代理同奥
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeServicePermeateClientRealConnectTypeAdvanced extends AbstractHandleUdpDistributeServicePermeateClientRealConnectTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
private final NettyClientProperties nettyClientProperties;// 服务端地址信息
|
||||
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
|
||||
|
||||
public ClientHandleUdpDistributeServicePermeateClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties, List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
// 创建真实端口监听
|
||||
byte[] clientIdBytes = msg.getClientId();
|
||||
byte[] visitorPort = msg.getVisitorPort();
|
||||
byte[] clientTargetIp = msg.getClientTargetIp();
|
||||
byte[] clientTargetPort = msg.getClientTargetPort();
|
||||
byte[] visitorIdBytes = msg.getVisitorId();
|
||||
InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient =
|
||||
InternalNetworkPenetrationRealClient
|
||||
.builder()
|
||||
.clientId(new String(clientIdBytes))
|
||||
.visitorPort(Integer.valueOf(new String(visitorPort)))
|
||||
.clientTargetIp(new String(clientTargetIp))
|
||||
.clientTargetPort(Integer.valueOf(new String(clientTargetPort)))
|
||||
.visitorId(new String(visitorIdBytes))
|
||||
.build();
|
||||
|
||||
// 绑定真实服务端口
|
||||
NettyUdpServerPermeateClientRealSocket.buildRealServer(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.client.AbstractHandleUdpDistributeServicePermeateClientTransferClientResponseTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.UdpMessageTypeEnums;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 下发客户端渗透客户端数据传输响应
|
||||
*
|
||||
*
|
||||
* @see UdpMessageTypeEnums#UDP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_RESPONSE
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeServicePermeateClientTransferClientResponseTypeAdvanced extends AbstractHandleUdpDistributeServicePermeateClientTransferClientResponseTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
// 把数据转到真实服务
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
|
||||
nextChannel.writeAndFlush(buf);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.advanced.udp.client.AbstractHandleUdpDistributeSingleClientRealAutoReadConnectTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeSingleClientRealAutoReadConnectTypeAdvanced extends AbstractHandleUdpDistributeSingleClientRealAutoReadConnectTypeAdvanced<NettyProxyMsg> {
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 获取访客ID
|
||||
byte[] visitorId = nettyProxyMsg.getVisitorId();
|
||||
// 获取访客对应的真实代理通道
|
||||
Channel realChannel = NettyRealIdContext.getReal(visitorId);
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if (nextChannel != null) {
|
||||
nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
|
||||
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.advanced.udp.client.AbstractHandleUdpDistributeSingleClientRealCloseVisitorTypeAdvanced;
|
||||
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeSingleClientRealCloseVisitorTypeAdvanced extends AbstractHandleUdpDistributeSingleClientRealCloseVisitorTypeAdvanced<NettyProxyMsg> {
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
// 关闭代理的真实通道
|
||||
byte[] visitorId = nettyProxyMsg.getVisitorId();
|
||||
NettyRealIdContext.clear(visitorId);
|
||||
NettyCommunicationIdContext.clear(visitorId);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
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.udp.client.AbstractHandleUdpDistributeSingleClientMessageTypeAdvanced;
|
||||
|
||||
/**
|
||||
* 接收服务端发送过来的聊天信息
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeSingleClientUdpMessageTypeAdvanced extends AbstractHandleUdpDistributeSingleClientMessageTypeAdvanced<NettyProxyMsg> {
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
byte[] clientId = nettyProxyMsg.getClientId();
|
||||
byte[] data = nettyProxyMsg.getData();
|
||||
log.info("接收客户端:{},发送过来的聊天信息:{}", new String(clientId), new String(data));
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
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.udp.client.AbstractHandleUdpDistributeStagingClosedTypeAdvanced;
|
||||
|
||||
/**
|
||||
* 服务端下发暂存关闭消息处理
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeStagingClosedTypeAdvanced extends AbstractHandleUdpDistributeStagingClosedTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
String clientId = new String(msg.getClientId());
|
||||
log.info("客户端:{}离线暂存关闭", clientId);
|
||||
// 修改redis 客户端暂存状态
|
||||
// String stagingStatusKey = StagingConfigKeyConstant.getStagingStatusKey(clientId);
|
||||
// stringRedisTemplate.opsForValue().set(stagingStatusKey, StagingStatus.CLOSED.name());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced;
|
||||
|
||||
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.udp.client.AbstractHandleUdpDistributeStagingOpenedTypeAdvanced;
|
||||
|
||||
/**
|
||||
* 服务端下发暂存开启消息处理
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleUdpDistributeStagingOpenedTypeAdvanced extends AbstractHandleUdpDistributeStagingOpenedTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
public ClientHandleUdpDistributeStagingOpenedTypeAdvanced() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param msg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
String clientId = new String(msg.getClientId());
|
||||
//客户端:{}离线暂存开启
|
||||
log.warn("Client: {} Offline temporary storage enabled", new String(msg.getClientId()));
|
||||
// 修改redis 客户端暂存状态
|
||||
// String stagingStatusKey = StagingConfigKeyConstant.getStagingStatusKey(clientId);
|
||||
// stringRedisTemplate.opsForValue().set(stagingStatusKey, StagingStatus.OPENED.name());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter;
|
||||
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler.NettyUdpClientHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientSocket;
|
||||
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;
|
||||
|
||||
public class NettyUdpClientFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
|
||||
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
private final NettyUdpClientSocket nettyUdpClientSocket;
|
||||
|
||||
public NettyUdpClientFilter(ChannelTypeAdapter channelTypeAdapter, NettyUdpClientSocket nettyUdpClientSocket) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
this.nettyUdpClientSocket = nettyUdpClientSocket;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
|
||||
/* * 解码和编码,应和服务端一致 * */
|
||||
// pipeline.addLast(new NettyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettMsgEncoder());
|
||||
// 解码、编码
|
||||
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
// pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
|
||||
//入参说明: 读超时时间、写超时时间、所有类型的超时时间、时间格式
|
||||
//因为服务端设置的超时时间是5秒,所以设置4秒
|
||||
|
||||
pipeline.addLast(new IdleStateHandler(0, 4, 0));
|
||||
pipeline.addLast("decoder", new StringDecoder());
|
||||
pipeline.addLast("encoder", new StringEncoder());
|
||||
pipeline.addLast("doHandler", new NettyUdpClientHandler(channelTypeAdapter, nettyUdpClientSocket)); //客户端的逻辑
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.handler.NettyUdpClientPermeateClientRealHandler;
|
||||
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 NettyUdpClientPermeateClientRealFilter 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 NettyUdpClientPermeateClientRealHandler());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.handler.NettyUdpClientPermeateClientTransferHandler;
|
||||
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 NettyUdpClientPermeateClientTransferFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateClientTransferFilter(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 NettyUdpClientPermeateClientTransferHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.handler.NettyUdpClientPermeateClientTransferRealHandler;
|
||||
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 NettyUdpClientPermeateClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateClientTransferRealFilter(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 NettyUdpClientPermeateClientTransferRealHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateClientVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler.NettyUdpClientPermeateClientVisitorHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
public class NettyUdpClientPermeateClientVisitorFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
|
||||
private final InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor;
|
||||
|
||||
public NettyUdpClientPermeateClientVisitorFilter(InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor) {
|
||||
this.internalNetworkClientPermeateClientVisitor = internalNetworkClientPermeateClientVisitor;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ChannelDuplexHandler());
|
||||
pipeline.addLast(new NettyUdpClientPermeateClientVisitorHandler(internalNetworkClientPermeateClientVisitor));
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.handler.NettyUdpClientPermeateServerTransferHandler;
|
||||
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 NettyUdpClientPermeateServerTransferFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateServerTransferFilter(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 IdleStateHandler(0, 4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
pipeline.addLast(new NettyUdpClientPermeateServerTransferHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateServerVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler.NettyUdpClientPermeateServerVisitorHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
public class NettyUdpClientPermeateServerVisitorFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
|
||||
private final InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor;
|
||||
|
||||
public NettyUdpClientPermeateServerVisitorFilter(InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor) {
|
||||
this.internalNetworkClientPermeateServerVisitor = internalNetworkClientPermeateServerVisitor;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ChannelDuplexHandler());
|
||||
pipeline.addLast(new NettyUdpClientPermeateServerVisitorHandler(internalNetworkClientPermeateServerVisitor));
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.handler.NettyUdpServerPermeateClientRealHandler;
|
||||
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 NettyUdpServerPermeateClientRealFilter 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 NettyUdpServerPermeateClientRealHandler());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler.NettyUdpServerPermeateClientTransferHandler;
|
||||
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 NettyUdpServerPermeateClientTransferFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpServerPermeateClientTransferFilter(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 NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
pipeline.addLast(new NettyUdpServerPermeateClientTransferHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* description
|
||||
*
|
||||
* @author 吴佳伟
|
||||
* @date 2023/09/13 10:29
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
private final NettyUdpClientSocket nettyUdpClientSocket;
|
||||
|
||||
|
||||
public NettyUdpClientHandler(ChannelTypeAdapter channelTypeAdapter, NettyUdpClientSocket nettyUdpClientSocket) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
this.nettyUdpClientSocket = nettyUdpClientSocket;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
|
||||
* belongs to
|
||||
* @param msg the message to handle
|
||||
*/
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg msg) {
|
||||
// log.info("第" + count + "次" + ",客户端接受的消息:" + msg);
|
||||
// log.info("第" + count + "次" + ",客户端接受的消息内容:" + new String(msg.getData()));
|
||||
// count++;
|
||||
// 接收服务端、或者是代理端的信息
|
||||
Channel channel = ctx.channel();
|
||||
// log.info("type:{},clientId:{},data:{}",msg.getMysqlType(),new String(msg.getClientId()),new String(msg.getData()));
|
||||
channelTypeAdapter.handler(channel, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立连接时
|
||||
*/
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
// 建立连接时
|
||||
log.info("When establishing a connection:{}" , new Date());
|
||||
ctx.fireChannelActive();
|
||||
String clientId = nettyUdpClientSocket.getClientId();
|
||||
// 处理客户端连接成功
|
||||
Channel channel = ctx.channel();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(UdpMessageType.UDP_CLIENT_CHANNEL_ACTIVE);
|
||||
nettyMsg.setClientId(clientId);
|
||||
channelTypeAdapter.handler(channel, nettyMsg);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭连接时
|
||||
*/
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
//服务端主动关闭当前客户端连接时
|
||||
log.info("When the server actively closes the current client connection:{}" , new Date());
|
||||
final EventLoop eventLoop = ctx.channel().eventLoop();
|
||||
eventLoop.schedule(() -> {
|
||||
try {
|
||||
nettyUdpClientSocket.newConnect2Server();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}, 1L, TimeUnit.SECONDS);
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳请求处理 * 每4秒发送一次心跳请求; *
|
||||
*/
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object obj) throws Exception {
|
||||
if (obj instanceof IdleStateEvent event) {
|
||||
if (IdleState.WRITER_IDLE.equals(event.state())) { //如果写通道处于空闲状态,就发送心跳命令
|
||||
String clientId = nettyUdpClientSocket.getClientId();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(UdpMessageType.UDP_TYPE_HEARTBEAT);
|
||||
nettyMsg.setData(clientId.getBytes(StandardCharsets.UTF_8));
|
||||
nettyMsg.setClientId(clientId.getBytes(StandardCharsets.UTF_8));
|
||||
ctx.writeAndFlush(nettyMsg);// 发送心跳数据
|
||||
} else if (event.state() == IdleState.WRITER_IDLE) { // 如果检测到写空闲状态,关闭连接
|
||||
// 离线、暂存通知
|
||||
String clientId = nettyUdpClientSocket.getClientId();
|
||||
Channel channel = ctx.channel();
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(UdpMessageType.UDP_DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION);
|
||||
nettyMsg.setClientId(clientId.getBytes(StandardCharsets.UTF_8));
|
||||
channelTypeAdapter.handler(channel, nettyMsg);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
} else {
|
||||
super.userEventTriggered(ctx, obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.UdpMessageType;
|
||||
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 NettyUdpClientPermeateClientRealHandler 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(UdpMessageType.UDP_REPORT_CLIENT_TRANSFER_CLIENT_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(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_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,70 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.UdpMessageType;
|
||||
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 NettyUdpClientPermeateClientTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateClientTransferHandler(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(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_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,73 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.UdpMessageType;
|
||||
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 NettyUdpClientPermeateClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateClientTransferRealHandler(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(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_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,133 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateClientVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced.ClientHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientPermeateClientVisitorTransferSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientPermeateServerVisitorTransferSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
|
||||
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.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateClientVisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
||||
private final InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor;
|
||||
// private final NettyChannelPool nettyChannelPool = new DefaultNettyChannelPool(10);
|
||||
|
||||
public NettyUdpClientPermeateClientVisitorHandler(InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor) {
|
||||
this.internalNetworkClientPermeateClientVisitor = internalNetworkClientPermeateClientVisitor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ctx
|
||||
* @throws Exception
|
||||
* @see NettyUdpClientPermeateServerVisitorTransferSocket
|
||||
* @see ClientHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
|
||||
*/
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
// 访客连接上代理服务器了
|
||||
Channel visitorChannel = ctx.channel();
|
||||
// 先不读取访客数据
|
||||
visitorChannel.config().setOption(ChannelOption.AUTO_READ, false);
|
||||
|
||||
|
||||
// 生成访客ID
|
||||
String visitorId = UUID.randomUUID().toString();
|
||||
// 绑定访客真实通道
|
||||
NettyRealIdContext.pushReal(visitorChannel, visitorId);
|
||||
// 当前通道绑定访客ID
|
||||
ChannelAttributeKeyUtils.buildVisitorId(visitorChannel, visitorId);
|
||||
|
||||
// 判断是否有可用的通道 如果没有创建新的通道
|
||||
// Channel transferChannel = nettyChannelPool.availableChannel(visitorId);
|
||||
// 创建访客连接客户端通道
|
||||
NettyUdpClientPermeateClientVisitorTransferSocket.buildTransferServer(internalNetworkClientPermeateClientVisitor,visitorChannel);
|
||||
log.info("客户端渗透客户端访客:【{}】端口连接成功了",visitorId);
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) {
|
||||
|
||||
// 访客通道
|
||||
Channel visitorChannel = ctx.channel();
|
||||
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel);
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(visitorChannel);
|
||||
byte[] bytes = new byte[buf.readableBytes()];
|
||||
buf.readBytes(bytes);
|
||||
// 获取客户端通道,而后进行数据下发
|
||||
log.debug("【客户端渗透客户端】访客端口成功接收数据:{}", new String(bytes));
|
||||
|
||||
// 使用访客的通信通道
|
||||
Integer visitorPort = internalNetworkClientPermeateClientVisitor.getVisitorPort();
|
||||
String clientId = internalNetworkClientPermeateClientVisitor.getNettyClientProperties().getClientId();
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
nettyProxyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST);
|
||||
nettyProxyMsg.setVisitorId(visitorId);
|
||||
nettyProxyMsg.setClientId(clientId);
|
||||
nettyProxyMsg.setVisitorPort(visitorPort);
|
||||
nettyProxyMsg.setData(bytes);
|
||||
nextChannel.writeAndFlush(nettyProxyMsg);
|
||||
log.debug("【客户端渗透客户端】访客端口成功发送数据了");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(channel);
|
||||
if (StringUtil.isNullOrEmpty(visitorId)) {
|
||||
super.channelInactive(ctx);
|
||||
return;
|
||||
}
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
// 通信通道自动读写打开 ,然后关闭通信通道
|
||||
|
||||
if (nextChannel != null && nextChannel.isActive()) {
|
||||
|
||||
// nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
|
||||
// 通知客户端 关闭访问通道、真实通道
|
||||
NettyProxyMsg myMsg = new NettyProxyMsg();
|
||||
myMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE);
|
||||
nextChannel.writeAndFlush(myMsg);
|
||||
}
|
||||
// 关闭 访客通信通道、访客真实通道
|
||||
NettyRealIdContext.clear(visitorId);
|
||||
NettyCommunicationIdContext.clear(visitorId);
|
||||
log.warn("【客户端渗透客户端】访客:【{}】端口断开连接",visitorId);
|
||||
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 {
|
||||
cause.printStackTrace();
|
||||
log.error("exceptionCaught");
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
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 NettyUdpClientPermeateServerTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpClientPermeateServerTransferHandler(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(UdpMessageType.UDP_UDP_REPORT_CLIENT_PERMEATE_SERVER_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);
|
||||
}
|
||||
/**
|
||||
* 心跳请求处理 * 每4秒发送一次心跳请求; *
|
||||
*/
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object obj) throws Exception {
|
||||
if (obj instanceof IdleStateEvent event) {
|
||||
if (IdleState.WRITER_IDLE.equals(event.state())) { //如果写通道处于空闲状态,就发送心跳命令
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(UdpMessageType.UDP_TYPE_HEARTBEAT);
|
||||
ctx.writeAndFlush(nettyMsg);// 发送心跳数据
|
||||
} else if (event.state() == IdleState.WRITER_IDLE) { // 如果检测到写空闲状态,关闭连接
|
||||
// 离线、暂存通知
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
} else {
|
||||
super.userEventTriggered(ctx, obj);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateServerVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.advanced.ClientHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket.NettyUdpClientPermeateServerVisitorTransferSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateServerVisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
||||
private final InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor;
|
||||
// private final NettyChannelPool nettyChannelPool = new DefaultNettyChannelPool(10);
|
||||
|
||||
public NettyUdpClientPermeateServerVisitorHandler(InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor) {
|
||||
this.internalNetworkClientPermeateServerVisitor = internalNetworkClientPermeateServerVisitor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ctx
|
||||
* @throws Exception
|
||||
* @see NettyUdpClientPermeateServerVisitorTransferSocket
|
||||
* @see ClientHandleUdpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
|
||||
*/
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
// 访客连接上代理服务器了
|
||||
Channel visitorChannel = ctx.channel();
|
||||
// 先不读取访客数据
|
||||
visitorChannel.config().setOption(ChannelOption.AUTO_READ, false);
|
||||
|
||||
// 生成访客ID
|
||||
String visitorId = UUID.randomUUID().toString();
|
||||
Integer visitorPort = internalNetworkClientPermeateServerVisitor.getVisitorPort();
|
||||
|
||||
log.info("this channel with visitor port:{} use visitorId:{}", visitorPort, visitorId);
|
||||
ChannelAttributeKeyUtils.buildVisitorId(visitorChannel, visitorId);
|
||||
// 判断是否有可用的通道 如果没有创建新的通道
|
||||
// Channel transferChannel = nettyChannelPool.availableChannel(visitorId);
|
||||
// 创建访客连接服务端通道
|
||||
NettyUdpClientPermeateServerVisitorTransferSocket.buildTransferServer(internalNetworkClientPermeateServerVisitor,visitorChannel);
|
||||
log.debug("客户端渗透服务端访客端口连接成功了,访客ID:{}", visitorId);
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) {
|
||||
|
||||
// 访客通道
|
||||
Channel visitorChannel = ctx.channel();
|
||||
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel);
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(visitorChannel);
|
||||
byte[] bytes = new byte[buf.readableBytes()];
|
||||
buf.readBytes(bytes);
|
||||
// 获取客户端通道,而后进行数据下发
|
||||
log.debug("【客户端渗透服务端】访客端口成功接收数据:{}", new String(bytes));
|
||||
|
||||
// 使用访客的通信通道
|
||||
Integer visitorPort = internalNetworkClientPermeateServerVisitor.getVisitorPort();
|
||||
String clientId = internalNetworkClientPermeateServerVisitor.getNettyClientProperties().getClientId();
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
nettyProxyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER);
|
||||
nettyProxyMsg.setVisitorId(visitorId);
|
||||
nettyProxyMsg.setClientId(clientId);
|
||||
nettyProxyMsg.setVisitorPort(visitorPort);
|
||||
nettyProxyMsg.setData(bytes);
|
||||
nextChannel.writeAndFlush(nettyProxyMsg);
|
||||
// 处理访客流量
|
||||
log.debug("【客户端渗透服务端】访客端口成功发送数据了 访客ID:{}", visitorId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
|
||||
log.info("channel inactive:{}", visitorId);
|
||||
if (StringUtil.isNullOrEmpty(visitorId)) {
|
||||
super.channelInactive(ctx);
|
||||
return;
|
||||
}
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
// 通信通道自动读写打开 ,然后关闭通信通道
|
||||
|
||||
if (nextChannel != null && nextChannel.isActive()) {
|
||||
// 通知服务端 关闭访问通道、真实通道
|
||||
NettyProxyMsg myMsg = new NettyProxyMsg();
|
||||
myMsg.setType(UdpMessageType.UDP_UDP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE);
|
||||
myMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(myMsg);
|
||||
//通信通道
|
||||
nextChannel.close();
|
||||
log.debug("关闭访问通道、真实通道 with visitorId:{}", visitorId);
|
||||
}else {
|
||||
log.debug("channel inactive:{}", nextChannel);
|
||||
}
|
||||
// 访客通道关闭
|
||||
channel.close();
|
||||
|
||||
log.warn("【客户端渗透服务端】访客端口断开连接,访客ID:{}", visitorId);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.info("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.info("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
log.error("exceptionCaught");
|
||||
|
||||
Channel channel = ctx.channel();
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(channel);
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
|
||||
// 使用通信通道 下发关闭访客
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null) {
|
||||
// 下发关闭访客
|
||||
NettyProxyMsg closeRealClient = new NettyProxyMsg();
|
||||
closeRealClient.setType(UdpMessageType.UDP_UDP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE);
|
||||
closeRealClient.setClientId(clientId);
|
||||
closeRealClient.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeRealClient);
|
||||
}
|
||||
|
||||
ctx.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
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;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
/**
|
||||
* 来自客户端 真实服务器返回的数据请求
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpServerPermeateClientRealHandler 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 visitor = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
NettyProxyMsg returnMessage = new NettyProxyMsg();
|
||||
returnMessage.setType(UdpMessageType.UDP_REPORT_CLIENT_TRANSFER);
|
||||
returnMessage.setVisitorId(visitorId);
|
||||
returnMessage.setClientId(clientId);
|
||||
returnMessage.setVisitorPort(visitorPort);
|
||||
returnMessage.setData(bytes);
|
||||
|
||||
visitor.writeAndFlush(returnMessage);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
// 客户端真实通信通道
|
||||
Channel visitor = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (visitor != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(UdpMessageType.UDP_REPORT_SERVICE_PERMEATE_CLIENT_CLIENT_CLOSE_VISITOR);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
visitor.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
// String vid = ctx.channel().attr(Constant.VID).get();
|
||||
// if (StringUtil.isNullOrEmpty(vid)) {
|
||||
// super.channelWritabilityChanged(ctx);
|
||||
// return;
|
||||
// }
|
||||
// Channel proxyChannel = Constant.vpc.get(vid);
|
||||
// if (proxyChannel != null) {
|
||||
// proxyChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
|
||||
// }
|
||||
|
||||
// 获取访客的传输通道
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
|
||||
if(ObjectUtils.isEmpty(visitorId)) {
|
||||
super.channelWritabilityChanged(ctx);
|
||||
return;
|
||||
}
|
||||
Channel visitorCommunicationChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (visitorCommunicationChannel != null) {
|
||||
log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
|
||||
visitorCommunicationChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOption;
|
||||
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.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
/**
|
||||
* 客户端访客通信通道 处理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpServerPermeateClientTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyUdpServerPermeateClientTransferHandler(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(UdpMessageType.UDP_REPORT_SERVICE_PERMEATE_CLIENT_CLIENT_CLOSE_VISITOR);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
// 处理客户端本地真实通道问题
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
if(ObjectUtils.isEmpty(visitorId)) {
|
||||
super.channelWritabilityChanged(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
// Channel realChannel = NettyRealIdContext.getReal(visitorId);
|
||||
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (realChannel != null) {
|
||||
log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.handler;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class UdpHeartBeatClientHandler extends ChannelInboundHandlerAdapter {
|
||||
private final int lossConnectCount = 0;
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
System.out.println("客户端循环心跳监测发送: " + new Date());
|
||||
if (evt instanceof IdleStateEvent event) {
|
||||
if (event.state() == IdleState.WRITER_IDLE) {
|
||||
ctx.writeAndFlush("biubiu");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.filter.NettyUdpClientPermeateClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientPermeateClientTransferRealFilter;
|
||||
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.UdpMessageType;
|
||||
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.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端连接真实服务
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateClientRealSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
|
||||
public static void buildRealServer(String clientId,
|
||||
String clientTargetIp,
|
||||
Integer clientTargetPort,
|
||||
Integer visitorPort,
|
||||
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.UDP_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 NettyUdpClientPermeateClientRealFilter())
|
||||
|
||||
;
|
||||
|
||||
|
||||
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);
|
||||
ChannelAttributeKeyUtils.buildVisitorPort(realChannel, visitorPort);
|
||||
|
||||
// 连接服务端 然后绑定通道
|
||||
// 新建一个通道处理
|
||||
newVisitorConnect2Server(clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorPort,
|
||||
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,
|
||||
Integer visitorPort,
|
||||
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.UDP_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 NettyUdpClientPermeateClientTransferRealFilter(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.setVisitorPort(visitorPort);
|
||||
nettyProxyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL);
|
||||
transferChannel.writeAndFlush(nettyProxyMsg);
|
||||
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
|
||||
|
||||
// 绑定客户端真实通信通道
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
|
||||
|
||||
ChannelAttributeKeyUtils.buildVisitorPort(transferChannel, visitorPort);
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
log.info("无法连接到服务端....");
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
newVisitorConnect2Server(clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorPort,
|
||||
visitorId,realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,292 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateClientVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientPermeateClientVisitorFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 内网穿透 客户端渗透客户端通道
|
||||
*
|
||||
* @see NettyVisitorPortContext
|
||||
* @see NettyClientVisitorContext
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateClientVisitorSocket implements PermeateVisitorSocket {
|
||||
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
private final NettyUdpClientPermeateClientVisitorFilter nettyUdpClientPermeateClientVisitorFilter;
|
||||
@Getter
|
||||
private final String clientId;
|
||||
@Getter
|
||||
private final int visitorPort;
|
||||
|
||||
public NettyUdpClientPermeateClientVisitorSocket(NettyUdpClientPermeateClientVisitorFilter nettyUdpClientPermeateClientVisitorFilter, String clientId, int visitorPort) {
|
||||
this.nettyUdpClientPermeateClientVisitorFilter = nettyUdpClientPermeateClientVisitorFilter;
|
||||
this.clientId = clientId;
|
||||
this.visitorPort = visitorPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动客户端本地端口渗透到服务端端口
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
Channel visitor = NettyVisitorPortContext.getVisitorChannel(visitorPort);
|
||||
if (visitor == null) {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
|
||||
|
||||
// 设置读缓冲区为2M
|
||||
.childOption(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
|
||||
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
// .childOption(ChannelOption.UDP_NODELAY, false)
|
||||
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认 AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
|
||||
|
||||
|
||||
.childHandler(nettyUdpClientPermeateClientVisitorFilter);
|
||||
|
||||
try {
|
||||
bootstrap.bind(visitorPort).sync().addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
Channel channel = future.channel();
|
||||
ChannelAttributeKeyUtils.buildVisitorPort(channel,visitorPort);
|
||||
// 这里时异步处理
|
||||
log.info("客户端:[{}]访客端口:[{}] 开启", clientId, visitorPort);
|
||||
NettyVisitorPortContext.pushVisitorChannel(visitorPort, channel);
|
||||
|
||||
} else {
|
||||
log.error("客户端:[{}]访客端口:[{}]绑定失败", clientId, visitorPort);
|
||||
}
|
||||
});
|
||||
NettyVisitorPortContext.pushVisitorSocket(visitorPort, this);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
log.warn("客户端渗透服务端:[{}]访客端口:[{}] 重复启动", clientId, visitorPort);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void close() {
|
||||
if (!bossGroup.isShutdown()) {
|
||||
bossGroup.shutdownGracefully();
|
||||
}
|
||||
if (!workerGroup.isShutdown()) {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
Channel visitor = NettyVisitorPortContext.getVisitorChannel(visitorPort);
|
||||
if (visitor != null) {
|
||||
|
||||
// close channel
|
||||
visitor.close();
|
||||
// remove visitor
|
||||
NettyVisitorPortContext.removeVisitorChannel(visitorPort);
|
||||
// remove client this
|
||||
NettyVisitorPortContext.removeVisitorSocket(visitorPort);
|
||||
log.warn("关闭客户端 :【{}】 访客户端口:【{}】", clientId, visitorPort);
|
||||
} else {
|
||||
log.warn("关闭访客端口失败 未找到客户端通道 客户端 :【{}】 访客户端口:【{}】", clientId, visitorPort);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final class NettyClientPermeateClientVisitorSocketBuilder {
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 客户端目标地址
|
||||
*/
|
||||
private String clientTargetIp;
|
||||
|
||||
/**
|
||||
* 客户端目标端口
|
||||
*/
|
||||
private Integer clientTargetPort;
|
||||
|
||||
|
||||
/**
|
||||
* 访问端口
|
||||
*/
|
||||
private Integer visitorPort;
|
||||
/**
|
||||
* 访客ID
|
||||
*/
|
||||
private String visitorId;
|
||||
/**
|
||||
* 目标客户端ID
|
||||
*/
|
||||
private String toClientId;
|
||||
|
||||
/**
|
||||
* 流量适配器
|
||||
*/
|
||||
private ChannelFlowAdapter channelFlowAdapter;
|
||||
/**
|
||||
* 服务端地址信息
|
||||
*/
|
||||
private NettyClientProperties nettyClientProperties;
|
||||
|
||||
/**
|
||||
* 处理器
|
||||
*/
|
||||
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
|
||||
|
||||
public static NettyClientPermeateClientVisitorSocketBuilder builder() {
|
||||
return new NettyClientPermeateClientVisitorSocketBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充客户端
|
||||
*
|
||||
* @param clientId 客户端
|
||||
* @return 返回当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定客户端目标IP
|
||||
*
|
||||
* @param clientTargetIp 客户端目标IP
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderClientTargetIp(String clientTargetIp) {
|
||||
this.clientTargetIp = clientTargetIp;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定客户端目标端口
|
||||
*
|
||||
* @param clientTargetPort 客户端目标端口
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderClientTargetPort(Integer clientTargetPort) {
|
||||
this.clientTargetPort = clientTargetPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定访客端口
|
||||
*
|
||||
* @param visitorPort 访客端口
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderVisitorPort(Integer visitorPort) {
|
||||
this.visitorPort = visitorPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定流量适配器
|
||||
*
|
||||
* @param channelFlowAdapter 流量适配器
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderChannelFlowAdapter(ChannelFlowAdapter channelFlowAdapter) {
|
||||
this.channelFlowAdapter = channelFlowAdapter;
|
||||
return this;
|
||||
} /**
|
||||
* 绑定流量适配器
|
||||
*
|
||||
* @param handleChannelTypeAdvancedList 流量适配器
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderHandleChannelTypeAdvancedList(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 服务端地址信息
|
||||
*
|
||||
* @param nettyClientProperties 客户服务端地址配置属性
|
||||
* @return 返回当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderNettyClientProperties(NettyClientProperties nettyClientProperties) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定访客ID
|
||||
*
|
||||
* @param visitorId 访客ID
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderVisitorId(String visitorId) {
|
||||
this.visitorId = visitorId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 目标客户端ID
|
||||
*
|
||||
* @param toClientId 目标客户端ID
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyClientPermeateClientVisitorSocketBuilder builderToClientId(String toClientId) {
|
||||
this.toClientId = toClientId;
|
||||
return this;
|
||||
}
|
||||
public NettyUdpClientPermeateClientVisitorSocket build() {
|
||||
if (clientTargetIp == null) {
|
||||
throw new IllegalArgumentException("clientTargetIp must not null");
|
||||
}
|
||||
if (clientTargetPort == null) {
|
||||
throw new IllegalArgumentException("clientTargetPort must not null");
|
||||
}
|
||||
if (visitorPort == null) {
|
||||
throw new IllegalArgumentException("visitorPort must not null");
|
||||
}
|
||||
InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor = new InternalNetworkClientPermeateClientVisitor();
|
||||
|
||||
internalNetworkClientPermeateClientVisitor.setFromClientId(nettyClientProperties.getClientId());
|
||||
internalNetworkClientPermeateClientVisitor.setToClientId(toClientId);
|
||||
internalNetworkClientPermeateClientVisitor.setTargetIp(clientTargetIp);
|
||||
internalNetworkClientPermeateClientVisitor.setTargetPort(clientTargetPort);
|
||||
internalNetworkClientPermeateClientVisitor.setVisitorPort(visitorPort);
|
||||
internalNetworkClientPermeateClientVisitor.setNettyClientProperties(nettyClientProperties);
|
||||
internalNetworkClientPermeateClientVisitor.setChannelFlowAdapter(channelFlowAdapter);
|
||||
internalNetworkClientPermeateClientVisitor.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
|
||||
|
||||
|
||||
NettyUdpClientPermeateClientVisitorFilter visitorFilter = new NettyUdpClientPermeateClientVisitorFilter(internalNetworkClientPermeateClientVisitor);
|
||||
return new NettyUdpClientPermeateClientVisitorSocket(visitorFilter, clientId, visitorPort);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.InternalNetworkClientPermeateClientVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientPermeateClientTransferFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
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;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端渗透服务端传输通道
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateClientVisitorTransferSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
/**
|
||||
* 连接服务端通信通道
|
||||
*/
|
||||
public static void buildTransferServer(InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor, Channel visitorChannel) {
|
||||
|
||||
|
||||
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.UDP_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 NettyUdpClientPermeateClientTransferFilter(new ChannelTypeAdapter(internalNetworkClientPermeateClientVisitor.getHandleChannelTypeAdvancedList())))
|
||||
;
|
||||
NettyClientProperties nettyClientProperties = internalNetworkClientPermeateClientVisitor.getNettyClientProperties();
|
||||
String inetHost = nettyClientProperties.getInetHost();
|
||||
int inetPort = nettyClientProperties.getInetPort();
|
||||
// local client id
|
||||
|
||||
String clientId = nettyClientProperties.getClientId();
|
||||
|
||||
String targetIp = internalNetworkClientPermeateClientVisitor.getTargetIp();
|
||||
Integer targetPort = internalNetworkClientPermeateClientVisitor.getTargetPort();
|
||||
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel);
|
||||
Integer visitorPort = internalNetworkClientPermeateClientVisitor.getVisitorPort();
|
||||
String toClientId = internalNetworkClientPermeateClientVisitor.getToClientId();
|
||||
|
||||
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
|
||||
log.info("Client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with clientId:【{}】 toClientId:【{}】 & visitorId:【{}】", inetHost, inetPort, clientId, toClientId, visitorId);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
|
||||
// 使用的客户端ID:{}
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
nettyProxyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL);
|
||||
// other clientId
|
||||
nettyProxyMsg.setClientId(toClientId);
|
||||
nettyProxyMsg.setVisitorPort(visitorPort);
|
||||
nettyProxyMsg.setClientTargetIp(targetIp);
|
||||
nettyProxyMsg.setClientTargetPort(targetPort);
|
||||
|
||||
nettyProxyMsg.setVisitorId(visitorId);
|
||||
transferChannel.writeAndFlush(nettyProxyMsg);
|
||||
// 绑定客户端真实通信通道
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
|
||||
// 传输通道打开后自动读取
|
||||
ChannelAttributeKeyUtils.buildNextChannel(visitorChannel, transferChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, visitorChannel);
|
||||
|
||||
|
||||
} else {
|
||||
log.info("无法连接到服务端....");
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
buildTransferServer(internalNetworkClientPermeateClientVisitor, visitorChannel);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,237 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.InternalNetworkClientPermeateServerVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientPermeateServerVisitorFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 内网穿透 客户端渗透服务端通道
|
||||
*
|
||||
* @see NettyVisitorPortContext
|
||||
* @see NettyClientVisitorContext
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateServerVisitorSocket implements PermeateVisitorSocket {
|
||||
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
private final NettyUdpClientPermeateServerVisitorFilter nettyUdpClientPermeateServerVisitorFilter;
|
||||
@Getter
|
||||
private final String clientId;
|
||||
@Getter
|
||||
private final int visitorPort;
|
||||
|
||||
public NettyUdpClientPermeateServerVisitorSocket(NettyUdpClientPermeateServerVisitorFilter nettyUdpClientPermeateServerVisitorFilter, String clientId, int visitorPort) {
|
||||
this.nettyUdpClientPermeateServerVisitorFilter = nettyUdpClientPermeateServerVisitorFilter;
|
||||
this.clientId = clientId;
|
||||
this.visitorPort = visitorPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动客户端本地端口渗透到服务端端口
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
PermeateVisitorSocket visitor = NettyVisitorPortContext.getVisitorSocket(visitorPort);
|
||||
if (visitor == null) {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
|
||||
|
||||
// 设置读缓冲区为2M
|
||||
.childOption(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
|
||||
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
// .childOption(ChannelOption.UDP_NODELAY, false)
|
||||
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认 AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
|
||||
|
||||
|
||||
.childHandler(nettyUdpClientPermeateServerVisitorFilter);
|
||||
|
||||
try {
|
||||
bootstrap.bind(visitorPort).sync().addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 这里时异步处理
|
||||
log.info("客户端:[{}]访客端口:[{}] 开启", clientId, visitorPort);
|
||||
Channel channel = future.channel();
|
||||
ChannelAttributeKeyUtils.buildVisitorPort(channel,visitorPort);
|
||||
} else {
|
||||
log.error("客户端:[{}]访客端口:[{}]绑定失败", clientId, visitorPort);
|
||||
}
|
||||
});
|
||||
NettyVisitorPortContext.pushVisitorSocket(visitorPort, this);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
log.warn("客户端渗透服务端:[{}]访客端口:[{}] 重复启动", clientId, visitorPort);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void close() {
|
||||
if (!bossGroup.isShutdown()) {
|
||||
bossGroup.shutdownGracefully();
|
||||
}
|
||||
if (!workerGroup.isShutdown()) {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
PermeateVisitorSocket permeateVisitorSocket = NettyVisitorPortContext.getVisitorSocket(visitorPort);
|
||||
if (permeateVisitorSocket != null) {
|
||||
log.warn("关闭客户端 :【{}】 访客户端口:【{}】", clientId, visitorPort);
|
||||
} else {
|
||||
log.warn("关闭访客端口失败 未找到客户端通道 客户端 :【{}】 访客户端口:【{}】", clientId, visitorPort);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final class NettyVisitorSocketBuilder {
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 客户端目标地址
|
||||
*/
|
||||
private String clientTargetIp;
|
||||
|
||||
/**
|
||||
* 客户端目标端口
|
||||
*/
|
||||
private Integer clientTargetPort;
|
||||
|
||||
|
||||
/**
|
||||
* 访问端口
|
||||
*/
|
||||
private Integer visitorPort;
|
||||
/**
|
||||
* 服务端地址信息
|
||||
*/
|
||||
private NettyClientProperties nettyClientProperties;
|
||||
|
||||
/**
|
||||
* 处理器
|
||||
*/
|
||||
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
|
||||
|
||||
public static NettyVisitorSocketBuilder builder() {
|
||||
return new NettyVisitorSocketBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充客户端
|
||||
*
|
||||
* @param clientId 客户端
|
||||
* @return 返回当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定客户端目标IP
|
||||
*
|
||||
* @param clientTargetIp 客户端目标IP
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderClientTargetIp(String clientTargetIp) {
|
||||
this.clientTargetIp = clientTargetIp;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定客户端目标端口
|
||||
*
|
||||
* @param clientTargetPort 客户端目标端口
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderClientTargetPort(Integer clientTargetPort) {
|
||||
this.clientTargetPort = clientTargetPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定访客端口
|
||||
*
|
||||
* @param visitorPort 访客端口
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderVisitorPort(Integer visitorPort) {
|
||||
this.visitorPort = visitorPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定流量适配器
|
||||
*
|
||||
* @param handleChannelTypeAdvancedList 流量适配器
|
||||
* @return 当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderHandleChannelTypeAdvancedList(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 服务端地址信息
|
||||
*
|
||||
* @param nettyClientProperties 客户服务端地址配置属性
|
||||
* @return 返回当前对象
|
||||
*/
|
||||
public NettyVisitorSocketBuilder builderNettyClientProperties(NettyClientProperties nettyClientProperties) {
|
||||
this.nettyClientProperties = nettyClientProperties;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public NettyUdpClientPermeateServerVisitorSocket build() {
|
||||
if (clientTargetIp == null) {
|
||||
throw new IllegalArgumentException("clientTargetIp must not null");
|
||||
}
|
||||
if (clientTargetPort == null) {
|
||||
throw new IllegalArgumentException("clientTargetPort must not null");
|
||||
}
|
||||
if (visitorPort == null) {
|
||||
throw new IllegalArgumentException("visitorPort must not null");
|
||||
}
|
||||
InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor = new InternalNetworkClientPermeateServerVisitor();
|
||||
|
||||
internalNetworkClientPermeateServerVisitor.setTargetIp(clientTargetIp);
|
||||
internalNetworkClientPermeateServerVisitor.setTargetPort(clientTargetPort);
|
||||
internalNetworkClientPermeateServerVisitor.setVisitorPort(visitorPort);
|
||||
internalNetworkClientPermeateServerVisitor.setNettyClientProperties(nettyClientProperties);
|
||||
internalNetworkClientPermeateServerVisitor.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
|
||||
|
||||
|
||||
NettyUdpClientPermeateServerVisitorFilter nettyUdpClientPermeateServerVisitorFilter = new NettyUdpClientPermeateServerVisitorFilter(internalNetworkClientPermeateServerVisitor);
|
||||
return new NettyUdpClientPermeateServerVisitorSocket(nettyUdpClientPermeateServerVisitorFilter, clientId, visitorPort);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.InternalNetworkClientPermeateServerVisitor;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientPermeateServerTransferFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
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;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端渗透服务端传输通道
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientPermeateServerVisitorTransferSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
/**
|
||||
* 连接服务端通信通道
|
||||
* <p>
|
||||
* internalNetworkClientPermeateServerVisitor
|
||||
*/
|
||||
public static void buildTransferServer(InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor, Channel visitorChannel) {
|
||||
|
||||
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.UDP_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 NettyUdpClientPermeateServerTransferFilter(new ChannelTypeAdapter(internalNetworkClientPermeateServerVisitor.getHandleChannelTypeAdvancedList())))
|
||||
;
|
||||
NettyClientProperties nettyClientProperties = internalNetworkClientPermeateServerVisitor.getNettyClientProperties();
|
||||
String inetHost = nettyClientProperties.getInetHost();
|
||||
int inetPort = nettyClientProperties.getInetPort();
|
||||
// local client id
|
||||
|
||||
String clientId = nettyClientProperties.getClientId();
|
||||
|
||||
String targetIp = internalNetworkClientPermeateServerVisitor.getTargetIp();
|
||||
Integer targetPort = internalNetworkClientPermeateServerVisitor.getTargetPort();
|
||||
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(visitorChannel);
|
||||
Integer visitorPort = internalNetworkClientPermeateServerVisitor.getVisitorPort();
|
||||
|
||||
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
|
||||
log.debug("Client creates a new visitor channel to connect to server IP: {}, connecting to server port: {}", inetHost, inetPort);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
|
||||
// 使用的客户端ID:{}
|
||||
log.info("Client ID used: {}", clientId);
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
|
||||
NettyProxyMsg myMsg = new NettyProxyMsg();
|
||||
myMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL);
|
||||
myMsg.setClientId(clientId);
|
||||
myMsg.setVisitorPort(visitorPort);
|
||||
myMsg.setClientTargetIp(targetIp);
|
||||
myMsg.setClientTargetPort(targetPort);
|
||||
myMsg.setVisitorId(visitorId);
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
|
||||
// 传输通道打开后自动读取
|
||||
ChannelAttributeKeyUtils.buildNextChannel(visitorChannel, transferChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, visitorChannel);
|
||||
|
||||
transferChannel.writeAndFlush(myMsg);
|
||||
|
||||
} else {
|
||||
log.warn("客户端渗透服务端通信通道中断....");
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
buildTransferServer(internalNetworkClientPermeateServerVisitor, visitorChannel);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpClientFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyServerContext;
|
||||
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.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端连接服务端
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpClientSocket {
|
||||
private final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
/**
|
||||
* 服务端host
|
||||
*/
|
||||
private final String inetHost;
|
||||
/**
|
||||
* 服务端端口
|
||||
*/
|
||||
private final int inetPort;
|
||||
/**
|
||||
* 当前客户端id
|
||||
*/
|
||||
@Getter
|
||||
private final String clientId;
|
||||
|
||||
/**
|
||||
* 当前连接的服务端ID
|
||||
*/
|
||||
private final String serverId;
|
||||
private final String appKey;
|
||||
private final String appSecret;
|
||||
/**
|
||||
* 客户端状态变更事件
|
||||
*/
|
||||
@Getter
|
||||
private final ClientChangeEvent clientChangeEvent;
|
||||
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList; // 处理服务端发送过来的数据类型
|
||||
|
||||
public NettyUdpClientSocket(String inetHost,
|
||||
int inetPort,
|
||||
String clientId,
|
||||
String serverId,
|
||||
String appKey,
|
||||
String appSecret,
|
||||
ClientChangeEvent clientChangeEvent,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
this.inetHost = inetHost;
|
||||
this.inetPort = inetPort;
|
||||
this.clientId = clientId;
|
||||
this.serverId = serverId;
|
||||
this.appKey = appKey;
|
||||
this.appSecret = appSecret;
|
||||
this.clientChangeEvent = clientChangeEvent;
|
||||
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
|
||||
}
|
||||
|
||||
public void newConnect2Server() throws InterruptedException {
|
||||
newConnect2Server(inetHost, inetPort, clientId, serverId, clientChangeEvent);
|
||||
}
|
||||
|
||||
protected void newConnect2Server(String inetHost, int inetPort, String clientId, String serverId, ClientChangeEvent clientChangeEvent) throws InterruptedException {
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
// .childOption(ChannelOption.UDP_NODELAY, false)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .childOption(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 NettyUdpClientFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList), this))
|
||||
;
|
||||
log.info("use clientId:{} connect to server IP:{},server port :{}", clientId, inetHost, inetPort);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
// 客户端连接服务端的channel
|
||||
Channel serviceChannel = future.channel();
|
||||
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
if (futureListener.isSuccess()) {
|
||||
|
||||
log.info("clientId:{},connect to server IP:{},server port :{} isSuccess ", clientId, inetHost, inetPort);
|
||||
// 告诉服务端这条连接是client的连接
|
||||
NettyProxyMsg nettyMsg = new NettyProxyMsg();
|
||||
nettyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_CONNECT_SUCCESS);
|
||||
nettyMsg.setClientId(clientId);
|
||||
String hostAddress = InetAddress.getLocalHost().getHostAddress();
|
||||
nettyMsg.setOriginalIpString(hostAddress);
|
||||
nettyMsg.setData((clientId).getBytes());
|
||||
nettyMsg.setAppKeyString(appKey);
|
||||
nettyMsg.setAppSecretString(appSecret);
|
||||
ChannelAttributeKeyUtils.buildClientId(serviceChannel, clientId);
|
||||
serviceChannel.writeAndFlush(nettyMsg);
|
||||
|
||||
NettyServerContext.pushServerEndpointChannel(serverId, clientId, serviceChannel);
|
||||
// 在线 客户端注册服务端成功
|
||||
clientChangeEvent.clientOnLine(inetHost, inetPort,serverId, clientId);
|
||||
} else {
|
||||
log.warn("Reconnect every 2 seconds....");
|
||||
// 离线
|
||||
NettyServerContext.removeServerEndpointChannels(serverId, clientId);
|
||||
clientChangeEvent.clientOffLine(inetHost, inetPort,serverId, clientId);
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
newConnect2Server(inetHost, inetPort, clientId, serverId, clientChangeEvent);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭连接
|
||||
*/
|
||||
|
||||
public void shutdown() {
|
||||
if ((eventLoopGroup != null) && (!eventLoopGroup.isShutdown())) {
|
||||
eventLoopGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,217 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.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.udp.filter.NettyUdpServerPermeateClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter.NettyUdpServerPermeateClientTransferFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrationRealClient;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.UdpMessageType;
|
||||
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.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 客户端连接真实服务
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyUdpServerPermeateClientRealSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
/**
|
||||
* 连接真实服务
|
||||
*
|
||||
* @param internalNetworkPenetrationRealClient 访客信息
|
||||
* @param nettyClientProperties 服务端地址信息
|
||||
*/
|
||||
public static void buildRealServer(InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
|
||||
buildNewRealServer(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param internalNetworkPenetrationRealClient 访客信息
|
||||
* @param nettyClientProperties 服务端地址信息
|
||||
*/
|
||||
private static void buildNewRealServer(InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
try {
|
||||
String clientId = internalNetworkPenetrationRealClient.getClientId();
|
||||
String clientTargetIp = internalNetworkPenetrationRealClient.getClientTargetIp();
|
||||
Integer clientTargetPort = internalNetworkPenetrationRealClient.getClientTargetPort();
|
||||
Integer visitorPort = internalNetworkPenetrationRealClient.getVisitorPort();
|
||||
String visitorId = internalNetworkPenetrationRealClient.getVisitorId();
|
||||
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.UDP_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 NettyUdpServerPermeateClientRealFilter())
|
||||
|
||||
;
|
||||
|
||||
|
||||
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("访客通过 客户端:【{}】,绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, clientTargetIp, clientTargetPort);
|
||||
// 客户端真实通道
|
||||
// NettyRealIdContext.pushReal(realChannel, visitorId);
|
||||
// 绑定访客ID到当前真实通道属性
|
||||
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
|
||||
ChannelAttributeKeyUtils.buildVisitorPort(realChannel, visitorPort);
|
||||
// 通知服务端访客连接成功
|
||||
|
||||
|
||||
// 新建一个通道处理
|
||||
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList,realChannel);
|
||||
|
||||
// 是否等 服务端相应访客通道已经可以自动读写
|
||||
// realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
// 模拟发送
|
||||
String byteData = "GET /swagger-ui/index.html HTTP/1.1\n" +
|
||||
"Host: 127.0.0.1:19080\n" +
|
||||
"Connection: keep-alive\n" +
|
||||
"Cache-Control: max-age=0\n" +
|
||||
"sec-ch-ua: \"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"\n" +
|
||||
"sec-ch-ua-mobile: ?0\n" +
|
||||
"sec-ch-ua-platform: \"macOS\"\n" +
|
||||
"Upgrade-Insecure-Requests: 1\n" +
|
||||
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\n" +
|
||||
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\n" +
|
||||
"Sec-Fetch-Site: none\n" +
|
||||
"Sec-Fetch-Mode: navigate\n" +
|
||||
"Sec-Fetch-User: ?1\n" +
|
||||
"Sec-Fetch-Dest: document\n" +
|
||||
"Accept-Encoding: gzip, deflate, br\n" +
|
||||
"Accept-Language: zh-CN,zh;q=0.9\n" +
|
||||
"Cookie: XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d; Hm_lvt_173e771eef816c412396d2cb4fe2d632=1703040917\n";
|
||||
// ChannelContext.ClientChannel clientChannel = ChannelContext.get(String.valueOf(visitorPort).getBytes(StandardCharsets.UTF_8));
|
||||
// Channel channel = clientChannel.getChannel();
|
||||
// channel.writeAndFlush(byteData.getBytes(StandardCharsets.UTF_8));
|
||||
// future.channel().attr(Constant.VID).set(internalNetworkPenetrationRealClient);
|
||||
// Constant.vrc.put(internalNetworkPenetrationRealClient, future.channel());
|
||||
// ProxySocket.connectProxyServer(internalNetworkPenetrationRealClient);
|
||||
} else {
|
||||
log.error("客户:【{}】,无法连接当前网络内的目标IP:【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建访客连接服务端
|
||||
*
|
||||
* @param internalNetworkPenetrationRealClient 内网穿透信息
|
||||
* @param nettyClientProperties 服务端配置信息
|
||||
* @param handleChannelTypeAdvancedList 处理器适配器
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
protected static void newVisitorConnect2Server(InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList,
|
||||
Channel realChannel) 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.UDP_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 NettyUdpServerPermeateClientTransferFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
|
||||
;
|
||||
|
||||
String inetHost = nettyClientProperties.getInetHost();
|
||||
int inetPort = nettyClientProperties.getInetPort();
|
||||
// local client id
|
||||
|
||||
String clientId = nettyClientProperties.getClientId();
|
||||
|
||||
|
||||
String visitorId = internalNetworkPenetrationRealClient.getVisitorId();
|
||||
Integer visitorPort = internalNetworkPenetrationRealClient.getVisitorPort();
|
||||
String clientTargetIp = internalNetworkPenetrationRealClient.getClientTargetIp();
|
||||
Integer clientTargetPort = internalNetworkPenetrationRealClient.getClientTargetPort();
|
||||
String visitorClientId = internalNetworkPenetrationRealClient.getClientId();
|
||||
|
||||
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
|
||||
log.info("Client creates a new visitor channel to connect to server IP: {}, connecting to server port: {}", inetHost, inetPort);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
|
||||
// 使用的客户端ID:{}
|
||||
log.info("Client ID used: {}" , visitorClientId);
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
|
||||
NettyProxyMsg myMsg = new NettyProxyMsg();
|
||||
myMsg.setType(UdpMessageType.UDP_REPORT_SINGLE_CLIENT_REAL_CONNECT);
|
||||
myMsg.setClientId(visitorClientId);
|
||||
myMsg.setVisitorPort(visitorPort);
|
||||
myMsg.setClientTargetIp(clientTargetIp);
|
||||
myMsg.setClientTargetPort(clientTargetPort);
|
||||
|
||||
myMsg.setVisitorId(visitorId);
|
||||
transferChannel.writeAndFlush(myMsg);
|
||||
// 绑定客户端真实通信通道
|
||||
NettyCommunicationIdContext.pushVisitor(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, visitorClientId);
|
||||
// 客户端真实通道自动读写打开
|
||||
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
|
||||
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
log.info("每隔2s重连....");
|
||||
// 离线
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList,realChannel);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.framework.lazy.cloud.network.heartbeat.client.EnableHeartbeatClientAutoConfiguration,\
|
||||
org.framework.lazy.cloud.network.heartbeat.client.EnableClientAutoConfiguration,\
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties,\
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.InitConfig,\
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.ClientAutoConfiguration
|
||||
|
@ -1,4 +1,3 @@
|
||||
org.framework.lazy.cloud.network.heartbeat.client.EnableHeartbeatClientAutoConfiguration
|
||||
org.framework.lazy.cloud.network.heartbeat.client.EnableClientAutoConfiguration
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.InitConfig
|
||||
org.framework.lazy.cloud.network.heartbeat.client.config.ClientAutoConfiguration
|
||||
|
@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>wu-lazy-cloud-heartbeat-common</artifactId>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.common;
|
||||
|
||||
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.tcp.AbstractTcpHandleChannelHeartbeatTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.client.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.udp.server.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
|
||||
@ -14,6 +15,15 @@ import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
|
||||
* UDP: 64~128
|
||||
*/
|
||||
public class UdpMessageType {
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
*
|
||||
* @see MessageTypeEnums#TCP_TYPE_HEARTBEAT
|
||||
* @see AbstractTcpHandleChannelHeartbeatTypeAdvanced
|
||||
*/
|
||||
public static final byte UDP_TYPE_HEARTBEAT = 0X00;
|
||||
|
||||
/**
|
||||
* 客户端UDP 上报连接成功
|
||||
*
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-network</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>wu-lazy-cloud-heartbeat-start</artifactId>
|
||||
|
@ -9,7 +9,7 @@ mvn -Pnative -DskipTests clean package native:compile
|
||||
```shell
|
||||
mvn spring-boot:build-image -Pnative
|
||||
|
||||
docker tag docker.io/library/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
docker tag docker.io/library/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
|
||||
@ -24,8 +24,8 @@ docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-cl
|
||||
|
||||
mvn clean install
|
||||
|
||||
docker build -t registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT .
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker build -t registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT .
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT
|
||||
|
||||
```
|
||||
|
||||
@ -33,7 +33,7 @@ docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-cl
|
||||
### run
|
||||
```shell
|
||||
|
||||
docker run -d -it --privileged --name client -p 6004:6004 registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it --privileged --name client -p 6004:6004 registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT
|
||||
|
||||
```
|
||||
|
||||
@ -73,7 +73,7 @@ kubeadm join apiserver.demo:6443 --token 2wtcsg.0af26p9wzfgvyf5a --discovery
|
||||
```
|
||||
|
||||
```RUN
|
||||
docker run -d -it --name client registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker run -d -it --name client registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-client-start:1.3.0-JDK17-SNAPSHOT
|
||||
|
||||
http://127.0.0.1:18080
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-heartbeat-start</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-heartbeat-start</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -11,7 +11,7 @@ mvn -Pnative -DskipTests clean package native:compile
|
||||
```shell
|
||||
mvn spring-boot:build-image -Pnative
|
||||
|
||||
docker tag docker.io/library/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-SNAPSHOT registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
docker tag docker.io/library/wu-lazy-cloud-heartbeat-server-start:1.3.0-JDK17-SNAPSHOT registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-NATIVE-SNAPSHOT
|
||||
|
||||
@ -23,8 +23,8 @@ docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-se
|
||||
|
||||
mvn clean install
|
||||
|
||||
docker build -t registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-SNAPSHOT .
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.2.9-JDK17-SNAPSHOT
|
||||
docker build -t registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.3.0-JDK17-SNAPSHOT .
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/wu-lazy/wu-lazy-cloud-heartbeat-server-start:1.3.0-JDK17-SNAPSHOT
|
||||
|
||||
```
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>top.wu2020</groupId>
|
||||
<artifactId>wu-lazy-cloud-heartbeat-start</artifactId>
|
||||
<version>1.2.9-JDK17-SNAPSHOT</version>
|
||||
<version>1.3.0-JDK17-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
Loading…
x
Reference in New Issue
Block a user