From ba90c2227b9a2d5916fd8cc8b26dc5fad9620f39 Mon Sep 17 00:00:00 2001 From: MrMicky Date: Sat, 28 Dec 2019 13:38:24 +0100 Subject: [PATCH] Fix HTTP server on outdated servers --- .../com/azuriom/azlink/common/AzLinkPlugin.java | 10 ++++++---- .../azlink/common/http/server/HttpHandler.java | 16 +++++++++++++--- universal-legacy/build.gradle | 5 +++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/com/azuriom/azlink/common/AzLinkPlugin.java b/common/src/main/java/com/azuriom/azlink/common/AzLinkPlugin.java index 45a09a9..d6f3735 100644 --- a/common/src/main/java/com/azuriom/azlink/common/AzLinkPlugin.java +++ b/common/src/main/java/com/azuriom/azlink/common/AzLinkPlugin.java @@ -74,6 +74,10 @@ public void init() { scheduler.scheduleAtFixedRate(fetcherTask, startDelay, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS); + if (config.hasInstantCommands()) { + platform.executeAsync(httpServer::startSafe); + } + if (!config.isValid()) { getLogger().warn("Invalid configuration, you can use '/azlink' to setup the plugin."); return; @@ -88,10 +92,6 @@ public void init() { getLogger().warn("Unable to verify website connection: " + e.getMessage() + " - " + e.getClass().getName()); } }); - - if (config.hasInstantCommands()) { - platform.executeAsync(httpServer::startSafe); - } } public void restartHttpServer() throws Exception { @@ -103,8 +103,10 @@ public void restartHttpServer() throws Exception { } public void shutdown() { + getLogger().info("Shutting down scheduler"); scheduler.shutdown(); + getLogger().info("Stopping HTTP server"); httpServer.stopSafe(); } diff --git a/common/src/main/java/com/azuriom/azlink/common/http/server/HttpHandler.java b/common/src/main/java/com/azuriom/azlink/common/http/server/HttpHandler.java index 4c744fb..55c09cf 100644 --- a/common/src/main/java/com/azuriom/azlink/common/http/server/HttpHandler.java +++ b/common/src/main/java/com/azuriom/azlink/common/http/server/HttpHandler.java @@ -24,17 +24,27 @@ public HttpHandler(AzLinkPlugin plugin) { @Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) { - if (!request.uri().equals("/")) { + //noinspection deprecation - Request#uri is not avaible on old MC versions :( + String uri = request.getUri(); + //noinspection deprecation - Request#method is not avaible on old MC versions :( + HttpMethod method = request.getMethod(); + + if (!uri.equals("/")) { close(ctx, writeResponse(HttpResponseStatus.NOT_FOUND, "Error: Not Found")); return; } - if (request.method() == HttpMethod.GET) { + if (method == HttpMethod.GET) { close(ctx, writeResponse(HttpResponseStatus.OK, "Status: OK")); return; } - if (request.method() == HttpMethod.POST) { + if (method == HttpMethod.POST) { + if (!plugin.getConfig().isValid()) { + close(ctx, writeResponse(HttpResponseStatus.UNPROCESSABLE_ENTITY, "Error: Invalid configuration")); + return; + } + String siteKeyHash = Hash.SHA_256.hash(plugin.getConfig().getSiteKey()); if (!siteKeyHash.equals(request.headers().get("Authorization"))) { diff --git a/universal-legacy/build.gradle b/universal-legacy/build.gradle index e15141c..9c8e7fc 100644 --- a/universal-legacy/build.gradle +++ b/universal-legacy/build.gradle @@ -9,6 +9,9 @@ dependencies { compile project(':azlink-sponge') compile 'com.google.code.gson:gson:2.8.5' + + compile 'io.netty:netty-handler:4.1.38.Final' + compile 'io.netty:netty-codec-http:4.1.38.Final' } shadowJar { @@ -16,6 +19,8 @@ shadowJar { relocate 'okio', 'com.azuriom.azlink.libs.okio' relocate 'okhttp3', 'com.azuriom.azlink.libs.okhttp3' + relocate 'com.google.gson', 'com.azuriom.azlink.libs.gson' + relocate 'io.netty', 'com.azuriom.azlink.libs.netty' } artifacts {