1 Commits

Author SHA1 Message Date
wujiawei
04cb5d1923 【fix】1.2.6-JDK17 2025-05-26 10:59:42 +08:00
660 changed files with 2526 additions and 24008 deletions

View File

@@ -1,25 +0,0 @@
### 背景:国庆期间的问题,如何在老家访问杭州办公室的网络呢
#### 实现方案:异地组网
#### 实现语言Java
#### 环境三个网络一台拥有公网IP的服务器、一台杭州本地机房内服务器、你老家所在网络中的一台电脑用你自己的就好了
#### 实现原理:![NetworkPermeateClientPermeateClient.png](NetworkPermeateClientPermeateClient.png)
#### 操作步骤拥有公网ip的服务器开发6001web、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
```
#### 操作步骤:杭州本地机房所在网络服务器启动客户端、你老家所在网络中启动客户端,命令如下
```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
```
```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
```
#### 操作步骤:配置端口
![client_permeate_port_pool.png](client_permeate_port_pool.png)
#### 操作步骤打开页面配置菜单看到如下界面from客户端ID你老家网络中的电脑to客户端ID
![client_permeate_client_mapping.png](client_permeate_client_mapping.png)
#### 连接使用在你老家使用my-home这个客户端所在机器上的ip+13306 即可访问杭州机房内的数据库的服务3306

View File

@@ -1,140 +0,0 @@
@startuml
title 服务端渗透客户端
actor 访客 as User
package "服务端(公网)"{
component [服务端开放端口]{
[默认UI页面端口:6001]
[默认tcp端口:7001] as tcp
[开放给客户端访问的端口:13306]
}
}
package "客户端(私有网络)"{
component [客户端端口]{
[默认UI页面端口:6004]
}
database "客户端所在网络中的mysql:3306" as target {
}
}
[User] ...right...> [开放给客户端访问的端口:13306]: 发送请求到 http://ip:13306
[开放给客户端访问的端口:13306] ...down...> [target]: 发送真实二进制请求到真实服务
note "无法直接访问" as N2
(User) .... N2
N2 ... (target)
@enduml
@startuml
title 服务端渗透服务端
actor 访客 as User
package "服务端(局域网)"{
component [服务端开放端口]{
[默认UI页面端口:6001]
[默认tcp端口:7001] as tcp
[开放给服务端访问的端口:13306]
}
database "服务端所在网络中的mysql:3306" as target {
}
}
[User] ...right...> [开放给服务端访问的端口:13306]: 发送请求到 http://ip:13306
[开放给服务端访问的端口:13306] ...down...> [target]: 发送真实二进制请求到真实服务
note "无法直接访问" as N2
(User) .. N2
N2 .. (target)
@enduml
@startuml
title 客户端渗透服务端
actor 访客 as User
package "服务端(公网)"{
component [服务端开放端口]{
[默认UI页面端口:6001]
[默认tcp端口:7001] as tcp
}
database "服务端所在网络中的mysql:3306" as target {
}
}
package "客户端(私有网络)"{
component [客户端端口]{
[默认UI页面端口:6004]
[客户端开放端口:13306]
}
}
[User] ...right...> [客户端开放端口:13306]: 发送请求到 http://ip:13306
[客户端开放端口:13306] ...up...> [target]: 发送真实二进制请求到真实服务
note "无法直接访问" as N2
(User) ...up... N2
N2 ...up.. (target)
@enduml
@startuml
title 客户端渗透客户端
actor 访客杭州 as User
package "服务端(公网)" as server{
component [服务端开放端口]{
[默认UI页面端口:6001]
[默认tcp端口:7001] as tcp
}
}
package "客户端(私有网络--杭州)" as client_hangzhou{
component [客户端端口]{
[默认UI页面端口:6004]
[客户端开放端口:13306]
}
}
package "客户端(私有网络--上海)" as client_shanghai{
component [上海客户端端口]{
[上海默认UI页面端口:6004]
}
database "服务端所在网络中的mysql:3306" as target {
}
}
server ...down...> client_hangzhou
server <...right... client_shanghai
[User] ...right...> [客户端开放端口:13306]: 发送请求到 http://ip:13306
[客户端开放端口:13306] ...up...> [target]: 发送真实二进制请求到真实服务
note "无法直接访问" as N2
(User) ...up... N2
N2 ...up.. (target)
@enduml

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

View File

@@ -33,19 +33,8 @@
wu-lazy-cloud-network
是一款基于([wu-framework-parent](https://gitee.com/wujiawei1207537021/wu-framework-parent)孵化出的项目内部使用Lazy
ORM操作数据库使用环境JDK17 Spring Boot 3.0.2。
主要功能:
- 服务端渗透客户端网络穿透对于没有公网IP的服务进行公网IP映射
- ![NetworkPermeateServerPermeateClient.png](NetworkPermeateServerPermeateClient.png)
- 服务端渗透服务端----本地同局域网内端口映射
- ![NetworkPermeateServerPermeateServer.png](NetworkPermeateServerPermeateServer.png)
- 客户端渗透服务端----本地端口映射到另一个服务端中的局域网端口
- ![NetworkPermeateClientPermeateServer.png](NetworkPermeateClientPermeateServer.png)
- 客户端渗透客户端----本地端口映射到另一个局域网端口
- ![NetworkPermeateClientPermeateClient.png](NetworkPermeateClientPermeateClient.png)
[UI](https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network-server-ui)
ORM操作数据库主要功能是网络穿透对于没有公网IP的服务进行公网IP映射
使用环境JDK17 Spring Boot 3.0.2
#### 项目地址
@@ -208,7 +197,7 @@ public class NettyClientSocket {
log.info("连接服务端成功");
// 告诉服务端这条连接是client的连接
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.TCP_REPORT_CLIENT_CONNECT_SUCCESS);
nettyMsg.setType(MessageType.REPORT_CLIENT_CONNECT_SUCCESS);
nettyMsg.setClientId(clientId);
nettyMsg.setData((clientId).getBytes());
ChannelAttributeKeyUtils.buildClientId(channel, clientId);
@@ -258,23 +247,24 @@ 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.2.6-JDK17-SNAPSHOT | 内网穿透公共模块(声明接口、枚举、常量、适配器、解析器) |
| [wu-lazy-cloud-heartbeat-client](wu-lazy-cloud-heartbeat-client) | 1.2.6-JDK17-SNAPSHOT | 客户端(支持二次开发) |
| [wu-lazy-cloud-heartbeat-server](wu-lazy-cloud-heartbeat-server) | 1.2.6-JDK17-SNAPSHOT | 服务端(支持二次开发) |
| [wu-lazy-cloud-network-ui](wu-lazy-cloud-heartbeat-server-ui) | 1.2.6-JDK17-SNAPSHOT | 服务端页面 |
| [wu-lazy-cloud-heartbeat-client-start](wu-lazy-cloud-heartbeat-sample/wu-lazy-cloud-heartbeat-client-sample) | 1.2.6-JDK17-SNAPSHOT | 客户端样例 |
| [wu-lazy-cloud-heartbeat-server-start](wu-lazy-cloud-heartbeat-sample/wu-lazy-cloud-heartbeat-server-sample) | 1.2.6-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.2.6-JDK17-SNAPSHOT | web容器 |
| [Lazy -ORM](https://gitee.com/wujiawei1207537021/wu-framework-parent/tree/master/wu-inner-intergration/wu-database-parent) | 1.2.6-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.2.6-JDK17-SNAPSHOT | 用户授权体系 |
### 使用环境
@@ -287,7 +277,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.2.6-JDK17-SNAPSHOT
http://127.0.0.1:18080/swagger-ui/index.html
@@ -296,15 +286,15 @@ public class NettyClientSocket {
#### 页面操作
启动项目后打开服务端界面
![url_info.png](url_info.png)
![img.png](url_info.png)
默认账号密码admin/admin
![login.png](login.png)
![img.png](login.png)
初始化项目
![init_menu.png](init_menu.png)
![img.png](init_menu.png)
添加角色
![init_role.png](init_role.png)
![img.png](init_role.png)
为用户授权
![img.png](authRoe2User.png)
@@ -314,26 +304,12 @@ public class NettyClientSocket {
客户端管理(客户端会自动注册)
![img.png](cloud_client.png)
## 服务端渗透
- 服务端口池管理(服务器端需要开放的端口)
![server_permeate_port_pool.png](server_permeate_port_pool.png)
网络映射管理(修改后者新增需要映射的客户端)
![img.png](mapping.png)
- 服务端渗透客户端(内网穿透)(修改后者新增需要映射的客户端
![server_permeate_client_mapping.png](server_permeate_client_mapping.png)
访客端口池管理(服务器端需要开放的端口
![img.png](visitor_port.png)
- 服务端渗透服务端
![server_permeate_server_mapping.png](server_permeate_server_mapping.png)
## 客户端渗透
- 客户端渗透端口池管理
![client_permeate_port_pool.png](client_permeate_port_pool.png)
- 客户端渗透客户端
![client_permeate_client_mapping.png](client_permeate_client_mapping.png)
- 客户端渗透服务端
![client_permeate_server_mapping.png](client_permeate_server_mapping.png)
## 报表
流量管理(每个客户端使用的流量)
![img.png](flow.png)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 86 KiB

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 124 KiB

BIN
mapping.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

128
pom.xml
View File

@@ -8,12 +8,12 @@
<parent>
<artifactId>wu-framework-parent</artifactId>
<groupId>top.wu2020</groupId>
<version>1.2.9-JDK17-SNAPSHOT</version>
<version>1.2.6-JDK17</version>
</parent>
<artifactId>wu-lazy-cloud-network</artifactId>
<packaging>pom</packaging>
<version>1.2.9-JDK17-SNAPSHOT</version>
<version>1.2.6-JDK17</version>
<description>云上云下</description>
@@ -70,12 +70,19 @@
<dependency>
<groupId>top.wu2020</groupId>
<artifactId>wu-framework-dependencies</artifactId>
<version>1.2.9-JDK17-SNAPSHOT</version>
<version>1.2.6-JDK17</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<profiles>
@@ -175,96 +182,55 @@
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Spring-Boot-Native-Processed>true</Spring-Boot-Native-Processed>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-tiny:latest</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<requiredVersion>22.3</requiredVersion>
</configuration>
<executions>
<execution>
<id>add-reachability-metadata</id>
<goals>
<goal>add-reachability-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>nativeTest</id>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-test-aot</id>
<goals>
<goal>process-test-aot</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.28</version>
<!-- 使用graalvm提供的可达性元数据很多第三方库就直接可以构建成可执行文件了 -->
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<requiredVersion>22.3</requiredVersion>
<!-- for agent -->
<agent>
<defaultMode>Standard</defaultMode>
<options>
<builtinCallerFilter>true</builtinCallerFilter>
<builtinHeuristicFilter>true</builtinHeuristicFilter>
<enableExperimentalPredefinedClasses>true
</enableExperimentalPredefinedClasses>
<enableExperimentalUnsafeAllocationTracing>true
</enableExperimentalUnsafeAllocationTracing>
<trackReflectionMetadata>true</trackReflectionMetadata>
</options>
<metadataCopy>
<merge>true</merge>
</metadataCopy>
</agent>
<!-- for metadata repository -->
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
</configuration>
<executions>
<execution>
<id>native-test</id>
<id>add-reachability-metadata</id>
<goals>
<goal>add-reachability-metadata</goal>
</goals>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 398 KiB

After

Width:  |  Height:  |  Size: 428 KiB

View File

@@ -8,26 +8,4 @@
[fix] 客户端添加按钮删除
[fix] 修改浏览器title内网穿透
[fix] HandleChannelTypeAdvanced 添加权重 order 越小越靠前
[fix] 新增网络映射新增、修改、删除、自动变更
#### 1.2.7-JDK17-SNAPSHOT
[fix] 修复网络流量统计同步线程导致io阻塞问题
[fix] 修改默认读缓冲区大小2M写缓冲区大小1M
[fix] 操作系统默认读写缓冲区大小212992cat /proc/sys/net/core/wmem_max 、cat /proc/sys/net/core/rmem_max、cat /proc/sys/net/core/wmem_default、 cat /proc/sys/net/core/rmem_default ),通过如下命令进行修改
sudo sysctl -w net.core.rmem_default=4194304
sudo sysctl -w net.core.rmem_max=4194304
sudo sysctl -w net.core.wmem_default=4194304
sudo sysctl -w net.core.wmem_max=4194304
[fix] 修复下线客户端、删除映射无法刷新问题
#### 1.2.8-JDK17-SNAPSHOT
[change] 原《内网穿透》更改为服务端渗透客户端
[change] 新增服务端渗透服务端----本地同局域网内端口映射
[change] 新增客户端渗透服务端----本地端口映射到另一个服务端中的局域网端口
[change] 新增客户端渗透客户端----本地端口映射到另一个局域网端口
#### 1.2.9-JDK17-SNAPSHOT
[change] 添加appkey&appsecret 验证
[change] 支持同一个客户端ID多次注册
[change] 修复通道关闭导致调度线程池submit异常问题
[change] 添加记录客户端IP
[change] 1.2.9为大版本,报文中添加数据无法向下兼容,建议服务端与客户端保持版本一致
#### 下一版本计划https
[fix] 新增网络映射新增、修改、删除、自动变更

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>top.wu2020</groupId>
<artifactId>wu-lazy-cloud-network</artifactId>
<version>1.2.9-JDK17-SNAPSHOT</version>
<version>1.2.6-JDK17</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -44,6 +44,13 @@
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</project>

View File

@@ -66,17 +66,7 @@ public class LazyNettyServerPropertiesQueryListCommand {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -65,17 +65,7 @@ public class LazyNettyServerPropertiesQueryOneCommand {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -66,17 +66,7 @@ public class LazyNettyServerPropertiesRemoveCommand {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -65,17 +65,7 @@ public class LazyNettyServerPropertiesStoryCommand {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -65,17 +65,7 @@ public class LazyNettyServerPropertiesUpdateCommand {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -65,17 +65,7 @@ public class LazyNettyServerPropertiesDTO {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -186,12 +186,10 @@ public class LazyNettyServerPropertiesApplicationImpl implements LazyNettyServer
String inetHost = lazyNettyServerProperties.getInetHost();
Integer inetPort = lazyNettyServerProperties.getInetPort();
String clientId = lazyNettyServerProperties.getClientId();
String appKey = lazyNettyServerProperties.getAppKey();
String appSecret = lazyNettyServerProperties.getAppSecret();
NettyClientSocket nettyClientSocket = new
NettyClientSocket(inetHost, inetPort, clientId,
NormalUsedString.DEFAULT,appKey,appSecret,
NormalUsedString.DEFAULT,
clientChangeEvent, handleChannelTypeAdvancedList);
cacheNettyClientSocketMap.put(lazyNettyServerProperties, nettyClientSocket);

View File

@@ -48,9 +48,7 @@ public class ClientAutoConfiguration implements CommandLineRunner {
String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort();
String clientId = nettyClientProperties.getClientId();
String appKey = nettyClientProperties.getAppKey();
String appSecret = nettyClientProperties.getAppSecret();
return new NettyClientSocket(inetHost, inetPort, clientId, NormalUsedString.DEFAULT, appKey,appSecret,clientChangeEvent, handleChannelTypeAdvancedList);
return new NettyClientSocket(inetHost, inetPort, clientId, NormalUsedString.DEFAULT, clientChangeEvent, handleChannelTypeAdvancedList);
}
/**
@@ -64,11 +62,9 @@ public class ClientAutoConfiguration implements CommandLineRunner {
String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort();
String clientId = nettyClientProperties.getClientId();
String appKey = nettyClientProperties.getAppKey();
String appSecret = nettyClientProperties.getAppSecret();
NettyClientSocket nettyClientSocket = new NettyClientSocket(
inetHost, inetPort,
clientId, NormalUsedString.DEFAULT,appKey,appSecret,
clientId, NormalUsedString.DEFAULT,
clientChangeEvent, handleChannelTypeAdvancedList);
Thread thread = new Thread(() -> {
try {

View File

@@ -2,12 +2,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.config;
import org.framework.lazy.cloud.network.heartbeat.client.netty.advanced.*;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.client.netty.advanced.*;
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.Role;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.util.List;
@@ -18,148 +19,72 @@ public class HeartbeatClientConfiguration {
/**
* 服务端 处理客户端心跳
*
* @return ClientTcpHandleChannelHeartbeatTypeAdvanced
* @return ClientHandleChannelHeartbeatTypeAdvanced
*/
@Bean
public ClientTcpHandleChannelHeartbeatTypeAdvanced clientChannelHeartbeatTypeAdvanced() {
return new ClientTcpHandleChannelHeartbeatTypeAdvanced();
public ClientHandleChannelHeartbeatTypeAdvanced clientChannelHeartbeatTypeAdvanced() {
return new ClientHandleChannelHeartbeatTypeAdvanced();
}
/**
* 处理 客户端代理的真实端口自动读写
*
* @return ClientHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced
* @return ClientHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced handleDistributeSingleClientRealAutoReadConnectTypeAdvanced() {
return new ClientHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced();
public ClientHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced handleDistributeSingleClientRealAutoReadConnectTypeAdvanced() {
return new ClientHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced();
}
/**
* 处理 接收服务端发送过来的聊天信息
*
* @return ClientHandleTcpDistributeSingleClientMessageTypeAdvanced
* @return ClientHandleDistributeSingleClientMessageTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeSingleClientMessageTypeAdvanced handleDistributeSingleClientMessageTypeAdvanced() {
return new ClientHandleTcpDistributeSingleClientMessageTypeAdvanced();
}
/**
* 处理 客户端渗透服务端数据传输通道连接成功
*
* @return ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced clientHandleDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced() {
return new ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced();
}
/**
* 处理 客户端渗透客户端数据传输通道连接成功
*
* @return ClientHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced clientHandleDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced() {
return new ClientHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced();
public ClientHandleDistributeSingleClientMessageTypeAdvanced handleDistributeSingleClientMessageTypeAdvanced() {
return new ClientHandleDistributeSingleClientMessageTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced clientHandleDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced() {
return new ClientHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced();
public ClientHandleDistributeSingleClientRealCloseVisitorTypeAdvanced handleDistributeSingleClientRealCloseVisitorTypeAdvanced() {
return new ClientHandleDistributeSingleClientRealCloseVisitorTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientTransferClientRequestTypeAdvanced clientHandleDistributeClientTransferClientTypeAdvanced() {
return new ClientHandleTcpDistributeClientTransferClientRequestTypeAdvanced();
public ClientReportHandleChannelTransferTypeAdvancedHandleDistribute handleChannelTransferTypeAdvancedHandleDistribute(NettyClientProperties nettyClientProperties) {
return new ClientReportHandleChannelTransferTypeAdvancedHandleDistribute(nettyClientProperties);
}
@Bean
public ClientHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced clientHandleDistributeClientTransferClientResponseTypeAdvanced() {
return new ClientHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced handleDistributeSingleClientRealCloseVisitorTypeAdvanced() {
return new ClientHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced();
public HandleDistributeConnectSuccessNotificationTypeAdvancedHandle handleDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
return new HandleDistributeConnectSuccessNotificationTypeAdvancedHandle(clientChangeEvent);
}
@Bean
public ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribute handleChannelTransferTypeAdvancedHandleDistribute(NettyClientProperties nettyClientProperties) {
return new ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribute(nettyClientProperties);
public HandleClientChannelActiveAdvanced handleClientChannelActiveAdvanced(NettyClientProperties nettyClientProperties) {
return new HandleClientChannelActiveAdvanced(nettyClientProperties);
}
@Bean
public HandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle handleDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
return new HandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle(clientChangeEvent);
public HandleDistributeDisconnectTypeAdvancedHandle handleDistributeDisconnectTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
return new HandleDistributeDisconnectTypeAdvancedHandle(clientChangeEvent);
}
@Bean
public HandleTcpClientChannelActiveAdvanced handleClientChannelActiveAdvanced(NettyClientProperties nettyClientProperties) {
return new HandleTcpClientChannelActiveAdvanced(nettyClientProperties);
public HandleDistributeStagingClosedTypeAdvanced handleDistributeStagingClosedTypeAdvanced() {
return new HandleDistributeStagingClosedTypeAdvanced();
}
@Bean
public HandleTcpDistributeDisconnectTypeAdvancedHandle handleDistributeDisconnectTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
return new HandleTcpDistributeDisconnectTypeAdvancedHandle(clientChangeEvent);
}
@Bean
public HandleTcpDistributeStagingClosedTypeAdvanced handleDistributeStagingClosedTypeAdvanced() {
return new HandleTcpDistributeStagingClosedTypeAdvanced();
}
@Bean
public HandleTcpDistributeStagingOpenedTypeAdvanced handleDistributeStagingOpenedTypeAdvanced() {
return new HandleTcpDistributeStagingOpenedTypeAdvanced();
}
/**
* 处理 客户端渗透服务端init信息
*
* @return ClientHandleTcpDistributeClientPermeateServerInitTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeClientPermeateServerInitTypeAdvanced clientHandleDistributeClientPermeateServerInitTypeAdvanced(NettyClientProperties nettyClientProperties) {
return new ClientHandleTcpDistributeClientPermeateServerInitTypeAdvanced(nettyClientProperties);
}
/**
* 处理 客户端渗透服务端init close 信息
*
* @return ClientHandleTcpDistributeClientPermeateServerCloseTypeAdvanced
*/
@Bean
public ClientHandleTcpDistributeClientPermeateServerCloseTypeAdvanced clientHandleDistributeClientPermeateServerCloseTypeAdvanced( ) {
return new ClientHandleTcpDistributeClientPermeateServerCloseTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientPermeateServerTransferTypeAdvanced clientHandleDistributeClientPermeateServerTransferTypeAdvanced( ) {
return new ClientHandleTcpDistributeClientPermeateServerTransferTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientPermeateClientCloseTypeAdvanced clientHandleDistributeClientPermeateClientCloseTypeAdvanced() {
return new ClientHandleTcpDistributeClientPermeateClientCloseTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientPermeateClientInitTypeAdvanced clientHandleDistributeClientPermeateClientInitTypeAdvanced() {
return new ClientHandleTcpDistributeClientPermeateClientInitTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced clientHandleDistributeClientPermeateClientTransferCloseTypeAdvanced() {
return new ClientHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced();
public HandleDistributeStagingOpenedTypeAdvanced handleDistributeStagingOpenedTypeAdvanced() {
return new HandleDistributeStagingOpenedTypeAdvanced();
}
@Bean
public ClientHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced clientHandleDistributeSingleClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
return new ClientHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced(nettyClientProperties, handleChannelTypeAdvancedList);
}
@Bean
public ClientHandleHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced clientHandleHandleDistributeClientPermeateServerTransferCloseTypeAdvanced() {
return new ClientHandleHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced();
public ClientHandleDistributeSingleClientRealConnectTypeAdvanced clientHandleDistributeSingleClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
return new ClientHandleDistributeSingleClientRealConnectTypeAdvanced(nettyClientProperties, handleChannelTypeAdvancedList);
}
}

View File

@@ -54,8 +54,6 @@ public class InitConfig implements CommandLineRunner, DisposableBean {
String clientId = nettyClientProperties.getClientId();
String inetHost = nettyClientProperties.getInetHost();
int inetPort = nettyClientProperties.getInetPort();
String appKey = nettyClientProperties.getAppKey();
String appSecret = nettyClientProperties.getAppSecret();
if (Objects.isNull(clientId) ||
Objects.isNull(inetHost)) {
log.warn("配置信息为空,请通过页面添加配置信息:{}", nettyClientProperties);
@@ -67,9 +65,6 @@ public class InitConfig implements CommandLineRunner, DisposableBean {
lazyNettyServerPropertiesDO.setInetPort(inetPort);
lazyNettyServerPropertiesDO.setType(PropertiesType.CONFIG);
lazyNettyServerPropertiesDO.setIsDeleted(false);
lazyNettyServerPropertiesDO.setAppKey(appKey);
lazyNettyServerPropertiesDO.setAppSecret(appSecret);
// 根据服务端端口、port 唯一性验证
boolean exists = lazyLambdaStream.exists(LazyWrappers.<LazyNettyServerPropertiesDO>lambdaWrapper()
.eq(LazyNettyServerPropertiesDO::getInetHost, inetHost)

View File

@@ -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;
@@ -30,19 +29,6 @@ public class NettyClientProperties {
*/
private String clientId;
/**
*
* 令牌key
*/
private String appKey;
/**
*
* 令牌密钥
*/
private String appSecret;
/**
* 是否开启 默认是
*/

View File

@@ -5,7 +5,6 @@ import lombok.experimental.Accessors;
import io.swagger.v3.oas.annotations.media.Schema;
import org.framework.lazy.cloud.network.heartbeat.common.enums.NettyClientStatus;
import org.framework.lazy.cloud.network.heartbeat.client.config.PropertiesType;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableField;
import java.lang.String;
import java.time.LocalDateTime;
@@ -66,17 +65,7 @@ public class LazyNettyServerProperties {
*/
@Schema(description ="类型配置、DB",name ="type",example = "")
private PropertiesType type;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
private String appSecret;
/**
*
* 更新时间

View File

@@ -1,15 +1,17 @@
package org.framework.lazy.cloud.network.heartbeat.client.infrastructure.entity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import org.framework.lazy.cloud.network.heartbeat.client.config.PropertiesType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.NettyClientStatus;
import org.framework.lazy.cloud.network.heartbeat.client.config.PropertiesType;
import org.wu.framework.lazy.orm.core.stereotype.LazyTable;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableField;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableFieldUnique;
import org.wu.framework.lazy.orm.core.stereotype.*;
import io.swagger.v3.oas.annotations.media.Schema;
import java.lang.String;
import java.time.LocalDateTime;
import java.lang.Integer;
/**
* describe 服务端配置信息
*
@@ -73,20 +75,6 @@ public class LazyNettyServerPropertiesDO {
@LazyTableField(name="is_deleted",comment="是否删除")
private Boolean isDeleted;
/**
* 令牌key
*/
@Schema(description = "令牌key", name = "appKey", example = "")
@LazyTableField(name = "app_key", comment = "令牌key")
private String appKey;
/**
* 令牌密钥
*/
@Schema(description = "令牌密钥", name = "appSecret", example = "")
@LazyTableField(name = "app_secret", comment = "令牌密钥")
private String appSecret;
/**
*
* 类型配置、DB

View File

@@ -1,64 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPermeate;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.util.List;
@NoArgsConstructor
@Data
public class InternalNetworkClientPermeateClientVisitor implements InternalNetworkPermeate {
/**
* 当前客户端ID
*/
private String fromClientId;
/**
* 目标客户端ID
*/
private String toClientId;
/**
* 目标地址
*/
private String targetIp;
/**
* 目标端口
*/
private Integer targetPort;
/**
* 访问端口
*/
private Integer visitorPort;
/**
* 流量适配器
*/
private ChannelFlowAdapter channelFlowAdapter;
/**
* 服务端地址信息
*/
private NettyClientProperties nettyClientProperties;
/**
* 通道处理器
*/
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
/**
* 访客ID
*/
private String visitorId;
/**
* 是否是ssl
*/
private boolean isSsl;
}

View File

@@ -1,46 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPermeate;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import java.util.List;
@NoArgsConstructor
@Data
public class InternalNetworkClientPermeateServerVisitor implements InternalNetworkPermeate {
/**
* 目标地址
*/
private String targetIp;
/**
* 目标端口
*/
private Integer targetPort;
/**
* 访问端口
*/
private Integer visitorPort;
/**
* 服务端地址信息
*/
private NettyClientProperties nettyClientProperties;
/**
* 通道处理器
*/
private List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
/**
* 是否是ssl
*/
private boolean isSsl;
}

View File

@@ -4,14 +4,14 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.advanced;
import io.netty.channel.Channel;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.tcp.AbstractTcpHandleChannelHeartbeatTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelHeartbeatTypeAdvanced;
/**
* 服务端 处理客户端心跳
* TCP_TYPE_HEARTBEAT
* TYPE_HEARTBEAT
*/
public class ClientTcpHandleChannelHeartbeatTypeAdvanced extends AbstractTcpHandleChannelHeartbeatTypeAdvanced<NettyProxyMsg> {
public class ClientHandleChannelHeartbeatTypeAdvanced extends AbstractHandleChannelHeartbeatTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
@@ -22,7 +22,7 @@ public class ClientTcpHandleChannelHeartbeatTypeAdvanced extends AbstractTcpHand
@Override
public void doHandler(Channel channel, NettyProxyMsg msg) {
NettyProxyMsg hb = new NettyProxyMsg();
hb.setType(MessageType.TCP_TYPE_HEARTBEAT);
hb.setType(MessageType.TYPE_HEARTBEAT);
// channel.writeAndFlush(hb);
}

View File

@@ -3,13 +3,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeSingleClientMessageTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeSingleClientMessageTypeAdvanced;
/**
* 接收服务端发送过来的聊天信息
*/
@Slf4j
public class ClientHandleTcpDistributeSingleClientMessageTypeAdvanced extends AbstractHandleTcpDistributeSingleClientMessageTypeAdvanced<NettyProxyMsg> {
public class ClientHandleDistributeSingleClientMessageTypeAdvanced extends AbstractHandleDistributeSingleClientMessageTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*

View File

@@ -5,11 +5,10 @@ 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.tcp.client.AbstractHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced;
@Slf4j
public class ClientHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced extends AbstractHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced<NettyProxyMsg> {
public class ClientHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced extends AbstractHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
@@ -22,9 +21,8 @@ public class ClientHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvance
byte[] visitorId = nettyProxyMsg.getVisitorId();
// 获取访客对应的真实代理通道
Channel realChannel = NettyRealIdContext.getReal(visitorId);
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel != null) {
nextChannel.config().setOption(ChannelOption.AUTO_READ, true);
if (realChannel != null) {
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
}
}

View File

@@ -5,10 +5,10 @@ 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.tcp.client.AbstractHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeSingleClientRealCloseVisitorTypeAdvanced;
@Slf4j
public class ClientHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced extends AbstractHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced<NettyProxyMsg> {
public class ClientHandleDistributeSingleClientRealCloseVisitorTypeAdvanced extends AbstractHandleDistributeSingleClientRealCloseVisitorTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*

View File

@@ -3,11 +3,11 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.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.socket.NettyServerPermeateClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientRealSocket;
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.tcp.client.AbstractHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeSingleClientRealConnectTypeAdvanced;
import java.util.List;
@@ -15,12 +15,12 @@ import java.util.List;
* 客户端创建真实代理同奥
*/
@Slf4j
public class ClientHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced extends AbstractHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced<NettyProxyMsg> {
public class ClientHandleDistributeSingleClientRealConnectTypeAdvanced extends AbstractHandleDistributeSingleClientRealConnectTypeAdvanced<NettyProxyMsg> {
private final NettyClientProperties nettyClientProperties;// 服务端地址信息
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
public ClientHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties, List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
public ClientHandleDistributeSingleClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties, List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
this.nettyClientProperties = nettyClientProperties;
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
}
@@ -50,7 +50,7 @@ public class ClientHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanc
.build();
// 绑定真实服务端口
NettyServerPermeateClientRealSocket.buildRealServer(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
NettyClientRealSocket.buildRealServer(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
}
}

View File

@@ -1,34 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 下发 客户端渗透服务端通信通道关闭
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE
*/
@Slf4j
public class ClientHandleHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 关闭本地通信通道
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
channel.close();
nextChannel.close();
}
}

View File

@@ -1,43 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientPermeateClientCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
/**
* 客户端渗透客户端init close 信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_CLOSE
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateClientCloseTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateClientCloseTypeAdvanced<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();
}
}
}

View File

@@ -1,71 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.socket.NettyClientPermeateClientVisitorSocket;
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.tcp.client.AbstractHandleTcpDistributeClientPermeateClientInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* 客户端渗透客户端init信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateClientInitTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateClientInitTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 初始化 客户端渗透服务端socket
byte[] fromClientIdBytes = nettyProxyMsg.getClientId();
byte[] visitorPortBytes = nettyProxyMsg.getVisitorPort();
byte[] clientTargetIpBytes = nettyProxyMsg.getClientTargetIp();
byte[] clientTargetPortBytes = nettyProxyMsg.getClientTargetPort();
byte[] toClientIdBytes = nettyProxyMsg.getData();
String fromClientId = new String(fromClientIdBytes);
String toClientId = new String(toClientIdBytes);
Integer visitorPort = Integer.parseInt(new String(visitorPortBytes));
String clientTargetIp = new String(clientTargetIpBytes);
Integer clientTargetPort = Integer.parseInt(new String(clientTargetPortBytes));
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
// ChannelFlowAdapter channelFlowAdapter = SpringContextHolder.getBean(ChannelFlowAdapter.class);
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
log.info("client permeate client from client_id:【{}】 to_client_id【{}】with visitor_port【{}】, clientTargetIp:【{}】clientTargetPort:【{}】",
fromClientId, toClientId, visitorPort, clientTargetIp, clientTargetPort);
NettyClientPermeateClientVisitorSocket nettyClientPermeateClientVisitorSocket =
NettyClientPermeateClientVisitorSocket.NettyClientPermeateClientVisitorSocketBuilder.builder()
.builderClientId(fromClientId)
.builderClientTargetIp(clientTargetIp)
.builderClientTargetPort(clientTargetPort)
.builderVisitorPort(visitorPort)
.builderNettyClientProperties(nettyClientProperties)
// .builderChannelFlowAdapter(channelFlowAdapter)
.builderToClientId(toClientId)
.builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)
.build();
try {
nettyClientPermeateClientVisitorSocket.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -1,35 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 下发客户端渗透客户端通信通道关闭
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced<NettyProxyMsg> {
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 关闭客户端真实通道、访客通道
Channel realChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
realChannel.close();// 真实通道关闭
channel.close(); // 数据传输通道关闭
}
}

View File

@@ -1,43 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientPermeateServerCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
/**
* 客户端渗透服务端init close 信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateServerCloseTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateServerCloseTypeAdvanced<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();
}
}
}

View File

@@ -1,63 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.socket.NettyClientPermeateServerVisitorSocket;
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.tcp.client.AbstractHandleTcpDistributeClientPermeateServerInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* 客户端渗透服务端init信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateServerInitTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateServerInitTypeAdvanced<NettyProxyMsg> {
private final NettyClientProperties nettyClientProperties;
public ClientHandleTcpDistributeClientPermeateServerInitTypeAdvanced(NettyClientProperties nettyClientProperties) {
this.nettyClientProperties = nettyClientProperties;
}
/**
* 处理当前数据
*
* @param channel 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
// 初始化 客户端渗透服务端socket
byte[] clientIdBytes = nettyProxyMsg.getClientId();
byte[] visitorPort = nettyProxyMsg.getVisitorPort();
byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
NettyClientPermeateServerVisitorSocket nettyClientPermeateServerVisitorSocket = NettyClientPermeateServerVisitorSocket.NettyVisitorSocketBuilder.builder()
.builderClientId(new String(clientIdBytes))
.builderClientTargetIp(new String(clientTargetIp))
.builderClientTargetPort(Integer.parseInt(new String(clientTargetPort)))
.builderVisitorPort(Integer.parseInt(new String(visitorPort)))
.builderNettyClientProperties(nettyClientProperties)
.builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)
.build();
try {
nettyClientPermeateServerVisitorSocket.start();
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
}
}
}

View File

@@ -1,52 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientPermeateServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 服务端处理客户端数据传输
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER
*/
@Slf4j
public class ClientHandleTcpDistributeClientPermeateServerTransferTypeAdvanced extends AbstractHandleTcpDistributeClientPermeateServerTransferTypeAdvanced<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);
}
}

View File

@@ -1,61 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.socket.NettyClientPermeateClientRealSocket;
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.tcp.client.AbstractHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.spring.utils.SpringContextHolder;
import java.util.ArrayList;
import java.util.List;
/**
* 客户端渗透客户端数据传输通道连接成功
*
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
*/
@Slf4j
public class ClientHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced<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());
NettyClientPermeateClientRealSocket.buildRealServer(
clientId,
clientTargetIp,
clientTargetPort,
visitorPort,
visitorId,
nettyClientProperties,
handleChannelTypeAdvancedList
);
}
}

View File

@@ -1,44 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 下发 客户端渗透客户端数据传输通道init 成功
*
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
*/
@Slf4j
public class ClientHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced extends AbstractHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced<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);
}
}

View File

@@ -1,41 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeClientTransferClientRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 下发客户端渗透客户端数据传输
*
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST
*/
@Slf4j
public class ClientHandleTcpDistributeClientTransferClientRequestTypeAdvanced extends AbstractHandleTcpDistributeClientTransferClientRequestTypeAdvanced<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);
}
}

View File

@@ -1,43 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateServerVisitorHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateServerVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.tcp.client.AbstractHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端渗透服务端数据传输通道连接成功
* @see NettyClientPermeateServerVisitorTransferSocket
* @see NettyClientPermeateServerVisitorHandler
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
*/
@Slf4j
public class ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced<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);
}
}

View File

@@ -1,41 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 下发客户端渗透客户端数据传输响应
*
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_RESPONSE
*/
@Slf4j
public class ClientHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced extends AbstractHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced<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);
}
}

View File

@@ -6,22 +6,22 @@ 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.tcp.client.AbstractHandleTcpDistributeServicePermeateClientTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeChannelTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 服务端处理客户端数据传输
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_TRANSFER
*/
@Slf4j
public class ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribute extends AbstractHandleTcpDistributeServicePermeateClientTransferTypeAdvanced<NettyProxyMsg> {
public class ClientReportHandleChannelTransferTypeAdvancedHandleDistribute extends AbstractHandleDistributeChannelTransferTypeAdvanced<NettyProxyMsg> {
private final NettyClientProperties nettyClientProperties;
public ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribute(NettyClientProperties nettyClientProperties) {
public ClientReportHandleChannelTransferTypeAdvancedHandleDistribute(NettyClientProperties nettyClientProperties) {
this.nettyClientProperties = nettyClientProperties;
}
@@ -40,9 +40,8 @@ public class ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribut
byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();
byte[] visitorId = nettyProxyMsg.getVisitorId();
// 真实服务通道
// Channel realChannel = NettyRealIdContext.getReal(new String(visitorId));
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
if (nextChannel == null) {
Channel realChannel = NettyRealIdContext.getReal(new String(visitorId));
if (realChannel == null) {
log.error("无法获取访客:{} 真实服务", new String(visitorId));
return;
}
@@ -52,7 +51,7 @@ public class ClientHandleChannelTransferTypeAdvancedHandleDistributeTcpDistribut
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
buf.writeBytes(nettyProxyMsg.getData());
nextChannel.writeAndFlush(buf);
realChannel.writeAndFlush(buf);
}

View File

@@ -4,16 +4,16 @@ 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.tcp.client.AbstractHandleTcpClientChannelActiveAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleClientChannelActiveAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
/**
* 客户端通道 is active
*/
public class HandleTcpClientChannelActiveAdvanced extends AbstractHandleTcpClientChannelActiveAdvanced<NettyProxyMsg> {
public class HandleClientChannelActiveAdvanced extends AbstractHandleClientChannelActiveAdvanced<NettyProxyMsg> {
private final NettyClientProperties nettyClientProperties;
public HandleTcpClientChannelActiveAdvanced(NettyClientProperties nettyClientProperties) {
public HandleClientChannelActiveAdvanced(NettyClientProperties nettyClientProperties) {
this.nettyClientProperties = nettyClientProperties;
}

View File

@@ -5,7 +5,7 @@ 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.tcp.client.AbstractHandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeConnectSuccessNotificationTypeAdvancedHandle;
import java.util.List;
@@ -13,12 +13,12 @@ import java.util.List;
* 客户端连接成功通知
*/
@Slf4j
public class HandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle extends AbstractHandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle<NettyProxyMsg> {
public class HandleDistributeConnectSuccessNotificationTypeAdvancedHandle extends AbstractHandleDistributeConnectSuccessNotificationTypeAdvancedHandle<NettyProxyMsg> {
private final ClientChangeEvent clientChangeEvent;
public HandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
public HandleDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
this.clientChangeEvent = clientChangeEvent;
}
@@ -36,8 +36,8 @@ public class HandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle ext
// 存储其他客户端状态
List<String> clientIdList = JSONObject.parseArray(new String(msg.getData()), String.class);
for (String clientId : clientIdList) {
clientChangeEvent.clientOnLine(clientId);
for (String tenantId : clientIdList) {
clientChangeEvent.clientOnLine(tenantId);
}
}

View File

@@ -5,7 +5,7 @@ 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.tcp.client.AbstractHandleTcpDistributeDisconnectTypeAdvancedHandle;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeDisconnectTypeAdvancedHandle;
/**
@@ -13,12 +13,12 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.tcp.client.Abs
* TYPE_DISCONNECT
*/
@Slf4j
public class HandleTcpDistributeDisconnectTypeAdvancedHandle extends AbstractHandleTcpDistributeDisconnectTypeAdvancedHandle<NettyProxyMsg> {
public class HandleDistributeDisconnectTypeAdvancedHandle extends AbstractHandleDistributeDisconnectTypeAdvancedHandle<NettyProxyMsg> {
private final ClientChangeEvent clientChangeEvent;
public HandleTcpDistributeDisconnectTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
public HandleDistributeDisconnectTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
this.clientChangeEvent = clientChangeEvent;
}

View File

@@ -3,13 +3,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeStagingClosedTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeStagingClosedTypeAdvanced;
/**
* 服务端下发暂存关闭消息处理
*/
@Slf4j
public class HandleTcpDistributeStagingClosedTypeAdvanced extends AbstractHandleTcpDistributeStagingClosedTypeAdvanced<NettyProxyMsg> {
public class HandleDistributeStagingClosedTypeAdvanced extends AbstractHandleDistributeStagingClosedTypeAdvanced<NettyProxyMsg> {
/**

View File

@@ -3,16 +3,16 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.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.tcp.client.AbstractHandleTcpDistributeStagingOpenedTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeStagingOpenedTypeAdvanced;
/**
* 服务端下发暂存开启消息处理
*/
@Slf4j
public class HandleTcpDistributeStagingOpenedTypeAdvanced extends AbstractHandleTcpDistributeStagingOpenedTypeAdvanced<NettyProxyMsg> {
public class HandleDistributeStagingOpenedTypeAdvanced extends AbstractHandleDistributeStagingOpenedTypeAdvanced<NettyProxyMsg> {
public HandleTcpDistributeStagingOpenedTypeAdvanced() {
public HandleDistributeStagingOpenedTypeAdvanced() {
}

View File

@@ -1,5 +1,6 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
@@ -10,9 +11,8 @@ import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClien
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 NettyClientFilter extends DebugChannelInitializer<SocketChannel> {
public class NettyClientFilter extends ChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
@@ -24,7 +24,7 @@ public class NettyClientFilter extends DebugChannelInitializer<SocketChannel> {
}
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/* * 解码和编码,应和服务端一致 * */

View File

@@ -1,28 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
import io.netty.buffer.UnpooledDirectByteBuf;
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.handler.NettyClientPermeateClientRealHandler;
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 NettyClientPermeateClientRealFilter 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 NettyClientPermeateClientRealHandler());
}
}

View File

@@ -1,46 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateClientTransferHandler;
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 NettyClientPermeateClientTransferFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateClientTransferFilter(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 NettyClientPermeateClientTransferHandler(channelTypeAdapter));
}
}

View File

@@ -1,46 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateClientTransferRealHandler;
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 NettyClientPermeateClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateClientTransferRealFilter(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 NettyClientPermeateClientTransferRealHandler(channelTypeAdapter));
}
}

View File

@@ -1,36 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateClientVisitorHandler;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettyClientPermeateClientVisitorFilter extends DebugChannelInitializer<SocketChannel> {
private final InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor;
public NettyClientPermeateClientVisitorFilter(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 NettyClientPermeateClientVisitorHandler(internalNetworkClientPermeateClientVisitor));
}
}

View File

@@ -1,45 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateServerTransferHandler;
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 NettyClientPermeateServerTransferFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateServerTransferFilter(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 NettyClientPermeateServerTransferHandler(channelTypeAdapter));
}
}

View File

@@ -1,36 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyClientPermeateServerVisitorHandler;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettyClientPermeateServerVisitorFilter extends DebugChannelInitializer<SocketChannel> {
private final InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor;
public NettyClientPermeateServerVisitorFilter(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 NettyClientPermeateServerVisitorHandler(internalNetworkClientPermeateServerVisitor));
}
}

View File

@@ -0,0 +1,30 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientRealHandler;
public class NettyClientRealFilter extends ChannelInitializer<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.
* @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 initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new NettyClientRealHandler());
// // 解码、编码
// 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());
}
}

View File

@@ -2,21 +2,21 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyServerPermeateClientTransferHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientVisitorRealHandler;
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 NettyServerPermeateClientTransferFilter extends DebugChannelInitializer<SocketChannel> {
public class NettyClientVisitorRealFilter extends ChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyServerPermeateClientTransferFilter(ChannelTypeAdapter channelTypeAdapter) {
public NettyClientVisitorRealFilter(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
@@ -30,13 +30,13 @@ public class NettyServerPermeateClientTransferFilter extends DebugChannelInitial
* the {@link Channel}.
*/
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
protected void initChannel(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 NettyServerPermeateClientTransferHandler(channelTypeAdapter));
pipeline.addLast(new NettyClientVisitorRealHandler(channelTypeAdapter));
}
}

View File

@@ -1,27 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.handler.NettyServerPermeateClientRealHandler;
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 NettyServerPermeateClientRealFilter 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 NettyServerPermeateClientRealHandler());
}
}

View File

@@ -62,7 +62,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<NettyProxyMs
// 处理客户端连接成功
Channel channel = ctx.channel();
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.TCP_CLIENT_CHANNEL_ACTIVE);
nettyMsg.setType(MessageType.CLIENT_CHANNEL_ACTIVE);
nettyMsg.setClientId(clientId);
channelTypeAdapter.handler(channel, nettyMsg);
@@ -97,7 +97,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<NettyProxyMs
if (IdleState.WRITER_IDLE.equals(event.state())) { //如果写通道处于空闲状态,就发送心跳命令
String clientId = nettyClientSocket.getClientId();
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.TCP_TYPE_HEARTBEAT);
nettyMsg.setType(MessageType.TYPE_HEARTBEAT);
nettyMsg.setData(clientId.getBytes(StandardCharsets.UTF_8));
nettyMsg.setClientId(clientId.getBytes(StandardCharsets.UTF_8));
ctx.writeAndFlush(nettyMsg);// 发送心跳数据
@@ -106,7 +106,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<NettyProxyMs
String clientId = nettyClientSocket.getClientId();
Channel channel = ctx.channel();
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.TCP_DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION);
nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION);
nettyMsg.setClientId(clientId.getBytes(StandardCharsets.UTF_8));
channelTypeAdapter.handler(channel, nettyMsg);
ctx.close();

View File

@@ -1,84 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* 来自客户端 真实服务器返回的数据请求
*/
@Slf4j
public class NettyClientPermeateClientRealHandler 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(MessageType.TCP_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(MessageType.TCP_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);
}
}

View File

@@ -1,76 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettyClientPermeateClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateClientTransferRealHandler(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(MessageType.TCP_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);
}
}

View File

@@ -1,133 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.advanced.ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateClientVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateServerVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
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 NettyClientPermeateClientVisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor;
// private final NettyChannelPool nettyChannelPool = new DefaultNettyChannelPool(10);
public NettyClientPermeateClientVisitorHandler(InternalNetworkClientPermeateClientVisitor internalNetworkClientPermeateClientVisitor) {
this.internalNetworkClientPermeateClientVisitor = internalNetworkClientPermeateClientVisitor;
}
/**
* @param ctx
* @throws Exception
* @see NettyClientPermeateServerVisitorTransferSocket
* @see ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
*/
@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);
// 创建访客连接客户端通道
NettyClientPermeateClientVisitorTransferSocket.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(MessageType.TCP_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(MessageType.TCP_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");
}
}

View File

@@ -1,93 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.MessageType;
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.nio.charset.StandardCharsets;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettyClientPermeateServerTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateServerTransferHandler(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(MessageType.TCP_TCP_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(MessageType.TCP_TYPE_HEARTBEAT);
ctx.writeAndFlush(nettyMsg);// 发送心跳数据
} else if (event.state() == IdleState.WRITER_IDLE) { // 如果检测到写空闲状态,关闭连接
// 离线、暂存通知
ctx.close();
}
} else {
super.userEventTriggered(ctx, obj);
}
}
}

View File

@@ -1,147 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.advanced.ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateServerVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
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 NettyClientPermeateServerVisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor;
// private final NettyChannelPool nettyChannelPool = new DefaultNettyChannelPool(10);
public NettyClientPermeateServerVisitorHandler(InternalNetworkClientPermeateServerVisitor internalNetworkClientPermeateServerVisitor) {
this.internalNetworkClientPermeateServerVisitor = internalNetworkClientPermeateServerVisitor;
}
/**
* @param ctx
* @throws Exception
* @see NettyClientPermeateServerVisitorTransferSocket
* @see ClientHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
*/
@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);
// 创建访客连接服务端通道
NettyClientPermeateServerVisitorTransferSocket.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(MessageType.TCP_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(MessageType.TCP_TCP_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(MessageType.TCP_TCP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE);
closeRealClient.setClientId(clientId);
closeRealClient.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeRealClient);
}
ctx.close();
}
}

View File

@@ -1,46 +1,46 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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 lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
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.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* 来自客户端 真实服务器返回的数据请求
*/
@Slf4j
public class NettyServerPermeateClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
public class NettyClientRealHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
public void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {
byte[] bytes = nettyByteBuf.getData();
log.debug("bytes.length:{}",bytes.length);
// 客户端发送真实数据到代理了
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
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());
Channel visitorChannel = NettyCommunicationIdContext.getVisitor(visitorId);
NettyProxyMsg returnMessage = new NettyProxyMsg();
returnMessage.setType(MessageType.TCP_REPORT_CLIENT_TRANSFER);
returnMessage.setType(MessageType.REPORT_CLIENT_TRANSFER);
returnMessage.setVisitorId(visitorId);
returnMessage.setClientId(clientId);
returnMessage.setVisitorPort(visitorPort);
returnMessage.setData(bytes);
visitor.writeAndFlush(returnMessage);
}
visitorChannel.writeAndFlush(returnMessage);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
@@ -51,11 +51,11 @@ public class NettyServerPermeateClientRealHandler extends SimpleChannelInboundHa
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// 客户端真实通信通道
Channel visitor = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
Channel visitor = NettyCommunicationIdContext.getVisitor(visitorId);
if (visitor != null) {
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(MessageType.TCP_REPORT_SERVICE_PERMEATE_CLIENT_CLIENT_CLOSE_VISITOR);
closeVisitorMsg.setType(MessageType.REPORT_SINGLE_CLIENT_CLOSE_VISITOR);
closeVisitorMsg.setVisitorId(visitorId);
visitor.writeAndFlush(closeVisitorMsg);
}
@@ -65,7 +65,6 @@ public class NettyServerPermeateClientRealHandler extends SimpleChannelInboundHa
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// String vid = ctx.channel().attr(Constant.VID).get();
// if (StringUtil.isNullOrEmpty(vid)) {
// super.channelWritabilityChanged(ctx);
@@ -76,19 +75,7 @@ public class NettyServerPermeateClientRealHandler extends SimpleChannelInboundHa
// 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());
}
super.channelWritabilityChanged(ctx);
}
@Override

View File

@@ -3,33 +3,25 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.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.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/**
* 客户端访客通信通道 处理器
*/
@Slf4j
public class NettyClientPermeateClientTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
public class NettyClientVisitorRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientPermeateClientTransferHandler(ChannelTypeAdapter channelTypeAdapter) {
public NettyClientVisitorRealHandler(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();
@@ -37,19 +29,25 @@ public class NettyClientPermeateClientTransferHandler extends SimpleChannelInbou
}
@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 nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
if (nextChannel != null) {
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
if (clientChannel != null) {
Channel channel = clientChannel.getChannel();
// 上报关闭这个客户端的访客通道
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
closeVisitorMsg.setType(MessageType.TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE);
closeVisitorMsg.setType(MessageType.REPORT_SINGLE_CLIENT_CLOSE_VISITOR);
closeVisitorMsg.setVisitorId(visitorId);
nextChannel.writeAndFlush(closeVisitorMsg);
channel.writeAndFlush(closeVisitorMsg);
}
super.channelInactive(ctx);
@@ -57,14 +55,8 @@ public class NettyClientPermeateClientTransferHandler extends SimpleChannelInbou
@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!");
super.channelWritabilityChanged(ctx);
}
@Override

View File

@@ -1,76 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.*;
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 NettyServerPermeateClientTransferHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyServerPermeateClientTransferHandler(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(MessageType.TCP_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);
}
}

View File

@@ -1,174 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.filter.NettyClientPermeateClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientPermeateClientTransferRealFilter;
import org.framework.lazy.cloud.network.heartbeat.common.*;
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 NettyClientPermeateClientRealSocket {
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.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientPermeateClientRealFilter())
;
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.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientPermeateClientTransferRealFilter(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(MessageType.TCP_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);
}
});
}
}

View File

@@ -1,292 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.filter.NettyClientPermeateClientVisitorFilter;
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 NettyClientPermeateClientVisitorSocket implements PermeateVisitorSocket {
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
private final NettyClientPermeateClientVisitorFilter nettyClientPermeateClientVisitorFilter;
@Getter
private final String clientId;
@Getter
private final int visitorPort;
public NettyClientPermeateClientVisitorSocket(NettyClientPermeateClientVisitorFilter nettyClientPermeateClientVisitorFilter, String clientId, int visitorPort) {
this.nettyClientPermeateClientVisitorFilter = nettyClientPermeateClientVisitorFilter;
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.TCP_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(nettyClientPermeateClientVisitorFilter);
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 NettyClientPermeateClientVisitorSocket 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);
NettyClientPermeateClientVisitorFilter visitorFilter = new NettyClientPermeateClientVisitorFilter(internalNetworkClientPermeateClientVisitor);
return new NettyClientPermeateClientVisitorSocket(visitorFilter, clientId, visitorPort);
}
}
}

View File

@@ -1,101 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.filter.NettyClientPermeateClientTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
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 NettyClientPermeateClientVisitorTransferSocket {
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.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientPermeateClientTransferFilter(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(MessageType.TCP_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);
}
});
}
}

View File

@@ -1,237 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.filter.NettyClientPermeateServerVisitorFilter;
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 NettyClientPermeateServerVisitorSocket implements PermeateVisitorSocket {
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
private final NettyClientPermeateServerVisitorFilter nettyClientPermeateServerVisitorFilter;
@Getter
private final String clientId;
@Getter
private final int visitorPort;
public NettyClientPermeateServerVisitorSocket(NettyClientPermeateServerVisitorFilter nettyClientPermeateServerVisitorFilter, String clientId, int visitorPort) {
this.nettyClientPermeateServerVisitorFilter = nettyClientPermeateServerVisitorFilter;
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.TCP_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(nettyClientPermeateServerVisitorFilter);
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 NettyClientPermeateServerVisitorSocket 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);
NettyClientPermeateServerVisitorFilter nettyClientPermeateServerVisitorFilter = new NettyClientPermeateServerVisitorFilter(internalNetworkClientPermeateServerVisitor);
return new NettyClientPermeateServerVisitorSocket(nettyClientPermeateServerVisitorFilter, clientId, visitorPort);
}
}
}

View File

@@ -1,100 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.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.filter.NettyClientPermeateServerTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
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 NettyClientPermeateServerVisitorTransferSocket {
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.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientPermeateServerTransferFilter(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(MessageType.TCP_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);
}
});
}
}

View File

@@ -6,9 +6,10 @@ 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.netty.filter.NettyClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyServerPermeateClientRealFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyServerPermeateClientTransferFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientVisitorRealFilter;
import org.framework.lazy.cloud.network.heartbeat.common.*;
import org.framework.lazy.cloud.network.heartbeat.common.*;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
@@ -21,7 +22,7 @@ import java.util.concurrent.TimeUnit;
* 客户端连接真实服务
*/
@Slf4j
public class NettyServerPermeateClientRealSocket {
public class NettyClientRealSocket {
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
/**
@@ -53,20 +54,7 @@ public class NettyServerPermeateClientRealSocket {
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.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyServerPermeateClientRealFilter())
;
.handler(new NettyClientRealFilter());
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
@@ -75,7 +63,7 @@ public class NettyServerPermeateClientRealSocket {
log.info("访客通过 客户端:【{}】,绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, clientTargetIp, clientTargetPort);
// 客户端真实通道
// NettyRealIdContext.pushReal(realChannel, visitorId);
NettyRealIdContext.pushReal(realChannel, visitorId);
// 绑定访客ID到当前真实通道属性
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
@@ -84,7 +72,7 @@ public class NettyServerPermeateClientRealSocket {
// 新建一个通道处理
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList,realChannel);
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
// 是否等 服务端相应访客通道已经可以自动读写
// realChannel.config().setOption(ChannelOption.AUTO_READ, true);
@@ -132,24 +120,11 @@ public class NettyServerPermeateClientRealSocket {
*/
protected static void newVisitorConnect2Server(InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient,
NettyClientProperties nettyClientProperties,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList,
Channel realChannel) throws InterruptedException {
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
// .option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyServerPermeateClientTransferFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
.handler(new NettyClientVisitorRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
;
String inetHost = nettyClientProperties.getInetHost();
@@ -172,38 +147,33 @@ public class NettyServerPermeateClientRealSocket {
// 使用的客户端ID:{}
log.info("Client ID used: {}" , visitorClientId);
future.addListener((ChannelFutureListener) futureListener -> {
Channel transferChannel = futureListener.channel();
Channel channel = futureListener.channel();
if (futureListener.isSuccess()) {
NettyProxyMsg myMsg = new NettyProxyMsg();
myMsg.setType(MessageType.TCP_REPORT_SINGLE_CLIENT_REAL_CONNECT);
myMsg.setType(MessageType.REPORT_SINGLE_CLIENT_REAL_CONNECT);
myMsg.setClientId(visitorClientId);
myMsg.setVisitorPort(visitorPort);
myMsg.setClientTargetIp(clientTargetIp);
myMsg.setClientTargetPort(clientTargetPort);
myMsg.setVisitorId(visitorId);
transferChannel.writeAndFlush(myMsg);
channel.writeAndFlush(myMsg);
// 绑定客户端真实通信通道
NettyCommunicationIdContext.pushVisitor(transferChannel, visitorId);
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
ChannelAttributeKeyUtils.buildClientId(transferChannel, visitorClientId);
NettyCommunicationIdContext.pushVisitor(channel, visitorId);
ChannelAttributeKeyUtils.buildVisitorId(channel, visitorId);
ChannelAttributeKeyUtils.buildClientId(channel, visitorClientId);
// 客户端真实通道自动读写打开
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
Channel visitor = NettyRealIdContext.getReal(visitorId);
visitor.config().setOption(ChannelOption.AUTO_READ, true);
} else {
log.info("每隔2s重连....");
// 离线
eventLoopGroup.schedule(() -> {
channel.eventLoop().schedule(() -> {
try {
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList,realChannel);
newVisitorConnect2Server(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);
} catch (InterruptedException e) {
e.printStackTrace();
}

View File

@@ -2,13 +2,16 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
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.filter.NettyClientFilter;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyServerContext;
@@ -16,7 +19,6 @@ import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdap
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;
@@ -44,8 +46,6 @@ public class NettyClientSocket {
* 当前连接的服务端ID
*/
private final String serverId;
private final String appKey;
private final String appSecret;
/**
* 客户端状态变更事件
*/
@@ -53,20 +53,11 @@ public class NettyClientSocket {
private final ClientChangeEvent clientChangeEvent;
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList; // 处理服务端发送过来的数据类型
public NettyClientSocket(String inetHost,
int inetPort,
String clientId,
String serverId,
String appKey,
String appSecret,
ClientChangeEvent clientChangeEvent,
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
public NettyClientSocket(String inetHost, int inetPort, String clientId, String serverId, 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;
}
@@ -79,17 +70,9 @@ public class NettyClientSocket {
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.TCP_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 NettyClientFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList), this))
;
log.info("use clientId:{} connect to server IP:{},server port :{}", clientId, inetHost, inetPort);
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
// 客户端连接服务端的channel
@@ -101,13 +84,9 @@ public class NettyClientSocket {
log.info("clientId:{},connect to server IP:{},server port :{} isSuccess ", clientId, inetHost, inetPort);
// 告诉服务端这条连接是client的连接
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(MessageType.TCP_REPORT_CLIENT_CONNECT_SUCCESS);
nettyMsg.setType(MessageType.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);
@@ -119,7 +98,7 @@ public class NettyClientSocket {
// 离线
NettyServerContext.removeServerEndpointChannels(serverId, clientId);
clientChangeEvent.clientOffLine(inetHost, inetPort,serverId, clientId);
eventLoopGroup.schedule(() -> {
futureListener.channel().eventLoop().schedule(() -> {
try {
newConnect2Server(inetHost, inetPort, clientId, serverId, clientChangeEvent);
} catch (InterruptedException e) {

View File

@@ -29,7 +29,7 @@
// Channel channel = clientChannel.getChannel();
//
// NettyProxyMsg nettyMsg = new NettyProxyMsg();
// nettyMsg.setType(MessageType.TCP_REPORT_CLIENT_STAGING_OPENED);
// nettyMsg.setType(MessageType.REPORT_CLIENT_STAGING_OPENED);
// nettyMsg.setData((clientId
// .getBytes(StandardCharsets.UTF_8)));
// nettyMsg.setClientId((clientId
@@ -52,7 +52,7 @@
// // 上报 当前通道暂存关闭了
// Channel channel = clientChannel.getChannel();
// NettyProxyMsg nettyMsg = new NettyProxyMsg();
// nettyMsg.setType(MessageType.TCP_REPORT_CLIENT_STAGING_CLOSED);
// nettyMsg.setType(MessageType.REPORT_CLIENT_STAGING_CLOSED);
// nettyMsg.setData((clientId
// .getBytes(StandardCharsets.UTF_8)));
// nettyMsg.setClientId((clientId

View File

@@ -5,7 +5,7 @@
<link rel="icon" href="./favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Java 使用 Lazy ORM 网络穿透客户端!</title>
<script type="module" crossorigin src="./assets/index-DjZ671Nk.js"></script>
<script type="module" crossorigin src="./assets/index-DrGmDmWm.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-BEQZ9T9z.css">
</head>
<body>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>top.wu2020</groupId>
<artifactId>wu-lazy-cloud-network</artifactId>
<version>1.2.9-JDK17-SNAPSHOT</version>
<version>1.2.6-JDK17</version>
</parent>
<artifactId>wu-lazy-cloud-heartbeat-common</artifactId>

View File

@@ -1,17 +1,14 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
/**
* 通道上下文
@@ -19,7 +16,7 @@ import java.util.stream.Collectors;
@Slf4j
public class ChannelContext {
private final static ConcurrentHashMap<String/*clientId*/, List<Channel>/*通道*/>
private final static ConcurrentHashMap<String/*clientId*/, ClientChannelImpl/*通道*/>
channelIdClientChannelDTOConcurrentHashMap = new ConcurrentHashMap<>();
/**
@@ -29,26 +26,17 @@ public class ChannelContext {
* @param clientId 客户端ID
*/
public static void push(Channel channel, String clientId) {
ChannelId channelId = channel.id();
ClientChannelImpl clientChannelImpl = new ClientChannelImpl();
clientChannelImpl.setChannelId(channelId);
clientChannelImpl.setChannel(channel);
clientChannelImpl.setClientId(clientId.getBytes(StandardCharsets.UTF_8));
// 如果客户端已经存在 移除
if (channelIdClientChannelDTOConcurrentHashMap.containsKey(clientId)) {
// clear(clientId);
List<Channel> existChannelList = new ArrayList<>();
List<Channel> oldChannels = channelIdClientChannelDTOConcurrentHashMap.get(clientId);
for (Channel existChannel : oldChannels) {
if (existChannel != null) {
if(existChannel.isActive()){
existChannelList.add(existChannel);
}else {
log.warn("close channel with client:{}", ChannelAttributeKeyUtils.getClientId(existChannel));
existChannel.close();
}
}
}
existChannelList.add(channel);
channelIdClientChannelDTOConcurrentHashMap.put(clientId, existChannelList);
}else {
channelIdClientChannelDTOConcurrentHashMap.putIfAbsent(clientId, Collections.synchronizedList(new ArrayList<>(List.of(channel))));
}
channelIdClientChannelDTOConcurrentHashMap.put(clientId, clientChannelImpl);
}
@@ -59,7 +47,14 @@ public class ChannelContext {
* @param clientId 客户端ID
*/
public static void push(Channel channel, byte[] clientId) {
push(channel,new String(clientId, StandardCharsets.UTF_8));
ChannelId channelId = channel.id();
ClientChannelImpl clientChannelImpl = new ClientChannelImpl();
clientChannelImpl.setChannelId(channelId);
clientChannelImpl.setChannel(channel);
clientChannelImpl.setClientId(clientId);
channelIdClientChannelDTOConcurrentHashMap.put(new String(clientId), clientChannelImpl);
}
/**
@@ -67,16 +62,8 @@ public class ChannelContext {
*
* @return 返回所有通道信息
*/
public static ConcurrentMap<String/*clientId*/, List<Channel>/*通道*/> getChannels() {
return channelIdClientChannelDTOConcurrentHashMap;
}
/**
* 获取所有通道
*
* @return 返回所有通道信息
*/
public static List<String> getClientIds() {
return new ArrayList<>(channelIdClientChannelDTOConcurrentHashMap.keySet().stream().toList());
public static List<ClientChannel> get() {
return new ArrayList<>(channelIdClientChannelDTOConcurrentHashMap.values());
}
@@ -86,7 +73,7 @@ public class ChannelContext {
* @param clientId 客户端ID
* @return 通道信息
*/
public static List<Channel> get(byte[] clientId) {
public static ClientChannel get(byte[] clientId) {
if (channelIdClientChannelDTOConcurrentHashMap
.containsKey(new String(clientId))) {
return channelIdClientChannelDTOConcurrentHashMap
@@ -104,33 +91,10 @@ public class ChannelContext {
* @param clientId 客户端ID
* @return 通道信息
*/
public static List<Channel> get(String clientId) {
public static ChannelContext.ClientChannel get(String clientId) {
return get(clientId.getBytes(StandardCharsets.UTF_8));
}
/**
* 根据通道ID获取通道信息
*
* @param clientId 客户端ID
* @return 通道信息
*/
public static Channel getLoadBalance(byte[] clientId) {
List<Channel> channels = get(clientId);
if(ObjectUtils.isEmpty(channels)){
return null;
}
channels = channels.stream().filter(Channel::isActive).collect(Collectors.toList());
return channels.get(0);
}
/**
* 根据通道ID获取通道信息
*
* @param clientId 客户端ID
* @return 通道信息
*/
public static Channel getLoadBalance(String clientId) {
return getLoadBalance(clientId.getBytes(StandardCharsets.UTF_8));
}
/**
* 关闭通道
@@ -138,13 +102,12 @@ public class ChannelContext {
* @param clientId 客户端ID
*/
public static void clear(String clientId) {
List<Channel> channels = get(clientId);
if (channels != null) {
ClientChannel clientChannel = get(clientId);
if (clientChannel != null) {
remove(clientId);
for (Channel channel : channels) {
if (channel != null && channel.isActive()) {
channel.close();
}
Channel channel = clientChannel.getChannel();
if (channel != null && channel.isActive()) {
channel.close();
}
} else {
// log warm
@@ -159,7 +122,7 @@ public class ChannelContext {
* @param clientId 客户端ID
*/
public static void remove(byte[] clientId) {
List<Channel> clientChannel = get(clientId);
ClientChannel clientChannel = get(clientId);
if (clientChannel != null) {
channelIdClientChannelDTOConcurrentHashMap.remove(new String(clientId));
} else {
@@ -174,7 +137,7 @@ public class ChannelContext {
* @param clientId 客户端ID
*/
public static void remove(String clientId) {
List<Channel> clientChannel = get(clientId);
ClientChannel clientChannel = get(clientId);
if (clientChannel != null) {
channelIdClientChannelDTOConcurrentHashMap.remove(clientId);
} else {
@@ -194,6 +157,10 @@ public class ChannelContext {
*/
byte[] getClientId();
/**
* 通道ID
*/
ChannelId getChannelId();
/**
* 通道
@@ -204,3 +171,29 @@ public class ChannelContext {
}
/**
* 客户端通道信息
*/
@Data
class ClientChannelImpl implements ChannelContext.ClientChannel {
/**
* 客户端ID
*/
private byte[] clientId;
/**
* 通道ID
*/
private ChannelId channelId;
/**
* 通道
*/
private Channel channel;
@Override
public String toString() {
return "ClientChannelImpl{" +
"clientId=" + new String(clientId) +
", channelId=" + channelId.asLongText() +
'}';
}
}

View File

@@ -1,67 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.common;
/**
* describe 内网渗透 客户端渗透服务端
*
* @author Jia wei Wu
* @date 2023/12/29 05:21 下午
**/
public interface InternalNetworkPermeate {
/**
* 目标ip
*
* @return
*/
String getTargetIp();
/**
* 目标ip
*
* @return
*/
void setTargetIp(String targetIp);
/**
* 目标端口
*
* @return
*/
Integer getTargetPort();
/**
* 目标端口
*
* @return
*/
void setTargetPort(Integer targetPort);
/**
* 访客端口
*
* @return
*/
Integer getVisitorPort();
/**
* 访客端口
*
* @return
*/
void setVisitorPort(Integer visitorPort);
/**
* 是否是ssl
*
* @return
*/
boolean isSsl();
/**
* 设置ssl
*/
void setSsl(boolean ssl);
}

View File

@@ -1,39 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* describe 内网穿透映射 真实服务端
*
* @author Jia wei Wu
* @date 2023/12/29 05:21 下午
**/
@Builder
@Data
@Accessors(chain = true)
public class InternalNetworkServerPermeateServerRealServer {
/**
* 客户端目标地址
*/
private String clientTargetIp;
/**
* 客户端目标端口
*/
private Integer clientTargetPort;
/**
* 访问端口
*/
private Integer visitorPort;
/**
* 是否是ssl
*/
private Boolean isSsl;
}

View File

@@ -1,406 +1,184 @@
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.tcp.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.tcp.server.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.AbstractHandleChannelHeartbeatTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.*;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.*;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.*;
/**
* @see MessageTypeEnums
* 数据取值范围 -128~ 127
* 当前约束范围 -100100
*
* TPC164
* UDP: 64~128
*/
public class MessageType {
/**
* 心跳
*
* @see MessageTypeEnums#TCP_TYPE_HEARTBEAT
* @see AbstractTcpHandleChannelHeartbeatTypeAdvanced
* @see MessageTypeEnums#TYPE_HEARTBEAT
* @see AbstractHandleChannelHeartbeatTypeAdvanced
*/
public static final byte TCP_TYPE_HEARTBEAT = 0X00;
public static final byte TYPE_HEARTBEAT = 0X00;
/**
* 客户端上报连接成功
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_CONNECT_SUCCESS
* @see AbstractHandleTcpClientConnectSuccessTypeAdvanced
* @see MessageTypeEnums#REPORT_CLIENT_CONNECT_SUCCESS
* @see AbstractHandleClientConnectSuccessTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_CONNECT_SUCCESS = 0X01;
public static final byte REPORT_CLIENT_CONNECT_SUCCESS = 0X01;
/**
* 上报 客户端断开连接
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_DISCONNECTION
* @see AbstractHandleTcpReportDisconnectTypeAdvanced
* @see MessageTypeEnums#REPORT_CLIENT_DISCONNECTION
* @see AbstractHandleReportDisconnectTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_DISCONNECTION = 0X02;
public static final byte REPORT_CLIENT_DISCONNECTION = 0X02;
/**
* 客户端上报暂存开启
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_STAGING_OPENED
* @see AbstractHandleTcpReportStagingOpenedTypeAdvanced
* @see MessageTypeEnums#REPORT_CLIENT_STAGING_OPENED
* @see AbstractHandleReportStagingOpenedTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_STAGING_OPENED = 0X03;
public static final byte REPORT_CLIENT_STAGING_OPENED = 0X03;
/**
* 客户端上报暂存关闭
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_STAGING_CLOSED
* @see AbstractHandleTcpReportStagingClosedTypeAdvanced
* @see MessageTypeEnums#REPORT_CLIENT_STAGING_CLOSED
* @see AbstractHandleReportStagingClosedTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_STAGING_CLOSED = 0X04;
public static final byte REPORT_CLIENT_STAGING_CLOSED = 0X04;
/**
* 上报 客户端数据传输(内网穿透数据回传)
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER
* @see AbstractHandleTcpReportServicePermeateClientTransferTypeAdvanced
* @see MessageTypeEnums#REPORT_CLIENT_TRANSFER
* @see AbstractHandleReportHandleChannelTransferTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_TRANSFER = 0X05;
public static final byte REPORT_CLIENT_TRANSFER = 0X05;
/**
* 上报 客户端创建需要代理的真实端口成功
*
* @see MessageTypeEnums#TCP_REPORT_SINGLE_CLIENT_REAL_CONNECT
* @see AbstractHandleTcpReportServicePermeateClientRealConnectTypeAdvanced
* @see MessageTypeEnums#REPORT_SINGLE_CLIENT_REAL_CONNECT
* @see AbstractHandleReportSingleClientRealConnectTypeAdvanced
*/
public static final byte TCP_REPORT_SINGLE_CLIENT_REAL_CONNECT = 0X06;
public static final byte REPORT_SINGLE_CLIENT_REAL_CONNECT = 0X06;
/**
* 上报 客户端关闭一个访客通道
*
* @see MessageTypeEnums#TCP_REPORT_SINGLE_CLIENT_CLOSE_VISITOR
* @see AbstractHandleTcpReportServicePermeateClientCloseVisitorTypeAdvanced
* @see MessageTypeEnums#REPORT_SINGLE_CLIENT_CLOSE_VISITOR
* @see AbstractHandleReportSingleClientCloseVisitorTypeAdvanced
*/
public static final byte TCP_REPORT_SERVICE_PERMEATE_CLIENT_CLIENT_CLOSE_VISITOR = 0X08;
public static final byte REPORT_SINGLE_CLIENT_CLOSE_VISITOR = 0X08;
/**
* 上报 客户端消息到另一个客户端
*
* @see MessageTypeEnums#TCP_REPORT_SINGLE_CLIENT_MESSAGE
* @see AbstractHandleTcpReportSingleClientMessage2OtherClientTypeAdvanced
* @see MessageTypeEnums#REPORT_SINGLE_CLIENT_MESSAGE
* @see AbstractHandleReportSingleClientMessage2OtherClientTypeAdvanced
*/
public static final byte TCP_REPORT_SINGLE_CLIENT_MESSAGE = 0X09;
public static final byte REPORT_SINGLE_CLIENT_MESSAGE = 0X09;
/**
* 服务端通道 is active
*
* @see MessageTypeEnums#TCP_SERVER_CHANNEL_ACTIVE
* @see AbstractHandleTcpReportServerChannelActiveTypeAdvanced
* @see MessageTypeEnums#SERVER_CHANNEL_ACTIVE
* @see AbstractHandleServerChannelActiveTypeAdvanced
*/
public static final byte TCP_SERVER_CHANNEL_ACTIVE = 0X10;
public static final byte SERVER_CHANNEL_ACTIVE = 0X10;
/**
* 上报 集群注册
*
* @see MessageTypeEnums#TCP_REPORT_CLUSTER_NODE_REGISTER_MESSAGE
* @see AbstractHandleTcpReportClusterNodeRegisterTypeAdvanced
* @see MessageTypeEnums#REPORT_CLUSTER_NODE_REGISTER_MESSAGE
* @see AbstractHandleReportClusterNodeRegisterTypeAdvanced
*/
public static final byte TCP_REPORT_CLUSTER_NODE_REGISTER_MESSAGE = 0X11;
/**
* 客户端渗透服务端类型------------------------------------------------------------------------------------
*
*/
/**
* 上报 客户端渗透服务端数据传输通道连接成功
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
* @see AbstractHandleTcpReportClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL = 0X12;
/**
* 上报 客户端渗透服务端init信息
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_SERVER_INIT
* @see AbstractHandleTcpReportClientPermeateServerInitTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_SERVER_INIT = 0X13;
/**
* 上报 客户端渗透服务端init close 信息
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_SERVER_CLOSE
* @see AbstractHandleTcpReportClientPermeateServerCloseTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_SERVER_CLOSE = 0X14;
/**
* 上报 客户端渗透服务端通信通道关闭
*
* @see MessageTypeEnums#TCP_TCP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE
* @see AbstractHandleTcpReportClientPermeateServerTransferCloseTypeAdvanced
*/
public static final byte TCP_TCP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE = 0X15;
/**
* 上报 客户端渗透服务端通信传输
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER
* @see AbstractHandleTcpReportClientPermeateServerTransferTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_SERVER_TRANSFER = 0X16;
/**
* 客户端渗透客户端类型------------------------------------------------------------------------------------
* 60~90
*/
/**
* 上报 客户端渗透客户端init信息
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_CLIENT_INIT
* @see AbstractHandleTcpReportClientPermeateClientInitTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_CLIENT_INIT = 0X19;
/**
* 上报 客户端渗透客户端init close 信息
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_CLIENT_CLOSE
* @see AbstractHandleTcpReportClientPermeateClientCloseTypeAdvanced
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_CLIENT_CLOSE = 0X20;
/**
* 上报 客户端渗透客户端数据传输通道连接成功
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
* @see AbstractHandleTcpReportClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
*/
public static final byte TCP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL = 0X21;
/**
* 上报 客户端渗透客户端数据传输通道init 成功
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_INIT_SUCCESSFUL
* @see AbstractHandleTcpReportClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL = 0X22;
/**
* 上报客户端渗透客户端数据传输请求
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER_CLIENT_REQUEST
* @see AbstractHandleTcpReportClientTransferClientTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST = 0X23;
/**
* 上报客户端渗透客户端数据传输结果
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_TRANSFER_CLIENT_RESPONSE
* @see AbstractHandleTcpReportClientTransferClientResponseTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_RESPONSE
*/
public static final byte TCP_REPORT_CLIENT_TRANSFER_CLIENT_RESPONSE = 0X24;
/**
* 上报客户端渗透客户端通信通道关闭
*
* @see MessageTypeEnums#TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
* @see AbstractHandleTcpReportClientPermeateClientTransferCloseTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
*/
public static final byte TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE = 0X25;
public static final byte REPORT_CLUSTER_NODE_REGISTER_MESSAGE = 0X11;
/**
* 下发 客户端接收连接成功通知
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_CONNECTION_SUCCESS_NOTIFICATION
* @see AbstractHandleTcpDistributeConnectSuccessNotificationTypeAdvancedHandle
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_CONNECTION_SUCCESS_NOTIFICATION
* @see AbstractHandleDistributeConnectSuccessNotificationTypeAdvancedHandle
*/
public static final byte TCP_DISTRIBUTE_CLIENT_CONNECTION_SUCCESS_NOTIFICATION = -0X01;
public static final byte DISTRIBUTE_CLIENT_CONNECTION_SUCCESS_NOTIFICATION = -0X01;
/**
* 下发 客户端断开连接通知
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION
* @see AbstractHandleTcpDistributeDisconnectTypeAdvancedHandle
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION
* @see AbstractHandleDistributeDisconnectTypeAdvancedHandle
*/
public static final byte TCP_DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION = -0X02;
public static final byte DISTRIBUTE_CLIENT_DISCONNECTION_NOTIFICATION = -0X02;
/**
* 下发 客户端暂存开启通知
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_STAGING_OPENED_NOTIFICATION
* @see AbstractHandleTcpDistributeStagingOpenedTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_STAGING_OPENED_NOTIFICATION
* @see AbstractHandleDistributeStagingOpenedTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_STAGING_OPENED_NOTIFICATION = -0X03;
public static final byte DISTRIBUTE_CLIENT_STAGING_OPENED_NOTIFICATION = -0X03;
/**
* 下发 客户端暂存关闭通知
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_STAGING_CLOSED_NOTIFICATION
* @see AbstractHandleTcpDistributeStagingClosedTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_STAGING_CLOSED_NOTIFICATION
* @see AbstractHandleDistributeStagingClosedTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_STAGING_CLOSED_NOTIFICATION = -0X04;
public static final byte DISTRIBUTE_CLIENT_STAGING_CLOSED_NOTIFICATION = -0X04;
/**
* 下发 客户端数据传输(内网穿透数据发送)
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER
* @see AbstractHandleTcpDistributeServicePermeateClientTransferTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_CLIENT_TRANSFER
* @see AbstractHandleDistributeChannelTransferTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_TRANSFER = -0X05;
public static final byte DISTRIBUTE_CLIENT_TRANSFER = -0X05;
/**
* 下发 客户端创建需要代理的真实端口
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT
* @see AbstractHandleTcpDistributeServicePermeateClientRealConnectTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT
* @see AbstractHandleDistributeSingleClientRealConnectTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT = -0X06;
public static final byte DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT = -0X06;
/**
* 下发 客户端代理的真实端口自动读写
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT_AUTO_READ
* @see AbstractHandleTcpDistributeSingleClientRealAutoReadConnectTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT_AUTO_READ
* @see AbstractHandleDistributeSingleClientRealAutoReadConnectTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT_AUTO_READ = -0X07;
public static final byte DISTRIBUTE_SINGLE_CLIENT_REAL_CONNECT_AUTO_READ = -0X07;
/**
* 下发 客户端关闭代理服务通道
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_SINGLE_CLIENT_REAL_CLOSE_VISITOR
* @see AbstractHandleTcpDistributeSingleClientRealCloseVisitorTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_SINGLE_CLIENT_REAL_CLOSE_VISITOR
* @see AbstractHandleDistributeSingleClientRealCloseVisitorTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_SERVER_PERMEATE_CLIENT_REAL_CLOSE_VISITOR = -0X08;
public static final byte DISTRIBUTE_SINGLE_CLIENT_REAL_CLOSE_VISITOR = -0X08;
/**
* 下发 客户端消息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_SINGLE_CLIENT_MESSAGE
* @see AbstractHandleTcpDistributeSingleClientMessageTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_SINGLE_CLIENT_MESSAGE
* @see AbstractHandleDistributeSingleClientMessageTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_SINGLE_CLIENT_MESSAGE = -0X09;
public static final byte DISTRIBUTE_SINGLE_CLIENT_MESSAGE = -0X09;
/**
* 客户端通道 is active
*
* @see MessageTypeEnums#TCP_CLIENT_CHANNEL_ACTIVE
* @see AbstractHandleTcpClientChannelActiveAdvanced
* @see MessageTypeEnums#CLIENT_CHANNEL_ACTIVE
* @see AbstractHandleClientChannelActiveAdvanced
*/
public static final byte TCP_CLIENT_CHANNEL_ACTIVE = -0X10;
public static final byte CLIENT_CHANNEL_ACTIVE = -0X10;
/**
* 下发 集群注册
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLUSTER_NODE_REGISTER_MESSAGE
* @see AbstractHandleTcpDistributeClusterNodeRegisterTypeAdvanced
* @see MessageTypeEnums#DISTRIBUTE_CLUSTER_NODE_REGISTER_MESSAGE
* @see AbstractHandleDistributeClusterNodeRegisterTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLUSTER_NODE_REGISTER_MESSAGE = -0X11;
/**
* 下发 客户端渗透服务端数据传输通道连接成功
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
* @see AbstractHandleTcpDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_TRANSFER_SERVER_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL = -0X12;
/**
* 下发 客户端渗透服务端init信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT
* @see AbstractHandleTcpDistributeClientPermeateServerInitTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT = -0X13;
/**
* 下发 客户端渗透服务端init close信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE
* @see AbstractHandleTcpDistributeClientPermeateServerCloseTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE = -0X14;
/**
* 下发 客户端渗透服务端通信通道关闭
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE
* @see AbstractHandleTcpDistributeClientPermeateServerTransferCloseTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER_CLOSE = -0X15;
/**
* 下发 客户端渗透服务端通信传输
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER
* @see AbstractHandleTcpDistributeClientPermeateServerTransferTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_SERVER_TRANSFER = -0X16;
/**
* 下发 客户端渗透客户端init信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT
* @see AbstractHandleTcpDistributeClientPermeateClientInitTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT = -0X19;
/**
* 下发 客户端渗透客户端init close信息
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_CLOSE
* @see AbstractHandleTcpDistributeClientPermeateClientCloseTypeAdvanced
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_CLOSE = -0X20;
/**
* 下发 客户端渗透客户端数据传输通道连接成功
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
* @see AbstractHandleTcpDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced
* @see MessageType#TCP_REPORT_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
*/
public static final byte TCP_DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL = -0X21;
/**
* 下发 客户端渗透客户端数据传输通道init 成功
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL
* @see AbstractHandleTcpDistributeClientTransferClientPermeateChannelInitSuccessfulTypeAdvanced
* @see MessageType#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL = -0X22;
/**
* 下发 客户端渗透客户端数据传输请求
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST
* @see AbstractHandleTcpDistributeClientTransferClientRequestTypeAdvanced
* @see MessageType#TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_REQUEST = -0X23;
/**
* 下发客户端渗透客户端数据传输响应
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_RESPONSE
* @see AbstractHandleTcpDistributeServicePermeateClientTransferClientResponseTypeAdvanced
* @see MessageType#TCP_REPORT_CLIENT_TRANSFER_CLIENT_RESPONSE
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_RESPONSE = -0X24;
/**
* 下发客户端渗透客户端通信通道关闭
*
* @see MessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
* @see AbstractHandleTcpDistributeClientPermeateClientTransferCloseTypeAdvanced
* @see MessageType#TCP_REPORT_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE
*/
public static final byte TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_TRANSFER_CLOSE = -0X25;
public static final byte DISTRIBUTE_CLUSTER_NODE_REGISTER_MESSAGE = -0X11;
}

View File

@@ -1,18 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@NoArgsConstructor
@Setter
@Getter
public class NettyByteBuf {
// body 长度 data 4
public static final int bodyLength = 4;
/**
* 消息传输数据
* byte[] 长度 4
*/
private byte[] data;
}

View File

@@ -56,9 +56,7 @@ public class NettyClientVisitorContext {
public static <T> void removeVisitorSocket(String clientId, T visitorSocket) {
List<Object> visitorSocketList = VISITOR_SOCKET.get(clientId);
if(!ObjectUtils.isEmpty(visitorSocketList)){
while (visitorSocketList.iterator().hasNext() && visitorSocketList.iterator().next().equals(visitorSocket)) {
visitorSocketList.iterator().remove();
}
visitorSocketList.remove(visitorSocket);
VISITOR_SOCKET.put(clientId,visitorSocketList);
}
}

View File

@@ -5,7 +5,6 @@ import lombok.Setter;
import java.util.Arrays;
@Deprecated
@Setter
@Getter
public class NettyMsg {

View File

@@ -3,7 +3,6 @@ package org.framework.lazy.cloud.network.heartbeat.common;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.wu.framework.core.utils.ObjectUtils;
import java.nio.charset.StandardCharsets;
@@ -14,8 +13,8 @@ import java.nio.charset.StandardCharsets;
@Setter
@Getter
public class NettyProxyMsg {
// body 长度 type 1 isSsl 1 appKey 4 appSecret 4 clientId 4 originalIp 4 clientTargetIp 4 clientTargetPort 4 visitorPort 4 visitorId 4 data 4
public static final int bodyLength = 1 + 1 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4;
// body 长度 type 1 clientId 4 clientTargetIp 4 clientTargetPort 4 visitorPort 4 visitorId 4 data 4
public static final int bodyLength = 1 + 4 + 4 + 4 + 4 + 4 + 4;
/**
@@ -26,34 +25,6 @@ public class NettyProxyMsg {
*/
private byte type;
/**
* 是否是ssl
* byte 长度 1
* 1 true
* 0 false
* @since 1.2.8
*/
private byte isSsl = 0;
/**
* 令牌key
* byte[] 长度 4
* @since 1.2.8
*/
private byte[] appKey;
/**
* 令牌密钥
* byte[] 长度 4
*
* @since 1.2.9
*/
private byte[] appSecret;
/**
* 原始IP
* byte[] 长度 4
*
* @since 1.2.9
*/
private byte[] originalIp;
/**
* 客户端ID
* byte[] 长度 4
@@ -106,78 +77,6 @@ public class NettyProxyMsg {
}
public void setAppKeyString(String appKey) {
if (ObjectUtils.isEmpty(appKey)) {
this.appKey = null;
} else {
this.appKey = appKey.getBytes(StandardCharsets.UTF_8);
}
}
public void setAppSecretString(String appSecret) {
if (ObjectUtils.isEmpty(appSecret)) {
this.appSecret = null;
} else {
this.appSecret = appSecret.getBytes(StandardCharsets.UTF_8);
}
}
/**
* 设置原始IP
*
* @param originalIp 原始IP
*/
public void setOriginalIpString(String originalIp) {
if (ObjectUtils.isEmpty(originalIp)) {
this.originalIp = null;
} else {
this.originalIp = originalIp.getBytes(StandardCharsets.UTF_8);
}
}
/**
* 获取应用密钥
*
* @return 应用密钥
*/
public String getAppSecretString() {
if (ObjectUtils.isEmpty(appSecret)) {
return null;
}
return new String(appSecret, StandardCharsets.UTF_8);
}
/**
* 获取应用key
*
* @return 应用key
*/
public String getAppKeyString() {
if (ObjectUtils.isEmpty(appKey)) {
return null;
}
return new String(appKey, StandardCharsets.UTF_8);
}
/**
* 获取原始IP字符串
*
* @return 原始IP字符串
*/
public String getOriginalIpString() {
if (ObjectUtils.isEmpty(originalIp)) {
return null;
}
return new String(originalIp, StandardCharsets.UTF_8);
}
public String getClientIdString() {
if (ObjectUtils.isEmpty(clientId)) {
return null;
}
return new String(clientId, StandardCharsets.UTF_8);
}
public void setClientTargetIp(byte[] clientTargetIp) {
this.clientTargetIp = clientTargetIp;
}
@@ -211,23 +110,4 @@ public class NettyProxyMsg {
}
public Boolean isSsl() {
return isSsl == 1;
}
public void isSsl(Boolean isSsl) {
if (isSsl) {
this.isSsl = 1;
} else {
this.isSsl = 0;
}
}
public byte getIsSsl() {
return isSsl;
}
public void setIsSsl(byte isSsl) {
this.isSsl = isSsl;
}
}

View File

@@ -1,44 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import java.util.concurrent.ConcurrentHashMap;
/**
* 渗透端口对应访客上下文
*/
public class NettyServerPermeateServerVisitorContext {
protected static final ConcurrentHashMap<Integer/*permeatePort*/, Object/*NettyServerPermeateServerVisitorSocket*/> PERMEATE_VISITOR_SOCKET = new ConcurrentHashMap<>();
/**
* 添加访客
*
* @param permeatePort 渗透端口
* @param permeateVisitorSocket 渗透访客socket
*/
public static <T> void pushServerPermeateServerVisitorSocket(Integer permeatePort, T permeateVisitorSocket) {
PERMEATE_VISITOR_SOCKET.put(permeatePort, permeateVisitorSocket);
}
/**
* 通过客户端ID获取客户端使用的访客socket
*
* @param <T> 渗透访客socket
* @param permeatePort 渗透端口
* @return 访客
*/
public static <T> T getServerPermeateServerVisitorSocket(Integer permeatePort) {
return (T) PERMEATE_VISITOR_SOCKET.getOrDefault(permeatePort, null);
}
/**
* 移除 渗透访客socket
*
* @param permeatePort 渗透端口
*/
public static void removeServerPermeateServerVisitorSockets(Integer permeatePort) {
PERMEATE_VISITOR_SOCKET.remove(permeatePort);
}
}

View File

@@ -1,78 +0,0 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import io.netty.channel.Channel;
import java.util.concurrent.ConcurrentHashMap;
/**
* 访客通信通道上下文(服务端、客户端 通信)
*/
public class NettyTransferChannelContext {
protected static final ConcurrentHashMap<String, Channel> VISITOR_ID = new ConcurrentHashMap<>();
/**
* 添加访客
*
* @param visitorId 访客id
* @param visitor 访客
*/
public static void pushVisitor(Channel visitor, String visitorId) {
VISITOR_ID.put(visitorId, visitor);
}
/**
* 添加访客
*
* @param visitorId 访客id
* @param visitor 访客
*/
public static void pushVisitor(Channel visitor, byte[] visitorId) {
VISITOR_ID.put(new String(visitorId), visitor);
}
/**
* 通过访客端口获取访客
*
* @param visitorId 访客id
* @return 访客
*/
public static Channel getVisitor(String visitorId) {
return VISITOR_ID.get(visitorId);
}
/**
* 通过访客端口获取访客
*
* @param visitorId 访客id
* @param <T> 访客范型
* @return 访客
*/
public static Channel getVisitor(byte[] visitorId) {
return getVisitor(new String(visitorId));
}
/**
* 移除访客
*
* @param visitorId 访客ID
*/
public static void clear(String visitorId) {
Channel visitor = getVisitor(visitorId);
if (visitor != null) {
VISITOR_ID.remove(visitorId);
visitor.close();
}
}
/**
* 移除访客
*
* @param visitorId 访客ID
*/
public static void clear(byte[] visitorId) {
clear(new String(visitorId));
}
}

View File

@@ -7,9 +7,10 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* 访客通信通道上下文服务端客户端 通信
*/
public class NettyVisitorIdChannelContext {
@Deprecated
public class NettyVisitorIdContext {
protected static final ConcurrentHashMap<String, Channel> VISITOR_ID = new ConcurrentHashMap<>();
protected static final ConcurrentHashMap<String, Object> VISITOR_ID = new ConcurrentHashMap<>();
/**
@@ -18,21 +19,11 @@ public class NettyVisitorIdChannelContext {
* @param visitorId 访客id
* @param visitor 访客
*/
public static void pushVisitor(Channel visitor, String visitorId) {
public static <T> void pushVisitor(T visitor, String visitorId) {
VISITOR_ID.put(visitorId, visitor);
}
/**
* 通过访客端口获取访客
*
* @param visitorId 访客id
* @return 访客
*/
public static Channel getVisitor(String visitorId) {
return VISITOR_ID.get(visitorId);
}
/**
* 通过访客端口获取访客
*
@@ -40,7 +31,18 @@ public class NettyVisitorIdChannelContext {
* @param <T> 访客范型
* @return 访客
*/
public static Channel getVisitor(byte[] visitorId) {
public static <T> T getVisitor(String visitorId) {
return (T) VISITOR_ID.get(visitorId);
}
/**
* 通过访客端口获取访客
*
* @param visitorId 访客id
* @param <T> 访客范型
* @return 访客
*/
public static <T> T getVisitor(byte[] visitorId) {
return getVisitor(new String(visitorId));
}

View File

@@ -1,8 +1,5 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import io.netty.channel.Channel;
import org.framework.lazy.cloud.network.heartbeat.common.socket.PermeateVisitorSocket;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -10,8 +7,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class NettyVisitorPortContext {
protected static final ConcurrentHashMap<Integer, Channel> VISITOR_PORT_CHANNEL = new ConcurrentHashMap<>();
protected static final ConcurrentHashMap<Integer, PermeateVisitorSocket> VISITOR_PORT_SOCKET = new ConcurrentHashMap<>();
protected static final ConcurrentHashMap<Integer, Object> VISITOR_PORT = new ConcurrentHashMap<>();
/**
@@ -20,20 +16,11 @@ public class NettyVisitorPortContext {
* @param visitorPort 访客端口
* @param visitor 访客
*/
public static void pushVisitorChannel(Integer visitorPort, Channel visitor) {
VISITOR_PORT_CHANNEL.put(visitorPort, visitor);
public static <T> void pushVisitor(Integer visitorPort, T visitor) {
VISITOR_PORT.put(visitorPort, visitor);
}
/**
* 添加访客
*
* @param visitorPort 访客端口
* @param visitorSocket 访客socket
*/
public static <T> void pushVisitorSocket(Integer visitorPort, PermeateVisitorSocket visitorSocket) {
VISITOR_PORT_SOCKET.put(visitorPort, visitorSocket);
}
/**
* 通过访客端口获取访客
*
@@ -41,45 +28,22 @@ public class NettyVisitorPortContext {
* @param <T> 访客范型
* @return 访客
*/
public static Channel getVisitorChannel(Integer visitorPort) {
return VISITOR_PORT_CHANNEL.get(visitorPort);
public static <T> T getVisitor(Integer visitorPort) {
return (T) VISITOR_PORT.get(visitorPort);
}
/**
* 通过访客端口获取访客socket
*
* @param visitorPort 访客端口
* @param <T> 访客范型
* @return 访客
*/
public static PermeateVisitorSocket getVisitorSocket(Integer visitorPort) {
return VISITOR_PORT_SOCKET.get(visitorPort);
}
/**
* 删除访客
*
* 删除访客
* @param visitorPort 访客通道
* @param <T> 访客通道范型
* @return 删除的访客通道
* @param <T> 访客通道范型
*/
public static void removeVisitorChannel(Integer visitorPort) {
Channel visitor = getVisitorChannel(visitorPort);
public static <T> T removeVisitor(Integer visitorPort){
T visitor = getVisitor(visitorPort);
if(visitor!=null){
VISITOR_PORT_CHANNEL.remove(visitorPort);
}
}
/**
* 删除访客 socket
*
* @param visitorPort 访客通道
* @param <T> 访客通道范型
*/
public static void removeVisitorSocket(Integer visitorPort) {
PermeateVisitorSocket visitor = getVisitorSocket(visitorPort);
if (visitor != null) {
VISITOR_PORT_SOCKET.remove(visitorPort);
VISITOR_PORT.remove(visitorPort);
}
return visitor;
}
}

Some files were not shown because too many files have changed in this diff Show More