Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
将 LittleServer 分为 Common Version 和 Legacy Version,Common Version 兼容 4…
Browse files Browse the repository at this point in the history
….1.14+ 版本,Legacy Version 兼容 4.0.0 - 4.1.14 版本,实装 CRC32 算法,实装自动更新功能,退出程序时自动关闭所有运行的服务端,最后一个 PREVIEW 版本。
  • Loading branch information
KasumiNova committed Sep 27, 2022
1 parent 3ee6a48 commit 39e37bd
Show file tree
Hide file tree
Showing 20 changed files with 1,293 additions and 1,020 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'github.kasuminova'
version '1.2.0-STABLE-PREVIEW_2'
version '1.2.0-STABLE-PREVIEW_4'


repositories {
Expand Down
2 changes: 1 addition & 1 deletion exe4j_configuration.exe4j
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<registry />
</searchSequence>
<classPath>
<archive location="C:/Users/Kasumi_Nova/IdeaProjects/BalloonServer/build/libs/BalloonServer-GUI-1.2.0-STABLE.jar" />
<archive location="C:/Users/Kasumi_Nova/IdeaProjects/BalloonServer/build/libs/BalloonServer-GUI-1.2.0-STABLE-PREVIEW_3.jar" />
</classPath>
</java>
</exe4j>
192 changes: 151 additions & 41 deletions src/main/java/github/kasuminova/balloonserver/BalloonServer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,17 @@
*/
public class ConfigurationManager {
public static void loadLittleServerConfigFromFile(String path, LittleServerConfig oldConfig) throws IOException {
LittleServerConfig config = JSON.parseObject(Files.newInputStream(Paths.get(path)), LittleServerConfig.class);
LittleServerConfig newConfig = JSON.parseObject(Files.newInputStream(Paths.get(path)), LittleServerConfig.class);

//配置文件版本验证
if (config.getConfigVersion() == 0) {
LittleServerConfig newConfig = new LittleServerConfig();
newConfig.setIp(config.getIp());
newConfig.setFileChangeListener(config.isFileChangeListener());
newConfig.setPort(config.getPort());
newConfig.setMainDirPath(config.getMainDirPath());
newConfig.setJksFilePath(config.getJksFilePath());
newConfig.setJksSslPassword(config.getJksSslPassword());
return;
}

oldConfig.setConfigVersion(config.getConfigVersion());
oldConfig.setIp(config.getIp());
oldConfig.setPort(config.getPort());
oldConfig.setMainDirPath(config.getMainDirPath());
oldConfig.setFileChangeListener(config.isFileChangeListener());
oldConfig.setJksFilePath(config.getJksFilePath());
oldConfig.setJksSslPassword(config.getJksSslPassword());
oldConfig.setCommonMode(config.getCommonMode());
oldConfig.setOnceMode(config.getOnceMode());
oldConfig.setConfigVersion(newConfig.getConfigVersion());
oldConfig.setIp(newConfig.getIp());
oldConfig.setPort(newConfig.getPort());
oldConfig.setMainDirPath(newConfig.getMainDirPath());
oldConfig.setFileChangeListener(newConfig.isFileChangeListener());
oldConfig.setJksFilePath(newConfig.getJksFilePath());
oldConfig.setJksSslPassword(newConfig.getJksSslPassword());
oldConfig.setCommonMode(newConfig.getCommonMode());
oldConfig.setOnceMode(newConfig.getOnceMode());
}
public static void loadBalloonServerConfigFromFile(String path, BalloonServerConfig oldConfig) throws IOException {
BalloonServerConfig config = JSON.parseObject(Files.newInputStream(Paths.get(path)), BalloonServerConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public LittleServerConfig() {
*/
public void reset() {
configVersion = 1;
ip = "0.0.0.0";
ip = "127.0.0.1";
port = 8080;
mainDirPath = "/res";
fileChangeListener = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.awt.*;

import static github.kasuminova.balloonserver.BalloonServer.GLOBAL_LOGGER;
import static github.kasuminova.balloonserver.BalloonServer.stopAllServers;

public class ConfirmExitDialog extends JDialog {
public ConfirmExitDialog(JFrame frame, BalloonServerConfig config) {
Expand Down Expand Up @@ -57,7 +58,8 @@ public ConfirmExitDialog(JFrame frame, BalloonServerConfig config) {
GLOBAL_LOGGER.warning("主程序配置文件保存失败!\n" + GUILogger.stackTraceToString(ex));
}
}

//停止所有正在运行的服务器并保存配置
stopAllServers(true);
System.exit(0);
}
//保存配置并最小化窗口
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public static JPanel getPanel() {
JLabel autoStartDefaultServerOnceDesc = new JLabel("此项选中后,BalloonServer 在启动时会自动启动主服务端的服务器,仅生效一次,生效后自动关闭.");

//自动检查更新
JLabel autoCheckUpdatesDesc = new JLabel("此项选中后,BalloonServer 在会定时检查最新更新.");
JLabel autoCheckUpdatesDesc = new JLabel("此项选中后,BalloonServer 在会在启动时检查最新更新.");

//自动更新
JLabel autoUpdateDesc = new JLabel("此项选中后,BalloonServer 在检查到更新后,会自动下载并自动重启应用更新(仅支持 exe 格式服务端).");
JLabel autoUpdateDesc = new JLabel("此项及“自动检查更新”项选中后,BalloonServer 在检查到更新后,会自动下载并自动重启应用更新,如果主服务端正在运行,则下次启动会自动启动服务器.");
//如果程序非 exe 格式则设置为禁用
if (!SettingsPanel.class.getProtectionDomain().getCodeSource().getLocation().getFile().endsWith(".exe")) {
if (!ARCHIVE_NAME.contains("e4j")) {
autoUpdate.setEnabled(false);
autoUpdate.setText("自动更新(不支持)");
autoUpdate.setText("自动更新(不支持,目前仅支持 exe 格式服务端)");
}

//关闭选项
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.URL;

import static github.kasuminova.balloonserver.BalloonServer.CONFIG;
import static github.kasuminova.balloonserver.BalloonServer.stopAllServers;

/**
* 中文系统托盘弹出菜单不乱码。
Expand Down Expand Up @@ -59,6 +60,8 @@ public void firePopupMenuWillBecomeInvisible() {
} catch (IOException ex) {
BalloonServer.GLOBAL_LOGGER.warning("保存主程序配置文件失败!");
}
//停止所有正在运行的服务器并保存配置
stopAllServers(true);
System.exit(0);
});
JMenuItem showMainFrame = new JMenuItem("显示窗口");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import github.kasuminova.balloonserver.Servers.LittleServerInterface;
import github.kasuminova.balloonserver.Utils.FileUtil;
import github.kasuminova.balloonserver.Utils.GUILogger;
import github.kasuminova.balloonserver.Utils.HashCalculator;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
Expand All @@ -31,11 +32,13 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ
private final LittleServerConfig config;
private final GUILogger logger;
private final JPanel requestListPanel;
private final String hashAlgorithm;
public HttpRequestHandler(LittleServerInterface serverInterface) {
this.resJson = serverInterface.getResJson();
this.config = serverInterface.getConfig();
this.logger = serverInterface.getLogger();
this.requestListPanel = serverInterface.getRequestListPanel();
this.hashAlgorithm = serverInterface.getHashAlgorithm();
}

@Override
Expand All @@ -60,6 +63,7 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) thro
index.put("common_mode", config.getCommonMode());
index.put("once_mode", config.getOnceMode());
index.put("update", config.getMainDirPath().replace("/", ""));
index.put("hash_algorithm", hashAlgorithm);
// 因为经过 HttpServerCodec 处理器的处理后消息被封装为 FullHttpRequest 对象
// 创建完整的响应对象
FullHttpResponse jsonResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK, Unpooled.copiedBuffer(index.toJSONString(), CharsetUtil.UTF_8));
Expand All @@ -73,8 +77,15 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) thro
return;
}

//如果使用旧版客户端获取新版服务端的缓存文件,则直接 403 请求并提示用户使用旧版服务端
if (hashAlgorithm.contains(HashCalculator.CRC32) && decodedURI.contains(config.getMainDirPath() + ".json")) {
sendError(ctx, HttpResponseStatus.FORBIDDEN, "当前服务端版本不兼容此版本客户端,请使用旧版服务端.(仅兼容 4.1.14+)\n".repeat(6), clientIP, decodedURI);
logger.error("检测到你可能正在使用旧版客户端获取新版服务端缓存文件,请使用旧版服务端。");
return;
}

//JSON 请求监听
if (decodedURI.contains(config.getMainDirPath() + ".json")) {
if (decodedURI.contains(config.getMainDirPath() + ".json") || decodedURI.contains(config.getMainDirPath() + "_crc32.json") && hashAlgorithm.contains(HashCalculator.CRC32)) {
// 检测 100 Continue,是否同意接收将要发送过来的实体
ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
// 经过 HttpServerCodec 处理器的处理后消息被封装为 FullHttpRequest 对象
Expand All @@ -97,6 +108,7 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) thro
sendError(ctx, HttpResponseStatus.FORBIDDEN, clientIP, decodedURI);
return;
}

//文件请求监听
File file = new File("." + decodedURI);
if (file.isHidden()||!file.exists()) {
Expand Down Expand Up @@ -213,7 +225,14 @@ private static String getClientIP(ChannelHandlerContext ctx, FullHttpRequest req

private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status, String clientIP, String decodedURI){
long start = System.currentTimeMillis();
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,status,Unpooled.copiedBuffer("Failure: "+status+"\r\n",CharsetUtil.UTF_8));
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,status,Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE,HttpHeaderValues.TEXT_PLAIN);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
printLog(String.valueOf(status.code()), start, clientIP, decodedURI);
}
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status, String message, String clientIP, String decodedURI){
long start = System.currentTimeMillis();
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,status,Unpooled.copiedBuffer(status + ": " + message + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE,HttpHeaderValues.TEXT_PLAIN);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
printLog(String.valueOf(status.code()), start, clientIP, decodedURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public boolean start() {
future = bootstrap.bind(new InetSocketAddress(ip, port)).sync();
String addressType = IPAddressUtil.checkAddress(ip);
assert addressType != null;

if ("v6".equals(addressType)) {
if (httpServerInitializer.isUseSsl()) {
logger.info(String.format("服务器已启动 (%sms),API 地址:http://%s:%s/index.json",
Expand Down
Loading

0 comments on commit 39e37bd

Please sign in to comment.