Rewrite Pterodactyl integration (#86)

* Update Command_ride.java

* ok, done (FS-156)

* Fixes FS-137

* grammar fix

* Remove a number of wipe commands we don't want anyway

We shouldn't be wiping any of this data within the server now anyway so let's not tempt fate again.

* Why overcomplicating things is a horrifically bad idea (#55)

The longer I look into this plugin's code, the angrier I will become.

This fixes 2 bugs in the /potion command:
- Fixes non-admins being able to clear other players
- Fixes NPE caused when trying to add potion effects to players who are not on the server as a non-admin.

Both of these issues are caused by overcomplicating seemingly simple solutions, hence the name of this commit.

Co-authored-by: Ryan <Wild1145@users.noreply.github.com>
Co-authored-by: Nathan Curran <30569566+Focusvity@users.noreply.github.com>

* Bug fixes, improvements, and removals (FS-192) (#46)

* Three fixes

* Fixes /tempban throwing a NullPointerException when trying to get a player who isn't on the server but was in the past
* Fixes /tempban banning players for 24 hours regardless of the duration defined
* Fixes /list -t throwing a NullPointerException when performed from a non-player source (such as Telnet)

* Removes hubworld entriely

* Configurable blacklists for tag, muted commands, and wildcard

Changes:
* Moves globally blocked commands to the `global` subsection of the original `blocked_commands` section. You *will* need to update your configurations
* /wildcard's command blacklist is now configurable under the `wildcard` section in `blocked_commands`.
* The commands muted players can't use are now configurable under the `muted` section in `blocked_commands`.
* Removes some commented-out globally blocked command entries.

Co-authored-by: Ryan <Wild1145@users.noreply.github.com>

* Switched from -c to -ci (#63)

Resolves FS-219

* Added new dependency to allow TFM to compile (#65)

* Name change for Fleek (#64)

* Add //g to blocked commands (#62)

Co-authored-by: Nathan Curran <30569566+Focusvity@users.noreply.github.com>

* Small modification to new TFGuilds (FS-255) (#67)

* Calculate the player count without offline players (FS-247) (#68)

Co-authored-by: Ryan <Wild1145@users.noreply.github.com>

* Use long instead of int and use offset to check time (FS-181) (#69)

* Use long instead of int and use offset to check time (FS-181)

* Remove star import

* Addresses FS-224 (#70)

* Bump version

* TotalFreedomMod May Update

* Retired support for 2021.02 fully. (#72)

* Rewrite Pterodactyl integration

- Rewrite Pterodactyl integration
- Bring back the restart command and fix the stop command to gracefully stop the server now
- I had to update for Java 16, sorry. I can't get it to compile on 11 anymore anyway

* Codacy

* Actually document how you set it up

Also, rename server key to client key. That was from the old API, renamed it to be consistent with 1.x API

* Be even clearer

* Update Command_restart.java

* Update pom.xml

Co-authored-by: Video <videogamesm12@gmail.com>
Co-authored-by: Elmon11 <elmon11bussiness@gmail.com>
Co-authored-by: Ryan <Wild1145@users.noreply.github.com>
Co-authored-by: Nathan Curran <30569566+Focusvity@users.noreply.github.com>
This commit is contained in:
Telesphoreo 2021-06-29 03:28:30 -07:00 committed by GitHub
parent f57fc56f4a
commit d88509bf11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 176 additions and 174 deletions

View File

@ -19,13 +19,13 @@ These versions are no longer under active development, however we will look to r
| Version | Supported | Support End: |
| ------------------- | ------------------ | ------------ |
| 2021.04 | :white_check_mark: | July 2021 |
| 2021.02 | :white_check_mark: | June 2021 |
### No Longer Supported
These versions are no longer supported at all. It is strongly advised to update if you are running any of these versions.
| Version | Supported | Support Ended: |
| ------------------- | ------------------ | ------------------- |
| 2021.02 | :x: | 6 June 2021 |
| 2020.11 | :x: | 3 May 2021 |
| 6.0.x (Pre-Release) | :x: | December 2020 |
| < 2020.11 | :x: | December 2020 |

44
pom.xml
View File

@ -5,7 +5,7 @@
<groupId>me.totalfreedom</groupId>
<artifactId>TotalFreedomMod</artifactId>
<version>2021.04</version>
<version>2021.05</version>
<packaging>jar</packaging>
<properties>
@ -39,6 +39,11 @@
</scm>
<repositories>
<repository>
<id>apache-snapshots</id>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
@ -109,6 +114,11 @@
<id>ess-repo</id>
<url>https://ci.ender.zone/plugin/repository/everything/</url>
</repository>
<repository>
<id>mattmalec-repo</id>
<url>https://repo.mattmalec.com/repository/releases</url>
</repository>
</repositories>
<dependencies>
@ -144,7 +154,7 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.8</version>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
@ -186,21 +196,21 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.1_255</version>
<version>4.3.0_277</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.coreprotect</groupId>
<artifactId>coreprotect</artifactId>
<version>19.3</version>
<version>19.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.4</version>
<version>7.0.5</version>
<scope>provided</scope>
</dependency>
@ -259,12 +269,26 @@
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.mattmalec</groupId>
<artifactId>Pterodactyl4J</artifactId>
<version>2.BETA_47</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
@ -299,7 +323,7 @@
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.0.2</version>
<version>4.0.5</version>
<executions>
<execution>
<id>get-the-git-infos</id>
@ -412,10 +436,12 @@
</plugin>
<!-- Shade -->
<!-- NOTE: This should only be temporary. This will need to be updated when a stable build comes out
that supports Java 16 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<version>3.3.0-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
@ -427,8 +453,7 @@
<relocations>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>me.totalfreedom.totalfreedommod.paperlib
</shadedPattern> <!-- Replace this -->
<shadedPattern>me.totalfreedom.totalfreedommod.paperlib</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
@ -446,6 +471,7 @@
<include>com.github.speedxx:Mojangson</include>
<include>org.bstats:bstats-bukkit</include>
<include>org.jetbrains:annotations</include>
<include>com.mattmalec:Pterodactyl4J</include>
</includes>
</artifactSet>
</configuration>

View File

@ -1,26 +1,30 @@
package me.totalfreedom.totalfreedommod;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import com.mattmalec.pterodactyl4j.Permission;
import com.mattmalec.pterodactyl4j.PowerAction;
import com.mattmalec.pterodactyl4j.PteroAction;
import com.mattmalec.pterodactyl4j.PteroBuilder;
import com.mattmalec.pterodactyl4j.application.entities.ApplicationUser;
import com.mattmalec.pterodactyl4j.application.entities.PteroApplication;
import com.mattmalec.pterodactyl4j.application.managers.UserAction;
import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.client.entities.ClientSubuser;
import com.mattmalec.pterodactyl4j.client.entities.PteroClient;
import joptsimple.internal.Strings;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.util.Response;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
public class Pterodactyl extends FreedomService
{
public final String URL = ConfigEntry.PTERO_URL.getString();
private final String SERVER_KEY = ConfigEntry.PTERO_SERVER_KEY.getString();
private final String ADMIN_KEY = ConfigEntry.PTERO_ADMIN_KEY.getString();
private final List<String> SERVER_HEADERS = Arrays.asList("Accept:Application/vnd.pterodactyl.v1+json", "Content-Type:application/json", "Authorization:Bearer " + SERVER_KEY);
private final List<String> ADMIN_HEADERS = Arrays.asList("Accept:Application/vnd.pterodactyl.v1+json", "Content-Type:application/json", "Authorization:Bearer " + ADMIN_KEY);
private final String CLIENT_KEY = ConfigEntry.PTERO_CLIENT_KEY.getString();
private final String IDENTIFIER = ConfigEntry.PTERO_SERVER_UUID.getString();
private final PteroApplication adminAPI = PteroBuilder.createApplication(URL, ADMIN_KEY);
private final PteroClient clientAPI = PteroBuilder.createClient(URL, CLIENT_KEY);
private final ClientServer server = clientAPI.retrieveServerByIdentifier(IDENTIFIER).execute();
private boolean enabled = !Strings.isNullOrEmpty(URL);
@ -43,163 +47,60 @@ public class Pterodactyl extends FreedomService
if (!admin.isActive() || admin.getRank() != Rank.SENIOR_ADMIN)
{
FLog.debug("Disabling ptero acc");
FLog.debug("Disabling Pterodactyl account");
removeAccountFromServer(id);
return;
}
FLog.debug("Enabling ptero acc");
FLog.debug("Enabling Pterodactyl account");
addAccountToServer(id);
}
@SuppressWarnings("unchecked")
public String createAccount(String username, String password)
{
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", password);
json.put("email", username.toLowerCase() + "@" + ConfigEntry.PTERO_DEFAULT_EMAIL_DOMAIN.getString());
json.put("first_name", username);
json.put("last_name", "\u200E"); // required, so I made it appear empty
Response response;
JSONObject jsonResponse;
try
{
response = FUtil.sendRequest(URL + "/api/application/users", "POST", ADMIN_HEADERS, json.toJSONString());
jsonResponse = response.getJSONMessage();
}
catch (IOException | ParseException e)
{
FLog.severe(e);
return null;
}
return ((JSONObject)jsonResponse.get("attributes")).get("id").toString();
UserAction action = adminAPI.getUserManager().createUser()
.setUserName(username)
.setEmail(username.toLowerCase() + "@" + ConfigEntry.PTERO_DEFAULT_EMAIL_DOMAIN.getString())
.setFirstName(username)
.setLastName("\u200E") // Required - make it appear empty
.setPassword(password);
return action.execute().getId();
}
public boolean deleteAccount(String id)
public void deleteAccount(String id)
{
JSONObject json = new JSONObject();
try
{
return FUtil.sendRequest(URL + "/api/application/users/" + id, "DELETE", ADMIN_HEADERS, json.toJSONString()).getCode() == 204;
}
catch (IOException e)
{
FLog.severe(e);
return false;
}
ApplicationUser username = adminAPI.retrieveUserById(id).execute();
PteroAction<Void> action = adminAPI.getUserManager().deleteUser(username);
action.execute();
}
@SuppressWarnings("unchecked")
public void addAccountToServer(String id)
{
String url = URL + "/api/client/servers/" + ConfigEntry.PTERO_SERVER_UUID.getString() + "/users";
JSONObject userData = getUserData(id);
if (userData == null)
{
FLog.severe("The Pterodactyl user with the ID of " + id + " was not found");
return;
}
JSONObject json = new JSONObject();
json.put("email", userData.get("email").toString());
json.put("permissions", Arrays.asList("control.console", "control.start", "control.restart", "control.stop", "control.kill"));
try
{
FUtil.sendRequest(url, "POST", SERVER_HEADERS, json.toJSONString());
}
catch (IOException e)
{
FLog.severe(e);
}
ApplicationUser username = adminAPI.retrieveUserById(id).execute();
String email = username.getEmail();
PteroAction<ClientServer> server = clientAPI.retrieveServerByIdentifier(IDENTIFIER);
server.execute().getSubuserManager().createUser()
.setEmail(email)
.setPermissions(Permission.CONTROL_PERMISSIONS).execute();
}
public void removeAccountFromServer(String id)
{
JSONObject userData = getUserData(id);
if (userData == null)
{
FLog.severe("The Pterodactyl user with the ID of " + id + " was not found");
return;
}
String url = URL + "/api/client/servers/" + ConfigEntry.PTERO_SERVER_UUID.getString() + "/users/" + userData.get("uuid");
try
{
FUtil.sendRequest(url, "DELETE", SERVER_HEADERS, null);
}
catch (IOException e)
{
FLog.severe(e);
}
ApplicationUser username = adminAPI.retrieveUserById(id).execute();
PteroAction<ClientServer> server = clientAPI.retrieveServerByIdentifier(IDENTIFIER);
ClientSubuser clientSubuser = server.execute().getSubuser(username.getUUID()).retrieve().execute();
server.execute().getSubuserManager().deleteUser(clientSubuser).execute();
}
public JSONObject getUserData(String id)
public void stopServer()
{
Response response;
JSONObject jsonResponse;
try
{
response = FUtil.sendRequest(URL + "/api/application/users/" + id, "GET", ADMIN_HEADERS, null);
jsonResponse = response.getJSONMessage();
}
catch (IOException | ParseException e)
{
FLog.severe(e);
return null;
}
return (JSONObject)jsonResponse.get("attributes");
clientAPI.setPower(server, PowerAction.STOP).execute();
}
// API patch function on users doesnt work rn, it throws 500 errors, so it's probably not written yet
@SuppressWarnings("unchecked")
public void setPassword(String id, String password)
public void restartServer()
{
JSONObject json = new JSONObject();
json.put("password", password);
try
{
FUtil.sendRequest(URL + "/api/application/users/" + id, "PATCH", ADMIN_HEADERS, json.toJSONString());
}
catch (IOException e)
{
FLog.severe(e);
}
}
public String getURL()
{
return URL;
}
public String getServerKey()
{
return SERVER_KEY;
}
public String getAdminKey()
{
return ADMIN_KEY;
}
public List<String> getServerHeaders()
{
return SERVER_HEADERS;
}
public List<String> getAdminHeaders()
{
return ADMIN_HEADERS;
clientAPI.setPower(server, PowerAction.RESTART).execute();
}
public boolean isEnabled()

View File

@ -17,11 +17,9 @@ import org.bukkit.entity.Player;
@CommandParameters(description = "Manage your Pterodactyl panel account", usage = "/<command> <create | delete>")
public class Command_panel extends FreedomCommand
{
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (!plugin.ptero.isEnabled())
{
msg("Pterodactyl integration is currently disabled.", ChatColor.RED);
@ -32,7 +30,7 @@ public class Command_panel extends FreedomCommand
if (playerData.getDiscordID() == null)
{
msg("You must have a linked discord account.", ChatColor.RED);
msg("You must have a linked Discord account.", ChatColor.RED);
return true;
}
@ -63,13 +61,11 @@ public class Command_panel extends FreedomCommand
}
plugin.ptero.addAccountToServer(id);
admin.setPteroID(id);
plugin.al.save(admin);
plugin.al.updateTables();
plugin.dc.sendPteroInfo(playerData, username, password);
msg("Successfully created your Pterodactyl account. Check your DMs from " + plugin.dc.formatBotTag() + " on discord to get your credentials.", ChatColor.GREEN);
msg("Successfully created your Pterodactyl account. Check your DMs from " + plugin.dc.formatBotTag() + " on Discord to get your credentials.", ChatColor.GREEN);
return true;
}
else if (args[0].equalsIgnoreCase("delete"))
@ -83,18 +79,10 @@ public class Command_panel extends FreedomCommand
return true;
}
boolean deleted = plugin.ptero.deleteAccount(admin.getPteroID());
if (!deleted)
{
msg("Failed to delete your Pterodactyl account.", ChatColor.RED);
return true;
}
plugin.ptero.deleteAccount(admin.getPteroID());
admin.setPteroID(null);
plugin.al.save(admin);
plugin.al.updateTables();
msg("Successfully deleted your Pterodactyl account.", ChatColor.GREEN);
return true;
}

View File

@ -0,0 +1,77 @@
package me.totalfreedom.totalfreedommod.command;
import java.util.HashMap;
import java.util.Map;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH)
@CommandParameters(description = "Restart the server", usage = "/<command> [reason]")
public class Command_restart extends FreedomCommand
{
private static final Map<CommandSender, String> RESTART_CONFIRM = new HashMap<>();
@Override
public boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole)
{
if (!plugin.ptero.isEnabled())
{
msg("Pterodactyl integration is currently disabled.", ChatColor.RED);
return true;
}
String reason = "Server is restarting!";
if (args.length != 0)
{
reason = StringUtils.join(args, " ");
}
if (sender.getName().equals("CONSOLE"))
{
restart(reason);
return true;
}
else if (RESTART_CONFIRM.containsKey(sender))
{
restart(RESTART_CONFIRM.get(sender));
return true;
}
msg("Warning: You're about to restart the server. Type /restart again to confirm you want to do this.");
RESTART_CONFIRM.put(sender, reason);
new BukkitRunnable()
{
@Override
public void run()
{
if (RESTART_CONFIRM.containsKey(sender))
{
RESTART_CONFIRM.remove(sender);
msg("Restart request expired.");
}
}
}.runTaskLater(plugin, 15 * 20);
return true;
}
public void restart(String reason)
{
FUtil.bcastMsg("Server is restarting!", ChatColor.LIGHT_PURPLE);
for (Player player : server.getOnlinePlayers())
{
player.kickPlayer(ChatColor.LIGHT_PURPLE + reason);
}
RESTART_CONFIRM.remove(sender);
plugin.ptero.restartServer();
}
}

View File

@ -20,7 +20,6 @@ public class Command_stop extends FreedomCommand
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
String reason = "Server is going offline, come back in about 20 seconds.";
if (args.length != 0)
@ -39,7 +38,6 @@ public class Command_stop extends FreedomCommand
return true;
}
msg("Warning: You're about to stop the server. Type /stop again to confirm you want to do this.");
STOP_CONFIRM.put(sender, reason);
@ -69,6 +67,13 @@ public class Command_stop extends FreedomCommand
STOP_CONFIRM.remove(sender);
server.shutdown();
if (plugin.ptero.isEnabled())
{
plugin.ptero.stopServer();
}
else
{
server.shutdown();
}
}
}

View File

@ -91,7 +91,7 @@ public enum ConfigEntry
PTERO_DEFAULT_EMAIL_DOMAIN(String.class, "ptero.default_email_domain"),
PTERO_SERVER_UUID(String.class, "ptero.server_uuid"),
PTERO_ADMIN_KEY(String.class, "ptero.admin_key"),
PTERO_SERVER_KEY(String.class, "ptero.server_key"),
PTERO_CLIENT_KEY(String.class, "ptero.client_key"),
//
SHOP_ENABLED(Boolean.class, "shop.enabled"),
SHOP_TITLE(String.class, "shop.title"),

View File

@ -85,18 +85,23 @@ discord:
# Owner role ID
server_owner_role_id: ''
# Pterodactyl
# Pterodactyl (requires Discord verification to be enabled)
ptero:
# URL - do not leave a trailing forward slash
url: ''
# The default email domain used to set email addresses for new users - do not include the @
default_email_domain: 'example.com'
# Server UUID
# Short Server UUID (for example: a538a220)
# Do NOT give the long Server UUID
server_uuid: ''
# Admin panel API key
# Create this key by going to Settings > Application API > Create New
# Make sure you give it Users and Servers permissions
admin_key: ''
# Server API key
server_key: ''
# Client API key
# Create a new user and add them as a subuser to the server with all control privileges.
# Then, login to the new user account and go to User Settings. Click on API Credentials and create a new one.
client_key: ''
# The shop
shop: