Skip to content

Commit

Permalink
v2.1 调整代码结构,支持native打包;clientId校验缺陷调整为用户名、密码校验
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Aug 12, 2024
1 parent 4f0aff2 commit 3b53718
Show file tree
Hide file tree
Showing 72 changed files with 1,584 additions and 222 deletions.
1 change: 1 addition & 0 deletions META-INF/native-image/.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22916
60 changes: 0 additions & 60 deletions common/pom.xml

This file was deleted.

28 changes: 0 additions & 28 deletions common/src/main/resources/log4j2.xml

This file was deleted.

23 changes: 0 additions & 23 deletions common/src/test/resources/log4j2.xml

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


<modules>
<module>common</module>
<module>run</module>
<module>_localtest</module>
<module>kafkademo</module>
</modules>

<properties>
<java.version>21</java.version>
<native.maven.plugin.version>0.10.2</native.maven.plugin.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
91 changes: 74 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@ hppt,一款可通过任意协议转发tcp端口的工具。
![hppt](_doc/img/1.jpg)

# 快速开始
[releases](https://github.com/codingmiao/hppt/releases)
页面下载最新版本编译好的hppt,或自行下载源码编译。
```shell
#jar
mvn clean package -DskipTests

#可选操作 打native包
su graalvm
cd run
mvn org.graalvm.buildtools:native-maven-plugin:build
```

本项目需要jdk21启动,请先前往[jdk官网](https://jdk.java.net/21/)
下载对应你操作系统版本的jdk,然后在[releases](https://github.com/codingmiao/hppt/releases)
页面下载最新版本编译好的hppt.zip,或自行下载源码编译
本项目编译成了可执行文件及jar包。

可执行文件无环境依赖、内存占用,小单因为没有jit支持,性能略逊于jar包执行;

jar包执行性能更好,但多消耗一些内存,如需jar包执行,请先前往[jdk官网](https://jdk.java.net/archive/)下载对应你操作系统版本的jdk21。

## 示例1 通过http端口,反向代理访问内部服务器SSH端口

假设你有一个服务器集群,仅有一个nginx提供了80/443端口对外访问(111.222.33.44:80),你想要访问集群中的应用服务器(192.168.0.2)的22端口,则可以按如下结构部署

![示例1](_doc/img/3.jpg)

1、在集群中任一服务器上新建一个hppt目录,并上传hppt.jar、ss.yml、log4j2.xml文件到此目录下:
1、在集群中任一服务器上新建一个hppt目录,并上传hppt.jar(也可用可执行文件 hppt.ext 或 hppt)、ss.yml、log4j2.xml文件到此目录下:

```
hppt
Expand All @@ -41,23 +54,43 @@ hppt
type: post
#服务http端口
port: 20871
# 允许的客户端id
clientIds:
- user1
- user2
# 允许的客户端账号和密码
clients:
- user: user1
password: 12345
- user: user2
password: 112233

```
(注1:作为快速演示,这里的type选择了最简单的post类型,此场景下最佳性能的协议为websocket,或是有独立端口的话可以配置hppt协议,ws、hppt版的说明奋力码字中。。)

(注2:实际应用中,为了确保安全,建议把clientId设置得更复杂一些)

执行如下命令运行服务端的hppt
执行如下命令运行服务端的hppt(3选1)

jar包运行
```shell
cd hppt
<jdk21_path>/bin/java -jar hppt.jar ss ss.yml
```

windows下可执行文件运行
```shell
cd hppt
chcp 65001
title "hppt"
hppt.exe ss ss.yml
pause
```

linux下可执行文件运行
```shell
cd hppt
./hppt ss ss.yml
#后台运行用命令 nohup ./hppt ss ss.yml >/dev/null &
```


在nginx上增加一段配置指向hppt

```
Expand Down Expand Up @@ -87,8 +120,10 @@ hppt
```yaml
# 和服务端的type保持一致
type: post
# 客户端id,必须在服务端的clientIds列表里才能成功连接,每个客户端用一个id,不要重复
clientId: user1
# 客户端用户名,每个sc进程用一个,不要重复
clientUser: user1
# 客户端密码
clientPassword: 12345

post:
# 服务端http地址,如无法直连,用nginx代理几次填nginx的地址也ok
Expand All @@ -108,13 +143,30 @@ forwards:

```

执行如下命令启动客户端的hppt
执行如下命令启动客户端的hppt(3选1)

jar包运行
```shell
cd hppt
<jdk21_path>/bin/java -jar hppt.jar sc sc.yml
```

windows下可执行文件运行
```shell
cd hppt
chcp 65001
title "hppt"
hppt.exe sc sc.yml
pause
```

linux下可执行文件运行
```shell
cd hppt
./hppt sc sc.yml
#后台运行用命令 nohup ./hppt ss ss.yml >/dev/null &
```

随后,你就可以在公司用linux连接工具访问localhost的10022端口,来登录应用服务器了


Expand All @@ -138,8 +190,10 @@ hppt
```yaml
# 通讯协议 本示例使用了性能最好的hppt协议,加r前缀表示客户端和服务端角色互换。这里也可以配http post或websocket
type: rhppt
# 客户端id
clientId: user1
# 客户端用户名,每个sc进程用一个,不要重复
clientUser: user1
# 客户端密码
clientPassword: 12345

# 服务端口
rhppt:
Expand Down Expand Up @@ -185,9 +239,12 @@ rhppt:
host: "111.222.33.44"
port: 20871

# 对应的的客户端id
clientIds:
- user1
# 允许的客户端账号和密码
clients:
- user: user1
password: 12345
- user: user2
password: 112233

```

Expand Down
68 changes: 63 additions & 5 deletions run/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,48 @@
<artifactId>run</artifactId>
<dependencies>
<dependency>
<groupId>org.wowtools.hppt</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.wowtools</groupId>
<artifactId>catframe-common</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-jakarta-servlet-api</artifactId>
<version>5.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down Expand Up @@ -83,7 +115,33 @@
</execution>
</executions>
</plugin>
<!-- 打native包 -->
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native.maven.plugin.version}</version>
<configuration>
<buildArgs>
<arg>-H:+AddAllCharsets</arg>
</buildArgs>
<mainClass>org.wowtools.hppt.run.Run</mainClass>
<imageName>hppt</imageName>
</configuration>
</plugin>
</plugins>
</build>

<pluginRepositories>
<pluginRepository>
<id>graalvm-native-build-tools-snapshots</id>
<name>GraalVM native-build-tools Snapshots</name>
<url>https://raw.githubusercontent.com/graalvm/native-build-tools/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static boolean afterWrite(ChannelFuture future, Object msg) {
}

private static void waitChannelWritable(Channel channel) {
while (!channel.isWritable()) {
while (!channel.isWritable() && channel.isOpen()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Expand Down
Loading

0 comments on commit 3b53718

Please sign in to comment.