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 {
private static String accessKey;
private final Plan plugin;
public BukkitPluginChannelListener(Plan plugin) {
@ -30,7 +32,9 @@ public class BukkitPluginChannelListener implements PluginMessageListener {
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
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)) {
plugin.getServerInfoManager().saveBungeeConnectionAddress(address);
@ -44,4 +48,12 @@ public class BukkitPluginChannelListener implements PluginMessageListener {
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) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
String accessKey = plugin.getWebServer().getWebAPI().generateNewAccessKey();
try (DataOutputStream out = new DataOutputStream(stream)) {
out.writeUTF("bungee_address");
out.writeUTF(plugin.getWebServer().getAccessAddress());
out.writeUTF(plugin.getWebServer().getAccessAddress() + "<!>" + accessKey);
}
server.sendData("Return", stream.toByteArray());
} catch (IOException e) {
} catch (Exception 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) {
String sender = variables.get("sender");
if (sender == null) {
Log.debug(getClass().getSimpleName() + ": Sender not Found");
return badRequest("Sender not present");
}
try {
UUID.fromString(sender);
} catch (Exception e) {
Log.debug(getClass().getSimpleName() + ": Invalid Sender UUID");
return badRequest("Faulty Sender value");
String accessKey = variables.get("accessKey");
WebAPIManager apiManager = MiscUtils.getIPlan().getWebServer().getWebAPI();
if (apiManager.isAuthorized(accessKey)) {
apiManager.authorize(accessKey);
} else {
Log.debug(getClass().getSimpleName() + ": Sender not Found");
return badRequest("Sender not present");
}
} else {
try {
UUID.fromString(sender);
} catch (Exception e) {
Log.debug(getClass().getSimpleName() + ": Invalid Sender UUID");
return badRequest("Faulty Sender value");
}
}
return onRequest(plugin, variables);
}

View File

@ -4,8 +4,9 @@
*/
package main.java.com.djrapitops.plan.systems.webserver.webapi;
import java.util.HashMap;
import java.util.Map;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
import java.util.*;
/**
* @author Fuzzlemann & Rsl1122
@ -13,12 +14,14 @@ import java.util.Map;
public class WebAPIManager {
private final Map<String, WebAPI> registry;
private final Set<String> accessKeys;
/**
* Constructor used to hide the public constructor
*/
public WebAPIManager() {
registry = new HashMap<>();
accessKeys = new HashSet<>();
}
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) {
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 main.java.com.djrapitops.plan.PlanBungee;
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.webapi.WebAPI;
@ -27,4 +29,18 @@ public class PingWebAPI extends WebAPI {
}
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();
}
}
}