From afc84ebae8f854958cfa0ff6df0bae91cedfc847 Mon Sep 17 00:00:00 2001 From: Gravita Date: Wed, 24 Mar 2021 19:47:38 +0700 Subject: [PATCH] [FEATURE] RemoteControlModule --- CODE_OF_CONDUCT.md | 19 ++++--- .../manangers/PingServerManager.java | 1 + .../socket/handlers/NettyWebAPIHandler.java | 53 ++++++++++++++++++- .../management/PingServerReportRequest.java | 1 + .../pro/gravit/utils/helper/CommonHelper.java | 2 +- .../gravit/launcher/server/ServerWrapper.java | 7 ++- modules | 2 +- 7 files changed, 71 insertions(+), 14 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index b5bc7406..178c8c47 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,4 +1,5 @@ # GravitLauncher GitConvention # + Цель конвенции — внедрить простые, прозрачные и эффективные правила работы с Git. Разработка GravitLauncher идёт на базе [Git Flow](https://leanpub.com/git-flow/read). Подробности ниже. @@ -14,19 +15,23 @@ | **bugfix-*** | Исправляет баг нового функционала | **release** | *bugfix-auth* | | **feature-*** | Добавляет новую возможность | **develop** | *feature-auth* | | **hotfix-*** | Вносит срочное исправление для production-а | **master** | *hotfix-auth* | + ----- ![Image of GitFlow](https://i.ytimg.com/vi/w2r0oLFtXAw/maxresdefault.jpg) ----- ## Коммиты ## -**Основные правила:** -1. Все коммиты должны быть на русском языке. -2. Запрещено использовать прошедшее время. -3. Обязательно должен быть использован префикс. -4. В конце не должно быть лишнего знака препинания. -5. Длина любой части не должна превышать 100 символов. -**Структура:** +**Основные правила:** + +1. Все коммиты должны быть на русском языке. +2. Запрещено использовать прошедшее время. +3. Обязательно должен быть использован префикс. +4. В конце не должно быть лишнего знака препинания. +5. Длина любой части не должна превышать 100 символов. + +**Структура:** + ``` [Префикс] <Сообщение> ``` diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java index cf5d8a0b..7a56985f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java @@ -11,6 +11,7 @@ public class PingServerManager { public static final long REPORT_EXPIRED_TIME = 20 * 1000; public final Map map = new HashMap<>(); private final LaunchServer server; + public PingServerManager(LaunchServer server) { this.server = server; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java index cc3de99a..a6e3b2c4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java @@ -1,13 +1,26 @@ package pro.gravit.launchserver.socket.handlers; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import pro.gravit.launcher.Launcher; import pro.gravit.launchserver.socket.NettyConnectContext; +import pro.gravit.utils.helper.LogHelper; +import java.net.URLDecoder; +import java.nio.charset.Charset; import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; import java.util.TreeSet; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + public class NettyWebAPIHandler extends SimpleChannelInboundHandler { private static final TreeSet severletList = new TreeSet<>(Comparator.comparingInt((e) -> -e.key.length())); private final NettyConnectContext context; @@ -38,7 +51,12 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) thro boolean isNext = true; for (SeverletPathPair pair : severletList) { if (msg.uri().startsWith(pair.key)) { - pair.callback.handle(ctx, msg, context); + try { + pair.callback.handle(ctx, msg, context); + } catch (Throwable e) { + LogHelper.error(e); + ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer("Internal Server Error 500".getBytes())), ctx.voidPromise()); + } isNext = false; break; } @@ -52,6 +70,39 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) thro @FunctionalInterface public interface SimpleSeverletHandler { void handle(ChannelHandlerContext ctx, FullHttpRequest msg, NettyConnectContext context) throws Exception; + + default Map getParamsFromUri(String uri) { + int ind = uri.indexOf("?"); + if (ind <= 0) { + return Map.of(); + } + String sub = uri.substring(ind + 1); + String[] result = sub.split("&"); + Map map = new HashMap<>(); + for (int i = 0; i < result.length; ++i) { + String c = URLDecoder.decode(result[i], Charset.defaultCharset()); + int index = c.indexOf("="); + if (index <= 0) { + continue; + } + String key = c.substring(0, index); + String value = c.substring(index); + map.put(key, value); + } + return map; + } + + default FullHttpResponse simpleResponse(HttpResponseStatus status, String body) { + return new DefaultFullHttpResponse(HTTP_1_1, status, body != null ? Unpooled.wrappedBuffer(body.getBytes()) : Unpooled.buffer()); + } + + default FullHttpResponse simpleJsonResponse(HttpResponseStatus status, Object body) { + return new DefaultFullHttpResponse(HTTP_1_1, status, body != null ? Unpooled.wrappedBuffer(Launcher.gsonManager.gson.toJson(body).getBytes()) : Unpooled.buffer()); + } + + default void sendHttpResponse(ChannelHandlerContext ctx, FullHttpResponse response) { + ctx.writeAndFlush(response, ctx.voidPromise()); + } } public static class SeverletPathPair { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java index c6e6ee8a..05e10aa2 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java @@ -26,6 +26,7 @@ public static class PingServerReport { //Server addional info public double tps; //Server tps public List users; + public PingServerReport(String name, int maxPlayers, int playersOnline) { this.name = name; this.maxPlayers = maxPlayers; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java index 47ceeb24..d56ee473 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java @@ -55,7 +55,7 @@ public static String multiReplace(Pattern[] pattern, String from, String replace } public static ScriptEngine newScriptEngine() { - if(nashornFactory == null) { + if (nashornFactory == null) { throw new UnsupportedOperationException("ScriptEngine not supported"); } return nashornFactory.getScriptEngine(); diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java index f783aaf5..66ca428a 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java @@ -81,9 +81,8 @@ public boolean auth() { for (ClientProfile p : result.profiles) { LogHelper.debug("Get profile: %s", p.getTitle()); boolean isFound = false; - for(ClientProfile.ServerProfile srv : p.getServers()) - { - if(srv != null && srv.name.equals(config.serverName)) { + for (ClientProfile.ServerProfile srv : p.getServers()) { + if (srv != null && srv.name.equals(config.serverName)) { this.serverProfile = srv; this.profile = p; Launcher.profile = p; @@ -92,7 +91,7 @@ public boolean auth() { break; } } - if(isFound) break; + if (isFound) break; } if (profile == null) { LogHelper.warning("Not connected to ServerProfile. May be serverName incorrect?"); diff --git a/modules b/modules index a246b4a9..8fd13394 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit a246b4a966c428e626258b1e295a6c83673ab5a2 +Subproject commit 8fd133947a9316de7b0abb7980ed0d8cb310e246