feat(添加了ssl支持): 4.2.9.16 添加SSL支持

2025-09-21-1
This commit is contained in:
叁玖领域 2025-09-21 16:40:33 +08:00
parent c14d77400d
commit 67f4513817
5 changed files with 132 additions and 22 deletions

View File

@ -3,16 +3,13 @@ name: Build and Release
on:
push:
branches:
- "rebuild-develop" # develop分支
- "rebuild-release" # release 分支
tags:
- 'build-*' # 打 tag 时跑 build + release
- "rebuild-develop" # develop 分支只构建
- "rebuild-release" # release 分支构建 + 发布
workflow_dispatch: # 允许手动触发
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
@ -32,8 +29,8 @@ jobs:
- name: Upload common jar
uses: actions/upload-artifact@v4
with:
name: Common
path: Common/build/libs/*.jar
name: Common
path: Common/build/libs/*.jar
- name: Upload common api jar
uses: actions/upload-artifact@v4
@ -44,40 +41,38 @@ jobs:
release:
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/build-') # 只有打 tag 才执行 release
if: github.ref_name == 'rebuild-release' # 仅在 release 分支执行 release job
steps:
# 下载 Common JAR
# 下载构建产物
- name: Download Common artifact
uses: actions/download-artifact@v4
with:
name: Common
path: ./build-artifacts/Common
# 下载 CommonApi JAR
- name: Download CommonApi artifact
uses: actions/download-artifact@v4
with:
name: CommonApi
path: ./build-artifacts/CommonApi
# 创建 Release
# 创建 GitHub Release
- name: Create GitHub Release
id: create_release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
body: |
自动发布所有子项目构建产物。
tag_name: "release-${{ github.run_number }}" # 可用 run_number 或自定义版本号
name: Release ${{ github.run_number }}
body: 自动发布所有子项目构建产物
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 上传子项目 JAR 到 Release
- name: Upload jars to Release separately
# 上传子项目 JAR
- name: Upload jars to Release
run: |
for file in ./build-artifacts/**/*.jar; do
echo "Uploading $file"
gh release upload "${GITHUB_REF_NAME}" "$file" --repo "${GITHUB_REPOSITORY}" --clobber
gh release upload "release-${GITHUB_RUN_NUMBER}" "$file" --repo "${GITHUB_REPOSITORY}" --clobber
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@ -32,9 +32,15 @@ import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
@ -56,6 +62,38 @@ public abstract class AbstractWebSocketClient {
* 端口
*/
protected volatile int Port;
/**
* 启用 SSL
*/
public void enableSSL() {
enableSSL(null);
}
/**
* 启用 SSL
* @param certFile 可选证书文件
*/
public void enableSSL(@Nullable File certFile) {
this.SslEnabled = true;
this.CertFile = certFile;
}
/**
* 禁用SSL
*/
public void disableSSL() {
this.SslEnabled = false;
}
/**
* 是否启用 SSL
*/
boolean SslEnabled;
/**
* 客户端可选信任证书
*/
File CertFile;
/**
* 客户端启动类
*/
@ -84,6 +122,14 @@ public abstract class AbstractWebSocketClient {
return this.ClientStatus;
}
/**
* 是否启用 SSL
*
* @return enable
*/
public boolean isSslEnabled() {
return this.SslEnabled;
}
/**
* Sets status.
*
@ -213,8 +259,21 @@ public abstract class AbstractWebSocketClient {
ClientBootstrap.channel(NioSocketChannel.class);
ClientBootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
protected void initChannel(NioSocketChannel ch) throws SSLException {
ChannelPipeline pipeline = ch.pipeline();
// SSL 支持
if (SslEnabled) {
SslContext sslCtx;
if (CertFile != null && CertFile.exists()) {
sslCtx = SslContextBuilder.forClient()
.trustManager(CertFile)
.build();
} else {
// 默认信任所有受信任的 CA
sslCtx = SslContextBuilder.forClient().build();
}
pipeline.addFirst(sslCtx.newHandler(ch.alloc(), Address, Port));
}
pipeline.addLast(DgLab.LOGGING_HANDLER,
new HttpClientCodec(),
new HttpObjectAggregator(65536),

View File

@ -31,9 +31,14 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.io.File;
import java.util.concurrent.CompletableFuture;
/**
@ -47,6 +52,21 @@ public abstract class AbstractWebSocketServer {
*/
protected static final Logger logger = LoggerFactory.getLogger(AbstractWebSocketServer.class);
/**
* Should enable SSL
*/
boolean SslEnabled;
/**
* Cert File
*/
File CertFile;
/**
* Key File
*/
File KeyFile;
private int Port;
/**
@ -131,6 +151,33 @@ public abstract class AbstractWebSocketServer {
return Port;
}
/**
* 获取SSL是否开启
*
* @return enable
*/
public boolean isSSLEnabled() {
return SslEnabled;
}
/**
* 启用SSL
* @param certFile 证书文件
* @param keyFile 私钥文件
*/
public void enableSSL(@NotNull File certFile, @NotNull File keyFile) {
this.SslEnabled = true;
this.CertFile = certFile;
this.KeyFile = keyFile;
}
/**
* 禁用SSL
*/
public void disableSSL() {
this.SslEnabled = false;
}
/**
* 提供实现此处添加对Message处理Handler
*
@ -159,8 +206,13 @@ public abstract class AbstractWebSocketServer {
ServerBootstrap.channel(NioServerSocketChannel.class);
ServerBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
protected void initChannel(NioSocketChannel ch) throws SSLException {
ChannelPipeline pipeline = ch.pipeline();
// 根据配置启用 SSL
if (SslEnabled) {
SslContext sslCtx = SslContextBuilder.forServer(CertFile, KeyFile).build();
pipeline.addFirst(sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast(DgLab.LOGGING_HANDLER);
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
@ -176,7 +228,7 @@ public abstract class AbstractWebSocketServer {
ServerChannel = channelFuture.channel();
ServerStatus = Status.RUNNING;
started();
logger.info("WebSocketServer start on the port of {}", Port);
logger.info("WebSocketServer start on the port of {}{}", Port, SslEnabled ? " (SSL)" : "");
logger.debug("WebSocketServer listening on port {}", Port);
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {

View File

@ -7,7 +7,7 @@ org.gradle.configuration-cache=true
org.gradle.configuration-cache.problems=warn
# ROOT
project_name=DgLab
project_version=4.2.8.16
project_version=4.2.9.16
project_group=top.r3944realms.dg_lab
# API

View File

@ -2,6 +2,10 @@
统一用4位版本对于测试性更新统一在其后加-Beta。
修复问题更新为加0.0.0.1,添加/移除新特性加0.0.1.0小部分重构更新加0.1.0.0大量重构加1.0.0.0
2025-09-21-1
project_version=4.2.9.16
* 添加了SSL支持
2025-09-13-1
project_version=4.2.8.16
* 重构项目结构