Added Whitelist checks

This commit is contained in:
Elias Müller 2023-12-09 21:21:22 +01:00
parent b72a5947b4
commit 0ea9738867
5 changed files with 110 additions and 4 deletions

View File

@ -24,7 +24,7 @@ repositories {
dependencies {
compileOnly "io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT"
compileOnly 'org.geysermc.floodgate:api:2.2.2-SNAPSHOT'
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
compileOnly 'org.apache.httpcomponents:httpclient:4.5.14'
}
def targetJavaVersion = 17

View File

@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.appliances.whitelist;
import java.util.UUID;
public class LoginException extends Exception {
public String title;
public String error;
public UUID user;
public String solution;
public LoginException(String title, String error, UUID user, String solution) {
super(error);
this.title = title;
this.error = error;
this.user = user;
this.solution = solution;
}
}

View File

@ -1,12 +1,27 @@
package eu.mhsl.craftattack.spawn.appliances.whitelist;
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
public class PlayerJoinListener extends ApplianceListener<Whitelist> {
@EventHandler
public void preLoginEvent(AsyncPlayerPreLoginEvent preLoginEvent) {
try {
getAppliance().login(preLoginEvent);
} catch (LoginException e) {
preLoginEvent.disallow(
AsyncPlayerPreLoginEvent.Result.KICK_WHITELIST,
Component.text()
.appendNewline().appendNewline()
.append(Component.text(e.title, NamedTextColor.DARK_RED)).appendNewline()
.append(Component.text(e.error, NamedTextColor.RED)).appendNewline().appendNewline()
.append(Component.text(e.solution, NamedTextColor.GRAY)).appendNewline().appendNewline()
.append(Component.text(e.user.toString(), NamedTextColor.DARK_GRAY)).appendNewline()
.build()
);
}
}
}

View File

@ -1,13 +1,83 @@
package eu.mhsl.craftattack.spawn.appliances.whitelist;
import com.google.gson.Gson;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import org.apache.http.client.utils.URIBuilder;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class Whitelist extends Appliance {
private record UserData(UUID uuid, String username, String firstname, String lastname) {}
private final URI apiEndpoint = URI.create(Objects.requireNonNull(localConfig().getString("api")));
public Whitelist() {
super("whitelist");
}
public void login(AsyncPlayerPreLoginEvent login) throws LoginException {
try {
UserData data = this.fetchUserData(login.getUniqueId());
if(!data.username.equalsIgnoreCase(login.getName()))
throw new LoginException(
"Nutzername geändert",
String.format("Der Name '%s' stimmt nicht mit '%s' überein.", data.username, login.getName()),
login.getUniqueId(),
"Bitte kontaktiere einen Admin, um Deine Anmeldedaten zu aktualisieren!"
);
} catch (LoginException e) {
throw e;
} catch (Exception e) {
throw new LoginException(
"Interner Serverfehler",
"Deine Zugangsdaten konnten nicht abgerufen werden.",
login.getUniqueId(),
"Versuche es später erneut oder kontaktiere einen Admin!"
);
}
}
private UserData fetchUserData(UUID uuid) throws LoginException {
URIBuilder uriBuilder = new URIBuilder(apiEndpoint);
uriBuilder.addParameter("uuid", uuid.toString());
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(uriBuilder.build())
.header("Content-Type", "application/json")
.GET()
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
if(httpResponse.statusCode() == 400)
throw new LoginException(
"Nicht angemeldet",
"Du bist derzeit nicht als Teilnehmer des CraftAttack-Projektes registriert!",
uuid,
"Melde Dich bei einem Admin für eine nachträgliche Anmeldung."
);
return new Gson().fromJson(httpResponse.body(), UserData.class);
} catch (IOException | InterruptedException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
protected @NotNull List<Listener> eventHandlers() {

View File

@ -21,7 +21,7 @@ countdown:
worldborder-after: 0
report:
api: https://mhsl.eu/craftattack/report
api: https://mhsl.eu/craftattack/api/report
event:
api: http://10.20.6.5:8080/
@ -43,3 +43,6 @@ help:
playerLimit:
maxPlayers: 0
whitelist:
api: https://mhsl.eu/craftattack/api/user