BugFixing the Connection between Bungee and Bukkit

This commit is contained in:
Rsl1122 2017-09-19 18:47:43 +03:00
parent 79b4793313
commit ccc562913c
12 changed files with 50 additions and 24 deletions

View File

@ -180,7 +180,7 @@ public class Plan extends BukkitPlugin<Plan> 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()) {

View File

@ -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<String> 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<String> 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) {

View File

@ -64,9 +64,9 @@ public class BukkitInformationManager extends InformationManager {
Optional<String> 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;
}

View File

@ -37,7 +37,7 @@ public class BungeeInformationManager extends InformationManager {
private Map<UUID, Map<UUID, String>> pluginsTabContent;
public BungeeInformationManager(PlanBungee plugin) throws SQLException {
usingBungeeWebServer = true;
usingAnotherWebServer = false;
pluginsTabContent = new HashMap<>();
this.plugin = plugin;
refreshBukkitServerMap();

View File

@ -22,7 +22,7 @@ import java.util.UUID;
* @author Rsl1122
*/
public abstract class InformationManager {
boolean usingBungeeWebServer;
boolean usingAnotherWebServer;
String webServerAddress;
Set<ISender> 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();

View File

@ -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> 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);
}
}

View File

@ -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();

View File

@ -68,7 +68,7 @@ public class APIResponseHandler {
Map<String, String> 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, () -> {

View File

@ -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];

View File

@ -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));

View File

@ -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<String, String> 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);
}
}

View File

@ -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()) {