diff --git a/pom.xml b/pom.xml index df3d14e4..df81a4a7 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.gnmyt MCDash - 1.0.2 + 1.0.3 8 @@ -49,7 +49,6 @@ 1.4 - org.reflections @@ -88,6 +87,13 @@ provided + + + com.squareup.okhttp3 + okhttp + 4.9.0 + + \ No newline at end of file diff --git a/src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java b/src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java index c9c0d2f3..4f8c0573 100644 --- a/src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java +++ b/src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java @@ -3,6 +3,7 @@ import com.sun.net.httpserver.HttpServer; import de.gnmyt.mcdash.api.config.ConfigurationManager; import de.gnmyt.mcdash.api.handler.DefaultHandler; +import de.gnmyt.mcdash.connector.MasterConnector; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.reflections.Reflections; @@ -27,11 +28,12 @@ public void onEnable() { server.setExecutor(null); server.start(); } catch (IOException e) { - System.out.println("Could not open the port for the web server: " + e.getMessage()); - Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin(getName())); + disablePlugin("Could not open the port for the web server: " + e.getMessage()); } registerRoutes(); + + new MasterConnector().register(); } @Override @@ -52,6 +54,16 @@ public void registerRoutes() { }); } + + /** + * Disables the plugin + * @param message The reason why the plugin should be disabled + */ + public static void disablePlugin(String message) { + System.out.println(getPrefix()+message); + Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin(getInstance().getName())); + } + /** * Gets the dashboard configuration * @return the dashboard configuration @@ -84,4 +96,12 @@ public static HttpServer getHttpServer() { public static String getRoutePackageName() { return getInstance().getClass().getPackage().getName()+".panel.routes"; } + + /** + * Gets the prefix of the plugin + * @return the prefix + */ + public static String getPrefix() { + return "["+getInstance().getName()+"] "; + } } diff --git a/src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java b/src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java index 33ffb917..42e27fd1 100644 --- a/src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java +++ b/src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java @@ -33,8 +33,12 @@ public boolean configExists() { * Generates a default configuration */ public void generateDefault() { + + // Server configuration + config.set("identifier", Integer.parseInt(String.format("%04d", new Random().nextInt(10000)))); + // Master configuration - config.set("masterIP", "localhost:5232"); + config.set("masterIP", "http://localhost:5232"); config.set("masterKey", "your-master-key"); // Wrapper configuration @@ -62,6 +66,15 @@ public Integer getInt(String path) { return config.getInt(path); } + /** + * Checks if the configuration file contains an string + * @param path The path you want to check + * @return true if the provided path exists in the config, otherwise false + */ + public boolean hasString(String path) { + return config.getString(path) != null; + } + /** * Gets the master ip from the configuration * @return the master ip @@ -74,7 +87,7 @@ public String getMasterIP() { * Gets the master key from the configuration * @return the master key */ - public String masterKey() { + public String getMasterKey() { return getString("masterKey"); } @@ -94,6 +107,15 @@ public String getWrapperKey() { return getString("wrapperKey"); } + + /** + * Gets the server identifier + * @return the server identifier + */ + public int getIdentifier() { + return getInt("identifier"); + } + /** * Saves the current configuration */ diff --git a/src/main/java/de/gnmyt/mcdash/connector/MasterConnector.java b/src/main/java/de/gnmyt/mcdash/connector/MasterConnector.java new file mode 100644 index 00000000..40e9b9d9 --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/connector/MasterConnector.java @@ -0,0 +1,76 @@ +package de.gnmyt.mcdash.connector; + +import de.gnmyt.mcdash.MinecraftDashboard; +import de.gnmyt.mcdash.api.config.ConfigurationManager; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class MasterConnector { + + private final String REQUEST_FORMAT = "%s/api/register"; + + private final OkHttpClient client = new OkHttpClient().newBuilder().build(); + private final ConfigurationManager config = MinecraftDashboard.getDashboardConfig(); + + private int attempted_trys = 0; + + /** + * Gets the local ip address of the computer + * @return the local ip address of the computer + */ + public String getLocalAddress() { + try { + return config.hasString("customIP") ? config.getString("customIP") : InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException ignored) { } + return "localhost"; + } + + /** + * Prepares the registration request + * @return the prepared registration request + */ + public Request prepareRequest() { + return new Request.Builder() + .url(String.format(REQUEST_FORMAT, config.getMasterIP())) + .post(new FormBody.Builder() + .addEncoded("identifier", String.valueOf(config.getIdentifier())) + .addEncoded("wrapperIP", getLocalAddress()+":"+config.getWrapperPort()) + .addEncoded("wrapperKey", config.getWrapperKey()) + .build()) + .header("Authorization", "Bearer " + config.getMasterKey()) + .build(); + } + + /** + * Updates the wrapper data in the master backend + */ + public void register() { + attempted_trys++; + + if (attempted_trys > 5) return; + + try { + Response response = client.newCall(prepareRequest()).execute(); + response.body().close(); + if (response.code() > 200) { + throw new Exception("Request not successful"); + } + System.out.println(MinecraftDashboard.getPrefix() + "Successfully registered the server"); + } catch (Exception e) { + + if (attempted_trys == 5) { + MinecraftDashboard.disablePlugin("Could not connect to master, please check the config.yml file"); + } else System.out.println(String.format("%sRegistration failed, retrying in 5 seconds... (try %d/5)", MinecraftDashboard.getPrefix(), attempted_trys)); + + try { Thread.sleep(5000); } catch (InterruptedException ignored) { } + + register(); + } + } + +}