From ccc562913cbc4d82255e667fd4854575ddfd8842 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 19 Sep 2017 18:47:43 +0300 Subject: [PATCH] BugFixing the Connection between Bungee and Bukkit --- .../main/java/com/djrapitops/plan/Plan.java | 2 +- .../plan/command/commands/DevCommand.java | 17 ++++++++++++----- .../info/BukkitInformationManager.java | 19 +++++++++++-------- .../info/BungeeInformationManager.java | 2 +- .../plan/systems/info/InformationManager.java | 6 +++--- .../info/server/BungeeServerInfoManager.java | 11 ++++++++++- .../systems/webserver/APIRequestHandler.java | 3 +++ .../systems/webserver/APIResponseHandler.java | 2 +- .../systems/webserver/ResponseHandler.java | 2 ++ .../plan/systems/webserver/WebServer.java | 2 +- .../plan/systems/webserver/webapi/WebAPI.java | 5 ++++- .../plan/utilities/file/FileUtil.java | 3 +-- 12 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 84c5011c4..cc58fcab6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -180,7 +180,7 @@ public class Plan extends BukkitPlugin implements IPlan { this.api = new API(this); - boolean usingBungeeWebServer = infoManager.isUsingBungeeWebServer(); + boolean usingBungeeWebServer = infoManager.isUsingAnotherWebServer(); boolean usingAlternativeIP = Settings.SHOW_ALTERNATIVE_IP.isTrue(); if (!usingAlternativeIP && serverVariableHolder.getIp().isEmpty()) { diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java index 91ad675ec..1f6d91134 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java @@ -43,14 +43,14 @@ public class DevCommand extends SubCommand { if (!Check.isTrue(args.length >= 2, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) { break; } - if (!webapi(args[1] + "webapi")) { + if (!webapi(args[1] + "webapi", args.length >= 3)) { sender.sendMessage("[Plan] No such API / Exception occurred."); } break; case "web": Optional bungeeConnectionAddress = plugin.getServerInfoManager().getBungeeConnectionAddress(); String accessAddress = plugin.getWebServer().getAccessAddress(); - sender.sendMessage((plugin.getInfoManager().isUsingBungeeWebServer() && bungeeConnectionAddress.isPresent()) + sender.sendMessage((plugin.getInfoManager().isUsingAnotherWebServer() && bungeeConnectionAddress.isPresent()) ? "Bungee: " + bungeeConnectionAddress.get() : "Local: " + accessAddress); break; default: @@ -59,16 +59,23 @@ public class DevCommand extends SubCommand { return true; } - private boolean webapi(String method) { + private boolean webapi(String method, boolean connectToBungee) { WebAPI api = plugin.getWebServer().getWebAPI().getAPI(method); if (api == null) { return false; } try { + String address = plugin.getWebServer().getAccessAddress(); + if (connectToBungee) { + Optional bungeeConnectionAddress = plugin.getServerInfoManager().getBungeeConnectionAddress(); + if (bungeeConnectionAddress.isPresent()) { + address = bungeeConnectionAddress.get(); + } + } if (api instanceof InspectWebAPI) { - ((InspectWebAPI) api).sendRequest(plugin.getWebServer().getAccessAddress(), UUID.randomUUID()); + ((InspectWebAPI) api).sendRequest(address, UUID.randomUUID()); } else { - api.sendRequest(plugin.getWebServer().getAccessAddress()); + api.sendRequest(address); } return true; } catch (WebAPIException e) { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java index 58b4c2719..1460db343 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java @@ -64,9 +64,9 @@ public class BukkitInformationManager extends InformationManager { Optional bungeeConnectionAddress = plugin.getServerInfoManager().getBungeeConnectionAddress(); if (bungeeConnectionAddress.isPresent()) { webServerAddress = bungeeConnectionAddress.get(); - usingBungeeWebServer = attemptConnection(); + usingAnotherWebServer = attemptConnection(); } else { - usingBungeeWebServer = false; + usingAnotherWebServer = false; } } @@ -79,7 +79,7 @@ public class BukkitInformationManager extends InformationManager { @Override public void cachePlayer(UUID uuid) { - if (usingBungeeWebServer) { + if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid)); } catch (WebAPIException e) { @@ -101,7 +101,7 @@ public class BukkitInformationManager extends InformationManager { } public void cacheInspectPluginsTab(UUID uuid, Class origin) { - if (usingBungeeWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) { + if (usingAnotherWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) { try { getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid); } catch (WebAPIException e) { @@ -118,7 +118,7 @@ public class BukkitInformationManager extends InformationManager { } public void cacheInspectPluginsTab(UUID uuid, String contents) { - if (usingBungeeWebServer) { + if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents); } catch (WebAPIException e) { @@ -141,7 +141,7 @@ public class BukkitInformationManager extends InformationManager { @Override public boolean isCached(UUID uuid) { - if (usingBungeeWebServer) { + if (usingAnotherWebServer) { try { return getWebAPI().getAPI(IsCachedWebAPI.class).isInspectCached(webServerAddress, uuid); } catch (WebAPIException e) { @@ -153,7 +153,7 @@ public class BukkitInformationManager extends InformationManager { @Override public boolean isAnalysisCached(UUID serverUUID) { - if (usingBungeeWebServer) { + if (usingAnotherWebServer) { try { return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID); } catch (WebAPIException e) { @@ -201,7 +201,7 @@ public class BukkitInformationManager extends InformationManager { } private void cacheAnalysisHtml() { - if (usingBungeeWebServer) { + if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, getAnalysisHtml()); } catch (WebAPIException e) { @@ -222,15 +222,18 @@ public class BukkitInformationManager extends InformationManager { @Override public boolean attemptConnection() { + Log.info("Attempting Bungee Connection.. ("+webServerAddress+")"); PingWebAPI api = getWebAPI().getAPI(PingWebAPI.class); try { api.sendRequest(webServerAddress); + Log.info("Bungee Connection OK"); return true; } catch (WebAPIConnectionFailException e) { plugin.getServerInfoManager().markConnectionFail(); } catch (WebAPIException e) { Log.toLog(this.getClass().getName(), e); } + Log.info("Bungee Connection Failed."); return false; } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java index 828907479..e16619dcf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java @@ -37,7 +37,7 @@ public class BungeeInformationManager extends InformationManager { private Map> pluginsTabContent; public BungeeInformationManager(PlanBungee plugin) throws SQLException { - usingBungeeWebServer = true; + usingAnotherWebServer = false; pluginsTabContent = new HashMap<>(); this.plugin = plugin; refreshBukkitServerMap(); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java index 124f747f0..124fbb9bb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java @@ -22,7 +22,7 @@ import java.util.UUID; * @author Rsl1122 */ public abstract class InformationManager { - boolean usingBungeeWebServer; + boolean usingAnotherWebServer; String webServerAddress; Set analysisNotification; @@ -76,8 +76,8 @@ public abstract class InformationManager { public abstract String getPluginsTabContent(UUID uuid); - public boolean isUsingBungeeWebServer() { - return usingBungeeWebServer; + public boolean isUsingAnotherWebServer() { + return usingAnotherWebServer; } public abstract String getWebServerAddress(); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/server/BungeeServerInfoManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/server/BungeeServerInfoManager.java index f602ecc4d..f7ff81165 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/server/BungeeServerInfoManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/server/BungeeServerInfoManager.java @@ -79,9 +79,11 @@ public class BungeeServerInfoManager { public void attemptConnection(ServerInfo server) { if (server == null) { + Log.debug("Attempted a connection to a null ServerInfo"); return; } try { + Log.info("Attempting to connect to Bukkit server.. (" + server.getWebAddress() + ")"); plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(server.getWebAddress()); connectedToServer(server); } catch (WebAPIException e) { @@ -90,14 +92,20 @@ public class BungeeServerInfoManager { } public void connectedToServer(ServerInfo server) { + Log.info("Connection to Bukkit (" + server.getWebAddress() + ") succeeded."); bukkitServers.put(server.getUuid(), server); onlineServers.add(server.getUuid()); } public void serverConnected(UUID serverUUID) { + Log.info("Received a connection from a Bukkit server.."); try { Optional serverInfo = db.getServerTable().getServerInfo(serverUUID); - serverInfo.ifPresent(this::attemptConnection); + serverInfo.ifPresent(server -> { + Log.info("Server Info found from DB: " + server.getName()); + attemptConnection(server); + } + ); } catch (SQLException e) { Log.toLog(this.getClass().getName(), e); } @@ -115,6 +123,7 @@ public class BungeeServerInfoManager { } public void serverHasGoneOffline(UUID serverUUID) { + Log.info("Bukkit Server Marked Offline"); onlineServers.remove(serverUUID); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIRequestHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIRequestHandler.java index b7b540ff3..34d048cbf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIRequestHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIRequestHandler.java @@ -4,6 +4,7 @@ */ package main.java.com.djrapitops.plan.systems.webserver; +import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import main.java.com.djrapitops.plan.systems.webserver.response.Response; @@ -26,9 +27,11 @@ public class APIRequestHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { + Headers responseHeaders = exchange.getResponseHeaders(); Request request = new Request(exchange); try { Response response = responseHandler.getAPIResponse(request); + response.setResponseHeaders(responseHeaders); response.send(exchange); } finally { exchange.close(); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java index 41335f6d9..008b2d5fb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java @@ -68,7 +68,7 @@ public class APIResponseHandler { Map variables = readVariables(requestBody); String sender = variables.get("sender"); - + Log.debug("Received WebAPI Request" + target + " from " + sender); if (!checkKey(sender)) { String error = "Server Key not given or invalid"; return PageCache.loadPage(error, () -> { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java index 230cf5ad7..88d40b0ef 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java @@ -74,6 +74,8 @@ public class ResponseHandler extends APIResponseHandler { if (args.length < 2) { return rootPageResponse(user, serverUUID); } + } else if (args.length < 2) { + return notFoundResponse(); } String page = args[1]; diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java index d603e4105..4a8d404a0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java @@ -90,7 +90,7 @@ public class WebServer { server = HttpServer.create(new InetSocketAddress(port), 10); } - if (plugin.getInfoManager().isUsingBungeeWebServer()) { + if (plugin.getInfoManager().isUsingAnotherWebServer()) { server.createContext("/", new APIRequestHandler(getWebAPI())); } else { server.createContext("/", new RequestHandler(plugin, this)); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java index 62c4ab4d2..0c4397b7f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java @@ -5,6 +5,7 @@ package main.java.com.djrapitops.plan.systems.webserver.webapi; import com.djrapitops.plugin.utilities.Verify; +import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; @@ -95,7 +96,7 @@ public abstract class WebAPI { String serverUUID = MiscUtils.getIPlan().getServerUuid().toString(); parameters.append("sender=").append(serverUUID).append("&"); for (Map.Entry entry : variables.entrySet()) { - parameters.append("&").append(entry.getKey()).append(entry.getValue()); + parameters.append("&").append(entry.getKey()).append("=").append(entry.getValue()); } byte[] toSend = parameters.toString().getBytes(); int length = toSend.length; @@ -103,6 +104,7 @@ public abstract class WebAPI { connection.setRequestProperty("Content-Length", Integer.toString(length)); connection.setUseCaches(false); + Log.debug("Sending WebAPI Request: " + this.getClass().getSimpleName() + " to " + address); try (DataOutputStream out = new DataOutputStream(connection.getOutputStream())) { out.write(toSend); } @@ -123,6 +125,7 @@ public abstract class WebAPI { } catch (SocketTimeoutException e) { throw new WebAPIConnectionFailException("Connection timed out after 10 seconds.", e); } catch (NoSuchAlgorithmException | KeyManagementException | IOException e) { + Log.toLog(this.getClass().getName(), e); throw new WebAPIConnectionFailException("API connection failed. address: " + address, e); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java index 5eb0967a1..7d5637934 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java @@ -1,7 +1,6 @@ package main.java.com.djrapitops.plan.utilities.file; import com.djrapitops.plugin.utilities.Verify; -import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -28,7 +27,7 @@ public class FileUtil { InputStream resourceStream = null; Scanner scanner = null; try { - Plan plugin = Plan.getInstance(); + IPlan plugin = MiscUtils.getIPlan(); File localFile = new File(plugin.getDataFolder(), fileName); if (localFile.exists()) {