Skip to content

Commit

Permalink
Merge pull request #3 from gnmyt/features/master-integration
Browse files Browse the repository at this point in the history
🔗 Added the master integration
  • Loading branch information
gnmyt authored Aug 3, 2021
2 parents 4830fe5 + b960926 commit af3d518
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.gnmyt</groupId>
<artifactId>MCDash</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down Expand Up @@ -49,7 +49,6 @@
<version>1.4</version>
</dependency>


<!-- Reflections API -->
<dependency>
<groupId>org.reflections</groupId>
Expand Down Expand Up @@ -88,6 +87,13 @@
<scope>provided</scope>
</dependency>

<!-- HTTP Client API -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>

</dependencies>

</project>
24 changes: 22 additions & 2 deletions src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()+"] ";
}
}
26 changes: 24 additions & 2 deletions src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <code>true</code> if the provided path exists in the config, otherwise <code>false</code>
*/
public boolean hasString(String path) {
return config.getString(path) != null;
}

/**
* Gets the master ip from the configuration
* @return the master ip
Expand All @@ -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");
}

Expand All @@ -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
*/
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/de/gnmyt/mcdash/connector/MasterConnector.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

}

0 comments on commit af3d518

Please sign in to comment.