Access Key System for First Bungee Pings after plugin channel message

This commit is contained in:
Rsl1122 2017-09-28 19:00:44 +03:00
parent 451c42f384
commit aa28bf7c86
5 changed files with 67 additions and 13 deletions

View File

@ -20,6 +20,8 @@ import java.io.IOException;
*/ */
public class BukkitPluginChannelListener implements PluginMessageListener { public class BukkitPluginChannelListener implements PluginMessageListener {
private static String accessKey;
private final Plan plugin; private final Plan plugin;
public BukkitPluginChannelListener(Plan plugin) { public BukkitPluginChannelListener(Plan plugin) {
@ -30,7 +32,9 @@ public class BukkitPluginChannelListener implements PluginMessageListener {
public void onPluginMessageReceived(String channel, Player player, byte[] message) { public void onPluginMessageReceived(String channel, Player player, byte[] message) {
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) { try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
String subChannel = in.readUTF(); String subChannel = in.readUTF();
String address = in.readUTF(); String[] data = in.readUTF().split("<!>");
String address = data[0];
accessKey = data[1];
if ("bungee_address".equals(subChannel)) { if ("bungee_address".equals(subChannel)) {
plugin.getServerInfoManager().saveBungeeConnectionAddress(address); plugin.getServerInfoManager().saveBungeeConnectionAddress(address);
@ -44,4 +48,12 @@ public class BukkitPluginChannelListener implements PluginMessageListener {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static String getAccessKey() {
return accessKey;
}
public static void usedAccessKey() {
accessKey = null;
}
} }

View File

@ -45,12 +45,14 @@ public class BungeePluginChannelListener implements Listener {
private void sendToBukkit(ServerInfo server) { private void sendToBukkit(ServerInfo server) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
String accessKey = plugin.getWebServer().getWebAPI().generateNewAccessKey();
try (DataOutputStream out = new DataOutputStream(stream)) { try (DataOutputStream out = new DataOutputStream(stream)) {
out.writeUTF("bungee_address"); out.writeUTF("bungee_address");
out.writeUTF(plugin.getWebServer().getAccessAddress()); out.writeUTF(plugin.getWebServer().getAccessAddress() + "<!>" + accessKey);
} }
server.sendData("Return", stream.toByteArray()); server.sendData("Return", stream.toByteArray());
} catch (IOException e) { } catch (Exception e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);
} }
} }

View File

@ -46,14 +46,21 @@ public abstract class WebAPI {
public Response processRequest(IPlan plugin, Map<String, String> variables) { public Response processRequest(IPlan plugin, Map<String, String> variables) {
String sender = variables.get("sender"); String sender = variables.get("sender");
if (sender == null) { if (sender == null) {
Log.debug(getClass().getSimpleName() + ": Sender not Found"); String accessKey = variables.get("accessKey");
return badRequest("Sender not present"); WebAPIManager apiManager = MiscUtils.getIPlan().getWebServer().getWebAPI();
} if (apiManager.isAuthorized(accessKey)) {
try { apiManager.authorize(accessKey);
UUID.fromString(sender); } else {
} catch (Exception e) { Log.debug(getClass().getSimpleName() + ": Sender not Found");
Log.debug(getClass().getSimpleName() + ": Invalid Sender UUID"); return badRequest("Sender not present");
return badRequest("Faulty Sender value"); }
} else {
try {
UUID.fromString(sender);
} catch (Exception e) {
Log.debug(getClass().getSimpleName() + ": Invalid Sender UUID");
return badRequest("Faulty Sender value");
}
} }
return onRequest(plugin, variables); return onRequest(plugin, variables);
} }

View File

@ -4,8 +4,9 @@
*/ */
package main.java.com.djrapitops.plan.systems.webserver.webapi; package main.java.com.djrapitops.plan.systems.webserver.webapi;
import java.util.HashMap; import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
import java.util.Map;
import java.util.*;
/** /**
* @author Fuzzlemann & Rsl1122 * @author Fuzzlemann & Rsl1122
@ -13,12 +14,14 @@ import java.util.Map;
public class WebAPIManager { public class WebAPIManager {
private final Map<String, WebAPI> registry; private final Map<String, WebAPI> registry;
private final Set<String> accessKeys;
/** /**
* Constructor used to hide the public constructor * Constructor used to hide the public constructor
*/ */
public WebAPIManager() { public WebAPIManager() {
registry = new HashMap<>(); registry = new HashMap<>();
accessKeys = new HashSet<>();
} }
public void registerNewAPI(WebAPI... api) { public void registerNewAPI(WebAPI... api) {
@ -27,6 +30,20 @@ public class WebAPIManager {
} }
} }
public boolean isAuthorized(String key) {
return accessKeys.contains(key);
}
public void authorize(String key) {
accessKeys.remove(key);
}
public String generateNewAccessKey() throws Exception {
String key = PassEncryptUtil.createHash(UUID.randomUUID().toString()).split(":")[4];
accessKeys.add(key);
return key;
}
public void registerNewAPI(WebAPI api) { public void registerNewAPI(WebAPI api) {
registry.put(api.getClass().getSimpleName().toLowerCase(), api); registry.put(api.getClass().getSimpleName().toLowerCase(), api);
} }

View File

@ -7,6 +7,8 @@ package main.java.com.djrapitops.plan.systems.webserver.webapi.universal;
import com.djrapitops.plugin.utilities.Compatibility; import com.djrapitops.plugin.utilities.Compatibility;
import main.java.com.djrapitops.plan.PlanBungee; import main.java.com.djrapitops.plan.PlanBungee;
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.systems.info.pluginchannel.BukkitPluginChannelListener;
import main.java.com.djrapitops.plan.systems.webserver.response.Response; import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
@ -27,4 +29,18 @@ public class PingWebAPI extends WebAPI {
} }
return success(); return success();
} }
@Override
public void sendRequest(String address) throws WebAPIException {
String accessKey = BukkitPluginChannelListener.getAccessKey();
if (accessKey != null) {
addVariable("accessKey", accessKey);
}
super.sendRequest(address);
if (accessKey != null) {
BukkitPluginChannelListener.usedAccessKey();
}
}
} }