2020-09-14 09:36:25 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import joptsimple.internal.Strings;
|
|
|
|
import lombok.Getter;
|
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
|
|
|
import me.totalfreedom.totalfreedommod.staff.StaffMember;
|
|
|
|
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);
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private boolean enabled = !Strings.isNullOrEmpty(URL);
|
|
|
|
|
|
|
|
public void onStart()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateAccountStatus(StaffMember staffMember)
|
|
|
|
{
|
|
|
|
String id = staffMember.getPteroID();
|
|
|
|
|
|
|
|
if (Strings.isNullOrEmpty(id) || !enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-28 02:51:15 +00:00
|
|
|
if (!staffMember.isActive() || staffMember.getRank() != Rank.SENIOR_ADMIN)
|
2020-09-14 09:36:25 +00:00
|
|
|
{
|
|
|
|
FLog.debug("Disabling ptero acc");
|
|
|
|
removeAccountFromServer(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLog.debug("Enabling aptero acc");
|
|
|
|
addAccountToServer(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getUserData(String id)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// API patch function on users doesnt work rn, it throws 500 errors, so it's probably not written yet
|
|
|
|
public void setPassword(String id, String password)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|