Fixed some WebAPI responses leading to infinite loops.

This commit is contained in:
Rsl1122 2017-11-10 10:22:33 +02:00
parent 209208eb51
commit 88d891333c
11 changed files with 102 additions and 43 deletions

View File

@ -0,0 +1,28 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* Group of WebAPIExceptions that can be considered a failed connection state on some occasions.
*
* @author Rsl1122
*/
public class WebAPIFailException extends WebAPIException {
public WebAPIFailException() {
}
public WebAPIFailException(String message) {
super(message);
}
public WebAPIFailException(String message, Throwable cause) {
super(message, cause);
}
public WebAPIFailException(Throwable cause) {
super(cause);
}
}

View File

@ -9,7 +9,7 @@ package main.java.com.djrapitops.plan.api.exceptions;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class WebAPIForbiddenException extends WebAPIException { public class WebAPIForbiddenException extends WebAPIFailException {
public WebAPIForbiddenException(String url) { public WebAPIForbiddenException(String url) {
super("Forbidden: " + url); super("Forbidden: " + url);
} }

View File

@ -0,0 +1,16 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* Thrown when WebAPI returns 404, usually when response is supposed to be false.
*
* @author Rsl1122
*/
public class WebAPIInternalErrorException extends WebAPIFailException {
public WebAPIInternalErrorException() {
super("Internal Error occurred on receiving server");
}
}

View File

@ -9,7 +9,7 @@ package main.java.com.djrapitops.plan.api.exceptions;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class WebAPINotFoundException extends WebAPIException { public class WebAPINotFoundException extends WebAPIFailException {
public WebAPINotFoundException() { public WebAPINotFoundException() {
super("Not Found"); super("Not Found");
} }

View File

@ -12,7 +12,6 @@ import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestSetupWebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestSetupWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebAPI;
import main.java.com.djrapitops.plan.utilities.Condition; import main.java.com.djrapitops.plan.utilities.Condition;
/** /**
@ -68,7 +67,7 @@ public class ManageSetupCommand extends SubCommand {
Config config = plugin.getMainConfig(); Config config = plugin.getMainConfig();
config.set(Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.getPath(), false); config.set(Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.getPath(), false);
config.set(Settings.BUNGEE_COPY_CONFIG.getPath(), true); config.set(Settings.BUNGEE_COPY_CONFIG.getPath(), true);
plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(address); // plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(address);
plugin.getWebServer().getWebAPI().getAPI(RequestSetupWebAPI.class).sendRequest(address); plugin.getWebServer().getWebAPI().getAPI(RequestSetupWebAPI.class).sendRequest(address);
sender.sendMessage("§eConnection successful, Plan may restart in a few seconds, if it doesn't something has gone wrong."); sender.sendMessage("§eConnection successful, Plan may restart in a few seconds, if it doesn't something has gone wrong.");
} catch (WebAPIException e) { } catch (WebAPIException e) {

View File

@ -10,7 +10,7 @@ import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.exceptions.ParseException; import main.java.com.djrapitops.plan.api.exceptions.ParseException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIForbiddenException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIFailException;
import main.java.com.djrapitops.plan.command.commands.AnalyzeCommand; import main.java.com.djrapitops.plan.command.commands.AnalyzeCommand;
import main.java.com.djrapitops.plan.data.AnalysisData; import main.java.com.djrapitops.plan.data.AnalysisData;
import main.java.com.djrapitops.plan.data.additional.HookHandler; import main.java.com.djrapitops.plan.data.additional.HookHandler;
@ -83,8 +83,8 @@ public class BukkitInformationManager extends InformationManager {
} else if (usingAnotherWebServer) { } else if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID); getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed to request Analysis refresh from Bungee.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
refreshAnalysis(serverUUID); refreshAnalysis(serverUUID);
@ -98,8 +98,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid)); getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid));
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed to request Inspect from Bungee.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
cachePlayer(uuid); cachePlayer(uuid);
@ -123,8 +123,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) { if (usingAnotherWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) {
try { try {
getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid); getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed send Player Plugins tab contents to BungeeCord.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
cacheInspectPluginsTab(uuid, origin); cacheInspectPluginsTab(uuid, origin);
@ -143,8 +143,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents); getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed send Player HTML to BungeeCord.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
cacheInspectPluginsTab(uuid, contents); cacheInspectPluginsTab(uuid, contents);
@ -169,8 +169,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isInspectCached(webServerAddress, uuid); return getWebAPI().getAPI(IsCachedWebAPI.class).isInspectCached(webServerAddress, uuid);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed check Bungee Player Cache status.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
return isCached(uuid); return isCached(uuid);
@ -187,8 +187,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID); return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed check Bungee Analysis Cache status.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
return isAnalysisCached(serverUUID); return isAnalysisCached(serverUUID);
@ -248,8 +248,8 @@ public class BukkitInformationManager extends InformationManager {
getWebAPI().getAPI(AnalysisReadyWebAPI.class).sendRequest(webServerAddress, serverUUID); getWebAPI().getAPI(AnalysisReadyWebAPI.class).sendRequest(webServerAddress, serverUUID);
updateNetworkPageContent(); updateNetworkPageContent();
return; return;
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException ignored) {
/* Do nothing */ Log.error("Failed to notify Bungee of Analysis Completion.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
} }
@ -265,8 +265,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, html); getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, html);
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException e) {
/* Do nothing */ Log.error("Failed to send Analysis HTML to Bungee Server.");
} catch (WebAPIException e) { } catch (WebAPIException e) {
attemptConnection(); attemptConnection();
cacheAnalysisHtml(html); cacheAnalysisHtml(html);
@ -337,7 +337,7 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(PostNetworkPageContentWebAPI.class).sendNetworkContent(webServerAddress, HtmlStructure.createServerContainer(plugin)); getWebAPI().getAPI(PostNetworkPageContentWebAPI.class).sendNetworkContent(webServerAddress, HtmlStructure.createServerContainer(plugin));
} catch (WebAPIForbiddenException ignored) { } catch (WebAPIFailException ignored) {
/* Do nothing */ /* Do nothing */
} catch (WebAPIException ignored) { } catch (WebAPIException ignored) {
attemptConnection(); attemptConnection();

View File

@ -136,18 +136,19 @@ public class BungeeServerInfoManager {
onlineServers.add(server.getUuid()); onlineServers.add(server.getUuid());
} }
public void serverConnected(UUID serverUUID) { public boolean serverConnected(UUID serverUUID) {
if (plugin.getServerUuid().equals(serverUUID)) { if (plugin.getServerUuid().equals(serverUUID)) {
return; return false;
} }
Log.info("Received a connection from a Bukkit server.."); Log.info("Received a connection from a Bukkit server..");
if (onlineServers.contains(serverUUID)) { if (onlineServers.contains(serverUUID)) {
sendConfigSettings(serverUUID); sendConfigSettings(serverUUID);
return; return true;
} }
try { try {
Optional<ServerInfo> serverInfo = db.getServerTable().getServerInfo(serverUUID); Optional<ServerInfo> serverInfo = db.getServerTable().getServerInfo(serverUUID);
serverInfo.ifPresent(server -> { if (serverInfo.isPresent()) {
ServerInfo server = serverInfo.get();
Log.info("Server Info found from DB: " + server.getName()); Log.info("Server Info found from DB: " + server.getName());
RunnableFactory.createNew("BukkitConnectionTask: " + server.getName(), new AbsRunnable() { RunnableFactory.createNew("BukkitConnectionTask: " + server.getName(), new AbsRunnable() {
@Override @Override
@ -157,11 +158,12 @@ public class BungeeServerInfoManager {
this.cancel(); this.cancel();
} }
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 3L); }).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 3L);
return true;
} }
);
} catch (SQLException e) { } catch (SQLException e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);
} }
return false;
} }
public Collection<ServerInfo> getOnlineBukkitServers() { public Collection<ServerInfo> getOnlineBukkitServers() {

View File

@ -52,6 +52,8 @@ public class WebServer {
StaticHolder.saveInstance(APIRequestHandler.class, plugin.getClass()); StaticHolder.saveInstance(APIRequestHandler.class, plugin.getClass());
StaticHolder.saveInstance(RequestHandler.class, plugin.getClass()); StaticHolder.saveInstance(RequestHandler.class, plugin.getClass());
StaticHolder.saveInstance(ResponseHandler.class, plugin.getClass());
StaticHolder.saveInstance(APIResponseHandler.class, plugin.getClass());
} }
private void registerWebAPIs() { private void registerWebAPIs() {

View File

@ -8,10 +8,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan; 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.*;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIForbiddenException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPINotFoundException;
import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.response.NotFoundResponse; import main.java.com.djrapitops.plan.systems.webserver.response.NotFoundResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response; import main.java.com.djrapitops.plan.systems.webserver.response.Response;
@ -112,15 +109,18 @@ public abstract class WebAPI {
} }
int responseCode = connection.getResponseCode(); int responseCode = connection.getResponseCode();
Log.debug("Response: " + responseCode);
switch (responseCode) { switch (responseCode) {
case 200: case 200:
return; return;
case 400: case 400:
throw new WebAPIException("Bad Request: " + url.toString() + "|" + parameters); throw new WebAPIFailException("Bad Request: " + url.toString() + "|" + parameters);
case 403: case 403:
throw new WebAPIForbiddenException(url.toString()); throw new WebAPIForbiddenException(url.toString());
case 404: case 404:
throw new WebAPINotFoundException(); throw new WebAPINotFoundException();
case 500:
throw new WebAPIInternalErrorException();
default: default:
throw new WebAPIException(url.toString() + "| Wrong response code " + responseCode); throw new WebAPIException(url.toString() + "| Wrong response code " + responseCode);
} }

View File

@ -4,6 +4,7 @@
*/ */
package main.java.com.djrapitops.plan.systems.webserver.webapi.bungee; package main.java.com.djrapitops.plan.systems.webserver.webapi.bungee;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
@ -33,7 +34,16 @@ public class PostHtmlWebAPI extends WebAPI {
if (htmlVariable == null) { if (htmlVariable == null) {
return badRequest("Html was null"); return badRequest("Html was null");
} }
String html = "<!DOCTYPE html>" + htmlVariable.split("<!DOCTYPE html>", 2)[1]; if (!htmlVariable.startsWith("<!DOCTYPE html>")) {
String[] split = htmlVariable.split("<!DOCTYPE html>", 2);
if (split.length <= 1) {
Log.debug(htmlVariable);
return badRequest("Html did not start with <!DOCTYPE html>");
}
htmlVariable = "<!DOCTYPE html>" + split[1];
}
String html = htmlVariable;
String target = variables.get("target"); String target = variables.get("target");
InformationManager infoManager = plugin.getInfoManager(); InformationManager infoManager = plugin.getInfoManager();
switch (target) { switch (target) {

View File

@ -27,7 +27,9 @@ public class PingWebAPI extends WebAPI {
@Override @Override
public Response onRequest(IPlan plugin, Map<String, String> variables) { public Response onRequest(IPlan plugin, Map<String, String> variables) {
if (Check.isBungeeAvailable()) { if (Check.isBungeeAvailable()) {
((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender"))); if (!((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender")))) {
return fail("Server info not found from the database");
}
} else if (!plugin.getInfoManager().isUsingAnotherWebServer()) { } else if (!plugin.getInfoManager().isUsingAnotherWebServer()) {
try { try {
String webAddress = variables.get("webAddress"); String webAddress = variables.get("webAddress");